開發與維運

藉助IoT平臺雲端數據解析能力,轉換Modbus,電力協議,hex數據

1.整體技術方案

在IoT場景中,很多傳感器採集到的都是二進制數據,或者私有協議格式數據流,設備端又不具備轉換成結構化JSON的能力,這時候我們可以藉助IoT物聯網平臺雲端自定義數據解析能力,轉換Modbus,電力協議,hex數據,私有協議為結構化的JSON,再流轉到業務系統。

數據流轉鏈路

image.png

消息變化

image.png

2.物聯網平臺開發

消息通信Topic

image.png

hex轉換腳本配置

原始數據:0x035e8192fd0000000d0000001b00000a8c
數據業務格式:
image.png
腳本配置
image.png

完整腳本內容

/**
 * 將設備自定義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數據

image.png

腳本轉換後日志

image.png

業務消息報文日誌

消息詳情(topic和payload)
image.png

Leave a Reply

Your email address will not be published. Required fields are marked *