/** * @description: default script * @param {any} value - Payload * @param {string} msgType - Message type, value is 'received' or 'publish' * @param {number} index - Index of the message, valid only when script is used in the publish message and timed message is enabled * @return {any} - Payload after script processing */ function handlePayload(value, msgType, index) { let hexData = value.replace(/\s+/g, ''); let time = hexData.substring(0,14); let data = hexData.substring(14); let date = parseInt(time.slice(0,4), 16) + '-' + parseInt(time.slice(4,6), 16) + '-' + parseInt(time.slice(6,8), 16) + 'T' + parseInt(time.slice(8,10), 16) + ':' + parseInt(time.slice(10,12), 16) + ':' + parseInt(time.slice(12,14), 16) let size = data.length / 8; let md = []; for( let i = 0; i < size; i++){ md.push(hexToFloat(data.slice(i * 8,i * 8 + 8))); } return date + ' [' + md.join(', ') + ' ]' ; } // 十六进制转换为浮点型高字节顺序(ABCD) function hexToFloat(hex) { // 将十六进制转换为十进制 var decimal = parseInt(hex, 16); // 创建一个32位视图的数组缓冲区 var buffer = new ArrayBuffer(4); // 使用Float32Array视图将缓冲区连接到浮点数 var floatView = new Float32Array(buffer); // 使用Uint32Array视图将缓冲区连接到无符号整数 var intView = new Uint32Array(buffer); // 将十进制数赋值给无符号整数视图 intView[0] = decimal; // 获取浮点数值 var float = floatView[0]; return float; } execute(handlePayload)
02-26
1314

09-29
2514

08-18
626
