Skip to content

Commit 4f3c3d7

Browse files
committed
add some browan/merryiot devices. decoders fixed to pass Fix JS codec / Buffer module unable to import ieee754 module. issue
1 parent 37ebd74 commit 4f3c3d7

24 files changed

Lines changed: 617 additions & 0 deletions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//Browan version:1.0.1
2+
function decodeUplink(input) {
3+
let decoded = {};
4+
switch (input.fPort) {
5+
case 136:
6+
decoded.MovingMode = ((input.bytes[0] - ((input.bytes[0] >> 4) * 16)) % 8) % 2;
7+
decoded.NoGNSSFix = (input.bytes[0] - ((input.bytes[0] >> 4) * 16)) >> 3;
8+
decoded.GNSSerror = (input.bytes[0] >> 4);
9+
decoded.battery_volt = (25 + (input.bytes[1] - ((input.bytes[1] >> 4) * 16))) / 10;
10+
decoded.temperature = input.bytes[2] - 32;
11+
let int_lat = (input.bytes[3] + input.bytes[4] * 256 + input.bytes[5] * 65536 + (input.bytes[6] - ((input.bytes[6] >> 4))) * 16777216);
12+
let int_lon = (input.bytes[7] + input.bytes[8] * 256 + input.bytes[9] * 65536 + (input.bytes[10] - (((input.bytes[10] >> 5) << 1) * 16)) * 16777216);
13+
let bit_lon = ((input.bytes[10] >> 4) % 2);
14+
decoded.accuracy = Math.pow(2, ((input.bytes[10] >> 5) + 2));
15+
decoded.latitude = twocomplement_Lat(int_lat, 27) / 1000000;
16+
decoded.longitude = twocomplement_Long(bit_lon, int_lon, 28) / 1000000;
17+
decoded.altitude = 2;
18+
// Decoded data
19+
return {data: decoded};
20+
}
21+
}
22+
23+
function twocomplement_Lat(inputNum, comtimes) {
24+
let count02 = (Math.pow(2, comtimes + 1)) - 1;
25+
let final_Lat;
26+
if ((inputNum >> comtimes) == 0) {
27+
final_Lat = inputNum;
28+
return final_Lat;
29+
} else {
30+
final_Lat = -(inputNum ^ count02) - 1;
31+
return final_Lat;
32+
}
33+
}
34+
35+
function twocomplement_Long(firstbit, inputNum, comtimes) {
36+
let count02 = (Math.pow(2, comtimes + 1)) - 1;
37+
let final_Long;
38+
if (firstbit == 0) {
39+
final_Long = inputNum;
40+
return final_Long;
41+
} else {
42+
final_Long = -(inputNum ^ count02) - 1;
43+
return final_Long;
44+
}
45+
}
46+
47+
/**
48+
* Encode downlink function.
49+
*
50+
* @param {object} input
51+
* @param {object} input.data Object representing the payload that must be encoded.
52+
* @param {Record<string, string>} input.variables Object containing the configured device variables.
53+
*
54+
* @returns {{bytes: number[]}} Byte array containing the downlink payload.
55+
*/
56+
function encodeDownlink(input) {
57+
return {
58+
// bytes: [225, 230, 255, 0]
59+
};
60+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
////////////* MerryIot CO2 sensor*////////////
2+
3+
//hex to binary function
4+
function hex2bin(hex){
5+
return (parseInt(hex, 16).toString(2)).padStart(8, '0');
6+
}
7+
8+
//MerryIot CO2 sensor
9+
function decodeUplink(input) {
10+
let fPort = input.fPort;
11+
let payloadlens = input.bytes.length;
12+
if(fPort==127 && payloadlens==7){
13+
let intput_list = input.bytes;
14+
let battery_int = intput_list[1];
15+
let battery_volt = (21 + battery_int) / 10;
16+
17+
let temperature_hex = intput_list[3].toString(16).padStart(2, '0') + intput_list[2].toString(16).padStart(2, '0');
18+
let temperature_val = parseInt(temperature_hex, 16);
19+
let temperature = temperature_val > 1250 ? (temperature_val - 65536) / 10 : temperature_val / 10;
20+
21+
let humidity = intput_list[4];
22+
23+
let co2_hex = intput_list[0].toString(16).padStart(2, '0');
24+
let co2_binary = hex2bin(co2_hex);
25+
let trigger_st = co2_binary.substring(7, 8);
26+
let button_st = co2_binary.substring(6, 7);
27+
let co2threshold_st = co2_binary.substring(3, 4);
28+
let co2calibration_st = co2_binary.substring(2, 3);
29+
30+
let trigger = parseInt(trigger_st);
31+
let button = parseInt(button_st);
32+
let co2threshold = parseInt(co2threshold_st);
33+
let co2calibration = parseInt(co2calibration_st);
34+
35+
let co2ppm_hex = intput_list[6].toString(16).padStart(2, '0') + intput_list[5].toString(16).padStart(2, '0');
36+
let co2_ppm = parseInt(co2ppm_hex, 16);
37+
co2_ppm = co2_ppm < 0 ? 0 : co2_ppm > 40000 ? 40000 : co2_ppm;
38+
39+
return {
40+
data: {
41+
battery_volt,
42+
temperature,
43+
humidity,
44+
trigger,
45+
button,
46+
co2threshold,
47+
co2calibration,
48+
co2_ppm
49+
},
50+
};
51+
}
52+
else if (fPort==127 && payloadlens==6){
53+
let intput_list = input.bytes;
54+
let battery_int = intput_list[1];
55+
let battery_volt = (21 + battery_int) / 10;
56+
57+
let temperature_int = intput_list[2];
58+
let temperature = temperature_int > 125 ? temperature_int - 256 : temperature_int;
59+
60+
let humidity = intput_list[3];
61+
62+
let co2_hex = intput_list[0].toString(16).padStart(2, '0');
63+
let co2_binary = hex2bin(co2_hex);
64+
let trigger_st = co2_binary.substring(7, 8);
65+
let button_st = co2_binary.substring(6, 7);
66+
let co2threshold_st = co2_binary.substring(3, 4);
67+
let co2calibration_st = co2_binary.substring(2, 3);
68+
69+
let trigger = parseInt(trigger_st);
70+
let button = parseInt(button_st);
71+
let co2threshold = parseInt(co2threshold_st);
72+
let co2calibration = parseInt(co2calibration_st);
73+
74+
let co2ppm_hex = intput_list[5].toString(16).padStart(2, '0') + intput_list[4].toString(16).padStart(2, '0');
75+
let co2_ppm = parseInt(co2ppm_hex, 16);
76+
co2_ppm = co2_ppm < 0 ? 0 : co2_ppm > 40000 ? 40000 : co2_ppm;
77+
78+
return {
79+
data: {
80+
battery_volt,
81+
temperature,
82+
humidity,
83+
trigger,
84+
button,
85+
co2threshold,
86+
co2calibration,
87+
co2_ppm
88+
},
89+
};
90+
}
91+
else{
92+
return {
93+
data: {
94+
fPort: input.fPort,
95+
payloadlength: input.bytes.length,
96+
message: 'Invalid fPort or payload length'
97+
},
98+
};
99+
}
100+
}
101+
102+
////////////* MerryIot CO2 sensor End !!*////////////
103+
104+
/**
105+
* Encode downlink function.
106+
*
107+
* @param {object} input
108+
* @param {object} input.data Object representing the payload that must be encoded.
109+
* @param {Record<string, string>} input.variables Object containing the configured device variables.
110+
*
111+
* @returns {{bytes: number[]}} Byte array containing the downlink payload.
112+
*/
113+
function encodeDownlink(input) {
114+
return {
115+
// bytes: [225, 230, 255, 0]
116+
};
117+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
////////////* MerryIot Open/Close sensor*////////////
2+
3+
//hex to binary function
4+
function hex2bin(hex){
5+
return (parseInt(hex, 16).toString(2)).padStart(8, '0');
6+
}
7+
8+
//MerryIot Open/Close sensor
9+
function decodeUplink(input) {
10+
let fPort = input.fPort;
11+
let payloadlens = input.bytes.length;
12+
if(fPort==120 && payloadlens==10){
13+
let intput_list = input.bytes;
14+
let battery_int = intput_list[1];
15+
let battery_volt = (21 + battery_int) / 10;
16+
17+
let temperature_hex = intput_list[3].toString(16).padStart(2, '0') + intput_list[2].toString(16).padStart(2, '0');
18+
let temperature_val = parseInt(temperature_hex, 16);
19+
let temperature = temperature_val > 1250 ? (temperature_val - 65536) / 10 : temperature_val / 10;
20+
21+
let humi = intput_list[4];
22+
23+
let door_hex = intput_list[0].toString(16).padStart(2, '0');
24+
let door_binary = hex2bin(door_hex);
25+
let open_st = door_binary.substring(7, 8);
26+
let button_st = door_binary.substring(6, 7);
27+
let tamper_st = door_binary.substring(5, 6);
28+
let tilt_st = door_binary.substring(4, 5);
29+
30+
let open = parseInt(open_st);
31+
let button = parseInt(button_st);
32+
let tamper = parseInt(tamper_st);
33+
let tilt = parseInt(tilt_st);
34+
35+
let time_hex = intput_list[6].toString(16).padStart(2, '0') + intput_list[5].toString(16).padStart(2, '0');
36+
let time = parseInt(time_hex, 16);
37+
38+
let count_hex = intput_list[9].toString(16).padStart(2, '0') + intput_list[8].toString(16) + intput_list[7].toString(16).padStart(2, '0');
39+
let count = parseInt(count_hex, 16);
40+
41+
return {
42+
data: {
43+
battery_volt,
44+
temperature,
45+
humi,
46+
open,
47+
button,
48+
tamper,
49+
tilt,
50+
time,
51+
count
52+
},
53+
};
54+
}
55+
else if (fPort==120 && payloadlens==9){
56+
let intput_list = input.bytes;
57+
let battery_int = intput_list[1];
58+
let battery_volt = (21 + battery_int) / 10;
59+
60+
let temperature_int = intput_list[2];
61+
let temperature;
62+
if(temperature_int > 125){
63+
temperature = temperature_int - 256;
64+
} else {
65+
temperature = temperature_int;
66+
}
67+
68+
let humi = intput_list[3];
69+
70+
let door_hex = intput_list[0].toString(16).padStart(2, '0');
71+
let door_binary = hex2bin(door_hex);
72+
let open_st = door_binary.substring(7, 8);
73+
let button_st = door_binary.substring(6, 7);
74+
let tamper_st = door_binary.substring(5, 6);
75+
let tilt_st = door_binary.substring(4, 5);
76+
77+
let open = parseInt(open_st);
78+
let button = parseInt(button_st);
79+
let tamper = parseInt(tamper_st);
80+
let tilt = parseInt(tilt_st);
81+
82+
let time_hex = intput_list[5].toString(16).padStart(2, '0') + intput_list[4].toString(16).padStart(2, '0');
83+
let time = parseInt(time_hex, 16);
84+
85+
let count_hex = intput_list[8].toString(16).padStart(2, '0') + intput_list[7].toString(16).padStart(2, '0') + intput_list[6].toString(16).padStart(2, '0');
86+
let count = parseInt(count_hex, 16);
87+
88+
return {
89+
data: {
90+
battery_volt,
91+
temperature,
92+
humi,
93+
open,
94+
button,
95+
tamper,
96+
tilt,
97+
time,
98+
count
99+
},
100+
};
101+
}
102+
else{
103+
return {
104+
data: {
105+
fPort: input.fPort,
106+
payloadlength: input.bytes.length,
107+
message: 'Invalid fPort or payload length'
108+
},
109+
};
110+
}
111+
}
112+
113+
////////////* MerryIot Open/Close sensor End !!*////////////
114+
115+
/**
116+
* Encode downlink function.
117+
*
118+
* @param {object} input
119+
* @param {object} input.data Object representing the payload that must be encoded.
120+
* @param {Record<string, string>} input.variables Object containing the configured device variables.
121+
*
122+
* @returns {{bytes: number[]}} Byte array containing the downlink payload.
123+
*/
124+
function encodeDownlink(input) {
125+
return {
126+
// bytes: [225, 230, 255, 0]
127+
};
128+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function decodeUplink(input) {
2+
const bytes = input.bytes;
3+
const fPort = input.fPort;
4+
const len = bytes.length;
5+
6+
if (fPort === 222 && len === 17) {
7+
const hex = bytes.map(b => b.toString(16).padStart(2, '0')).join('');
8+
const bootloader = hex.slice(2, 10).match(/../g).reverse().join('');
9+
const HW_ID = hex.slice(10, 18).match(/../g).reverse().join('');
10+
const FW_CRC = hex.slice(18, 26).match(/../g).reverse().join('');
11+
return { data: { fPort, bootloader, HW_ID, FW_CRC } };
12+
}
13+
else if (fPort === 204 && len === 12) {
14+
const hex = bytes.map(b => b.toString(16).padStart(2, '0')).join('');
15+
const thh_keepalive = parseInt(hex.slice(2, 6).match(/../g).reverse().join(''), 16);
16+
const thh_Monitor = parseInt(hex.slice(8, 12).match(/../g).reverse().join(''), 16);
17+
const thh_temptrig = bytes[7];
18+
const thh_rhtrig = bytes[9];
19+
return { data: { fPort, len, thh_keepalive, thh_Monitor, thh_temptrig, thh_rhtrig } };
20+
}
21+
else if (fPort === 103 && len === 8) {
22+
const battery_volt = (25 + (bytes[1] & 0x0F)) / 10;
23+
const temperature = (bytes[2] & 0x7F) - 32;
24+
const humidity = bytes[3] & 0x3F;
25+
const thh_type = (bytes[0] >> 4) & 0x01;
26+
return { data: { fPort, battery_volt, temperature, humidity, thh_type, CO2: 0, VOC: 0 } };
27+
}
28+
return { data: { fPort, payloadlength: len, message: 'Invalid fPort or payload length' } };
29+
}
30+
31+
/**
32+
* Encode downlink function.
33+
*
34+
* @param {object} input
35+
* @param {object} input.data Object representing the payload that must be encoded.
36+
* @param {Record<string, string>} input.variables Object containing the configured device variables.
37+
*
38+
* @returns {{bytes: number[]}} Byte array containing the downlink payload.
39+
*/
40+
function encodeDownlink(input) {
41+
return {
42+
// bytes: [225, 230, 255, 0]
43+
};
44+
}

0 commit comments

Comments
 (0)