-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
230 lines (196 loc) · 6.49 KB
/
app.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
"use strict";
const Homey = require("homey");
const SmartBus = require("smart-bus");
const HdlDevicelist = require("./hdl/hdl_devicelist");
class HDLSmartBus extends Homey.App {
async onInit() {
this._busConnected = false;
this._bus = null;
this._controller = null;
this._hdlDevicelist = new HdlDevicelist();
// All new devicetypes must be added here
this._driverlist = [
"dimmer",
"relay",
"tempsensor",
"multisensor",
"universal-switch",
"floorheater",
"curtain",
"panel",
];
this._hdlFoundUnits = {}
for (let i = 0; i < this._driverlist.length; i++) {
this._hdlFoundUnits[this._driverlist[i]] = {}
}
this.log("Homey HDL SmartBus app has been initialized...");
try {
await this.connect();
} catch (err) {
this.log(err.message);
}
}
async connect() {
let hdl_ip_address = this.homey.settings.get("hdl_ip_address");
let hdl_subnet = this.homey.settings.get("hdl_subnet");
let hdl_id = this.homey.settings.get("hdl_id");
let hdl_motion = this.homey.settings.get("hdl_universal_motion");
// Set the universal motion if not set
if (hdl_motion == undefined || hdl_motion == "") {
this.homey.settings.set("hdl_universal_motion", "212");
}
// Return if settings are not defined
if (
hdl_ip_address == undefined ||
hdl_ip_address == "" ||
hdl_subnet == undefined ||
hdl_subnet == "" ||
hdl_id == undefined ||
hdl_id == ""
)
return;
// Return if not proper ip, subnet or id
const ipRegex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/g;
const subnetRegex = /^\d{1,3}$/g;
if (!hdl_ip_address.match(ipRegex)) return;
if (!hdl_subnet.match(subnetRegex)) return;
let hdl_subnet_int = parseInt(hdl_subnet);
if (hdl_subnet_int < 1) return;
if (hdl_subnet_int > 254) return;
if (hdl_id != undefined && hdl_id != "") {
if (!hdl_id.match(subnetRegex)) return;
let hdl_id_int = parseInt(hdl_id);
if (hdl_id_int < 1) return;
if (hdl_id_int > 254) return;
}
// Close if the bus is already is running
this._busConnected = false;
if (this._bus != null) {
this._bus.close();
this._bus = null;
}
// Connect the bus
this._bus = new SmartBus({
gateway: hdl_ip_address,
port: 6000,
});
this._controller = this._bus.controller(`${hdl_subnet}.${hdl_id}`);
// Send out a discovery ping
this._controller.send(
{
target: "255.255",
command: 0x000e,
},
function (err) {
if (err) {
this.homey.app.log(err);
}
}
);
// Listen to bus
this._bus.on("command", (signal) => {
try {
this.homey.app._signalReceived(signal);
} catch (err) {
console.log(`Could not parse received data: ${err}`);
}
});
// Set the bus as connected
this._busConnected = true;
this.log("Homey HDL SmartBus is running...");
this.log("Initializing recurring update...");
setInterval(async () => {
this.homey.app.callForUpdate(this._bus);
}, 60000);
}
async callForUpdate(bus) {
this.log("Update called - looping through drivers");
for (let i = 0; i < this._driverlist.length; i++) {
this.homey.drivers
.getDriver(this._driverlist[i])
.getDevices()
.forEach(function (device) {
device.requestUpdate();
});
}
}
isBusConnected() {
if (this._bus === null) {
return false;
}
return this._busConnected;
}
bus() {
return this._bus;
}
controller() {
return this._controller;
}
getDevicesOfType(drivername) {
return this._hdlFoundUnits[drivername];
}
// Get the signature of a device with channels
devSignChnld(id, channel){
let hdl_subnet = this.homey.settings.get("hdl_subnet");
return {
id: `${hdl_subnet}.${id}.${channel}`,
address: `${hdl_subnet}.${id}`,
channel: channel
};
}
valueOK(type, value) {
if (typeof value === "undefined") return false;
switch (type) {
case "temperature":
if (! typeof value === "number") return false;
if (value < -40 || value > 80) return false;
return true;
case "humidity":
if (! typeof value === "number") return false;
if (value < 0 || value > 100) return false;
return true;
case "lux":
if (! typeof value === "number") return false;
if (value < 0 || value > 100000) return false;
return true;
}
return true;
}
async _updateDevice(hdlSenderType, signal) {
if (signal.code == 0xF) return; // Ignore discovery signals
const unknownDeviceMessages = ["invalid_device", "device is not defined", "Could not get device by device data"]
await this.homey.drivers.getDriver(hdlSenderType).updateValues(signal).catch((error) => {
if (! (unknownDeviceMessages.includes(error.message))) {
this.log(`Error for ${hdlSenderType} ${signal.sender.id}: ${error.message}`);
}
});
}
async _signalReceived(signal) {
// Check to see that the subnet is the same
let allowed_subnets = [parseInt(this.homey.settings.get("hdl_subnet")), 255];
if (!allowed_subnets.includes(parseInt(signal.sender.subnet))) return;
// Catch errors when trying to access the signal.data
try {
var dataFromSignal = signal.data;
} catch(err) {
var dataFromSignal = undefined;
}
// Check for universal switch
if (dataFromSignal != undefined && dataFromSignal.switch != undefined) {
await this._updateDevice("universal-switch", signal);
}
// Check if the device type is known
var foundType = await this._hdlDevicelist.typeOfDevice(signal.sender.type.toString());
// Return if no type was found
if (foundType == null) return;
// Allow failing signal data only if the device is a curtain
// (curtains are extending the signal data)
if (!foundType == "curtain" && dataFromSignal == undefined) return;
if (signal.sender.id == undefined) return;
// Add the device to the list of found devices
this._hdlFoundUnits[foundType][signal.sender.id] = signal.sender;
// Update the driver with the signal
await this._updateDevice(foundType, signal);
}
}
module.exports = HDLSmartBus;