-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
312 lines (273 loc) · 10.8 KB
/
main.ts
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
/*
* Created with @iobroker/create-adapter v2.5.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
import * as utils from "@iobroker/adapter-core";
import { ApSystemsEz1Client } from "./lib/ApSystemsEz1Client";
// Load your modules here, e.g.:
// import * as fs from "fs";
class ApSystemsEz1 extends utils.Adapter {
private pollIntervalInMilliSeconds: number = 60;
private apiClient!: ApSystemsEz1Client;
public constructor(options: Partial<utils.AdapterOptions> = {}) {
super({
...options,
name: "ap-systems-ez1",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("objectChange", this.onObjectChange.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.
*/
private async onReady(): Promise<void> {
// Initialize your adapter here
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
this.log.info("config ipAddress: " + this.config.ipAddress);
this.log.info("config port: " + this.config.port);
this.log.info("config pollIntervalInSeconds: " + this.config.pollIntervalInSeconds);
this.log.info("config ignoreConnectionErrorMessages: " + this.config.ignoreConnectionErrorMessages);
if (!this.config?.ipAddress || !this.config?.port || !this.config?.pollIntervalInSeconds) {
this.log.error("Can not start with in valid config. Please open config.");
return;
}
this.pollIntervalInMilliSeconds = this.config.pollIntervalInSeconds * 1000;
this.apiClient = new ApSystemsEz1Client(this.log, this.config.ipAddress, this.config.port, this.config?.ignoreConnectionErrorMessages);
setInterval(() => {
this.setDeviceInfoStates();
this.setOutputDataStates();
this.setAlarmInfoStates();
this.setOnOffStatusState();
this.setMaxPowerState();
}, this.pollIntervalInMilliSeconds); // poll every <60> seconds
// // In order to get state updates, you need to subscribe to them. The following line adds a subscription for our variable we have created above.
// this.subscribeStates("testVariable");
// // You can also add a subscription for multiple states. The following line watches all states starting with "lights."
// // this.subscribeStates("lights.*");
// // Or, if you really must, you can also watch all states. Don't do this if you don't need to. Otherwise this will cause a lot of unnecessary load on the system:
// // this.subscribeStates("*");
// /*
// setState examples
// you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
// */
// // the variable testVariable is set to true as command (ack=false)
// await this.setStateAsync("testVariable", true);
// // same thing, but the value is flagged "ack"
// // ack should be always set to true if the value is received from or acknowledged from the target system
// await this.setStateAsync("testVariable", { val: true, ack: true });
// // same thing, but the state is deleted after 30s (getState will return null afterwards)
// await this.setStateAsync("testVariable", { val: true, ack: true, expire: 30 });
}
private setDeviceInfoStates() : void {
this.apiClient.getDeviceInfo().then(async (deviceInfo) => {
this.log.info(`deviceInfo: ${JSON.stringify(deviceInfo)}`);
if (deviceInfo !== undefined) {
const res = deviceInfo.data;
const strings = [
{ name: "DeviceId", value: res.deviceId },
{ name: "DevVer", value: res.devVer },
{ name: "Ssid", value: res.ssid },
{ name: "IpAddr", value: res.ipAddr }
];
strings.forEach(async (element) => {
if (!(await this.getStateAsync(element.name))) {
this.createState("DeviceInfo", "", element.name,
{
type: "string",
role: "text",
read: true,
write: false
}, () => this.log.info(`state ${element.name} created`));
}
await this.setStateAsync(`DeviceInfo.${element.name}`, { val: element.value, ack: true });
});
const numbers = [
{ name: "MaxPower", value: res.maxPower },
{ name: "MinPower", value: res.minPower }
];
numbers.forEach(async (element) => {
if (!(await this.getStateAsync(element.name))) {
this.createState("DeviceInfo", "", element.name,
{
type: "number",
role: "value",
read: true,
write: false
}, () => this.log.info(`state ${element.name} created`));
}
await this.setStateAsync(`DeviceInfo.${element.name}`, { val: element.value, ack: true });
});
}
});
}
private setOutputDataStates() : void {
this.apiClient.getOutputData().then(async (outputData) => {
this.log.info(`outputData: ${JSON.stringify(outputData)}`);
if (outputData !== undefined) {
const res = outputData.data;
const numbers = [
{ name: "CurrentPower_1", value: res.p1 },
{ name: "CurrentPower_2", value: res.p2 },
{ name: "CurrentPower_Total", value: res.p1 + res.p2 },
{ name: "EnergyToday_1", value: res.e1 },
{ name: "EnergyToday_2", value: res.e2 },
{ name: "EnergyToday_Total", value: res.e1 + res.e2 },
{ name: "EnergyLifetime_1", value: res.te1 },
{ name: "EnergyLifetime_2", value: res.te2 },
{ name: "EnergyLifetime_Total", value: res.te1 + res.te2 },
];
numbers.forEach(async (element) => {
if (!(await this.getStateAsync(element.name))) {
this.createState("OutputData", "", element.name,
{
type: "number",
role: "value",
read: true,
write: false
}, () => this.log.info(`state ${element.name} created`));
}
await this.setStateAsync(`OutputData.${element.name}`, { val: element.value, ack: true });
});
}
});
}
private setAlarmInfoStates() : void {
this.apiClient.getAlarmInfo().then(async (alarmInfo) => {
this.log.info(`alarmInfo: ${JSON.stringify(alarmInfo)}`);
if (alarmInfo !== undefined) {
const res = alarmInfo.data;
const numbers = [
{ name: "OffGrid", value: res.og },
{ name: "ShortCircuitError_1", value: res.isce1 },
{ name: "ShortCircuitError_2", value: res.isce2 },
{ name: "OutputFault", value: res.oe },
];
numbers.forEach(async (element) => {
if (!(await this.getStateAsync(element.name))) {
this.createState("AlarmInfo", "", element.name,
{
type: "string",
role: "text",
read: true,
write: false
}, () => this.log.info(`state ${element.name} created`));
}
const value = element.value === "0" ? "Normal" : "Alarm";
await this.setStateAsync(`AlarmInfo.${element.name}`, { val: value, ack: true });
});
}
});
}
private setOnOffStatusState() : void {
this.apiClient.getOnOffStatus().then(async (onOffStatus) => {
this.log.info(`onOffStatus: ${JSON.stringify(onOffStatus)}`);
if (onOffStatus !== undefined) {
const res = onOffStatus.data;
if (!(await this.getStateAsync("OnOffStatus"))) {
this.createState("OnOffStatus", "", "OnOffStatus",
{
type: "string",
role: "text",
read: true,
write: false
}, () => this.log.info(`state OnOffStatus created`));
const value = res.status === "0" ? "on" : "off";
await this.setStateAsync(`OnOffStatus.OnOffStatus`, { val: value, ack: true });
}
}
});
}
private setMaxPowerState() : void {
this.apiClient.getMaxPower().then(async (maxPower) => {
this.log.info(`maxPower: ${JSON.stringify(maxPower)}`);
if (maxPower !== undefined) {
const res = maxPower.data;
if (!(await this.getStateAsync("MaxPower"))) {
this.createState("MaxPower", "", "MaxPower",
{
type: "string",
role: "text",
read: true,
write: false
}, () => this.log.info(`state MaxPower created`));
await this.setStateAsync(`MaxPower.MaxPower`, { val: res.maxPower, ack: true });
}
}
});
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
*/
private onUnload(callback: () => void): void {
try {
// Here you must clear all timeouts or intervals that may still be active
// clearTimeout(timeout1);
// clearTimeout(timeout2);
// ...
// clearInterval(interval1);
callback();
} catch (e) {
callback();
}
}
// If you need to react to object changes, uncomment the following block and the corresponding line in the constructor.
// You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`.
// /**
// * Is called if a subscribed object changes
// */
// private onObjectChange(id: string, obj: ioBroker.Object | null | undefined): void {
// 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
*/
private onStateChange(id: string, state: ioBroker.State | null | undefined): void {
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`);
}
}
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
// /**
// * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
// * Using this method requires "common.messagebox" property to be set to true in io-package.json
// */
// private onMessage(obj: ioBroker.Message): void {
// 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);
// }
// }
// }
private async handleClientError(error: unknown): Promise<void> {
if (error instanceof Error) {
this.log.error(`Unknown error: ${error}. Stack: ${error.stack}`)
} else {
this.log.error(`Unknown error: ${error}`)
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
module.exports = (options: Partial<utils.AdapterOptions> | undefined) => new ApSystemsEz1(options);
} else {
// otherwise start the instance directly
(() => new ApSystemsEz1())();
}