MQTTX运行脚本

/**
* @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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值