阿里云物联网平台物模型功能测试示例Demo

简介: 物模型指将物理空间中的实体数字化,并在云端构建该实体的数据模型。在物联网平台中,定义物模型即定义产品功能。完成功能定义后,系统将自动生成该产品的物模型。物模型描述产品是什么,能做什么,可以对外提供哪些服务。物模型将产品功能类型分为三类:属性、服务、和事件。定义了这三类功能,即完成了物模型的定义。

概述

物模型指将物理空间中的实体数字化,并在云端构建该实体的数据模型。在物联网平台中,定义物模型即定义产品功能。完成功能定义后,系统将自动生成该产品的物模型。物模型描述产品是什么,能做什么,可以对外提供哪些服务。物模型将产品功能类型分为三类:属性、服务、和事件。定义了这三类功能,即完成了物模型的定义。

功能类型 说明
属性(Property) 一般用于描述设备运行时的状态,如环境监测设备所读取的当前环境温度等。属性支持GET和SET请求方式。应用系统可发起对属性的读取和设置请求。
服务(Service) 设备可被外部调用的能力或方法,可设置输入参数和输出参数。相比于属性,服务可通过一条指令实现更复杂的业务逻辑,如执行某项特定的任务。
事件(Event) 设备运行时的事件。事件一般包含需要被外部感知和处理的通知信息,可包含多个输出参数。如,某项任务完成的信息,或者设备发生故障或告警时的温度等,事件可以被订阅和推送。

使用: 设备端可以上报属性和事件;云端可以向设备端发送设置属性和调用服务的指令。

Step By Step

1、产品物模型model.json(替换产品productKey导入即可)

{
    "schema":"https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
    "profile":{
        "productKey":"a1qLU******"
    },
    "properties":[
        {
            "identifier":"Temperature",
            "name":"温度",
            "accessMode":"rw",
            "desc":"电机工作温度",
            "required":false,
            "dataType":{
                "type":"float",
                "specs":{
                    "min":"-55",
                    "max":"200",
                    "unit":"℃",
                    "step":"0.1"
                }
            }
        }
    ],
    "events":[
        {
            "identifier":"post",
            "name":"post",
            "type":"info",
            "required":true,
            "desc":"属性上报",
            "method":"thing.event.property.post",
            "outputData":[
                {
                    "identifier":"Temperature",
                    "name":"温度",
                    "dataType":{
                        "type":"float",
                        "specs":{
                            "min":"-55",
                            "max":"200",
                            "unit":"℃",
                            "step":"0.1"
                        }
                    }
                }
            ]
        },
        {
            "identifier":"event1",
            "name":"异常事件",
            "type":"info",
            "required":false,
            "method":"thing.event.event1.post",
            "outputData":[
                {
                    "identifier":"event1",
                    "name":"事件参数1",
                    "dataType":{
                        "type":"int",
                        "specs":{
                            "min":"1",
                            "max":"100",
                            "step":"1"
                        }
                    }
                }
            ]
        }
    ],
    "services":[
        {
            "identifier":"set",
            "name":"set",
            "required":true,
            "callType":"async",
            "desc":"属性设置",
            "method":"thing.service.property.set",
            "inputData":[
                {
                    "identifier":"Temperature",
                    "name":"温度",
                    "dataType":{
                        "type":"float",
                        "specs":{
                            "min":"-55",
                            "max":"200",
                            "unit":"℃",
                            "step":"0.1"
                        }
                    }
                }
            ],
            "outputData":[
                
            ]
        },
        {
            "identifier":"get",
            "name":"get",
            "required":true,
            "callType":"async",
            "desc":"属性获取",
            "method":"thing.service.property.get",
            "inputData":[
                "Temperature"
            ],
            "outputData":[
                {
                    "identifier":"Temperature",
                    "name":"温度",
                    "dataType":{
                        "type":"float",
                        "specs":{
                            "min":"-55",
                            "max":"200",
                            "unit":"℃",
                            "step":"0.1"
                        }
                    }
                }
            ]
        },
        {
            "identifier":"addFuctionServiceAsync",
            "name":"异步加法服务",
            "required":false,
            "callType":"async",
            "method":"thing.service.addFuctionServiceAsync",
            "inputData":[
                {
                    "identifier":"add1",
                    "name":"加数1",
                    "dataType":{
                        "type":"int",
                        "specs":{
                            "min":"1",
                            "max":"100",
                            "step":"1"
                        }
                    }
                },
                {
                    "identifier":"add2",
                    "name":"加数2",
                    "dataType":{
                        "type":"int",
                        "specs":{
                            "min":"1",
                            "max":"100",
                            "step":"1"
                        }
                    }
                }
            ],
            "outputData":[
                {
                    "identifier":"result",
                    "name":"结果",
                    "dataType":{
                        "type":"int",
                        "specs":{
                            "min":"1",
                            "max":"200",
                            "step":"1"
                        }
                    }
                }
            ]
        },
        {
            "identifier":"addFuctionServiceSync",
            "name":"加法服务",
            "required":false,
            "callType":"sync",
            "desc":"同步功能",
            "method":"thing.service.addFuctionServiceSync",
            "inputData":[
                {
                    "identifier":"add2",
                    "name":"加数2",
                    "dataType":{
                        "type":"int",
                        "specs":{
                            "min":"1",
                            "max":"100",
                            "step":"1"
                        }
                    }
                },
                {
                    "identifier":"add1",
                    "name":"加数1",
                    "dataType":{
                        "type":"int",
                        "specs":{
                            "min":"1",
                            "max":"100",
                            "step":"1"
                        }
                    }
                }
            ],
            "outputData":[
                {
                    "identifier":"result",
                    "name":"计算结果",
                    "dataType":{
                        "type":"int",
                        "specs":{
                            "min":"1",
                            "max":"200",
                            "step":"1"
                        }
                    }
                }
            ]
        }
    ]
}

