Skip to content

LDS03A stopped working in Chripstack v 4.17 #42

Description

@Externalnet

Today my LDS03A stopped working in Chripstack v 4.17
The following events were logged:

level:"ERROR"
code:"UPLINK_CODEC"
description:"JS error: Error: invalid redefinition of global identifier in module code at main:46:10 "
deduplication_id:"b2ade2d4-5a36-4cae-a244-458d0176072d"

Below the AI (Gemini) corrected javascipt decoder for Chripstack 4.17

function decodeUplink(input) {
    return { 
        data: Decode(input.fPort, input.bytes, input.variables)
    };   
}

function datalog(i, bytes) {
  var aa=(bytes[0+i]&0x02)?"TRUE":"FALSE"; 
  var bb=(bytes[0+i]&0x01)?"OPEN":"CLOSE";  
  var cc=(bytes[1+i]<<16 | bytes[2+i]<<8 | bytes[3+i]).toString(10);
  var dd=(bytes[4+i]<<16 | bytes[5+i]<<8 | bytes[6+i]).toString(10);
  var ee= getMyDate((bytes[7+i]<<24 | bytes[8+i]<<16 | bytes[9+i]<<8 | bytes[10+i]).toString(10));
  var string='['+aa+','+bb+','+cc+','+dd+','+ee+']'+',';  
  
  return string;
}

function getzf(c_num){ 
  if(parseInt(c_num) < 10)
    c_num = '0' + c_num; 
  return c_num; 
}

function getMyDate(str){ 
  var c_Date;
  if(str > 9999999999)
     c_Date = new Date(parseInt(str));
  else 
     c_Date = new Date(parseInt(str) * 1000);
  
  var c_Year = c_Date.getFullYear(), 
  c_Month = c_Date.getMonth()+1, 
  c_Day = c_Date.getDate(),
  c_Hour = c_Date.getHours(), 
  c_Min = c_Date.getMinutes(), 
  c_Sen = c_Date.getSeconds();
  return c_Year +'-'+ getzf(c_Month) +'-'+ getzf(c_Day) +' '+ getzf(c_Hour) +':'+ getzf(c_Min) +':'+getzf(c_Sen); 
}

function Decode(fPort, bytes, variables) {
  if(fPort==0x03)
  {
    var data_sum = ""; // Initialiseer data_sum
    for(var i=0;i<bytes.length;i=i+11)
    {
      var data= datalog(i,bytes);
      if(i==0)
        data_sum=data;
      else
        data_sum+=data;
    }
    return{ DATALOG:data_sum };
  }
  else if(fPort==0x04)
  {
    return {
      TDC: bytes[0]<<16 | bytes[1]<<8 | bytes[2],
      DISALARM: bytes[3]&0x01,
      KEEP_STATUS1: bytes[4]&0x01,
      KEEP_TIME1: bytes[5]<<8 | bytes[6],
      KEEP_STATUS2: bytes[7]&0x01,
      KEEP_TIME2: bytes[8]<<8 | bytes[9],
      ALARM_TDC: bytes[10]
    };
  }
  else if(fPort==0x05)
  {
    var sensor = (bytes[0]==0x0A) ? "LDS03A" : "UNKNOWN";
    var freq_bands = ["", "EU868", "US915", "IN865", "AU915", "KZ865", "RU864", "AS923", "AS923_1", "AS923_2", "AS923_3", "CN470", "EU433", "KR920", "MA869"];
    
    return {
      SENSOR_MODEL: sensor,
      FIRMWARE_VERSION: (bytes[1]&0x0f)+'.'+(bytes[2]>>4&0x0f)+'.'+(bytes[2]&0x0f),
      FREQUENCY_BAND: freq_bands[bytes[3]] || "UNKNOWN",
      SUB_BAND: (bytes[4]==0xff) ? "NULL" : bytes[4],
      BAT: (bytes[5]<<8 | bytes[6])/1000,
    };
  }
  else
  { 
    var alarm1=(bytes[0]&0x02)?"TRUE":"FALSE"; 
    var door1_open_status=(bytes[0]&0x01)?"OPEN":"CLOSE";  
    var open1_times=bytes[1]<<16 | bytes[2]<<8 | bytes[3];
    var open1_duration=bytes[4]<<16 | bytes[5]<<8 | bytes[6];
    
    var res = {
      ALARM1:alarm1,
      DOOR1_OPEN_STATUS:door1_open_status,
      DOOR1_OPEN_TIMES:open1_times,
      LAST_DOOR1_OPEN_DURATION:open1_duration,
      TIME: getMyDate((bytes[7]<<24 | bytes[8]<<16 | bytes[9]<<8 | bytes[10]).toString(10))
    };

    if(bytes.length >= 18) {
      res.ALARM2 = (bytes[11]&0x02)?"TRUE":"FALSE";
      res.DOOR2_OPEN_STATUS = (bytes[11]&0x01)?"OPEN":"CLOSE";
      res.DOOR2_OPEN_TIMES = bytes[12]<<16 | bytes[13]<<8 | bytes[14];
      res.LAST_DOOR2_OPEN_DURATION = bytes[15]<<16 | bytes[16]<<8 | bytes[17];
    }
    return res;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions