UTC Conversion using Function

This works !!!

could you help me understand if we can use the shift operator to read the data from start to end .
image
instead of the loops that has been used

 
    var newMsg = {};
  
 
 

var data_time =  msg.payload.data.substr(16, 8)  ;
var Temp = Number(hexTo8p8(msg.payload.data.substr(16+8, 4)));
var Humidity = Number(hexTo8p8(msg.payload.data.substr(16 + 8+4, 4)));
var data =  msg.payload.data.substr(16, 8);

  for(var i=1;i<=30;i++)
  {
      data_time = msg.payload.data.substr(16 * i, 8  );
      Temp = Number(hexTo8p8(msg.payload.data.substr(24 * i, 4  )));
      Humidity = Number(hexTo8p8(msg.payload.data.substr(28 * i, 4  )));
      data = msg.payload.data.substr(16 * i, 8 );
      let UTC_ISO = new Date(parseInt(msg.payload.data.substr(16 * i, 8), 16) * 1000).toISOString();
      var converted_payload = UTC_ISO.split(/\.|T/).slice(0, 2).join(" ");
      
      var checktime = 1546337502;
      if(checktime>data_time)
      {

      }

      
      newMsg.payload = [{
        
          time: data_time,
          utc: converted_payload,
 
          temp: Temp,
          humidity: Humidity,
       
      },
      {

          history: 1 
   



      }];
      node.send(newMsg);

 
  }  


 
 
     
        
   
 


return null;

function calculateDistance(rssi) {
    let P = -69; // @TODO This value should come from MQTT message
    let n = 3;
    let d = Math.pow(10, ((P - rssi) / (10 * n))); //(n ranges from 2 to 4)
    return d * 2;
}


function hexTo8p8(/** @type {string} */ value) {
    let intValue = parseInt(value, 16);
    let signed = (intValue & 0x8000) > 0 ? -1/10 : 1;
    return signed * intValue / Math.pow(2, 8);
}



function int2float( /** @type {string} */value_int) {
    // Create new Buffer based on array bytes
    const buf = Buffer.from(value_int);
    // swap the words
    const temp0 = buf[0];
    const temp1 = buf[1];
    buf[0] = buf[2];
    buf[1] = buf[3];
    buf[2] = temp0;
    buf[3] = temp1;

    // Represent these bytes as 32-bit unsigned int
    const value = buf.readInt32BE();

    // save the value
    msg.payload = value;

    return msg;
}


Similarly can we convert it to unix time stamp in the same way ?

This looks like a job for node-red-contrib-buffer-parser

I think you should probably be using node-red-contrib-buffer-parser to decode your buffer. That node is designed for exactly this sort of issue.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.