⬇️MClimate CO2 PIR lite 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 "setKeepAlive": {
bytes.push(0x02);
bytes.push(input.data.setKeepAlive);
break;
}
case "getKeepAliveTime": {
bytes.push(0x12);
break;
}
case "getDeviceVersions": {
bytes.push(0x04);
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 "setCo2BoundaryLevels": {
var good_medium = input.data.setCo2BoundaryLevels.good_medium;
var medium_bad = input.data.setCo2BoundaryLevels.medium_bad;
var good_mediumFirstPart = good_medium & 0xff;
var good_mediumSecondPart = (good_medium >> 8) & 0xff;
var medium_badFirstPart = medium_bad & 0xff;
var medium_badSecondPart = (medium_bad >> 8) & 0xff;
bytes.push(0x1E);
bytes.push(good_mediumSecondPart);
bytes.push(good_mediumFirstPart);
bytes.push(medium_badSecondPart);
bytes.push(medium_badFirstPart);
break;
}
case "getCo2BoundaryLevels": {
bytes.push(0x1F);
break;
}
case "setAutoZeroValue": {
var autoZeroValue = input.data.setAutoZeroValue;
var autoZeroValueFirstPart = autoZeroValue & 0xff;
var autoZeroValueSecondPart = (autoZeroValue >> 8) & 0xff;
bytes.push(0x20);
bytes.push(autoZeroValueSecondPart);
bytes.push(autoZeroValueFirstPart);
break;
}
case "getAutoZeroValue": {
bytes.push(0x21);
break;
}
case "setCo2MeasurementPeriod": {
var good_zone = input.data.setCo2MeasurementPeriod.good_zone;
var medium_zone = input.data.setCo2MeasurementPeriod.medium_zone;
var bad_zone = input.data.setCo2MeasurementPeriod.bad_zone;
bytes.push(0x24);
bytes.push(good_zone);
bytes.push(medium_zone);
bytes.push(bad_zone);
break;
}
case "getCo2MeasurementPeriod": {
bytes.push(0x25);
break;
}
case "setCo2AutoZeroPeriod": {
bytes.push(0x2A);
bytes.push(input.data.setCo2AutoZeroPeriod);
break;
}
case "getCo2AutoZeroPeriod": {
bytes.push(0x2B);
break;
}
case "setUplinkSendingOnButtonPress": {
bytes.push(0x2E);
bytes.push(input.data.setUplinkSendingOnButtonPress);
break;
}
case "getUplinkSendingOnButtonPress": {
bytes.push(0x2F);
break;
}
case "setPIRSensorStatus": {
bytes.push(0x3C);
bytes.push(input.data.setPIRSensorStatus);
break;
}
case "getPIRSensorStatus": {
bytes.push(0x3D);
break;
}
case "setPIRSensorSensitivity": {
bytes.push(0x3E);
bytes.push(input.data.setPIRSensorSensitivity);
break;
}
case "getPIRSensorSensitivity": {
bytes.push(0x3F);
break;
}
case "setPIRMeasurementPeriod": {
bytes.push(0x48);
bytes.push(input.data.setPIRMeasurementPeriod);
break;
}
case "getPIRMeasurementPeriod": {
bytes.push(0x49);
break;
}
case "setPIRCheckPeriod": {
var time = input.data.setPIRCheckPeriod;
var timeFirstPart = time & 0xff;
var timeSecondPart = (time >> 8) & 0xff;
bytes.push(0x4A);
bytes.push(timeSecondPart);
bytes.push(timeFirstPart);
break;
}
case "getPIRCheckPeriod": {
bytes.push(0x4B);
break;
}
case "setPIRBlindPeriod": {
var time = input.data.setPIRBlindPeriod;
var timeFirstPart = time & 0xff;
var timeSecondPart = (time >> 8) & 0xff;
bytes.push(0x4C);
bytes.push(timeSecondPart);
bytes.push(timeFirstPart);
break;
}
case "getPIRBlindPeriod": {
bytes.push(0x4D);
break;
}
case "sendCustomHexCommand": {
var sendCustomHexCommand = input.data.sendCustomHexCommand;
for (var i = 0; i < sendCustomHexCommand.length; i += 2) {
var byte = parseInt(sendCustomHexCommand.substr(i, 2), 16);
bytes.push(byte);
}
break;
}
default: {
}
}
}
return {
bytes: bytes,
fPort: 1,
warnings: [],
errors: [],
};
}
function decodeDownlink(input) {
return {
data: {
bytes: input.bytes,
},
warnings: [],
errors: [],
};
}
// example downlink commands
// {"setPIRBlindPeriod":20} --> 0x4C0014
// {"setCo2MeasurementPeriod":{"good_zone":15,"medium_zone":21,"bad_zone":21}}
// {"sendCustomHexCommand":"080F15"} --> 0x080F15
Last updated
Was this helpful?