forked from anerdins/nibepi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
executable file
·359 lines (342 loc) · 13.7 KB
/
backend.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*MIT License
Copyright (c) 2019 Fredrik Anerdin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const version = "1.1.0";
/*var exec = require('child_process').exec;
exec(`sudo chrt -a -f -p 99 ${process.pid}`, function(error, stdout, stderr) {
if(error) {
} else {
console.log('High priority backend is started')
}
});*/
const serialport = require('serialport');
const nack = [0x15];
const ack = [0x06];
var myPort;
const sendQueue = [[192,107,6,115,176,1,0,0,0,111],[192,107,6,212,190,0,0,0,0,199]];
const getQueue = [];
const rmuQueue = [];
var regQueue = [];
var regCount = 0;
var msgOut = Buffer.alloc(0);
var rmu_buffer = Buffer.alloc(0);
var red = false;
var fs_start = false;
var rmu_start = false;
let other_start = false;
var startReset = true;
let checkACK = {};
process.on('message', (m) => {
if(m.start===true) {
if(m.port!==undefined) {
portName = m.port;
myPort = new serialport(portName, 9600);
myPort.on('open', showPortOpen);
myPort.on('data', analyzeData);
myPort.on('close', showPortClose);
myPort.on('error', showError);
} else {
if(process.connected===true) {
process.send({type:"log",data:'Error starting backend, no serial port specified',level:"error",kind:"Serialport"});
}
}
} else if(m.type=="reqData") {
getQueue.unshift(m.data);
} else if(m.type=="setData") {
sendQueue.push(m.data);
} else if(m.type=="rmuSet") {
rmuQueue.push(m.data);
} else if(m.type=="regRegister") {
regQueue = m.data;
} else if(m.type=="red") {
red = m.data;
}
});
process.on('disconnect', (m) => {
if(red===false) {
console.log('Shutting down the core.')
process.exit(99);
}
});
function showPortOpen() {
//console.log(`Core started on serial port ${portName}`);
}
function showPortClose() {
console.log('Port closed. Data rate: ' + myPort.baudRate);
}
function showError(error) {
if(process.connected===true) {
process.send({type:"fault",data:{from:"core",message:"The core could not be started, check the serialport"}});
}
myPort.removeAllListeners();
if(process.connected===true) {
process.disconnect();
console.log('Error in the core, shuting it down.')
process.exit(99);
}
}
function analyzeData(data) {
if(process.connected===true) {
process.send({type:"log",data:Array.from(data),level:"core",kind:"RAW"});
}
// Sometimes and ACK [0x06] is the first byte, and the second byte is the start. We shift it.
if(data[0]===0x06 && data[1]===0x5C) {
//console.log('ACK')
data = data.slice(1)
} else if(data[0]===0x06 && data[1]===undefined) {
//console.log('ACK')
}
//Check for start message, 0x5C if it's Nibe F series.
if(fs_start===false) {
if(data[0]===0x5C) {
fs_start = true;
}
}
if(fs_start===true) {
msgOut = Buffer.concat([msgOut,data]);
if(msgOut.length>=3) {
if(startReset===true) {
startReset = false;
sendQueue.push([192,107,6,115,176,1,0,0,0,111]);
}
// Only decode objects we know
if(msgOut[2]!==0x19 && msgOut[2]!==0x1A && msgOut[2]!==0x1B && msgOut[2]!==0x1C&& msgOut[2]!==0x20) {
msgOut = Buffer.alloc(0)
fs_start = false;
}
if(msgOut.length>=5) {
let msgLength = msgOut[4]+6;
// Check if we have the full length of the message.
if(msgOut.length>=msgLength) {
// We have full length
if(msgOut[2]==0x19 || msgOut[2]==0x1A || msgOut[2]==0x1B || msgOut[2]==0x1C) {
if(checkACK[msgOut[2]]===undefined) {
if(msgOut.length>=6) {
if(msgOut[msgOut[4]+6]===0x06 || msgOut[msgOut[4]+6]===0xC0) {
checkACK[msgOut[2]] = true;
msgOut = Buffer.alloc(0)
fs_start = false;
} else {
checkACK[msgOut[2]] = false;
msgOut = Buffer.alloc(0)
fs_start = false;
}
} else {
msgLength++;
}
return;
}
}
msgOut = msgOut.slice(0, msgLength);
checkMessage(msgOut).then(data => {
makeResponse(data).then(result => {
if(startReset===false) {
result = Array.from(result);
let doubleFound = false;
for (i = 5; i < result.length - 1; i = i + 1) {
if(result[i]===92 && result[i+1]===92) {
result.splice(i, 1);
result[4] = result[4]-1;
doubleFound = true;
}
}
if(doubleFound===true) {
var calcChecksum = 0;
for(i = 2; i < (result[4] + 5); i++) {
calcChecksum ^= result[i];
}
result[result.length-1] = calcChecksum;
}
if(process.connected===true) {
process.send({type:"data",data:result,rmu:checkACK[result[2]]});
process.send({type:"log",data:result,level:"debug",kind:"OK"});
}
}
}, err => {
})
}, err => {
err = Array.from(err);
console.log(`CHKSUM ERROR: ${err}`)
if(process.connected===true) {
process.send({type:"log",data:err,level:"error",kind:"CHKSUM"});
}
myPort.write(nack);
})
} else {
// Message is not ready yet.
}
}
}
}
}
const checkMessage = (data) => {
const promise = new Promise((resolve,reject) => {
let error = data;
msgOut = Buffer.alloc(0);
fs_start = false;
msgChecksum = data[data[4]+5];
var calcChecksum = 0;
for(i = 2; i < (data[4] + 5); i++) {
calcChecksum ^= data[i];
}
if((msgChecksum==calcChecksum) || (msgChecksum==0xC5 && calcChecksum==0x5C)) {
resolve(data);
} else {
reject(error)
}
});
return promise;
}
const makeResponse = (data) => {
const promise = new Promise((resolve,reject) => {
// Read from heatpump
if(data[2]==0x19 || data[2]==0x1A || data[2]==0x1B || data[2]==0x1C) { // < System
if(data[3]==0x60) { // Send data message
if(checkACK[data[2]]===false) {
if(rmuQueue.length!==0) {
var lastMsg = rmuQueue.pop();
if(lastMsg!==undefined) {
if(Number(lastMsg.ackback[2])==Number(data[2])) {
let address = lastMsg.address;
if(process.connected===true) {
process.send({type:"ack",data:{register:address,ack:true}});
process.send({type:"log",data:`Register: ${address}, Buffer: ${JSON.stringify(lastMsg)}`,level:"core",kind:"RMU SENT"});
}
if(lastMsg.data[3]===6) {
if(process.connected===true) {
process.send({type:"data",data:lastMsg.ackback,rmu:checkACK[lastMsg.ackback[2]]});
process.send({type:"log",data:lastMsg,level:"debug",kind:"RMU ACKBACK"});
}
}
myPort.write(lastMsg.data);
} else {
rmuQueue.push(lastMsg);
myPort.write(ack);
resolve(data);
}
} else {
myPort.write(ack);
resolve(data);
}
} else {
myPort.write(ack);
resolve(data);
}
} else {
resolve(data);
}
} else if(data[3]==0x62) {
// Message updates
if(checkACK[data[2]]===false) {
if(process.connected===true) {
let logOut = Array.from(data);
process.send({type:"log",data:logOut,level:"debug",kind:"RMU DATA"});
}
myPort.write(ack);
resolve(data);
} else {
resolve(data);
}
resolve(data);
} else if(data[3]==0x63) {
if(checkACK[data[2]]===false) {
myPort.write([192,96,2,99,0,193]);
resolve(data);
} else {
resolve(data);
}
} else if(data[3]==0xEE) {
if(checkACK[data[2]]===false) {
if(process.connected===true) {
process.send({type:"log",data:"Sending RMU Version v259",level:"debug",kind:"RMU ACK"});
}
myPort.write([192,238,3,238,3,1,193]);
resolve(data);
} else {
resolve(data);
}
} else {
if(checkACK[data[2]]===false) {
myPort.write(ack);
resolve(data);
} else {
resolve(data);
}
}
} else if(data[3]==105 && data[4]==0x00) {
if(getQueue!==undefined && getQueue.length!==0) {
var lastMsg = getQueue.pop();
if(lastMsg!==undefined) {
myPort.write(lastMsg);
resolve(data);
} else if(getQueue.length!==0) {
lastMsg = getQueue.pop();
if(lastMsg!==undefined) {
myPort.write(lastMsg);
resolve(data);
} else {
myPort.write(ack);
resolve(data);
}
} else {
myPort.write(ack);
resolve(data);
}
} else {
if(regQueue.length!==0) {
if(regCount>=regQueue.length) regCount = 0;
myPort.write(regQueue[regCount]);
regCount++;
resolve(data);
} else {
myPort.write(ack);
resolve(data);
}
}
// Write to heatpump
} else if(data[3]==107 && data[4]==0x00) {
if(sendQueue.length!==0) {
var lastMsg = sendQueue.pop();
if(lastMsg!==undefined) {
let address = (lastMsg[4] * 256 + lastMsg[3]);
if(process.connected===true) {
process.send({type:"ack",data:{register:address,ack:true}});
process.send({type:"log",data:`Register: ${address}, Buffer: ${JSON.stringify(lastMsg)}`,level:"debug",kind:"SENT"});
}
myPort.write(lastMsg);
resolve(data);
} else {
myPort.write(ack);
resolve(data);
}
} else {
myPort.write(ack);
resolve(data);
}
} else {
if(data[3]==109) {
} else if(data[3]==238) {
} else if(data[3]==106 || data[3]==104) {
} else {
}
myPort.write(ack);
resolve(data);
}
});
return promise;
}