开发者社区> 技术课堂的搬运工~> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

阿里云数据分析最佳实践:二维数据可视化 + 设备数据下发

简介: 这里分别演示通过二维数据可视化功能展示设备位置 + 通过数据分析实现定时下发数据到设备。
+关注继续查看

作者:俏巴

概述

物联网数据分析,又称Link Analytics,是阿里云为物联网开发者提供的设备智能分析服务,全链路覆盖了设备数据生成、管理(存储)、清洗、分析及可视化等环节。有效降低数据分析门槛,助力物联网开发工作。这里分别演示通过二维数据可视化功能展示设备位置 + 通过数据分析实现定时下发数据到设备。

Step By Step

1、创建产品,导入物模型,参考链接

物模型json内容

{
  "schema": "https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
  "profile": {
    "productKey": "a1kVHWEOsM2"
  },
  "properties": [
    {
      "identifier": "GeoLocation",
      "name": "地理位置",
      "accessMode": "rw",
      "required": true,
      "dataType": {
        "type": "struct",
        "specs": [
          {
            "identifier": "Longitude",
            "name": "经度",
            "dataType": {
              "type": "double",
              "specs": {
                "min": "-180",
                "max": "180",
                "unit": "°",
                "unitName": "度",
                "step": "0.01"
              }
            }
          },
          {
            "identifier": "Latitude",
            "name": "纬度",
            "dataType": {
              "type": "double",
              "specs": {
                "min": "-90",
                "max": "90",
                "unit": "°",
                "unitName": "度",
                "step": "0.01"
              }
            }
          },
          {
            "identifier": "Altitude",
            "name": "海拔",
            "dataType": {
              "type": "double",
              "specs": {
                "min": "0",
                "max": "9999",
                "unit": "m",
                "unitName": "米",
                "step": "0.01"
              }
            }
          },
          {
            "identifier": "CoordinateSystem",
            "name": "坐标系统",
            "dataType": {
              "type": "enum",
              "specs": {
                "1": "WGS_84",
                "2": "GCJ_02"
              }
            }
          }
        ]
      }
    },
    {
      "identifier": "CurrentHumidity",
      "name": "湿度",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "double",
        "specs": {
          "min": "0",
          "max": "100",
          "unit": "%",
          "unitName": "百分比",
          "step": "0.01"
        }
      }
    },
    {
      "identifier": "CurrentTemperature",
      "name": "温度",
      "accessMode": "rw",
      "required": false,
      "dataType": {
        "type": "double",
        "specs": {
          "min": "-40",
          "max": "120",
          "unit": "℃",
          "unitName": "摄氏度",
          "step": "0.01"
        }
      }
    }
  ],
  "events": [
    {
      "identifier": "post",
      "name": "post",
      "type": "info",
      "required": true,
      "desc": "属性上报",
      "method": "thing.event.property.post",
      "outputData": [
        {
          "identifier": "GeoLocation",
          "name": "地理位置",
          "dataType": {
            "type": "struct",
            "specs": [
              {
                "identifier": "Longitude",
                "name": "经度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-180",
                    "max": "180",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Latitude",
                "name": "纬度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-90",
                    "max": "90",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Altitude",
                "name": "海拔",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "0",
                    "max": "9999",
                    "unit": "m",
                    "unitName": "米",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "CoordinateSystem",
                "name": "坐标系统",
                "dataType": {
                  "type": "enum",
                  "specs": {
                    "1": "WGS_84",
                    "2": "GCJ_02"
                  }
                }
              }
            ]
          }
        },
        {
          "identifier": "CurrentHumidity",
          "name": "湿度",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%",
              "unitName": "百分比",
              "step": "0.01"
            }
          }
        },
        {
          "identifier": "CurrentTemperature",
          "name": "温度",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "-40",
              "max": "120",
              "unit": "℃",
              "unitName": "摄氏度",
              "step": "0.01"
            }
          }
        }
      ]
    }
  ],
  "services": [
    {
      "identifier": "set",
      "name": "set",
      "required": true,
      "callType": "async",
      "desc": "属性设置",
      "method": "thing.service.property.set",
      "inputData": [
        {
          "identifier": "GeoLocation",
          "name": "地理位置",
          "dataType": {
            "type": "struct",
            "specs": [
              {
                "identifier": "Longitude",
                "name": "经度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-180",
                    "max": "180",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Latitude",
                "name": "纬度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-90",
                    "max": "90",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Altitude",
                "name": "海拔",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "0",
                    "max": "9999",
                    "unit": "m",
                    "unitName": "米",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "CoordinateSystem",
                "name": "坐标系统",
                "dataType": {
                  "type": "enum",
                  "specs": {
                    "1": "WGS_84",
                    "2": "GCJ_02"
                  }
                }
              }
            ]
          }
        },
        {
          "identifier": "CurrentHumidity",
          "name": "湿度",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%",
              "unitName": "百分比",
              "step": "0.01"
            }
          }
        },
        {
          "identifier": "CurrentTemperature",
          "name": "温度",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "-40",
              "max": "120",
              "unit": "℃",
              "unitName": "摄氏度",
              "step": "0.01"
            }
          }
        }
      ],
      "outputData": []
    },
    {
      "identifier": "get",
      "name": "get",
      "required": true,
      "callType": "async",
      "desc": "属性获取",
      "method": "thing.service.property.get",
      "inputData": [
        "GeoLocation",
        "CurrentHumidity",
        "CurrentTemperature"
      ],
      "outputData": [
        {
          "identifier": "GeoLocation",
          "name": "地理位置",
          "dataType": {
            "type": "struct",
            "specs": [
              {
                "identifier": "Longitude",
                "name": "经度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-180",
                    "max": "180",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Latitude",
                "name": "纬度",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "-90",
                    "max": "90",
                    "unit": "°",
                    "unitName": "度",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "Altitude",
                "name": "海拔",
                "dataType": {
                  "type": "double",
                  "specs": {
                    "min": "0",
                    "max": "9999",
                    "unit": "m",
                    "unitName": "米",
                    "step": "0.01"
                  }
                }
              },
              {
                "identifier": "CoordinateSystem",
                "name": "坐标系统",
                "dataType": {
                  "type": "enum",
                  "specs": {
                    "1": "WGS_84",
                    "2": "GCJ_02"
                  }
                }
              }
            ]
          }
        },
        {
          "identifier": "CurrentHumidity",
          "name": "湿度",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "0",
              "max": "100",
              "unit": "%",
              "unitName": "百分比",
              "step": "0.01"
            }
          }
        },
        {
          "identifier": "CurrentTemperature",
          "name": "温度",
          "dataType": {
            "type": "double",
            "specs": {
              "min": "-40",
              "max": "120",
              "unit": "℃",
              "unitName": "摄氏度",
              "step": "0.01"
            }
          }
        }
      ]
    }
  ]
}

