-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
366 lines (325 loc) · 12.2 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
"use strict";
const utils = require("@iobroker/adapter-core"); // Adapter core module
const net = require("net"); // Node.js net module for TCP connections
class HuaweiCharger extends utils.Adapter {
constructor(options) {
super({
...options,
name: "huawei-charger",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("unload", this.onUnload.bind(this));
this.client = null; // Placeholder for TCP client
this.reconnectInterval = null;
this.isConnected = false;
this.writeCache = []; // Cache for write operations while disconnected
}
async onReady() {
this.setState("info.connection", false, true);
this.log.info("Connecting to Huawei Charger...");
this.connectToCharger();
}
connectToCharger() {
const ipAddress = this.config.ipAddress;
const port = this.config.port || 502; // Default port 502
const unitId = this.config.unitId || 1; // Default unit ID 1
if (!ipAddress || !port) {
this.log.error("IP address or port is not specified. Please check your configuration.");
return;
}
this.client = new net.Socket();
this.client.connect(port, ipAddress, () => {
this.isConnected = true;
this.clearReconnectInterval();
this.setState("info.connection", true, true);
this.log.info("Successfully connected to Huawei Charger.");
this.setupObjects(); // Set up the object tree
this.processWriteCache(); // Process cached writes
});
this.client.on('data', (data) => {
this.log.debug(`Received data: ${data.toString('hex')}`);
this.processIncomingData(data);
});
this.client.on('error', (err) => {
this.log.error(`Connection error: ${err.message}`);
this.isConnected = false;
this.setState("info.connection", false, true);
this.scheduleReconnect();
});
this.client.on('close', () => {
if (this.isConnected) {
this.log.info("Connection to Huawei Charger closed.");
this.isConnected = false;
this.setState("info.connection", false, true);
this.scheduleReconnect();
}
});
}
scheduleReconnect() {
if (this.reconnectInterval) return; // Avoid multiple reconnect intervals
const reconnectInterval = this.config.reconnectInterval || 60000; // Default to 1 minute
this.reconnectInterval = setInterval(() => {
if (!this.isConnected) {
this.log.info("Attempting to reconnect to Huawei Charger...");
this.connectToCharger();
}
}, reconnectInterval);
}
clearReconnectInterval() {
if (this.reconnectInterval) {
clearInterval(this.reconnectInterval);
this.reconnectInterval = null;
}
}
// Setup the objects in the ioBroker object tree
async setupObjects() {
// Phase L1 Output Voltage
await this.setObjectNotExistsAsync('charger.phaseL1.voltage', {
type: 'state',
common: {
name: 'Phase L1 Output Voltage',
type: 'number',
role: 'value.voltage',
unit: 'V',
read: true,
write: false
},
native: {}
});
// Phase L2 Output Voltage
await this.setObjectNotExistsAsync('charger.phaseL2.voltage', {
type: 'state',
common: {
name: 'Phase L2 Output Voltage',
type: 'number',
role: 'value.voltage',
unit: 'V',
read: true,
write: false
},
native: {}
});
// Phase L3 Output Voltage
await this.setObjectNotExistsAsync('charger.phaseL3.voltage', {
type: 'state',
common: {
name: 'Phase L3 Output Voltage',
type: 'number',
role: 'value.voltage',
unit: 'V',
read: true,
write: false
},
native: {}
});
// Phase L1 Output Current
await this.setObjectNotExistsAsync('charger.phaseL1.current', {
type: 'state',
common: {
name: 'Phase L1 Output Current',
type: 'number',
role: 'value.current',
unit: 'A',
read: true,
write: false
},
native: {}
});
// Phase L2 Output Current
await this.setObjectNotExistsAsync('charger.phaseL2.current', {
type: 'state',
common: {
name: 'Phase L2 Output Current',
type: 'number',
role: 'value.current',
unit: 'A',
read: true,
write: false
},
native: {}
});
// Phase L3 Output Current
await this.setObjectNotExistsAsync('charger.phaseL3.current', {
type: 'state',
common: {
name: 'Phase L3 Output Current',
type: 'number',
role: 'value.current',
unit: 'A',
read: true,
write: false
},
native: {}
});
// Total Output Power
await this.setObjectNotExistsAsync('charger.totalPower', {
type: 'state',
common: {
name: 'Total Output Power',
type: 'number',
role: 'value.power',
unit: 'kW',
read: true,
write: false
},
native: {}
});
// Combined Voltage of all Phases
await this.setObjectNotExistsAsync('charger.combined.voltage', {
type: 'state',
common: {
name: 'Combined Voltage (average)',
type: 'number',
role: 'value.voltage',
unit: 'V',
read: true,
write: false
},
native: {}
});
// Combined Current of all Phases
await this.setObjectNotExistsAsync('charger.combined.current', {
type: 'state',
common: {
name: 'Combined Current (L1 + L2 + L3)',
type: 'number',
role: 'value.current',
unit: 'A',
read: true,
write: false
},
native: {}
});
// Max Charging Power (writable)
await this.setObjectNotExistsAsync('charger.maxChargingPower', {
type: 'state',
common: {
name: 'Max Charging Power',
type: 'number',
role: 'level',
unit: 'kW',
min: 0,
max: 22,
def: 22,
read: true,
write: true
},
native: {}
});
// Charging Control (writable with selection)
await this.setObjectNotExistsAsync('charger.chargingControl', {
type: 'state',
common: {
name: 'Charging Control',
type: 'number',
role: 'state',
states: {
0: 'Standby',
1: 'Paused',
2: 'Charging'
},
def: 0,
read: true,
write: true
},
native: {}
});
}
// Process incoming data and update corresponding objects
processIncomingData(data) {
try {
const functionCode = data.readUInt8(7);
const registerData = data.slice(8);
if (functionCode === 3) { // Read Holding Registers
const phaseL1Voltage = registerData.length >= 4 ? Math.round((registerData.readUInt32BE(0) / 10000000) * 100) / 100 : 0;
const phaseL2Voltage = registerData.length >= 8 ? Math.round((registerData.readUInt32BE(4) / 10000000) * 100) / 100 : 0;
const phaseL3Voltage = registerData.length >= 12 ? Math.round((registerData.readUInt32BE(8) / 10000000) * 100) / 100 : 0;
const phaseL1Current = registerData.length >= 16 ? Math.round((registerData.readUInt32BE(12) / 10) * 100) / 100 : 0;
const phaseL2Current = registerData.length >= 20 ? Math.round((registerData.readUInt32BE(16) / 10) * 100) / 100 : 0;
const phaseL3Current = registerData.length >= 24 ? Math.round((registerData.readUInt32BE(20) / 10) * 100) / 100 : 0;
const totalPower = registerData.length >= 28 ? Math.round((registerData.readUInt32BE(24) / 10) * 100) / 100 : 0;
let combinedVoltageSum = 0;
let combinedVoltageCount = 0;
if (phaseL1Voltage > 0) {
combinedVoltageSum += phaseL1Voltage;
combinedVoltageCount++;
}
if (phaseL2Voltage > 0) {
combinedVoltageSum += phaseL2Voltage;
combinedVoltageCount++;
}
if (phaseL3Voltage > 0) {
combinedVoltageSum += phaseL3Voltage;
combinedVoltageCount++;
}
const combinedVoltage = combinedVoltageCount > 0 ? Math.round((combinedVoltageSum / combinedVoltageCount) * 100) / 100 : 0;
const combinedCurrent = Math.round((phaseL1Current + phaseL2Current + phaseL3Current) * 100) / 100;
this.setStateAsync('charger.phaseL1.voltage', { val: phaseL1Voltage, ack: true });
this.setStateAsync('charger.phaseL2.voltage', { val: phaseL2Voltage, ack: true });
this.setStateAsync('charger.phaseL3.voltage', { val: phaseL3Voltage, ack: true });
this.setStateAsync('charger.phaseL1.current', { val: phaseL1Current, ack: true });
this.setStateAsync('charger.phaseL2.current', { val: phaseL2Current, ack: true });
this.setStateAsync('charger.phaseL3.current', { val: phaseL3Current, ack: true });
this.setStateAsync('charger.totalPower', { val: totalPower, ack: true });
this.setStateAsync('charger.combined.voltage', { val: combinedVoltage, ack: true });
this.setStateAsync('charger.combined.current', { val: combinedCurrent, ack: true });
}
} catch (error) {
this.log.error(`Failed to process incoming data: ${error.message}`);
}
}
// Handle state changes
onStateChange(id, state) {
if (state && !state.ack) {
this.log.info(`State ${id} changed: ${state.val}`);
if (id === `${this.namespace}.charger.maxChargingPower`) {
const maxChargingPower = state.val * 10; // Convert to correct format for sending
this.writeToCharger(8192, maxChargingPower);
} else if (id === `${this.namespace}.charger.chargingControl`) {
const chargingControl = state.val;
this.writeToCharger(8198, chargingControl);
}
}
}
// Write data to the charger
writeToCharger(register, value) {
if (this.client && this.isConnected) {
const buffer = Buffer.alloc(6);
buffer.writeUInt16BE(register, 0);
buffer.writeUInt32BE(value, 2);
this.client.write(buffer, () => {
this.log.info(`Wrote value ${value} to register ${register}`);
});
} else {
this.log.warn("Cannot write to charger, not connected.");
this.writeCache.push({ register, value });
}
}
processWriteCache() {
if (this.writeCache.length > 0 && this.isConnected) {
this.log.info("Processing cached write operations...");
this.writeCache.forEach(entry => {
this.writeToCharger(entry.register, entry.value);
});
this.writeCache = []; // Clear cache after processing
}
}
onUnload(callback) {
try {
if (this.client) {
this.client.destroy();
}
this.clearReconnectInterval();
this.log.info("Connection to Huawei Charger closed.");
callback();
} catch (e) {
callback();
}
}
}
if (require.main !== module) {
module.exports = (options) => new HuaweiCharger(options);
} else {
new HuaweiCharger();
}