1.整體技術方案
在IoT場景中,很多傳感器采集到的都是二進制資料,或者私有協定格式資料流,裝置端又不具備轉換成結構化JSON的能力,這時候我們可以借助IoT物聯網平台雲端自定義資料解析能力,轉換Modbus,電力協定,hex資料,私有協定為結構化的JSON,再流轉到業務系統。
資料流轉鍊路

消息變化
2.物聯網平台開發
消息通信Topic
hex轉換腳本配置
原始資料:0x035e8192fd0000000d0000001b00000a8c
資料業務格式:
腳本配置
完整腳本内容
/**
* 将裝置自定義topic資料轉換為json格式資料, 裝置上報資料到物聯網平台時調用
* 入參:topic 字元串,裝置上報消息的topic
* 入參:rawData byte[]數組 不能為空
* 出參:jsonObj JSON對象 不能為空
*/
function transformPayload(topic, rawData) {
var jsonObj = {}
//原始hex資料 : 0x035e8192fd0000000d0000001b00000a8c
/*
{
"heartbeat": 15,
"id": 1585549855,
"steps": 2700,
"speed": 56
}
*/
if (topic.endsWith('/user/update')) {
var uint8Array = new Uint8Array(rawData.length);
for (var i = 0; i < rawData.length; i++) {
uint8Array[i] = rawData[i] & 0xff;
}
var dataView = new DataView(uint8Array.buffer, 0);
var fHead = uint8Array[0]; // command
if (fHead == 0x03) {
//
jsonObj['id'] = dataView.getInt32(1);
//心跳
jsonObj['heartbeat'] = dataView.getInt32(5);
//速度
jsonObj['speed'] = dataView.getInt32(9);
//總步數
jsonObj['steps'] = dataView.getInt32(13);
}
}
return jsonObj;
}
3.裝置開發
裝置上報hex原始資料
// 消息Topic攜帶?_sn=default辨別
const topic = '/aiDerw9823s/dn308/user/update'+'?_sn=default';
// 原始資料
var payloadArray = [ 3, 94, 129, 169, 59, 0, 0, 0, 23, 0, 0, 0, 79, 0, 0, 30, 220 ];
var payload = new Buffer(payloadArray);
// 釋出資料到topic
client.publish(topic, payload);
4.聯調日志
裝置上報原始hex資料
腳本轉換後日志
業務消息封包日志
消息詳情(topic和payload)