AQI Sensor Downlink encoder
1
const toHex = (cmdName, cmdId, ...params) => {
2
cmdId.toString(16).padStart(2, '0') + params.reduce((paramString, param) => {
3
return paramString += param.padStart(2, '0')
4
}, "")
5
}
6
7
const getJoinRetryPeriod = () => {
8
return toHex('GetJoinRetryPeriod', 0x19);
9
}
10
11
const getKeepAliveTime = () => {
12
return toHex('GetKeepAliveTime', 0x12);
13
}
14
15
16
const getUplinkType = () => {
17
return toHex('GetUplinkType', 0x1B);
18
}
19
20
const getDeviceVersion = () => {
21
return toHex('GetDeviceVersion', 0x04);
22
}
23
24
const getWatchDogParams = () => {
25
return toHex('GetWatchDogParams', 0x1D);
26
}
27
28
29
const receivedKeepaliveCommand = () => {
30
return toHex('ReceivedKeepalive', 0x55);
31
}
32
33
34
const setJoinRetryPeriod = (period) => {
35
// period should be passed in minutes
36
let periodToPass = (period * 60) / 5;
37
return toHex('SetJoinRetryPeriod', 0x10, parseInt(periodToPass).toString(16));
38
}
39
40
const setKeepAlive = (time) => {
41
return toHex( 'SetKeepAlive', 0x02, parseInt(time).toString(16));
42
}
43
44
const setUplinkType = (type) => {
45
return toHex( 'SetUplinkType', 0x11, type);
46
}
47
48
const setBuzzer = (volume, frequency, activeTime, onTime, offTime) => {
49
let byte = parseInt(volume).toString(2).padStart(4, '0') + parseInt(frequency).toString(2).padStart(4, '0');
50
let volumeAndFreq = (parseInt(byte, 2)).toString(16);
51
52
return toHex(
53
'SetBuzzer',
54
0x03,
55
volumeAndFreq,
56
parseInt(activeTime).toString(16),
57
parseInt(onTime/10).toString(16),
58
parseInt(offTime/10).toString(16)
59
);
60
}
61
62
const setAqiLed = (redBehavior, redDuration, greenBehavior, greenDuration, blueBehavior, blueDuration) => {
63
let red = (parseInt(parseInt(redBehavior).toString(2).padStart(3, '0') + parseInt(redDuration/10).toString(2).padStart(5, '0'),2)).toString(16);
64
let green = (parseInt(parseInt(greenBehavior).toString(2).padStart(3, '0') + parseInt(greenDuration/10).toString(2).padStart(5, '0'),2)).toString(16);
65
let blue = (parseInt(parseInt(blueBehavior).toString(2).padStart(3, '0') + parseInt(blueDuration/10).toString(2).padStart(5, '0'),2)).toString(16);
66
67
return toHex(
68
'SetAqiLed',
69
0x05,
70
red,
71
green,
72
blue
73
);
74
}
75
76
const setWatchDogParams (periodConfirmenUplinks, periodUnconfirmenUplinks, deviceKeepAlive) {
77
78
let enabledConfirmed = periodConfirmenUplinks !== false;
79
let enabledUnconfirmed = periodUnconfirmenUplinks !== false;
80
81
// ATTENTION! periodConfirmenUplinks !MUST! BE DIVIDABLE BY DEVICE KEEPALIVE PERIOD + 7
82
let WDPconfirmed = enabledConfirmed ? (periodConfirmenUplinks - 7) / deviceKeepAlive : 0;
83
let WDPunconfirmed = enabledUnconfirmed ? periodUnconfirmenUplinks : 0;
84
85
return toHex(
86
'SetWatchDogParams',
87
0x1C,
88
decToHex(WDPconfirmed),
89
decToHex(WDPunconfirmed)
90
)
91
}
Copied!
Copy link