-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
302 lines (268 loc) · 10.7 KB
/
main.js
File metadata and controls
302 lines (268 loc) · 10.7 KB
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
'use strict';
/*
* Created with @iobroker/create-adapter v3.1.2
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const { SerialPort } = require('serialport');
const { ReadlineParser } = require('@serialport/parser-readline');
const connectionTimeoutMs = 15 * 1000;
const portRetryDelayMs = 30 * 1000;
// Types, units of TIC fields
// TODO: add descriptions with translations
// TODO: add tri-phase
// TODO: add 'standard' mode
const ticStateCommon = {
ADCO: { type: 'string' },
OPTARIF: { type: 'string' },
ISOUSC: { type: 'number', unit: 'A', role: 'value.current' },
BASE: { type: 'number', unit: 'Wh', role: 'value.energy' },
HCHC: { type: 'number', unit: 'Wh', role: 'value.energy' },
HCHP: { type: 'number', unit: 'Wh', role: 'value.energy' },
EJPHN: { type: 'number', unit: 'Wh', role: 'value.energy' },
EJPHPM: { type: 'number', unit: 'Wh', role: 'value.energy' },
BBRHCJB: { type: 'number', unit: 'Wh', role: 'value.energy' },
BBRHPJB: { type: 'number', unit: 'Wh', role: 'value.energy' },
BBRHCJW: { type: 'number', unit: 'Wh', role: 'value.energy' },
BBRHPJW: { type: 'number', unit: 'Wh', role: 'value.energy' },
BBRHCJR: { type: 'number', unit: 'Wh', role: 'value.energy' },
BBRHPJR: { type: 'number', unit: 'Wh', role: 'value.energy' },
PEJP: { type: 'number' /* In minutes */ },
PTEC: { type: 'string' },
DEMAIN: { type: 'string' },
IINST: { type: 'number', unit: 'A', role: 'value.current' },
ADPS: { type: 'number', unit: 'A', role: 'value.current' },
IMAX: { type: 'number', unit: 'A', role: 'value.current' },
PAPP: { type: 'number', unit: 'VA', role: 'value.power.reactive' },
HHPHC: { type: 'string' },
MOTDETAT: { type: 'string' },
};
class Linky extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options] - Adapter options
*/
constructor(options) {
super({
...options,
name: 'linky',
});
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
this.port = null;
this.adco = null;
this.connectionTimeout = null;
this.portRetryTimer = null;
this.knownObjects = [];
}
// Set/reset connection
setConnected(connected) {
this.setState('info.connection', connected, true);
if (!connected) {
this.adco = null;
if (this.connectionTimeout) {
this.clearTimeout(this.connectionTimeout);
this.connectionTimeout = null;
}
}
}
// Set connection timer
startConnectionTimer() {
if (this.connectionTimeout) {
this.clearTimeout(this.connectionTimeout);
}
this.connectionTimeout = this.setTimeout(async () => {
this.connectionTimeout = null;
this.log.warn(`Connection timeout for ${this.adco}`);
this.setConnected(false);
this.closePort(() => {
this.retryOpen();
});
}, connectionTimeoutMs);
}
closePort(callback) {
if (this.portRetryTimer) {
this.clearTimeout(this.portRetryTimer);
this.portRetryTimer = null;
}
if (this.port) {
this.port.close(error => {
if (error) {
callback(error);
} else {
this.log.info(`Closed serial port: ${this.config.serialPort}`);
this.port = null;
callback();
}
});
} else {
callback();
}
}
retryOpen() {
this.log.debug(`Retrying port open ${this.config.serialPort} in ${portRetryDelayMs}ms`);
this.portRetryTimer = this.setTimeout(() => {
this.portRetryTimer = null;
this.openPort();
}, portRetryDelayMs);
}
openPort() {
this.log.info(`Opening serial port: ${this.config.serialPort}`);
try {
this.port = new SerialPort({
path: this.config.serialPort,
baudRate: 1200,
dataBits: 7,
parity: 'even',
});
this.port.on('error', error => {
this.log.error(`Serial port error: ${error.message}`);
this.port = null;
this.setConnected(false);
this.retryOpen();
});
this.port.on('open', () => {
this.log.debug('Serial port opened');
this.parsePort();
});
} catch (error) {
this.log.error(`Failed to open serial port ${this.config.serialPort}: ${error.message}`);
this.retryOpen();
}
}
async checkAndSetState(name, value) {
// Create channel for ADCO if we haven't done already.
if (!this.knownObjects.includes(this.adco)) {
this.log.debug(`Creating channel: ${this.adco}`);
try {
await this.setObjectNotExists(this.adco, {
type: 'channel',
common: {
name: this.adco,
},
native: {},
});
this.knownObjects.push(this.adco);
} catch (error) {
this.log.error(`Failed to create channel for ${this.adco}: ${error.message}`);
}
}
const stateName = `${this.adco}.${name}`;
// Create state for value if we haven't done already.
if (!this.knownObjects.includes(stateName)) {
this.log.debug(`Creating state: ${stateName}`);
try {
await this.setObjectNotExists(stateName, {
type: 'state',
common: { read: true, write: false, ...ticStateCommon[name] },
native: {},
// TODO: other attributes?
});
this.knownObjects.push(stateName);
} catch (error) {
this.log.error(`Failed to create state for ${stateName}: ${error.message}`);
}
}
// Set state value only if it has changed.
try {
this.setStateChanged(stateName, {
ack: true,
val: ticStateCommon[name].type === 'number' ? Number(value) : value,
});
} catch (error) {
this.log.error(`Failed to set state value ${stateName} -> ${value}: ${error.message}`);
}
}
parsePort() {
if (!this.port || !this.port.isOpen) {
this.log.error('Cannot parse port because it is not open');
} else {
const parser = this.port.pipe(new ReadlineParser({ delimiter: '\n' }));
parser.on('data', async data => {
// data is a string
this.log.silly(data);
const parts = data.split(/\s+/);
if (parts.length != 4) {
this.log.warn(`Unexpected data: ${parts.length} parts (expected 4)`);
} else {
const name = parts.shift();
const value = parts.shift();
const checksum = parts.shift();
const theirChecksumInt = checksum.charCodeAt(0);
// From Teleinfo document, how to calculate checksum:
//
// Calculate the sum "S1" of all characters from the beginning of the "Label" field up
// to and including the delimiter between the "Data" and "Checksum" fields;
//
// This sum is truncated to 6 bits (this operation is performed using a logical AND with 0x3F);
//
// To obtain the checksum result, add the previous result S2 to 0x20
let ourChecksumInt = 0;
for (let lp = 0; lp < name.length + value.length + 1; lp++) {
ourChecksumInt += data.charCodeAt(lp);
this.log.silly(`checksum: char ${lp} is ${data.charCodeAt(lp)} so far ${ourChecksumInt}`);
}
ourChecksumInt = (ourChecksumInt & 0x3f) + 0x20;
this.log.silly(`checksum complete: ${ourChecksumInt}`);
if (ourChecksumInt != theirChecksumInt) {
this.log.warn(`Checksum error. Ours (${ourChecksumInt}) does not match ${theirChecksumInt})`);
} else {
if (ticStateCommon[name] === undefined) {
// We don't know what this field is so ignore it
this.log.warn(`Unknown label (ignoring): ${name}`);
} else if (name === 'ADCO') {
// Store this as last ADCO seen which all following fields belong to.
if (this.adco !== value) {
// Only log if changed.
this.adco = value;
this.log.info(`Found ADCO '${this.adco}'`);
this.setConnected(true);
}
// If we got ADCO, the connection must be alive, so reset the timer.
this.startConnectionTimer();
} else if (this.adco) {
this.checkAndSetState(name, value);
} else {
this.log.warn(`Received ${name} ${value} but no ADCO known yet`);
}
}
}
});
}
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
this.setConnected(false);
if (!this.config.serialPort) {
this.log.error('No serial port configured! Please define the serial port in adapter settings.');
this.disable();
} else {
this.openPort();
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
*
* @param {() => void} callback - Callback function
*/
onUnload(callback) {
try {
this.closePort(callback);
} catch (error) {
this.log.error(`Error during unloading: ${error.message}`);
callback();
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options] - Adapter options
*/
module.exports = options => new Linky(options);
} else {
// otherwise start the instance directly
new Linky();
}