-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
431 lines (344 loc) · 12 KB
/
main.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
'use strict';
/*
* Created with @iobroker/create-adapter v1.21.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
// Load your modules here, e.g.:
const state_attr = require(__dirname + '/lib/state_attr.js'); // Attribute library
// Modules for Serial port needed for direct USB connection
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
// Modules requird for TCP-IP conection
const net = require('net');
// Opentherm specific librarys to translate hex-values
const checkhex = require(__dirname + '/lib/otgwdec');
const translatehex = require(__dirname + '/lib/openthermdec');
let client, values, objtype, serialPort;
// const fs = require('fs');
class Opentherm extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'opentherm',
});
this.on('ready', this.onReady.bind(this));
this.on('objectChange', this.onObjectChange.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
// this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Load configuration to global variabl
const ipaddr = this.config.ipaddr;
const otport = this.config.port;
const useUSB = this.config.byUSB;
const useTCP = this.config.byTCPIP;
const USB_Device = this.config.USBDevice;
// Create & reset connection stat at adapter start
// await this.create_state('info.connection', 'Connected', true);
this.setState('info.connection', { val: false, ack: true });
if (useUSB){
// Handle USB connection
try {
serialPort = new SerialPort(USB_Device);
this.log.info('OpenTherm : Succesfully connected on : ' + '/dev/ttyUSB0');
this.setState('info.connection', { val: true, ack: true });
} catch (error) {
this.log.error(`${error}`);
}
// Connection error handling
serialPort.on('error', (error) => {
this.log.error(JSON.stringify(error));
this.setState('info.connection', { val: (JSON.stringify(error)), ack: true });
});
const parser = serialPort.pipe(new Readline({ delimiter: '\r\n' }));
parser.on('data', (data) => {
this.log.info(data);
this.datahandler(data);
});
// Event at successful connection by USB
serialPort.on('open', (error) => {
this.log.error('Issues opening USB data connection : ' + error);
// Get a list of all USB devices
// SerialPort..( (err, results) => {
//
// if (err) {
// throw err;
// } else {
// this.log.error(' USB devices found : ' + results);
// }
// });
});
}
if(useTCP){
let completeData = '';
// Handle TCP-IP connection
try {
// @ts-ignore
client = net.connect({ host: ipaddr, port: otport},
() => { //'connect' listener
// doChannelCreate();
this.log.info('OpenTherm : Succesfully connected on : ' + ipaddr);
this.setState('info.connection', { val: true, ack: true });
});
} catch (error) {
this.log.error('OpenTherm : Connection failed on : ' + ipaddr);
}
// Connection error handling
client.on('error', (error) => {
try {
this.log.error(JSON.stringify(error));
// this.setState('info.connection', { val: (JSON.stringify(error)), ack: true });
} catch (error) {
this.log.error(`${error}`);
}
});
client.on('data', (data) => {
try {
const read = data.toString();
completeData += read;
// Check if data is correct. Only one data line to datahandler
if (completeData.match(/\r\n/)) {
//this.log.info('Response: ' + completeData);
this.datahandler(completeData);
completeData = '';
}
} catch (error) {
this.log.error(`${error}`);
}
});
}
}
async datahandler(data){
const devData = this.config.DevMode;
const DevLogging = this.config.DevLogging;
// Write data message to log
if (DevLogging){this.log.info('Data received : ' + data);}
this.create_state('.info.message', 'Received Data', data);
// Run check on data input if received message has correct format
const verify = checkhex.checkinput(this,data);
// Only call translation function when received value is valid
if (verify != undefined) {
// Translate OpenTherm message to human readable objects and values
values = translatehex.translate_input(this, verify);
// Handle received data to object structure and values
if (values != undefined) {
if (DevLogging){this.log.info('Translated values : ' + JSON.stringify(values));}
// Handle array and split to unique data objects
for (const i in values) {
objtype = await this.toObjtype(values[i].Value);
// if (DevLogging){this.log.info('Values of [i] : ' + i);}
if (DevLogging){this.log.info('Raw values of [i] : ' + JSON.stringify(values[i]));}
// if (DevLogging){this.log.info('Device value of [i] : ' + JSON.stringify(values[i].Device));}
//if (devLogging){this.log.info('Raw attribute lookkup : ' + JSON.stringify(attributes))}
// if (DevLogging){this.log.info('attribute lookkup : ' + JSON.stringify(state_attr['master/status/ch']));}
// if (DevLogging){this.log.info('Combined data with attribute values : ' + JSON.stringify(state_attr[values[i].Device]));}
let channel = state_attr[values[i].Device].channel;
let value = values[i].Value;
// const name = state_attr[values[i].Device].name;
const name = values[i].Device;
// const description = state_attr[values[i].Device].description;
// const role = state_attr[values[i].Device].role;
// const unit = state_attr[values[i].Device].unit;
// const write = state_attr[values[i].Device].write;
const cat = values[i].msgType;
// Round values if they are numbers to ensure only 2 digits after comma
// To-Do : Analyse if rounding function is realy working !
if (objtype == 'number') {
value = Math.round(value * 10)/10;
}
if (devData){
this.create_state('_Dev.' + cat + '.' + name,name,value);
// doStateCreate('_Dev.' + cat + '.' + name,description,objtype,role,unit,write);
// this.setState('_Dev.' + cat + '.' + name, { val: value, ack: true });
}
//Write all channels to 'raw' tree for developer purposes
// if (DevLogging){this.log.info(name + ' with value : ' + value + unit);} // IF this loggin is needed move to state creation
// Read only Read-ACK related values and store in states (we need to find out which datatype for which state must be used!)
if (values[i].msgType == '4'){
if (channel != '') {channel = channel + '.';}
// doStateCreate(channel + name,description,objtype,role,unit,write);
this.create_state(channel + name, name, value);
// this.setState(channel + name, { val: value, ack: true });
// if (DevLogging){this.log.info('Data written to state : ' + name);}
}
}
}
}
}
async create_state(state, name, value){
this.log.debug('Create_state called for : ' + state + ' with value : ' + value);
try {
// Try to get details from state lib, if not use defaults. throw warning is states is not known in attribute list
if((state_attr[name] === undefined)){this.log.warn('State attribute definition missing for + ' + name);}
const writable = (state_attr[name] !== undefined) ? state_attr[name].write || false : false;
const state_name = (state_attr[name] !== undefined) ? state_attr[name].name || name : name;
const role = (state_attr[name] !== undefined) ? state_attr[name].role || 'state' : 'state';
const type = (state_attr[name] !== undefined) ? state_attr[name].type || 'mixed' : 'mixed';
const unit = (state_attr[name] !== undefined) ? state_attr[name].unit || '' : '';
this.log.debug('Write value : ' + writable);
await this.setObjectNotExistsAsync(state, {
type: 'state',
common: {
name: state_name,
read: role,
role: role,
type: type,
unit: unit,
write : writable
},
native: {},
});
await this.setState(state, {val: value, ack: true});
// Subscribe on state changes if writable
if (writable === true) {this.subscribeStates(state);}
} catch (error) {
this.log.error('Create state error = ' + error);
}
}
async toObjtype (value) {
// Lets first ensure what kind of datatype we have
if (value == 'true' || value == 'false') {
objtype = 'boolean';
} else if (Number.isNaN(parseFloat(value)) === false) {
objtype = 'number';
} else {
objtype = 'string';
}
return objtype;
}
// Create logic channels for states
async doChannelCreate(){
const DevLogging = this.config.DevLogging;
const devData = this.config.DevMode;
// Create channels for RAW-Data if Dev-Mode is activated
if (devData){this.doChannelCreateDev();}
this.createChannel('','config',{
'name': 'config'
});
this.createChannel('','control',{
'name': 'control'
});
this.createChannel('','fault',{
'name': 'faul'
});
this.createChannel('','info',{
'name': 'info'
});
this.createChannel('','status',{
'name': 'status'
});
if (DevLogging){this.log.info('Channels create');}
}
async doChannelCreateDev(){
this.setObjectNotExists('_Dev', {
type: 'device',
common: {
name: 'Raw data seperated by MessageType',
},
native: {},
});
this.createChannel('_Dev','0',{
'name': 'Read-Data || msgType : 0'
});
this.createChannel('_Dev','1',{
'name': 'Write-Data || msgType : 1'
});
this.createChannel('_Dev','2',{
'name': 'Read-Ack || msgType : 2'
});
this.createChannel('_Dev','3',{
'name': 'Write-Ack || msgType : 3'
});
this.createChannel('_Dev','4',{
'name': 'Data-Inv || msgType : 4'
});
this.createChannel('_Dev','5',{
'name': 'Unk-DataId || msgType : 5'
});
this.createChannel('_Dev','6',{
'name': '????????? || msgType : 6'
});
this.createChannel('_Dev','7',{
'name': '????????? || msgType : 7'
});
this.createChannel('_Dev','8',{
'name': '????????? || msgType : 8'
});
this.log.info('Channels created');
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed object changes
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
onObjectChange(id, obj) {
if (obj) {
// The object was changed
this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
// The object was deleted
this.log.info(`object ${id} deleted`);
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state) {
// The state was changed
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
}
// /**
// * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
// * Using this method requires 'common.message' property to be set to true in io-package.json
// * @param {ioBroker.Message} obj
// */
// onMessage(obj) {
// if (typeof obj === 'object' && obj.message) {
// if (obj.command === 'send') {
// // e.g. send email or pushover or whatever
// this.log.info('send command');
// // Send response in callback if required
// if (obj.callback) this.sendTo(obj.from, obj.command, 'Message received', obj.callback);
// }
// }
// }
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Opentherm(options);
} else {
// otherwise start the instance directly
new Opentherm();
}