forked from sg-app/ioBroker.fenecon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
323 lines (283 loc) · 9.16 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
"use strict";
/*
* Created with @iobroker/create-adapter v2.6.3
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
// Load your modules here, e.g.:
const axios = require("axios");
const States = require("./lib/states")
let createdChannel = [];
let createdState = [];
class Fenecon extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "fenecon",
});
this.on("ready", this.onReady.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
try {
// The adapters config (in the instance object everything under the attribute "native") is accessible via
if (!this.config.ipAddress) {
this.log.error(`[onReady] FEMS IP address is empty - please check configuration of ${this.namespace}`);
return;
}
if (!this.config.ipAddress.match("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}")) {
this.log.error(`[onReady] IP address has wrong format - please check configuration of ${this.namespace}`);
return;
}
if (this.config.refreshIntervall < 5 || this.config.refreshIntervall > 3600) {
this.log.error(`[onReady] Refresh interval only allows a range from 5s to 3600s - please check configuration of ${this.namespace}`);
return;
}
// http://x:user@<ipAddress>/rest/channel/
this.log.debug("[onReady] Adapter successful started.");
this.apiClient = axios.create({
baseURL: `http://x:owner@${this.config.ipAddress}/rest/channel/`,
timeout: 5000,
responseType: "json",
responseEncoding: "utf8"
});
this.log.debug("[onReady] axios instance successful created.");
await this.loadData();
}
catch (err) {
this.log.error(`[onReady] Error during startup with message: ${err.message}`);
}
}
async loadData() {
try {
this.log.debug("[loadData] Load data from FEMS.");
if (!this.apiClient) {
this.log.error("[loadData] Apiclient not instanced.");
return;
}
/** @type {{ data: any[] }} */
let response = { data: [] };
if (!this.config.useCustomEndpoints) {
const apiResponse = await this.apiClient.get(".*/.*");
if (apiResponse.status === 200) {
response.data.push(...apiResponse.data);
}
} else {
const endpoints = this.config.endpoints;
const endpointResponses = await Promise.all(endpoints.map(endpoint => {
const getEndpoint = `${endpoint.componentId}/${endpoint.channelId}`;
return this.apiClient ? this.apiClient.get(getEndpoint) : Promise.reject(new Error("[loadData] API client is not defined"));
}));
endpointResponses.forEach(endpointResponse => {
this.log.silly(`[loadData] response ${endpointResponse.status}: ${JSON.stringify(endpointResponse.data)}`);
if (endpointResponse.status === 200) {
response.data.push(...endpointResponse.data);
}
});
}
this.log.silly(`[loadData] combined response: ${JSON.stringify(response.data)}`);
if (response.data.length > 0) {
await this.createUpdateState(response.data);
await this.calculateAutarchy();
await this.calculateSelfConsumption();
}
this.log.debug("[loadData] REST request done and states updated.");
}
catch (err) {
this.log.error(`[loadData] Error during loading data: ${err.message}`);
}
finally {
// Restart request load Data!
this.timer = this.setTimeout(async () => {
this.log.debug("[loadData] Start next request.");
this.timer = null;
await this.loadData();
}, this.config.refreshIntervall * 1000);
}
}
/**
* Updates state.
* @param {object} data AXIOS Response Object Data
*/
async createUpdateState(data) {
try {
for (const item of data) {
const [channel, state] = item.address.split("/");
if (!channel || !state) continue;
const channelName = this.name2id(channel);
const stateName = this.name2id(state);
const id = `${channel}.${state}`;
const type = this.typeTranslation(item.type);
if (type === "boolean") {
item.value = !!item.value;
}
await this.createFolderObject(channelName);
await this.createStateObject(id, item, stateName);
this.log.debug(`[createUpdateState] StateId ${id} setState.`);
await this.setState(id, { val: item.value, ack: true });
}
} catch (err) {
this.log.error("[createUpdateState] Exception. " + err.message);
}
}
/**
* @param {string} channelName
*/
async createFolderObject(channelName) {
if (!createdChannel.includes(channelName)) {
this.log.debug(`[createFolderObject] Channel ${channelName} not exists. Extend Object.`);
await this.extendObject(channelName, {
_id: channelName,
type: "channel",
common: {
name: channelName
},
native: {}
});
createdChannel.push(channelName);
}
}
/**
* @param {string} id
* @param {{ address: string; text: any; accessMode: string; type: any; unit: any; }} item
* @param {any} stateName
*/
async createStateObject(id, item, stateName) {
if (!createdState.includes(id)) {
this.log.debug(`[createStateObject] StateId ${id} not exists. Extend state.`);
const existingState = States.find(f => f.address === item.address);
await this.extendObject(id, {
common: {
name: stateName,
desc: item.text,
role: existingState?.role ?? "state",
write: false,
read: item.accessMode === "RO" || item.accessMode === "RW",
type: this.typeTranslation(item.type),
unit: item.unit
},
type: "state",
native: {}
});
createdState.push(id);
}
}
name2id(pName) {
return (pName || '').replace(this.FORBIDDEN_CHARS, '_');
}
typeTranslation(inType) {
const types = {
INTEGER: "number",
LONG: "number",
DOUBLE: "number",
FLOAT: "number",
BOOLEAN: "boolean",
STRING: "string"
};
return types[inType];
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
if (this.timer) {
this.clearTimeout(this.timer);
this.timer = null;
}
callback();
} catch (e) {
callback();
}
}
async calculateAutarchy() {
const gridActivePowerId = "_sum.GridActivePower";
const consumptionActivePowerId = "_sum.ConsumptionActivePower";
if (!createdState.includes(gridActivePowerId) || !createdState.includes(consumptionActivePowerId)) {
return;
}
const [gridActivePowerState, consumptionActivePowerState] = await Promise.all([
this.getStateAsync(gridActivePowerId),
this.getStateAsync(consumptionActivePowerId)
]);
const gridActivePower = gridActivePowerState?.val;
const consumptionActivePower = consumptionActivePowerState?.val;
let autarchy = 0;
if (gridActivePower != null && consumptionActivePower != null && Number.isInteger(gridActivePower) && Number.isInteger(consumptionActivePower)) {
if (+consumptionActivePower <= 0)
autarchy = 100;
else
autarchy = Math.round(Math.max(0, Math.min(100, (1 - +gridActivePower / +consumptionActivePower) * 100)));
}
await this.createFolderObject("_sum");
await this.createStateObject(
"_sum.Autarchy",
{
address: "_sum/Autarchy",
text: "Autarchy",
accessMode: "RO",
type: "INTEGER",
unit: "%"
},
"Autarchy");
await this.setState("_sum.Autarchy", { val: autarchy, ack: true });
}
async calculateSelfConsumption() {
const gridActivePowerId = "_sum.GridActivePower";
const productionActivePowerId = "_sum.ProductionActivePower";
if (!createdState.includes(gridActivePowerId) || !createdState.includes(productionActivePowerId)) {
return;
}
const [gridActivePowerState, productionActivePowerState] = await Promise.all([
this.getStateAsync(gridActivePowerId),
this.getStateAsync(productionActivePowerId)
]);
let sellToGrid = gridActivePowerState?.val;
const productionActivePower = productionActivePowerState?.val;
let selfConsumption = 0;
if (sellToGrid != null && productionActivePower != null && Number.isInteger(sellToGrid) && Number.isInteger(productionActivePower)) {
if (+sellToGrid <= 0)
sellToGrid = +sellToGrid * -1
if (+productionActivePower > 0) {
selfConsumption = (1 - (+sellToGrid / +productionActivePower)) * 100;
selfConsumption = Math.max(0, Math.min(100, Math.floor(selfConsumption)));
// // At least 0 %
// selfConsumption = Math.max(selfConsumption, 0);
// // At most 100 %
// selfConsumption = Math.min(selfConsumption, 100);
// selfConsumption = Math.floor(selfConsumption);
}
}
await this.createFolderObject("_sum");
await this.createStateObject(
"_sum.SelfConsumption",
{
address: "_sum/SelfConsumption",
text: "SelfConsumption",
accessMode: "RO",
type: "INTEGER",
unit: "%"
},
"SelfConsumption");
await this.setState("_sum.SelfConsumption", { val: selfConsumption, ack: true });
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Fenecon(options);
} else {
// otherwise start the instance directly
new Fenecon();
}