This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
299 lines (271 loc) · 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
// @ts-nocheck
"use strict";
/*
* Created with @iobroker/create-adapter v2.3.0
*/
const utils = require("@iobroker/adapter-core");
const NodeFetch = (...args) => import("node-fetch").then(({default: fetch}) => fetch(...args));
// @ts-ignore
class Bayernluft extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "bayernluft",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.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
// Reset the connection indicator during startup
await this.setStateAsync("info.connection", false, true);
if (this.config.devices == null) {
this.log.error("No devices has been set, disabling adapter!");
this.disable();
return;
}
this.pullInterval = setInterval(async () => {
await this.checkDevices();
}, this.config.pullInterval * 1000);
// Indicate that the connection has been established
await this.setStateAsync("info.connection", true, true);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
async onUnload(callback) {
try {
await this.setStateAsync("info.connection", false, true);
this.pullInterval && clearInterval(this.pullInterval);
callback();
} catch (e) {
callback();
}
}
/**
* Checking Devices and updating Objects/States
*/
async checkDevices() {
for await(const device of this.GetDevices() || []) {
const exporttxt = await this.GetHttpRequest("http://" + device.ip + ":" + device.port + "/export.txt", device.name);
const deviceInfo = await this.GetHttpRequest("http://" + device.ip + ":" + device.port + "/?export=0", device.name);
if(exporttxt == null || deviceInfo == null) continue;
// Create Data Objects
await this.setObjectNotExistsAsyncEasy(device.name + ".data.date", "state", exporttxt.data.date.replaceAll("~~", ""), deviceInfo.data.date, "string", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".data.time", "state", exporttxt.data.time.replaceAll("~~", ""), deviceInfo.data.time, "string", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".data.name", "state", exporttxt.data.name.replaceAll("~~", ""), deviceInfo.data.name, "string", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".data.mac", "state", exporttxt.data.mac.replaceAll("~~", ""), deviceInfo.data.mac, "string", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".data.localip", "state", exporttxt.data.local_IP.replaceAll("~~", ""), deviceInfo.data.local_IP, "string", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".data.rssi", "state", exporttxt.data.rssi.replaceAll("~~", ""), deviceInfo.data.rssi, "string", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".data.fw_maincontroller", "state", exporttxt.data.fw_MainController.replaceAll("~~", ""), deviceInfo.data.fw_MainController, "string", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".data.fw_wifi", "state", exporttxt.data.fw_WiFi.replaceAll("~~", ""), deviceInfo.data.fw_WiFi, "string", "indicator", true, false);
// Create Parameter Objects
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.temperature_in", "state", exporttxt.parameter.temperature_In.replaceAll("~~", ""), deviceInfo.parameter.temperature_In, "number", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.temperature_out", "state", exporttxt.parameter.temperature_Out.replaceAll("~~", ""), deviceInfo.parameter.temperature_Out, "number", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.temperature_fresh", "state", exporttxt.parameter.temperature_Fresh.replaceAll("~~", ""), deviceInfo.parameter.temperature_Fresh, "number", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.rel_humidity_in", "state", exporttxt.parameter.rel_Humidity_In.replaceAll("~~", ""), deviceInfo.parameter.rel_Humidity_In, "number", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.rel_humidity_out", "state", exporttxt.parameter.rel_Humidity_Out.replaceAll("~~", ""), deviceInfo.parameter.rel_Humidity_Out, "number", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.abs_humidity_in", "state", exporttxt.parameter.abs_Humidity_In.replaceAll("~~", ""), deviceInfo.parameter.abs_Humidity_In, "number", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.abs_humidity_out", "state", exporttxt.parameter.abs_Humidity_Out.replaceAll("~~", ""), deviceInfo.parameter.abs_Humidity_Out, "number", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.efficiency", "state", exporttxt.parameter.efficiency.replaceAll("~~", ""), deviceInfo.parameter.efficiency, "number", "indicator", true, false);
await this.setObjectNotExistsAsyncEasy(device.name + ".parameter.humidity_transport", "state", exporttxt.parameter.humidity_Transport.replaceAll("~~", ""), deviceInfo.parameter.humidity_Transport, "number", "indicator", true, false);
// Create States Objects
await this.setObjectNotExistsAsyncEasy(device.name + ".states.speed_in", "state", exporttxt.states.speed_In.replaceAll("~~", ""), parseInt(deviceInfo.states.speed_In), "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.speed_out", "state", exporttxt.states.speed_Out.replaceAll("~~", ""), parseInt(deviceInfo.states.speed_Out), "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.antifreeze", "state", exporttxt.states.antiFreeze.replaceAll("~~", ""), parseInt(deviceInfo.states.antiFreeze), "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.systemon", "state", exporttxt.states.SystemOn.replaceAll("~~", ""), deviceInfo.states.SystemOn, "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.Antifreeze", "state", exporttxt.states.AntiFreeze.replaceAll("~~", ""), deviceInfo.states.AntiFreeze, "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.fixed_speed", "state", exporttxt.states.Fixed_Speed.replaceAll("~~", ""), deviceInfo.states.Fixed_Speed, "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.defrosting", "state", exporttxt.states.Defrosting.replaceAll("~~", ""), deviceInfo.states.Defrosting, "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.landlord_mode", "state", exporttxt.states.Landlord_Mode.replaceAll("~~", ""), deviceInfo.states.Landlord_Mode, "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.cross_ventilation", "state", exporttxt.states.Cross_Ventilation.replaceAll("~~", ""), deviceInfo.states.Cross_Ventilation, "number", "indicator", true, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".states.timer_active", "state", exporttxt.states.Timer_active.replaceAll("~~", ""), deviceInfo.states.Timer_active, "number", "indicator", true, true);
// Create Commands
await this.setObjectNotExistsAsyncEasy(device.name + ".commands.setSpeed", "state", "Speed", 1, "number", "level", true, true, 1, 10);
await this.setObjectNotExistsAsyncEasy(device.name + ".commands.powerOn", "state", "Power On", false, "boolean", "button", false, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".commands.powerOff", "state", "Power Off", false, "boolean", "button", false, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".commands.setAuto", "state", "Automatic Mode", false, "boolean", "button", false, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".commands.buttonPower", "state", "Power Button", false, "boolean", "button", false, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".commands.buttonTimer", "state", "Timer Button", false, "boolean", "button", false, true);
await this.setObjectNotExistsAsyncEasy(device.name + ".commands.syncTime", "state", "Sync Time", false, "boolean", "button", false, true);
await this.subscribeStatesAsync(device.name + ".commands.*");
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (!state) return;
if(state.val == false) return;
const id_splits = id.split(".");
const realid = id_splits[2] + "." + id_splits[3] + "." + id_splits[4];
const device = await this.GetDeviceByName(id_splits[2]);
if(id.includes(".setSpeed")) {
const res = await this.sendHttpRequest("http://" + device.ip + ":" + device.port + "/?speed=" + state.val, device.name);
if(!res) return this.log.error("An error has occured while trying to set Device " + device.name + " Speed to " + state.val);
await this.setState(device.name + ".states.speed_in", state.val, true);
await this.setState(device.name + ".states.speed_out", state.val, true);
await this.setState(realid, state.val, true);
} else if(id.includes(".powerOn")) {
const res = await this.sendHttpRequest("http://" + device.ip + ":" + device.port + "/?power=on");
if(!res) return this.log.error("An error has occured while trying to power on device " + device.name);
await this.setState(device.name + ".states.SystemOn", 1, true);
await this.setState(realid, false);
} else if(id.includes(".powerOff")) {
const res = await this.sendHttpRequest("http://" + device.ip + ":" + device.port + "/?power=off");
if(!res) return this.log.error("An error has occured while trying to power off device " + device.name);
await this.setState(device.name + ".states.SystemOn", 0, true);
await this.setState(realid, false);
} else if(id.includes(".setAuto")) {
const res = await this.sendHttpRequest("http://" + device.ip + ":" + device.port + "/?speed=0");
if(!res) return this.log.error("An error has occured while trying to set automatic mode for device " + device.name);
await this.setState(device.name + ".states.speed_in", 0, true);
await this.setState(device.name + ".states.speed_out", 0, true);
await this.setState(realid, false);
} else if(id.includes(".buttonPower")) {
const res = await this.sendHttpRequest("http://" + device.ip + ":" + device.port + "/?button=power");
if(!res) return this.log.error("An error has occured while trying to send power button for device " + device.name);
await this.setState(realid, false);
} else if(id.includes(".buttonTimer")) {
const res = await this.sendHttpRequest("http://" + device.ip + ":" + device.port + "/?button=timer");
if(!res) return this.log.error("An error has occured while trying to send power button to device " + device.name);
await this.setState(realid, false);
} else if(id.includes(".syncTime")) {
const res = await this.sendHttpRequest("http://" + device.ip + ":" + device.port + "/index.html?TimeSync=1");
if(!res) return this.log.error("An error has occured while trying to sync time for device " + device.name);
await this.setState(realid, false);
}
}
/**
* Creates an object in the object db. Existing objects are overwritten.
* @param {String} id ID
* @param {"host" | "instance" | "config" | "channel" | "state" | "device" | "folder" | "enum" | "meta" | "adapter" | "user" | "group" | "script" | "chart"} type Type
* @param {ioBroker.StringOrTranslated} name The name of this object as a simple string or an object with translations
* @param {ioBroker.ObjectCommon.def} def the default value
* @param {ioBroker.CommonType} common_type Type of this state. See https://github.com/ioBroker/ioBroker/blob/master/doc/SCHEMA.md#state-commonrole for a detailed description
* @param {String} role role of the state (used in user interfaces to indicate which widget to choose)
* @param {Boolean} read if this state is readable
* @param {Boolean} write if this state is writable
* @param {Number} min Minimum (Optional)
* @param {Number} max Maximum (Optional)
*/
async setObjectNotExistsAsyncEasy(id, type, name, def, common_type, role, read, write, min = -1, max = -1) {
const object = await this.getObjectAsync(id);
if (object == null) {
if (min != -1 && max != -1) {
await this.setObjectNotExistsAsync(id, { type: type, common: { name: name, def: def, type: common_type, role: role, read: read, write: write, min: min, max: max } });
} else {
await this.setObjectNotExistsAsync(id, { type: type, common: { name: name, def: def, type: common_type, role: role, read: read, write: write } });
}
} else {
if(id.includes(".data.")) {
await this.setStateAsync(id, def, "string");
} else if(id.includes(".parameter.")) {
await this.setStateAsync(id, parseFloat(def), "number");
} else if(id.includes(".states.")) {
await this.setStateAsync(id, parseFloat(def), "number");
}
}
}
/**
* Returns all devices in config
* @returns All Devices in Config
*/
GetDevices() {
return this.config.devices;
}
/**
* Get Device Info by Name
* @param {String} name Device name
*/
async GetDeviceByName(name) {
const devices = await this.GetDevices();
if(devices == null || !devices) return null;
let device = null;
for await(const devicen of devices) {
if(devicen.name == name) {
device = devicen;
break;
}
}
return device;
}
/**
*
* @param {String} url URL to get Data from
* @param {String} deviceName Device Name
*/
async GetHttpRequest(url, deviceName) {
let response = null;
try {
response = await NodeFetch(url);
} catch(error) {
if (error.code == "ETIMEDOUT") {
this.log.error("An error has occured while trying to get response from device " + deviceName + ". The Connection timed out!");
return null;
}
if (error.code == "ECONNREFUSED") {
this.log.error("An error has occured while trying to get response from device " + deviceName + ". The Connection has been refused!");
return null;
}
this.log.error("An unexpected error has occred while trying to get response from device " + deviceName + ".");
return null;
}
let data = null;
try {
data = await response.json();
} catch(error) {
if(error.type == "invalid-json") {
this.log.error("An error has occured while trying to format json data. Did you setup the template correctly? Go to LINK to setup template correctly");
return null;
}
this.log.error("Unexpected Error while trying to format json data! " + error);
return null;
}
if (data == null) return null;
return data;
}
/**
* Send HTTP Request without returning any data
* @param {String} url URL to send the Command
* @param {String} deviceName Device Name
*/
async sendHttpRequest(url, deviceName) {
let response = null;
try {
response = await NodeFetch(url);
} catch(error) {
if (error.code == "ETIMEDOUT") {
this.log.error("An error has occured while trying to send request to device " + deviceName + ". The Connection timed out!");
return null;
}
if (error.code == "ECONNREFUSED") {
this.log.error("An error has occured while trying to send request to device " + deviceName + ". The Connection has been refused!");
return null;
}
this.log.error("An unexpected error has occred while trying to send request to device " + deviceName + ".");
}
if (response.status == 200 && response.statusText == "OK") return true;
return false;
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Bayernluft(options);
} else {
// otherwise start the instance directly
new Bayernluft();
}