-
Notifications
You must be signed in to change notification settings - Fork 1
/
datacake_decoder.js
224 lines (199 loc) · 6.79 KB
/
datacake_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
function Decoder(bytes, port) {
// bytes is of type Buffer
// see https://github.com/4-20ma/ModbusMaster/blob/master/src/ModbusMaster.h
var modbus_code = {
0x00: "Success",
0x01: "IllegalFunction",
0x02: "IllegalDataAddress",
0x03: "IllegalDataValue",
0x04: "SlaveDeviceFailure",
0xE0: "InvalidSlaveID",
0xE1: "InvalidFunction",
0xE2: "ResponseTimedOut",
0xE3: "InvalidCRC"
};
var bytesToInt = function (bytes) {
var i = 0;
for (var x = 0; x < bytes.length; x++) {
i |= +(bytes[x] << (x * 8));
}
return i;
};
var modbus = function (bytes) {
if (bytes.length !== modbus.BYTES) {
throw new Error('Modbus status must have exactly 1 byte');
}
return {
"code": bytes[0],
"text": modbus_code[bytes[0]]
};
};
modbus.BYTES = 1;
var unixtime = function (bytes) {
if (bytes.length !== unixtime.BYTES) {
throw new Error('Unix time must have exactly 4 bytes');
}
return bytesToInt(bytes);
};
unixtime.BYTES = 4;
var uint8 = function (bytes) {
if (bytes.length !== uint8.BYTES) {
throw new Error('int must have exactly 1 byte');
}
return bytesToInt(bytes);
};
uint8.BYTES = 1;
var uint16 = function (bytes) {
if (bytes.length !== uint16.BYTES) {
throw new Error('int must have exactly 2 bytes');
}
return bytesToInt(bytes);
};
uint16.BYTES = 2;
var uint16fp1 = function (bytes) {
if (bytes.length !== uint16.BYTES) {
throw new Error('int must have exactly 2 bytes');
}
var res = bytesToInt(bytes) * 0.1;
return res.toFixed(1);
};
uint16fp1.BYTES = 2;
var uint32 = function (bytes) {
if (bytes.length !== uint32.BYTES) {
throw new Error('int must have exactly 4 bytes');
}
return bytesToInt(bytes);
};
uint32.BYTES = 4;
var latLng = function (bytes) {
if (bytes.length !== latLng.BYTES) {
throw new Error('Lat/Long must have exactly 8 bytes');
}
var lat = bytesToInt(bytes.slice(0, latLng.BYTES / 2));
var lng = bytesToInt(bytes.slice(latLng.BYTES / 2, latLng.BYTES));
return [lat / 1e6, lng / 1e6];
};
latLng.BYTES = 8;
var temperature = function (bytes) {
if (bytes.length !== temperature.BYTES) {
throw new Error('Temperature must have exactly 2 bytes');
}
var isNegative = bytes[0] & 0x80;
var b = ('00000000' + Number(bytes[0]).toString(2)).slice(-8)
+ ('00000000' + Number(bytes[1]).toString(2)).slice(-8);
if (isNegative) {
var arr = b.split('').map(function (x) { return !Number(x); });
for (var i = arr.length - 1; i > 0; i--) {
arr[i] = !arr[i];
if (arr[i]) {
break;
}
}
b = arr.map(Number).join('');
}
var t = parseInt(b, 2);
if (isNegative) {
t = -t;
}
t = t / 1e2;
return t.toFixed(1);
};
temperature.BYTES = 2;
var humidity = function (bytes) {
if (bytes.length !== humidity.BYTES) {
throw new Error('Humidity must have exactly 2 bytes');
}
var h = bytesToInt(bytes);
return h / 1e2;
};
humidity.BYTES = 2;
// Based on https://stackoverflow.com/a/37471538 by Ilya Bursov
// quoted by Arjan here https://www.thethingsnetwork.org/forum/t/decode-float-sent-by-lopy-as-node/8757
function rawfloat(bytes) {
if (bytes.length !== rawfloat.BYTES) {
throw new Error('Float must have exactly 4 bytes');
}
// JavaScript bitwise operators yield a 32 bits integer, not a float.
// Assume LSB (least significant byte first).
var bits = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
var sign = (bits >>> 31 === 0) ? 1.0 : -1.0;
var e = bits >>> 23 & 0xff;
var m = (e === 0) ? (bits & 0x7fffff) << 1 : (bits & 0x7fffff) | 0x800000;
var f = sign * m * Math.pow(2, e - 150);
return f.toFixed(1);
}
rawfloat.BYTES = 4;
var bitmap = function (byte) {
if (byte.length !== bitmap.BYTES) {
throw new Error('Bitmap must have exactly 1 byte');
}
var i = bytesToInt(byte);
var bm = ('00000000' + Number(i).toString(2)).substr(-8).split('').map(Number).map(Boolean);
// Only Weather Sensor
return ['res5', 'res4', 'res3', 'res2', 'res1', 'res0', 'dec_ok', 'batt_ok']
.reduce(function (obj, pos, index) {
obj[pos] = bm[index];
return obj;
}, {});
};
bitmap.BYTES = 1;
var decode = function (bytes, mask, names) {
var maskLength = mask.reduce(function (prev, cur) {
return prev + cur.BYTES;
}, 0);
if (bytes.length < maskLength) {
throw new Error('Mask length is ' + maskLength + ' whereas input is ' + bytes.length);
}
names = names || [];
var offset = 0;
return mask
.map(function (decodeFn) {
var current = bytes.slice(offset, offset += decodeFn.BYTES);
return decodeFn(current);
})
.reduce(function (prev, cur, idx) {
prev[names[idx] || idx] = cur;
return prev;
}, {});
};
if (typeof module === 'object' && typeof module.exports !== 'undefined') {
module.exports = {
unixtime: unixtime,
uint8: uint8,
uint16: uint16,
uint32: uint32,
temperature: temperature,
humidity: humidity,
latLng: latLng,
bitmap: bitmap,
rawfloat: rawfloat,
uint16fp1: uint16fp1,
modbus: modbus,
decode: decode
};
}
if (bytes.length === 1) {
return { "modbus": modbus(bytes) };
}
if (port === 1) {
return decode(
bytes,
[modbus, uint8, uint8, rawfloat, rawfloat, rawfloat,
rawfloat, rawfloat, rawfloat
],
['modbus', 'status', 'faultcode', 'energytoday', 'energytotal', 'totalworktime',
'outputpower', 'gridvoltage', 'gridfrequency'
]
);
} else {
return decode(
bytes,
[modbus, rawfloat, rawfloat, rawfloat, temperature, temperature,
rawfloat, rawfloat
],
['modbus', 'pv1voltage', 'pv1current', 'pv1power', 'tempinverter', 'tempipm',
'pv1energytoday', 'pv1energytotal'
]
);
}
}