⬇️Flood Sensor Downlink encoder

Universal Encoder:

Supports: The Thinks Network, Milesight, DataCake, Chirpstack

// Milesight
function Encode(port, obj) {
    var encoded = encodeDownlink({ fPort: port, data: obj }).bytes;
    return encoded;
  }
  
  function Encoder(port, obj) {
    var encoded = encodeDownlink({ fPort: port, data: obj }).bytes;
    return encoded;
  }
  // The Things Industries / Main
  function encodeDownlink(input) {
    var bytes = [];
    for (key in input.data) {
      switch (key) {
        case "getKeepAliveTime": {
          bytes.push(0x12);
          break;
        }
        case "setJoinRetryPeriod": {
          var periodToPass = (input.data.setJoinRetryPeriod * 60) / 5;
          periodToPass = int(periodToPass);
          bytes.push(0x10);
          bytes.push(periodToPass);
          break;
        }
        case "getJoinRetryPeriod": {
          bytes.push(0x19);
          break;
        }
  
        case "setUplinkType": {
          bytes.push(0x11);
          bytes.push(input.data.setUplinkType);
          break;
        }
        case "getUplinkType": {
          bytes.push(0x1B);
          break;
        }
        case "setWatchDogParams": {
          bytes.push(0x1C);
          bytes.push(input.data.SetWatchDogParams.confirmedUplinks);
          bytes.push(input.data.SetWatchDogParams.unconfirmedUplinks);
          break;
        }
        case "getWatchDogParams": {
          bytes.push(0x1D);
          break;
        }
        case "setFloodAlarmTime": {
          bytes.push(0x04);
          // Convert time parameter to bytes
          const time = input.data.setFloodAlarmTime;
          bytes.push(time);
          break;
        }
        case "setKeepAlive": {
          bytes.push(0x05);
          // Set the keep alive time value
          bytes.push(input.data.setKeepAlive);
          break;
        }
        case "getFloodAlarmTime": {
          bytes.push(0x06);
          break;
        }
        case "getDeviceVersion": {
          bytes.push(0x07);
          break;
        }
        case "setFloodEventSendTime": {
          bytes.push(0x08);
          // Convert time parameter to bytes
          const time = input.data.setFloodEventSendTime;
          bytes.push(time);
          break;
        }
        case "getFloodEventSendTime": {
          bytes.push(0x09);
          break;
        }
        case "setFloodEventUplinkType": {
          bytes.push(0x13);
          // Set the uplink type value
          bytes.push(input.data.setFloodEventUplinkType);
          break;
        }
        case "getFloodEventUplinkType": {
          bytes.push(0x14);
          break;
        }
      
        default: {
        }
      }
    }
  
    return {
      bytes: bytes,
      fPort: 1,
      warnings: [],
      errors: [],
    };
  }
  
  function decodeDownlink(input) {
    return {
      data: {
        bytes: input.bytes,
      },
      warnings: [],
      errors: [],
    };
  }
  

Last updated

Was this helpful?