-
Notifications
You must be signed in to change notification settings - Fork 0
/
VartaFetcher.js
177 lines (148 loc) · 4.94 KB
/
VartaFetcher.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
"use strict";
const EventEmitter = require("events");
const ModbusRTU = require("modbus-serial");
const Register = require("./Register").Register;
const DataType = require("./Register").DataType;
// Source: https://community.symcon.de/t/modbus-aus-varta-stromspeicher/51144/5
const Registers = {
NUM_BATTERY_MODULES: new Register(1064, 1, DataType.UINT16),
STATE: new Register(1065, 1, DataType.UINT16),
SOC: new Register(1068, 1, DataType.UINT16),
INSTALLED_CAPACITY: new Register(1071, 1, DataType.UINT16),
GRID_POWER: new Register(1078, 1, DataType.SINT16),
ACTIVE_POWER: new Register(1066, 1, DataType.SINT16),
};
const State = {
INIT: "MODBUS_INIT",
IDLE: "MODBUS_IDLE",
NEXT: "MODBUS_NEXT",
READ_SUCCESS: "MODBUS_READ_SUCCESS",
READ_ERROR: "MODBUS_READ_ERROR",
CONNECT_SUCCESS: "MODBUS_CONNECT_SUCCESS",
CONNECT_ERROR: "MODBUS_CONNECT_ERROR",
};
const BatteryState = {
BUSY: 0, // e.g. Booting
RUN: 1, // ready to charge/discharge
CHARGE: 2,
DISCHARGE: 3,
STANDBY: 4,
ERROR: 5,
PASSIVE: 6, // Service
ISLANDING: 7,
};
class VartaFetcher extends EventEmitter {
constructor(config) {
super();
this.timeout = 1000;
this.updateInterval = config.updateInterval;
this.clientId = config.clientId;
this.port = config.port;
this.ip = config.ip;
this.client = new ModbusRTU();
this.client.setID(this.clientId);
this.client.setTimeout(this.timeout);
this.state = State.INIT;
}
async connect() {
try {
await this.disconnect();
await this.client.connectTCP(this.ip, { port: this.port });
this.state = State.CONNECT_SUCCESS;
this.log(`Connected.`);
} catch (error) {
this.state = State.CONNECT_ERROR;
this.emit("ERROR", State.CONNECT_ERROR);
this.log(`Connection error.`);
console.log(error);
}
}
async disconnect() {
await this.client.close(() => {
console.log("VartaFetcher connection closed.");
});
}
async readRegister(register) {
const data = (await this.client.readHoldingRegisters(register.address, register.length)).data;
return register.convertData(data);
}
async readData() {
try {
const batteryState = await this.readRegister(Registers.STATE);
const batteryStateString = this.getBatteryStateString(batteryState);
const data = {
state: batteryStateString,
soc: await this.readRegister(Registers.SOC),
gridPower: await this.readRegister(Registers.GRID_POWER),
activePower: await this.readRegister(Registers.ACTIVE_POWER),
};
this.emit("DATA", data);
this.state = State.READ_SUCCESS;
// this.log("Successfully read modbus data.");
} catch (error) {
this.state = State.READ_ERROR;
this.emit("ERROR", State.READ_ERROR);
this.log("Modbus read error:");
this.log(JSON.stringify(error));
}
}
async run() {
switch (this.state) {
case State.INIT:
await this.connect();
break;
case State.NEXT:
await this.readData();
break;
case State.CONNECT_SUCCESS:
await this.readData();
break;
case State.CONNECT_ERROR:
await this.connect();
break;
case State.READ_SUCCESS:
await this.readData();
break;
case State.READ_ERROR:
if (this.client.isOpen) {
this.state = State.NEXT;
} else {
await this.connect();
}
break;
default:
// nothing to do, keep scanning until actionable case
}
setTimeout(() => {
this.run();
}, this.updateInterval);
}
getBatteryStateString(state) {
switch (state) {
case BatteryState.BUSY:
return "BATTERY_BUSY";
case BatteryState.RUN:
return "BATTERY_RUN";
case BatteryState.CHARGE:
return "BATTERY_CHARGE";
case BatteryState.DISCHARGE:
return "BATTERY_DISCHARGE";
case BatteryState.STANDBY:
return "BATTERY_STANDBY";
case BatteryState.ERROR:
return "BATTERY_ERROR";
case BatteryState.PASSIVE:
return "BATTERY_PASSIVE";
case BatteryState.ISLANDING:
return "BATTERY_ISLANDING";
default:
return "BATTERY_UNKOWN";
}
}
log(msg) {
console.log(`[VartaESS Data Fetcher (ID: ${this.clientId})] ${msg}`);
}
}
module.exports = {
VartaFetcher,
};