2、设备端Code Sample(基于开源Java MQTT Client)

import com.alibaba.fastjson.JSONObject;
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 IoTThingTest {

    public static String productKey = "a1qLU******";
    public static String deviceName = "device1";
    public static String deviceSecret = "cA9wzIM6bL2QI6DgAaSO0FPg********";
    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/event/property/post_reply";
    // 物模型-事件上报topic
    private static String eventPubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/event1/post";

    // 物模型-事件上报响应topic
    private static String eventSubTopic = "/sys/" + productKey + "/" + deviceName + "/thing/event/event1/post_reply";


    private static MqttClient mqttClient;

    public static void main(String [] args){

        // 初始化Client
        initAliyunIoTClient();
        try {
            mqttClient.subscribe(subTopic); // 订阅Topic
            mqttClient.subscribe(eventSubTopic);
        } 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

                // 服务调用处理
                if (s.contains("Async") || s.contains("rrpc")) {
                    String content = new String((byte[]) mqttMessage.getPayload());
                    System.out.println("服务请求Topic:" + s);
                    System.out.println("服务指令:" + content);

                    JSONObject request = JSONObject.parseObject(content);
                    JSONObject params = request.getJSONObject("params");
                    if (!params.containsKey("add1")) { // 检查入参
                        System.out.println("不包含参数add1");
                        return;
                    }
                    Integer input1 = params.getInteger("add1"); // 获取入参
                    Integer input2 = params.getInteger("add2"); // 获取入参
                    JSONObject response = new JSONObject();
                    JSONObject data = new JSONObject();
                    data.put("result", input1 + input2);
                    response.put("id", request.get("id"));
                    response.put("code", 200);
                    response.put("data", data);

                    String responseTopic = s;
                    // 服务响应
                    if (s.contains("rrpc")) {
                        // 同步服务调用响应Topic
                        responseTopic = s.replace("request", "response");
                    } else {
                        // 异步服务调用响应Topic
                        responseTopic = responseTopic + "_reply";
                    }
                    MqttMessage message1 = new MqttMessage(response.toString().getBytes("utf-8"));
                    System.out.println("responseTopic: " + responseTopic);
                    mqttClient.publish(responseTopic, message1);
                }
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

            }
        });

        // 属性上报
        postDeviceProperties();
        // 事件上报
        postDeviceEvent();
    }

    /**
     * 初始化 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.setConnectionTimeout(10);
//        connOpts.setCleanSession(true);
        connOpts.setCleanSession(false);

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

        mqttClient.connect(connOpts);
    }

    /**
     * 事件上报
     */
    private static void postDeviceEvent() {

        try {
            //上报数据
            //高级版 物模型-属性上报payload
            System.out.println("事件上报");
            String payloadJson = "{\"params\":{\"event1\":23}}";
            MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
            message.setQos(1);
            mqttClient.publish(eventPubTopic, message);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

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

        try {
            //上报数据
            //高级版 物模型-属性上报payload
            System.out.println("上报属性值");
            String payloadJson = "{\"params\":{\"Temperature\":13}}";
            MqttMessage message = new MqttMessage(payloadJson.getBytes("utf-8"));
            message.setQos(1);
            mqttClient.publish(pubTopic, message);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

参考链接:基于开源JAVA MQTT Client连接阿里云IoT

3、启动设备查看属性及事件上报情况

  • 3.1 服务端情况

_

_

  • 3.2 设备端reply情况
上报属性值
事件上报
Sub message
Topic : /sys/a1qLU******/device1/thing/event/property/post_reply
{"code":200,"data":{},"id":"null","message":"success","method":"thing.event.property.post","version":"1.0"}
Sub message
Topic : /sys/a1qLU******/device1/thing/event/event1/post_reply
{"code":200,"data":{},"id":"null","message":"success","method":"thing.event.event1.post","version":"1.0"}

4、异步服务调用

  • 4.1 说明

通过InvokeThingServiceInvokeThingsService接口调用服务,物联网平台采用异步方式下行推送请求,设备也采用异步方式返回结果。 此时,服务选择为异步调用方式,物联网平台订阅此处的异步响应Topic。异步调用的结果,可以使用规则引擎数据流转功能获取,也可以使用服务端订阅获取。

_

  • 4.3 设备端日志
Sub message
Topic : /sys/a1qLU******/device1/thing/service/addFuctionServiceAsync
{"method":"thing.service.addFuctionServiceAsync","id":"2015524670","params":{"add2":2,"add1":2},"version":"1.0.0"}
服务请求Topic:/sys/a1qLU******/device1/thing/service/addFuctionServiceAsync
服务指令:{"method":"thing.service.addFuctionServiceAsync","id":"2015524670","params":{"add2":2,"add1":2},"version":"1.0.0"}
responseTopic: /sys/a1qLU******/device1/thing/service/addFuctionServiceAsync_reply
  • 4.4 控制台日志

_

  • 4.5 AMQP 服务端订阅获取的服务响应
Content:{"iotId":"*******","code":200,"data":{"result":4},"requestId":"2009566194","topic":"/sys/*******/device1/thing/service/addFuctionServiceAsync_reply","source":"DEVICE","gmtCreate":1583658474852,"productKey":"a1qLU******","deviceName":"device1"}

5、同步服务调用

  • 5.1 说明

通过InvokeThingServiceInvokeThingsService接口调用服务,物联网平台直接使用RRPC同步方式下行推送请求。此时,服务选择为同步调用方式,物联网平台订阅RRPC对应Topic。

_

  • 5.3 设备端日志
Sub message
Topic : /sys/a1qLU******/device1/rrpc/request/1236608506958775809
{"method":"thing.service.addFuctionServiceSync","id":"2015301903","params":{"add2":2,"add1":2},"version":"1.0.0"}
服务请求Topic:/sys/a1qLU******/device1/rrpc/request/1236608506958775809
服务指令:{"method":"thing.service.addFuctionServiceSync","id":"2015301903","params":{"add2":2,"add1":2},"version":"1.0.0"}
responseTopic: /sys/a1qLU******/device1/rrpc/response/1236608506958775809
  • 5.4 控制台日志

_

更多参考

设备属性、事件、服务
同步服务调用
基于开源Java MQTT Client的阿里云物联网平台RRPC功能测试
阿里云物联网平台规则引擎综述

相关实践学习
钉钉群中如何接收IoT温控器数据告警通知
本实验主要介绍如何将温控器设备以MQTT协议接入IoT物联网平台,通过云产品流转到函数计算FC,调用钉钉群机器人API,实时推送温湿度消息到钉钉群。
阿里云AIoT物联网开发实战
本课程将由物联网专家带你熟悉阿里云AIoT物联网领域全套云产品,7天轻松搭建基于Arduino的端到端物联网场景应用。 开始学习前,请先开通下方两个云产品,让学习更流畅: IoT物联网平台:https://iot.console.aliyun.com/ LinkWAN物联网络管理平台:https://linkwan.console.aliyun.com/service-open
相关文章
|
2月前
|
存储 弹性计算 运维
阿里云服务器ECS经济型e实例详细介绍_性能测试和租用价格
阿里云服务器ECS经济型e实例详细介绍_性能测试和租用价格,阿里云服务器ECS推出经济型e系列,经济型e实例是阿里云面向个人开发者、学生、小微企业,在中小型网站建设、开发测试、轻量级应用等场景推出的全新入门级云服务器,CPU采用Intel Xeon Platinum架构处理器,支持1:1、1:2、1:4多种处理器内存配比,e系列性价比优选
|
4月前
|
架构师 测试技术 网络性能优化
dpdk课程学习之练习笔记七(vpp环境搭建及plugin demo测试)
dpdk课程学习之练习笔记七(vpp环境搭建及plugin demo测试)
163 0
|
4月前
|
存储 测试技术 区块链
阿里云、百度云及移动云对象存储横向性能对比测试
在企业的数字化转型进程中,我们观察到越来越多的公司将其IT基础设施迁移到云端。随着企业业务的持续运营,无论是储存、处理、分享还是删除,都会产生大量的数据,这就要求有一个既可靠又高效的系统来管理和存储这些信息。对象存储产品在这个场景中扮演了至关重要的角色。它们以一种可扩展、安全、持久的方式,有效地满足了对大规模非结构化数据存储的需求。 尽管市场上云计算提供商众多,各自都有自己独特的对象存储产品,面对这样的丰富选择,如何寻找最符合企业需求的产品呢?这正是企业今天寻求解答的问题。 在本篇文章中,我们将深入进行一项横向对比测试,专门对阿里云OSS、百度云BOS和移动云EOS这三大云服务提供商的对象
1363 0
|
2月前
|
消息中间件 DataWorks 物联网
MQTT问题之接入阿里云物联网平台如何解决
MQTT接入是指将设备或应用通过MQTT协议接入到消息服务器,以实现数据的发布和订阅;本合集着眼于MQTT接入的流程、配置指导以及常见接入问题的解决方法,帮助用户实现稳定可靠的消息交换。
170 1
|
8天前
|
消息中间件 存储 JavaScript
阿里云IOC物联网异步处理基础概念
该内容介绍了异步处理基础和消息队列的相关概念。首先,同步处理指任务完成后才能执行其他操作,而异步则允许任务并行执行,不阻塞程序。异步能提高系统并发性和响应性,但也增加复杂性和资源消耗。接着,提到了消息队列,包括生产者、消费者、队列、broker和topic等概念,并指出在IoT中,设备作为生产者发送消息到特定topic,消费者从队列获取数据。最后,简要介绍了AMQP协议,它是用于应用程序间消息传递的开放标准,常用于分布式系统和物联网,如RabbitMQ和Apache Qpid。课程将以Apache Qpid为例接收IoT数据。
119 6
阿里云IOC物联网异步处理基础概念
|
1月前
|
弹性计算 缓存 测试技术
阿里云2核4g服务器(费用价格/性能测试/支持人数)
阿里云2核4g服务器能支持多少人访问?2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
1月前
|
弹性计算 缓存 测试技术
2核4g服务器能支持多少人访问?阿里云2核4G服务器并发数测试
2核4g服务器能支持多少人访问?阿里云2核4G服务器并发数测试,2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
2月前
|
弹性计算 数据中心
阿里云香港服务器详细介绍_BGP多线精品测试_CN2高速网络
阿里云香港服务器中国香港数据中心网络线路类型BGP多线精品,中国电信CN2高速网络高质量、大规格BGP带宽,运营商精品公网直连中国内地,时延更低,优化海外回中国内地流量的公网线路,可以提高国际业务访问质量。阿里云百科来详细介绍阿里云香港云服务器
|
2月前
|
存储 弹性计算 运维
阿里云经济型e实例详细介绍_性能测试_使用限制说明
阿里云服务器ECS推出经济型e系列,经济型e实例是阿里云面向个人开发者、学生、小微企业,在中小型网站建设、开发测试、轻量级应用等场景推出的全新入门级云服务器,CPU采用Intel Xeon Platinum架构处理器
|
2月前
|
弹性计算 测试技术 数据中心
阿里云香港服务器BGP多线精品网络_CN2性能测试_中国香港主机测试
阿里云香港服务器BGP多线精品网络_CN2性能测试_中国香港主机测试,阿里云香港服务器中国香港数据中心网络线路类型BGP多线精品,中国电信CN2高速网络高质量、大规格BGP带宽,运营商精品公网直连中国内地,时延更低,优化海外回中国内地流量的公网线路,可以提高国际业务访问质量

相关产品

  • 物联网平台