-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwallbox.js
505 lines (485 loc) · 18 KB
/
wallbox.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**
* Wallbox setter and getter.
*
* Due to large main.js file, wallbox functionality was put in this separate file.
*
* @link https://github.com/ka-vaNu/ioBroker.e3dc-rscp/tree/Support-Wallbox
* @file This files defines the wallbox class.
* @author ka-vaNu.
* @since 1.1.0
*/
const helper = require( "./helper" );
const rscpWbType2Locked = {
0: "UNLOCKED",
1: "LOCKED",
16: "NOT_CONNECTED"
};
const rscpWbSunmode = {
"1": "SUNMODE",
"2": "MIXEDMODE",
};
const rscpWbActivePhases = {
1: "1-Phase",
3: "3-Phase",
};
const rscpBool = {
0: "NO",
1: "YES",
};
class wallbox {
constructor( settings, adapter, systemDictionary ) {
this.settings = settings || {};
this.adapter = adapter;
this.systemDictionary = systemDictionary;
this.rscpTag = require( "./lib/RscpTags.json" );
}
queueWbRequestData( sml ) {
this.adapter.clearFrame();
this.adapter.addTagtoFrame( "TAG_WB_REQ_CONNECTED_DEVICES", sml );
for ( let i = 0; i <= this.adapter.maxIndex["WB"]; i++ ) {
const pos = this.adapter.startContainer( "TAG_WB_REQ_DATA" );
this.adapter.addTagtoFrame( "TAG_WB_INDEX", "", i );
this.adapter.addTagtoFrame( "TAG_WB_REQ_STATUS", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_ERROR_CODE", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_MODE", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_DEVICE_ID", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_DEVICE_NAME", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_DEVICE_STATE", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_APP_SOFTWARE", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_BOOTLOADER_SOFTWARE", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_HW_VERSION", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_FLASH_VERSION", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_ENERGY_ALL", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_ENERGY_SOLAR", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_SOC", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_POWER_L1", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_POWER_L2", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_POWER_L3", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_ACTIVE_PHASES", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_MODE", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_ENERGY_L1", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_ENERGY_L2", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_ENERGY_L3", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_DEVICE_ID", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_ERROR_CODE", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_FIRMWARE_VERSION", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PM_MAX_PHASE_POWER", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_DIAG_INFOS", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_DIAG_WARNINGS", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_DIAG_ERRORS", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_DIAG_TEMP_1", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_DIAG_TEMP_2", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_EXTERN_DATA_SUN", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_EXTERN_DATA_NET", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_EXTERN_DATA_ALL", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_EXTERN_DATA_ALG", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PARAM_1", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_PARAM_2", sml );
this.adapter.endContainer( pos );
}
this.adapter.pushFrame();
}
queueWbRequestExternData( sml ) {
this.adapter.clearFrame();
this.adapter.addTagtoFrame( "TAG_WB_REQ_CONNECTED_DEVICES", sml );
for ( let i = 0; i <= this.adapter.maxIndex["WB"]; i++ ) {
const pos = this.adapter.startContainer( "TAG_WB_REQ_DATA" );
this.adapter.addTagtoFrame( "TAG_WB_INDEX", "", i );
this.adapter.addTagtoFrame( "TAG_WB_REQ_EXTERN_DATA_SUN", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_EXTERN_DATA_NET", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_EXTERN_DATA_ALL", sml );
this.adapter.addTagtoFrame( "TAG_WB_REQ_EXTERN_DATA_ALG", sml );
this.adapter.endContainer( pos );
}
this.adapter.pushFrame();
}
queueWbSetData( id ) {
const lp = "[queueWbSetData] ";
this.adapter.log.debug( lp + "State changed: " + id );
const baseId = id.substr( 0, id.lastIndexOf( "." ) );
const tag = id.substr( id.lastIndexOf( "." ) + 1 );
this.adapter.log.silly( lp + "baseId: " + baseId );
this.adapter.log.silly( lp + "tag: " + tag );
// Expects EXTERN_DATA (length 6) and EXTERN_DATA_LEN =6 /
// Byte 1: 1-Sonnenmode / 2-Mischmode /
// Byte 2: Strombegrenzung für alle / Modes, [1 ? 32] A /
// Byte 3: PreCharge (1: +5% / 2: -5%) /numPhases
// Byte 4: > 0: Anzahl Phasen tauschen /
// Byte 5: > 0: Typ 2, Laden abbrechen /
// Byte 6: > 0: Schuko, Best�tigung für ?AN? /
// SUNMODE
switch ( tag ) {
case "SunMode":
this.adapter.getState( baseId + ".SunMode", ( err, sunmodeState ) => {
if ( err ) {
this.adapter.log.error( err.message );
}
const sunmodeVal = ( sunmodeState ? sunmodeState.val : 0 );
const sunmode = helper.toHex( sunmodeVal );
this.adapter.log.silly( "sunmode: " + sunmode );
this.adapter.setState( baseId + ".SunMode", sunmodeVal, true );
this.buildWbExternData( sunmode + " 00 00 00 00 00" );
} );
break;
case "PowerLimitation":
// Power_Limitation
this.adapter.getState( baseId + ".PowerLimitation", ( err, powerLimitationState ) => {
if ( err ) {
this.adapter.log.error( err.message );
}
const powerLimitationVal = ( powerLimitationState ? powerLimitationState.val : 10 );
const powerLimitation = helper.toHex( powerLimitationVal );
this.adapter.log.silly( "powerLimitationVal: " + powerLimitation );
this.adapter.setState( baseId + ".PowerLimitation", powerLimitationVal, true );
this.buildWbExternData( "00 " + powerLimitation + " 00 00 00 00 " );
} );
break;
case "Precharge":
// Precharge
this.adapter.getState( baseId + ".Precharge", ( err, prechargeState ) => {
if ( err ) {
this.adapter.log.error( err.message );
}
const prechargeVal = ( prechargeState ? prechargeState.val : 10 );
const precharge = helper.toHex( prechargeVal );
this.adapter.log.silly( "prechargeVal: " + precharge );
this.adapter.setState( baseId + ".Precharge", prechargeVal, true );
this.buildWbExternData( "00 00 " + precharge + " 00 00 00 " );
} );
break;
case "TogglePhases":
// Toggle Phases 1->3, 3->1
this.adapter.getState( baseId + ".TogglePhases", ( err, togglePhasesState ) => {
if ( err ) {
this.adapter.log.error( err.message );
}
const togglePhasesVal = ( togglePhasesState ? togglePhasesState.val : 0 );
const togglePhases = helper.toHex( togglePhasesVal );
this.adapter.log.silly( "togglePhasesVal: " + togglePhasesVal );
// Reset toggle phases
this.adapter.setState( baseId + ".TogglePhases", 0, true );
this.buildWbExternData( "00 00 00 " + togglePhases + " 00 00 " );
} );
break;
case "ToggleChargingType2":
// Abort Charging Type 2 Plug
this.adapter.getState( baseId + ".ToggleChargingType2", ( err, abortChargingState ) => {
if ( err ) {
this.adapter.log.error( err.message );
}
const abortChargingVal = ( abortChargingState ? abortChargingState.val : 0 );
const abortCharging = helper.toHex( abortChargingVal );
this.adapter.log.silly( "abortChargingVal: " + abortChargingVal );
// reset abort charging
this.adapter.setState( baseId + ".ToggleChargingType2", 0, true );
this.buildWbExternData( "00 00 00 00 " + abortCharging + " 00 " );
} );
break;
default:
break;
}
// schuko unklar, zunächst ignorieren
// Aktualisierung beauftragen
this.queueWbRequestExternData( "" );
}
buildWbExternData( str ) {
const lp = "[buildWbExternData] ";
this.adapter.log.debug( lp + "Building Frame" );
this.adapter.log.silly( lp + "TAG_WB_EXTERN_DATA: " + str );
this.adapter.clearFrame();
const c1 = this.adapter.startContainer( "TAG_WB_REQ_DATA" );
this.adapter.addTagtoFrame( "TAG_WB_INDEX", "", 0 ); // Index der Wallbox = 0
const c2 = this.adapter.startContainer( "TAG_WB_REQ_SET_EXTERN" );
this.adapter.addTagtoFrame( "TAG_WB_EXTERN_DATA", "", str );
this.adapter.addTagtoFrame( "TAG_WB_EXTERN_DATA_LEN", "", 6 ); // Länge des bytearray
this.adapter.endContainer( c2 );
this.adapter.endContainer( c1 );
this.adapter.pushFrame( c1 );
this.adapter.log.silly( lp + "End Building Frame" );
}
storeWbExternData( shortId, tree, path ) {
const lp = "[storeWbExternData] ";
this.adapter.log.silly( lp + "Storing Data" );
const wbPath = "WB." + path.split( "." )[0];
this.adapter.log.silly( lp + "ExternData wbPath : " + wbPath );
if ( this.adapter.config.query_wb ) {
// Falls erforderlich wird die Struktur erzeugt
this.generateStates( wbPath );
// Tree verarbeiten
let container = "";
tree.forEach( token => {
this.adapter.log.silly( lp + "ExternData Token : " + JSON.stringify( token ) );
const tag = shortId.split( "." )[1];
if ( this.rscpTag[token.tag].DataType == "Container" ) {
this.adapter.log.silly( lp + "Container : " + container );
}
if ( this.rscpTag[token.tag].TagNameGlobal in ["TAG_WB_EXTERN_DATA_SUN", "TAG_WB_EXTERN_DATA_NET", "TAG_WB_EXTERN_DATA_ALL", "TAG_WB_EXTERN_DATA_ALG"] ) {
this.adapter.extendObject( "WB." + path + tag, { type: "channel", common: { role: "value" } } );
}
if ( this.rscpTag[token.tag].TagNameGlobal == "TAG_WB_EXTERN_DATA" ) {
this.adapter.log.silly( lp + "ExternData fullPath : " + wbPath + "." + tag + ".EXTERN_DATA" );
this.adapter.setObjectNotExists( wbPath + "." + tag + ".EXTERN_DATA", {
type: "state",
common: {
name: this.systemDictionary["EXTERN_DATA"][this.adapter.language],
type: "string",
role: "value",
read: true,
write: false,
unit: "",
def: ""
},
native: {},
} );
this.adapter.setState( wbPath + "." + tag + ".EXTERN_DATA", token.content, true );
this.adapter.log.silly( lp + "ExternData shortId : " + shortId );
this.adapter.log.silly( lp + "ExternData Content : " + token.content );
container = shortId.split( "." )[1];
this.adapter.log.silly( lp + "Container : " + container );
let extData = [];
switch ( container ) {
case "EXTERN_DATA_SUN":
extData = token.content.split( " " );
this.adapter.log.silly( lp + "EXTERN_DATA_SUN : " + JSON.stringify( extData ) );
this.adapter.log.silly( lp + "SunPower[W] : " + parseInt( extData[1] + extData[2], 16 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_SUN.SunPower", parseInt( extData[1] + extData[0], 16 ), true );
break;
case "EXTERN_DATA_NET":
extData = token.content.split( " " );
this.adapter.log.silly( lp + "EXTERN_DATA_NET : " + JSON.stringify( extData ) );
this.adapter.log.silly( lp + "GridPower[W] : " + parseInt( extData[1] + extData[2], 16 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_NET.GridPower", parseInt( extData[1] + extData[0], 16 ), true );
break;
case "EXTERN_DATA_ALL":
extData = token.content.split( " " );
this.adapter.log.silly( lp + "EXTERN_DATA_ALL : " + JSON.stringify( extData ) );
this.adapter.log.silly( lp + "TotalPower[W] : " + parseInt( extData[1] + extData[2], 16 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_ALL.TotalPower", parseInt( extData[1] + extData[0], 16 ), true );
break;
case "EXTERN_DATA_ALG":
extData = token.content.split( " " );
this.adapter.log.silly( lp + "EXTERN_DATA_ALG : " + JSON.stringify( extData ) );
// Byte 2 Aktive Phasen
this.adapter.log.silly( lp + "ActivePhases : " + parseInt( extData[1], 16 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_ALG.ActivePhases", parseInt( extData[1], 16 ), true );
/* Byte 3 WB-Status
EXTERN_DATA_ALG
A8 1010 1000 - Sonnenmodus, Not Canceled, Lädt , Locked
C8 1100 1000 - Sonnenmodus, Canceled, Lädt nicht, Locked
48 0100 1000 - MixedMode , Canceled, Lädt nicht, Locked
*/
this.adapter.log.silly( lp + "WB-Status : " + parseInt( extData[1], 16 ) );
// Bit 7 - Wert 128 - Sonnenmodus / Mixedmode
this.adapter.log.silly( lp + "SunMode : " + ( ( parseInt( extData[2], 16 ) & 128 ) == 128 ? 1 : 2 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_ALG.SunMode", ( parseInt( extData[2], 16 ) & 128 ) == 128 ? 1 : 2, true );
// Bit 6 - Wert 64 - Charging canceled
this.adapter.log.silly( lp + "CarChargingCanceled : " + ( ( parseInt( extData[2], 16 ) & 64 ) == 64 ? 1 : 0 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_ALG.CarChargingCanceled", ( parseInt( extData[2], 16 ) & 64 ) == 64 ? 1 : 0, true );
// Bit 5 - Wert 32 - Charging
this.adapter.log.silly( lp + "CarCharging : " + ( ( parseInt( extData[2], 16 ) & 32 ) == 32 ? 1 : 0 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_ALG.CarCharging", ( parseInt( extData[2], 16 ) & 32 ) == 32 ? 1 : 0, true );
// Bit 4 ist immer 0
// Bit 3 - Wert 8 - Locked
this.adapter.log.silly( lp + "Type2Locked : " + ( ( parseInt( extData[2], 16 ) & 8 ) == 8 ? 1 : 0 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_ALG.Type2Locked", ( parseInt( extData[2], 16 ) & 8 ) == 8 ? 1 : 0, true );
// Bit 3,2,1 sind immer 0
// Byte 4 : Ladeleistung - OK bei DEVICE_NAME "Easy Connect"
this.adapter.log.silly( lp + "PowerLimitation : " + parseInt( extData[3], 16 ) );
this.adapter.setState( wbPath + ".EXTERN_DATA_ALG.PowerLimitation", parseInt( extData[3], 16 ), true );
break;
default:
break;
}
}
} );
}
}
generateStates( wbPath ){
this.adapter.setObjectNotExists( "WB", {
type: "device",
common: {
name: this.systemDictionary["WB"][this.adapter.language],
role: "wallbox",
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".Control.SunMode", {
type: "state",
common: {
name: this.systemDictionary["SunMode"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: true,
unit: "",
states: rscpWbSunmode,
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".Control.PowerLimitation", {
type: "state",
common: {
name: this.systemDictionary["PowerLimitation"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: true,
unit: "A",
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".Control.TogglePhases", {
type: "state",
common: {
name: this.systemDictionary["TogglePhases"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: true,
unit: "",
states: rscpBool,
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".Control.ToggleChargingType2", {
type: "state",
common: {
name: this.systemDictionary["ToggleChargingType2"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: true,
unit: "",
states: rscpBool,
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_SUN.SunPower", {
type: "state",
common: {
name: this.systemDictionary["SunPower"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "W",
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_NET.GridPower", {
type: "state",
common: {
name: this.systemDictionary["GridPower"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "W",
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_ALL.TotalPower", {
type: "state",
common: {
name: this.systemDictionary["TotalPower"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "W",
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_ALG.CarCharging", {
type: "state",
common: {
name: this.systemDictionary["CarCharging"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "",
states: rscpBool,
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_ALG.CarChargingCanceled", {
type: "state",
common: {
name: this.systemDictionary["CarChargingCanceled"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "",
states: rscpBool,
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_ALG.Type2Locked", {
type: "state",
common: {
name: this.systemDictionary["Type2Locked"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "",
states: rscpWbType2Locked,
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_ALG.ActivePhases", {
type: "state",
common: {
name: this.systemDictionary["ActivePhases"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "",
states: rscpWbActivePhases,
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_ALG.SunMode", {
type: "state",
common: {
name: this.systemDictionary["SunMode"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "",
states: rscpWbSunmode,
def: 0
},
native: {},
} );
this.adapter.setObjectNotExists( wbPath + ".EXTERN_DATA_ALG.PowerLimitation", {
type: "state",
common: {
name: this.systemDictionary["PowerLimitation"][this.adapter.language],
type: "number",
role: "value",
read: true,
write: false,
unit: "A",
def: 0
},
native: {},
} );
}
}
module.exports = wallbox;