-
Notifications
You must be signed in to change notification settings - Fork 9
/
functions.js
63 lines (55 loc) · 1.82 KB
/
functions.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
module.exports = {
parseResult: (bodyResult, config) => {
var result = {};
// initialize all values to 0
for (const key of Object.keys(config.values)) {
result[config.values[key].name] = 0;
}
for (const id in bodyResult) {
const set = bodyResult[id];
// iterate over messages from sma device
for (const key in set) {
if (config.values.hasOwnProperty(key)) {
const value = set[key];
if ((value != null) && (value[config.id] != null)) {
var values = []; // value array
// iterate over all elements in the message
for (const elm of value[config.id]) {
// if element contains an object with more than one key save the whole object
if (Object.keys(elm).length > 1) {
values.push(elm);
}
else {
var tmp = 0;
if (elm.val) {
// if element contains an object with only one key store it (mostly tags?)
if (elm.val[0]) {
tmp = elm.val[0];
}
else {
if (typeof elm.val === 'number' && config.values[key].divider) {
tmp = elm.val / config.values[key].divider;
}
else {
tmp = elm.val;
}
}
}
// add values to the array
values.push(tmp);
}
}
// store value in output message
if (values.length > 1) {
result[config.values[key].name] = values;
}
else {
result[config.values[key].name] = values[0];
}
}
}
}
}
return result;
}
};