-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
205 lines (180 loc) · 8.42 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { IoTDataPlaneClient, PublishCommand } from "@aws-sdk/client-iot-data-plane";
// Please fill in your `endpoint` in the `region` below. For example: 'us-east-1'
const client = new IoTDataPlaneClient({ region: 'us-east-1' });
export const handler = async (event) => {
var data = Buffer.from(event.PayloadData, 'base64');
var chars = [...data];
var params = Decoder(chars, event.WirelessMetadata.LoRaWAN.FPort);
params["EUI"]= event.WirelessMetadata.LoRaWAN.DevEui;
// Please fill in the MQTT message topic you will use when sending to the MQTT server in the "topic" section below.
const command = new PublishCommand({
topic: "sdk/test/python",
payload: JSON.stringify(params),
qos: 0
});
return new Promise((resolve, reject) => {
client.send(command)
.then((data) => {
// console.log('=== Publish ===');
// console.log("publish data: ", data);
const returnData = {
statusCode: 200,
body: params,
};
resolve(returnData);
})
.catch((err) => {
// console.log('=== Error ===');
// console.log("ERROR => " + JSON.stringify(err));
const returnData = {
statusCode: 400,
body: err,
};
reject(returnData);
});
});
};
function Decoder(bytes, fport) {
var response = {};
lppDecode(bytes, 1).forEach(function (field) {
response[field['name'] + '_' + field['channel']] = field['value'];
});
return response;
}
function lppDecode(bytes) {
var sensor_types = {
0: { 'size': 1, 'name': 'digital_in', 'signed': false, 'divisor': 1 },
1: { 'size': 1, 'name': 'digital_out', 'signed': false, 'divisor': 1 },
2: { 'size': 2, 'name': 'analog_in', 'signed': true, 'divisor': 100 },
3: { 'size': 2, 'name': 'analog_out', 'signed': true, 'divisor': 100 },
100: { 'size': 4, 'name': 'generic', 'signed': false, 'divisor': 1 },
101: { 'size': 2, 'name': 'illuminance', 'signed': false, 'divisor': 1 }, //unit:Lux
102: { 'size': 1, 'name': 'presence', 'signed': false, 'divisor': 1 },
103: { 'size': 2, 'name': 'temperature', 'signed': true, 'divisor': 10 }, //unit:℃
104: { 'size': 1, 'name': 'humidity', 'signed': false, 'divisor': 2 },
112: { 'size': 2, 'name': 'humidity_prec', 'signed': true, 'divisor': 10 }, //unit:%RH
113: { 'size': 6, 'name': 'accelerometer', 'signed': true, 'divisor': 1000 },
115: { 'size': 2, 'name': 'barometer', 'signed': false, 'divisor': 10 }, //unit:hPa
116: { 'size': 2, 'name': 'voltage', 'signed': false, 'divisor': 100 },
117: { 'size': 2, 'name': 'current', 'signed': false, 'divisor': 1000 },
118: { 'size': 4, 'name': 'frequency', 'signed': false, 'divisor': 1 },
120: { 'size': 1, 'name': 'percentage', 'signed': false, 'divisor': 1 },
121: { 'size': 2, 'name': 'altitude', 'signed': true, 'divisor': 1 },
125: { 'size': 2, 'name': 'concentration', 'signed': false, 'divisor': 1 },
128: { 'size': 2, 'name': 'power', 'signed': false, 'divisor': 1 },
130: { 'size': 4, 'name': 'distance', 'signed': false, 'divisor': 1000 },
131: { 'size': 4, 'name': 'energy', 'signed': false, 'divisor': 1000 },
132: { 'size': 2, 'name': 'direction', 'signed': false, 'divisor': 1 },
133: { 'size': 4, 'name': 'time', 'signed': false, 'divisor': 1 },
134: { 'size': 6, 'name': 'gyrometer', 'signed': true, 'divisor': 100 },
135: { 'size': 3, 'name': 'colour', 'signed': false, 'divisor': 1 },
136: { 'size': 9, 'name': 'gps', 'signed': true, 'divisor': [10000, 10000, 100] },
137: { 'size': 11, 'name': 'gps', 'signed': true, 'divisor': [1000000, 1000000, 100] },
138: { 'size': 2, 'name': 'voc', 'signed': false, 'divisor': 1 },
142: { 'size': 1, 'name': 'switch', 'signed': false, 'divisor': 1 },
188: { 'size': 2, 'name': 'soil_moist', 'signed': false, 'divisor': 10 },
190: { 'size': 2, 'name': 'wind_speed', 'signed': false, 'divisor': 100 }, //unit:m/s
191: { 'size': 2, 'name': 'wind_direction', 'signed': false, 'divisor': 1 }, //unit:°
192: { 'size': 2, 'name': 'soil_ec', 'signed': false, 'divisor': 1000 },
193: { 'size': 2, 'name': 'soil_ph_h', 'signed': false, 'divisor': 100 },
194: { 'size': 2, 'name': 'soil_ph_l', 'signed': false, 'divisor': 10 },
195: { 'size': 2, 'name': 'pyranometer', 'signed': false, 'divisor': 1 },
203: { 'size': 1, 'name': 'light', 'signed': false, 'divisor': 1 },
//Tongdy 20230805
211: { 'size': 2, 'name': 'co2', 'signed': true, 'divisor': 1 }, //unit:ppm
212: { 'size': 2, 'name': 'tvoc', 'signed': true, 'divisor': 1000 }, //unit:mg/m3
213: { 'size': 2, 'name': 'pm0.3', 'signed': true, 'divisor': 1 }, //unit:ug/m3
214: { 'size': 2, 'name': 'pm0.5', 'signed': true, 'divisor': 1 }, //unit:ug/m3
215: { 'size': 2, 'name': 'pm1', 'signed': true, 'divisor': 1 }, //unit:ug/m3
216: { 'size': 2, 'name': 'pm2.5', 'signed': true, 'divisor': 1 }, //unit:ug/m3
217: { 'size': 2, 'name': 'pm4', 'signed': true, 'divisor': 1 }, //unit:ug/m3
218: { 'size': 2, 'name': 'pm10', 'signed': true, 'divisor': 1 }, //unit:ug/m3
219: { 'size': 2, 'name': 'pm100', 'signed': true, 'divisor': 1 }, //unit:ug/m3
220: { 'size': 2, 'name': 'co', 'signed': true, 'divisor': 10 }, //unit:mg/m3
221: { 'size': 2, 'name': 'o3', 'signed': true, 'divisor': 1 }, //unit:ug/m3
222: { 'size': 2, 'name': 'so2', 'signed': true, 'divisor': 1 }, //unit:ug/m3
223: { 'size': 2, 'name': 'no2', 'signed': true, 'divisor': 1 }, //unit:ug/m3
224: { 'size': 2, 'name': 'hcho', 'signed': true, 'divisor': 1000 }, //unit:mg/m3
225: { 'size': 2, 'name': 'noise', 'signed': true, 'divisor': 10 }, //unit:dB(A)
};
function arrayToDecimal(stream, is_signed, divisor) {
var value = 0;
for (var i = 0; i < stream.length; i++) {
if (stream[i] > 0xFF)
throw 'Byte value overflow!';
value = (value << 8) | stream[i];
}
if (is_signed) {
var edge = 1 << (stream.length) * 8; // 0x1000..
var max = (edge - 1) >> 1; // 0x0FFF.. >> 1
value = (value > max) ? value - edge : value;
}
value /= divisor;
return value;
}
var sensors = [];
var i = 0;
while (i < bytes.length) {
var s_no = bytes[i++];
var s_type = bytes[i++];
if (typeof sensor_types[s_type] == 'undefined') {
throw 'Sensor type error!: ' + s_type;
}
var s_value = 0;
var type = sensor_types[s_type];
switch (s_type) {
case 113: // Accelerometer
case 134: // Gyrometer
s_value = {
'x': arrayToDecimal(bytes.slice(i + 0, i + 2), type.signed, type.divisor),
'y': arrayToDecimal(bytes.slice(i + 2, i + 4), type.signed, type.divisor),
'z': arrayToDecimal(bytes.slice(i + 4, i + 6), type.signed, type.divisor)
};
break;
case 136: // GPS Location
s_value = {
'latitude': arrayToDecimal(bytes.slice(i + 0, i + 3), type.signed, type.divisor[0]),
'longitude': arrayToDecimal(bytes.slice(i + 3, i + 6), type.signed, type.divisor[1]),
'altitude': arrayToDecimal(bytes.slice(i + 6, i + 9), type.signed, type.divisor[2])
};
break;
case 137: // Precise GPS Location
s_value = {
'latitude': arrayToDecimal(bytes.slice(i + 0, i + 4), type.signed, type.divisor[0]),
'longitude': arrayToDecimal(bytes.slice(i + 4, i + 8), type.signed, type.divisor[1]),
'altitude': arrayToDecimal(bytes.slice(i + 8, i + 11), type.signed, type.divisor[2])
};
sensors.push({
'channel': s_no,
'type': s_type,
'name': 'location',
'value': "(" + s_value.latitude + "," + s_value.longitude + ")"
});
sensors.push({
'channel': s_no,
'type': s_type,
'name': 'altitude',
'value': s_value.altitude
});
break;
case 135: // Colour
s_value = {
'r': arrayToDecimal(bytes.slice(i + 0, i + 1), type.signed, type.divisor),
'g': arrayToDecimal(bytes.slice(i + 1, i + 2), type.signed, type.divisor),
'b': arrayToDecimal(bytes.slice(i + 2, i + 3), type.signed, type.divisor)
};
break;
default: // All the rest
s_value = arrayToDecimal(bytes.slice(i, i + type.size), type.signed, type.divisor);
break;
}
sensors.push({
'channel': s_no,
'type': s_type,
'name': type.name,
'value': s_value
});
i += type.size;
}
return sensors;
}