本文主要內容:
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進行查詢: