本文主要内容:
1. 了解设备孪生的概念,用途
2. 实战:
服务端根据设备ID=”device01“ 修改tag,设置为region=”浦东“,plant="张江高科技园区";
服务端查询plant=”张江高科技园区“的设备并将deviceid列出来;
设备通过reported属性修改connectivity=”cellular“ (当前联网状态为 移动网络)
服务端查询reported属性修改connectivity=”cellular“,并列出device id。
视频讲解:
图文内容:
设备孪生(Device Twin)是什么?
设备孪生是Azure IoT Hub维护的一个Json 数据库,每一个存在于IoT Hub中的设备,都有一个Json文件,同时提供了可以查询/修改这些文件的API/SDK。
设备孪生的Json文件结构?
Tags:
通常用来标记不常变动的数据,比如,当前的设备安装地址,当前设备所属的用户信息,设备的硬件型号等等。这个值只能由服务端进行读/写。
Properties:
Desired 属性:
服务端可读写,设备侧可读,可订阅变更通知事件,通常用法是 服务端进行修改,客户端收到修改并进行操作,例如,服务端修改为 更改上传频率为20秒,设备会订阅到这个变更事件,然后执行修改上传频率为20秒。
Reported属性:
服务端只读,设备侧可读写,通常用法是,设备上报某个值的变更,服务端通过查询,得知设备值是多少。
注意:Desired 和Reported 属性可以配套使用,例如,服务器通知 某个device的 desired 属性版本号从1.0变更为2.0,设备侧收到变更通知,执行下载固件,安装固件,
案例步骤:
本案例参考:https://docs.azure.cn/zh-cn/iot-hub/iot-hub-node-node-twin-getstarted
1. 创建服务端代码,AddTagsAndQuery.js, 代码放到文件夹A中,使用如下服务端代码:
'use strict'; var iothub = require('azure-iothub'); var connectionString = ' your iot hub string'; var registry = iothub.Registry.fromConnectionString(connectionString); registry.getTwin('device01', function(err, twin){ if (err) { console.error(err.constructor.name + ': ' + err.message); } else { var patch = { tags: { location: { region: '浦东', plant: '张江高科技园区' } } }; twin.update(patch, function(err) { if (err) { console.error('Could not update twin: ' + err.constructor.name + ': ' + err.message); } else { console.log(twin.deviceId + ' twin updated successfully'); queryTwins(); } }); } }); var queryTwins = function() { var query = registry.createQuery("SELECT * FROM devices WHERE tags.location.plant = '张江高科技园区'", 100); query.nextAsTwin(function(err, results) { if (err) { console.error('Failed to fetch the results: ' + err.message); } else { console.log("Devices in 张江高科技园区: " + results.map(function(twin) {return twin.deviceId}).join(',')); } }); query = registry.createQuery("SELECT * FROM devices WHERE properties.reported.connectivity.type = 'cellular'", 100); query.nextAsTwin(function(err, results) { if (err) { console.error('Failed to fetch the results: ' + err.message); } else { console.log("Devices using cellular network: " + results.map(function(twin) {return twin.deviceId}).join(',')); } }); };
在文件夹A中,依次执行如下代码准备服务端SDK环境:
npm init --yes npm install azure-iothub --save node AddTagsAndQuery.js
代码执行结果如下,根据张江高科技园区能查询出结果,根据cellular查询不到结果。
2. 创建设备端代码,reportconnectivity.js, 代码放到文件夹B中,使用如下设备侧代码:
'use strict'; var Client = require('azure-iot-device').Client; var Protocol = require('azure-iot-device-mqtt').Mqtt; var connectionString = 'your device string'; var client = Client.fromConnectionString(connectionString, Protocol); client.open(function(err) { if (err) { console.error('could not open IotHub client'); } else { console.log('client opened'); client.getTwin(function(err, twin) { if (err) { console.error('could not get twin'); } else { var patch = { connectivity: { type: 'cellular' } }; twin.properties.reported.update(patch, function(err) { if (err) { console.error('could not update twin'); } else { console.log('twin state reported'); process.exit(); } }); } }); } });
在文件夹B中依次执行如下代码,安装SDK:
npm init --yes npm install azure-iot-device azure-iot-device-mqtt --save node ReportConnectivity.js
执行结果如下:
此时,我们再次执行服务端查询,本次能查询出device01使用的是cellular网络,结果如下,
同时,我们也可以在portal进行查询: