-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decoder.js
189 lines (178 loc) · 7.92 KB
/
Decoder.js
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
function get_device_name(device, value) {
return device + "_" + value;
}
function Decoder(request) {
var data = JSON.parse(request.body);
var device = data.device;
console.log(device);
var decoded = {};
decoded.sensor = data.body.node_id;
if (typeof data.body.voltage_1 != 'undefined') {
decoded[get_device_name(data.body.node_id, "voltage")] = data.body.voltage_1;
}
if (typeof data.body.humidity_2 != 'undefined') {
decoded[get_device_name(data.body.node_id, "humidity")] = data.body.humidity_2;
}
if (typeof data.body.temperature_3 != 'undefined') {
decoded[get_device_name(data.body.node_id, "temperature")] = data.body.temperature_3;
}
if (typeof data.body.barometer_4 != 'undefined') {
decoded[get_device_name(data.body.node_id, "pressure")] = data.body.barometer_4;
}
if (typeof data.body.illuminance_5 != 'undefined') {
decoded[get_device_name(data.body.node_id, "illuminance")] = data.body.illuminance_5;
}
if (typeof data.body.humidity_6 != 'undefined') {
decoded[get_device_name(data.body.node_id, "humidity")] = data.body.humidity_6;
}
if (typeof data.body.temperature_7 != 'undefined') {
decoded[get_device_name(data.body.node_id, "temperature")] = data.body.temperature_7;
}
if (typeof data.body.barometer_8 != 'undefined') {
decoded[get_device_name(data.body.node_id, "pressure")] = data.body.barometer_8;
}
if (typeof data.body.analog_9 != 'undefined') {
decoded[get_device_name(data.body.node_id, "voc")] = data.body.analog_9;
}
if (typeof data.body.gps_10 != 'undefined') {
decoded[get_device_name(data.body.node_id, "location")] = data.body.gps_10;
}
if (typeof data.body.gps_10 != 'undefined') {
decoded[get_device_name(data.body.node_id, "location")] = data.body.gps_10;
}
if (typeof data.body.temperature_11 != 'undefined') {
decoded[get_device_name(data.body.node_id, "soil_temp")] = data.body.temperature_11;
}
if (typeof data.body.humidity_12 != 'undefined') {
decoded[get_device_name(data.body.node_id, "soil_humid")] = data.body.humidity_12;
}
if (typeof data.body.illuminance_15 != 'undefined') {
decoded[get_device_name(data.body.node_id, "illuminance")] = data.body.illuminance_15;
}
if (typeof data.body.voc_16 != 'undefined') {
decoded[get_device_name(data.body.node_id, "voc")] = data.body.voc_16;
}
if (typeof data.body.analog_in_17 != 'undefined') {
decoded[get_device_name(data.body.node_id, "mq2_gas")] = data.body.analog_in_17;
}
if (typeof data.body.percentage_18 != 'undefined') {
decoded[get_device_name(data.body.node_id, "mq2_gas_perc")] = data.body.percentage_18;
}
if (typeof data.body.analog_in_19 != 'undefined') {
decoded[get_device_name(data.body.node_id, "co2_gas")] = data.body.analog_in_19;
}
if (typeof data.body.percentage_20 != 'undefined') {
decoded[get_device_name(data.body.node_id, "co2_gas_perc")] = data.body.percentage_20;
}
if (typeof data.body.analog_in_21 != 'undefined') {
decoded[get_device_name(data.body.node_id, "mq3_gas")] = data.body.analog_in_21;
}
if (typeof data.body.percentage_22 != 'undefined') {
decoded[get_device_name(data.body.node_id, "mq3_gas_perc")] = data.body.percentage_22;
}
if (typeof data.body.analog_in_23 != 'undefined') {
decoded[get_device_name(data.body.node_id, "tof_distance")] = data.body.analog_in_23;
}
if (typeof data.body.presence_24 != 'undefined') {
decoded[get_device_name(data.body.node_id, "tof_valid")] = data.body.presence_24;
}
if (typeof data.body.gyrometer_25 != 'undefined') {
decoded[get_device_name(data.body.node_id, "gyro")] = data.body.gyrometer_25;
}
if (typeof data.body.digital_in_26 != 'undefined') {
decoded[get_device_name(data.body.node_id, "gesture")] = data.body.digital_in_26;
}
if (typeof data.body.analog_in_27 != 'undefined') {
decoded[get_device_name(data.body.node_id, "uvi")] = data.body.analog_in_27;
}
if (typeof data.body.illuminance_28 != 'undefined') {
decoded[get_device_name(data.body.node_id, "uvs")] = data.body.illuminance_28;
}
if (typeof data.body.analog_29 != 'undefined') {
decoded[get_device_name(data.body.node_id, "ina219_current")] = data.body.analog_29;
}
if (typeof data.body.analog_30 != 'undefined') {
decoded[get_device_name(data.body.node_id, "ina219_voltage")] = data.body.analog_30;
}
if (typeof data.body.analog_31 != 'undefined') {
decoded[get_device_name(data.body.node_id, "ina219_voltage")] = data.body.analog_31;
}
if (typeof data.body.presence_32 != 'undefined') {
decoded[get_device_name(data.body.node_id, "tp_left")] = data.body.presence_32;
}
if (typeof data.body.presence_33 != 'undefined') {
decoded[get_device_name(data.body.node_id, "tp_center")] = data.body.presence_33;
}
if (typeof data.body.presence_34 != 'undefined') {
decoded[get_device_name(data.body.node_id, "tp_right")] = data.body.presence_34;
}
if (typeof data.body.concentration_35 != 'undefined') {
decoded[get_device_name(data.body.node_id, "co2")] = data.body.concentration_35;
}
if (typeof data.body.temperature_39 != 'undefined') {
decoded[get_device_name(data.body.node_id, "mlx_temperature")] = data.body.temperature_39;
}
if (typeof data.body.voc_40 != 'undefined') {
decoded[get_device_name(data.body.node_id, "pm1_0")] = data.body.voc_40;
}
if (typeof data.body.voc_41 != 'undefined') {
decoded[get_device_name(data.body.node_id, "pm2_5")] = data.body.voc_41;
}
if (typeof data.body.voc_42 != 'undefined') {
decoded[get_device_name(data.body.node_id, "pm10")] = data.body.voc_42;
}
if (typeof data.body.presence_43 != 'undefined') {
decoded[get_device_name(data.body.node_id, "earthqake_alert")] = data.body.presence_43;
}
if (typeof data.body.analog_44 != 'undefined') {
decoded[get_device_name(data.body.node_id, "earthquake_si")] = data.body.analog_44;
}
if (typeof data.body.analog_45 != 'undefined') {
decoded[get_device_name(data.body.node_id, "earthquake_pga")] = data.body.analog_45;
}
if (typeof data.body.presence_46 != 'undefined') {
decoded[get_device_name(data.body.node_id, "earthquake_shutoff")] = data.body.presence_46;
}
if (typeof data.body.presence_47 != 'undefined') {
decoded[get_device_name(data.body.node_id, "earthquake_collapse")] = data.body.presence_47;
}
if (typeof data.body.presence_48 != 'undefined') {
decoded[get_device_name(data.body.node_id, "switch")] = data.body.presence_48;
}
if (typeof data.body.wind_speed_49 != 'undefined') {
decoded[get_device_name(data.body.node_id, "wind_speed")] = data.body.wind_speed_49;
}
if (typeof data.body.wind_direction_50 != 'undefined') {
decoded[get_device_name(data.body.node_id, "wind_direction")] = data.body.wind_direction_50;
}
if (typeof data.body.analog_61 != 'undefined') {
decoded[get_device_name(data.body.node_id, "water_level")] = data.body.analog_61;
}
if (typeof data.body.presence_62 != 'undefined') {
decoded[get_device_name(data.body.node_id, "low_level_alert")] = data.body.presence_62;
}
if (typeof data.body.presence_63 != 'undefined') {
decoded[get_device_name(data.body.node_id, "overflow_alert")] = data.body.presence_63;
}
if (("tower_lat" in data) && ("tower_lon" in data)) {
decoded.tower_location = "(" + data.tower_lat + "," + data.tower_lon + ")";
}
if (("where_lat" in data) && ("where_lon" in data)) {
decoded.device_location = "(" + data.where_lat + "," + data.where_lon + ")";
}
decoded.rssi = data.rssi;
decoded.bars = data.bars;
decoded.temp = data.temp;
decoded.orientation = data.orientation;
decoded.card_temperature = data.body.temperature;
// Array where we store the fields that are being sent to Datacake
var datacakeFields = []
// take each field from decodedElsysFields and convert them to Datacake format
for (var key in decoded) {
if (decoded.hasOwnProperty(key)) {
datacakeFields.push({ field: key.toUpperCase(), value: decoded[key], device: device })
}
}
// forward data to Datacake
return datacakeFields;
}