image.png

2、设备端通过开源MQTT SDK上传数据,基于开源JAVA MQTT Client连接阿里云IoT

import com.alibaba.taro.AliyunIoTSignUtil;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.util.HashMap;
import java.util.Map;

public class IoTDemoPubSubDemo {

    // 设备三元组信息
    public static String productKey = "a1kVH******";
    public static String deviceName = "device1";
    public static String deviceSecret = "XADek3EYXzzTtxJ6a****************";
    public static String regionId = "cn-shanghai";

    // 物模型-属性上报topic
    private static String pubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
    // 物模型-属性订阅topic
    private static String subTopic = "/sys/" + productKey + "/" + deviceName + "/thing/service/property/set";
    private static MqttClient mqttClient;

    public static void main(String [] args){

        initAliyunIoTClient();
        // 汇报属性
        postDeviceProperties();
        try {
            mqttClient.subscribe(subTopic); // 订阅Topic
        } catch (MqttException e) {
            System.out.println("error:" + e.getMessage());
            e.printStackTrace();
        }

        // 设置订阅监听
        mqttClient.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable throwable) {
                System.out.println("connection Lost");
            }

            @Override
            public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
                System.out.println("Sub message");
                System.out.println("Topic : " + s);
                System.out.println(new String(mqttMessage.getPayload())); //打印输出消息payLoad
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

            }
        });

    }

    /**
     * 初始化 Client 对象
     */
    private static void initAliyunIoTClient() {

        try {
            // 构造连接需要的参数
            String clientId = "java" + System.currentTimeMillis();
            Map<String, String> params = new HashMap<>(16);
            params.put("productKey", productKey);
            params.put("deviceName", deviceName);
            params.put("clientId", clientId);
            String timestamp = String.valueOf(System.currentTimeMillis());
            params.put("timestamp", timestamp);
            // cn-shanghai
            String targetServer = "tcp://" + productKey + ".iot-as-mqtt."+regionId+".aliyuncs.com:1883";

            String mqttclientId = clientId + "|securemode=3,signmethod=hmacsha1,timestamp=" + timestamp + "|";
            String mqttUsername = deviceName + "&" + productKey;
            String mqttPassword = AliyunIoTSignUtil.sign(params, deviceSecret, "hmacsha1");

            connectMqtt(targetServer, mqttclientId, mqttUsername, mqttPassword);

        } catch (Exception e) {
            System.out.println("initAliyunIoTClient error " + e.getMessage());
        }
    }

    public static void connectMqtt(String url, String clientId, String mqttUsername, String mqttPassword) throws Exception {

        MemoryPersistence persistence = new MemoryPersistence();
        mqttClient = new MqttClient(url, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        // MQTT 3.1.1
        connOpts.setMqttVersion(4);
        connOpts.setAutomaticReconnect(false);
//        connOpts.setCleanSession(true);
        connOpts.setCleanSession(false);

        connOpts.setUserName(mqttUsername);
        connOpts.setPassword(mqttPassword.toCharArray());
        connOpts.setKeepAliveInterval(60);

        mqttClient.connect(connOpts);
    }

    /**
     * 汇报属性
     */
    private static void postDeviceProperties() {

        try {
            System.out.println("上报属性值");
            String payloadJson = "{\"params\":{\"CurrentHumidity\":12.3,\"CurrentTemperature\":12.3,\"GeoLocation\":{\"CoordinateSystem\":1,\"Latitude\":29.93089,\"Longitude\":121.59923,\"Altitude\":10}}}";
            // https://yq.aliyun.com/articles/706989
            MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
            message.setQos(1);
            mqttClient.publish(pubTopic, message);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

payLoad设备参考链接

3、属性上报情况查看

image.png

4、通过物联网数据分析中的二维数据可视化功能,接入设备位置到地图

image.png

image.png

image.png

5、物联网数据分析通过SQL将数据下发至设备


5.1 查询数据
image.png
image.png

image.png

5.2 使用SQL下发数据

insert into ${pk.a1kVH******.device1} select 53.3 as CurrentHumidity;  -- 导入数据到表,下发数据到设备

image.png

5.3 设备端订阅情况

上报属性值
Sub message
Topic : /sys/a1kVH******/device1/thing/service/property/set
{"method":"thing.service.property.set","id":"419651605","params":{"CurrentHumidity":53.3},"version":"1.0.0"}

参考链接
快速接入设备位置到地图
数据开发之分析决策直达设备

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
中国厂商零突破!阿里云获AM魔力象限提名
近日,国际权威咨询机构Gartner发布《Magic Quadrant for Access Management》魔力象限报告,在访问管理领域有强大产品功能和丰富实践案例的阿里云被提及,打破了多年来国内无厂商进入报告的现状,实现零突破。
193 0
拔得头筹 | 阿里云混合云荣膺IPv6最佳实践奖
10月21日,由全球IPv6论坛及下一代互联网国家工程中心主办的“2021全球IPv6下一代互联网峰会”在广州南沙开幕。作为下一代互联网产业领域的全球性年度盛会,来自全球产业精英通过线上线下的方式齐聚一堂,共研下一代互联网新技术,共议全球下一代互联网新生态,全面推进我国IPv6商用部署,推动全球下一代互联网产业蓬勃发展。
320 0
阿里云创峰会•遂昌专场圆满结束,阿里云携手遂昌共话“数字绿谷”发展未来
聚焦遂昌数字生态经济发展,从“科创、文创、农创”三个方向,共同探讨遂昌“数字绿谷”建设。
208 0
阿里云总监课第四期:软硬件全栈专家褚霸携专家团独家分享弹性计算最佳实践
对于很多公共云用户来说,弹性计算是上云第一站。课程由弹性计算一线专家团队倾力打造,以理论讲解和实际操作结合的方式,从省钱窍门、使用误区等角度切入,覆盖背后的技术原理和架构方案,在带您找到弹性计算最佳打开方式的同时,让您建立起完善的知识体系。
2139 0
阿里云视频点播转码多场景化最佳实践
在面对不同行业用户丰富的转码场景需求时,如何将用户偏向自身业务特性的定制化需求通用化、产品化并赋予给其他用户使用?这是点播转码一直在思考并努力去解决的,本文由阿里云视频点播技术团队张立磊撰写,讲述视频点播针对多种业务场景提供的适应多场景化的转码处理方案,如何实现将用户定制化的场景需求进行抽象提取,最终以适用于其他点播用户的同样或类似的业务场景需求。
4444 0
一篇文章读懂阿里云企业级数据库最佳实践
今天阿里数据库不再是简单的电商业务,而是涵盖了视频娱乐、IM、地图、在线零售、新零售、物流、在线旅游、音乐、IoT等纵多领域。2017年双十一交易额达1682亿,数据库交易峰值也以数十倍的速度在增长。
4293 0
关于举办“天德π客”创业论坛——“基于阿里云的大数据实践—海量日志分析”的通知
随着互联网、云计算、物联网、社交网络等技术的兴起和普及,全球数据的增长快于任何一个时期,可以称作是爆炸性增长。收集大量数据,并在数据中发现趋势,能使企业能够更快、更平稳、更有效地发展。然而,大数据对许多企业和数据专业人员来说,它仍然很难理解,那么,什么是大数据分析?如何利用阿里云数加平台进行海量数据分析,帮助企业更好地利用数据资源?“天德π客”众创空间特举办本期论坛——“基于阿里云的大数据实践——海量日志分析”,邀请华北电力大学电力系统及其自动化博士,阿里云大数据高级认证讲师宋亚奇主讲。
2999 0
278
文章
0
问答
来源圈子
更多
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载