forked from anaxyd/homey-airthings
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
315 lines (247 loc) · 8.93 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
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
'use strict';
const Homey = require('homey');
const bufferpack = require('/lib/bufferpack.js');
/*
Notes etc
Node.js modules readme:
https://apps.developer.athom.com/tutorial-App%20Store.html
modules in use so far:
- https://github.com/ryanrolds/bufferpack
*/
class AirthingsApp extends Homey.App {
async onInit() {
this.log('AirthingsApp is running...!');
}
getWaveValues(macAddress, pollTimeout) {
return new Promise(async(resolve, reject) => {
this.log(macAddress)
this.log(pollTimeout)
let timeout = pollTimeout * 1000;
const ble = Homey.ManagerBLE;
try {
this.log('Searching for: !',macAddress);
const advertisement = await ble.find(macAddress, timeout);
this.log('Found: !',macAddress);
await this.sleep(1000);
const peripheral = await advertisement.connect();
const rssi = peripheral.rssi;
await this.sleep(1000);
this.log('Connected !',macAddress);
const services = await peripheral.discoverAllServicesAndCharacteristics();
this.log('Services Discovered !',macAddress);
const dataService = await services.find(service => service.uuid === "b42e1f6eade711e489d3123b93f75cba");
const characteristics = await dataService.discoverCharacteristics();
//Radon Short Term Average
const staDataChar = await characteristics.find(characteristic => characteristic.uuid === "b42e01aaade711e489d3123b93f75cba");
const staData = await staDataChar.read();
//Radon Long Term Average
const ltaDataChar = await characteristics.find(characteristic => characteristic.uuid === "b42e0a4cade711e489d3123b93f75cba");
const ltaData = await ltaDataChar.read();
//Temperature
const tempDataChar = await characteristics.find(characteristic => characteristic.uuid === "2a6e");
const tempData = await tempDataChar.read();
//Humidity
const humDataChar = await characteristics.find(characteristic => characteristic.uuid === "2a6f");
const humData = await humDataChar.read();
//ALS + Light
const alsDataChar = await characteristics.find(characteristic => characteristic.uuid == "b42e1096ade711e489d3123b93f75cba")
const alsData = await alsDataChar.read();
await peripheral.disconnect();
const format = "<h";
const sensorSTA = bufferpack.unpack(format, staData)[0];
const sensorLTA = bufferpack.unpack(format, ltaData)[0];
const sensorTemperature = bufferpack.unpack(format, tempData)[0];
const sensorHumidity = bufferpack.unpack(format, humData)[0];
const formatTwo = "<B";
const sensorLight = bufferpack.unpack(formatTwo, alsData)[0] & 0xf0;
// This is the matching format for the binary data for unpacking.
//const format = "<xbxbHHHHHHxxxx";
//const unpacked = bufferpack.unpack(format, sensorData);
// Sensordata unpacked looks like this:
// (humidity, light, sh_rad, lo_rad, temp, pressure, co2, voc)
// Example:
// [ 81, 0, 10, 0, 2387, 48689, 366, 116 ]
// Some of the values requires minor math to get correct values
let sensorValues = {
humidity: sensorHumidity / 100,
light: sensorLight,
shortTermRadon: sensorSTA,
longTermRadon: sensorLTA,
temperature: sensorTemperature / 100,
rssi: rssi
}
resolve(sensorValues)
} catch (error) {
reject(error)
}
})
}
getWavePlusValues(macAddress, pollTimeout) {
return new Promise(async(resolve, reject) => {
this.log(macAddress)
this.log(pollTimeout)
let timeout = pollTimeout*1000;
const ble = Homey.ManagerBLE;
try {
const advertisement = await ble.find(macAddress, timeout);
const peripheral = await advertisement.connect();
const rssi = peripheral.rssi;
this.log('Connected !',macAddress);
const services = await peripheral.discoverAllServicesAndCharacteristics();
this.log('Services Discovered !',macAddress);
const dataService = await services.find(service => service.uuid === "b42e1c08ade711e489d3123b93f75cba");
const characteristics = await dataService.discoverCharacteristics();
const data = await characteristics.find(characteristic => characteristic.uuid === "b42e2a68ade711e489d3123b93f75cba");
const sensorData = await data.read();
await peripheral.disconnect();
// This is the matching format for the binary data for unpacking.
const format = "<xBBxHHHHHHxxxx";
const unpacked = bufferpack.unpack(format, sensorData);
// Sensordata unpacked looks like this:
// (humidity, light, sh_rad, lo_rad, temp, pressure, co2, voc)
// Example:
// [ 81, 0, 10, 0, 2387, 48689, 366, 116 ]
// Some of the values requires minor math to get correct values
let sensorValues = {
humidity: unpacked[0] / 2,
light: unpacked[1],
shortTermRadon: unpacked[2],
longTermRadon: unpacked[3],
temperature: unpacked[4] / 100,
pressure: unpacked[5] / 50,
co2: unpacked[6],
voc: unpacked[7],
rssi: rssi
}
resolve(sensorValues)
} catch (error) {
reject(error)
}
})
}
getWaveMiniValues(macAddress, pollTimeout) {
return new Promise(async(resolve, reject) => {
this.log(macAddress)
this.log(pollTimeout)
let timeout = pollTimeout*1000;
const ble = Homey.ManagerBLE;
try {
const advertisement = await ble.find(macAddress, timeout);
const peripheral = await advertisement.connect();
const rssi = peripheral.rssi;
this.log('Connected To Mini !',macAddress);
const services = await peripheral.discoverAllServicesAndCharacteristics();
this.log('Services Discovered Mini !',macAddress);
const dataService = await services.find(service => service.uuid === "b42e3882ade711e489d3123b93f75cba");
const characteristics = await dataService.discoverCharacteristics();
const data = await characteristics.find(characteristic => characteristic.uuid === "b42e3b98ade711e489d3123b93f75cba");
const sensorData = await data.read();
await peripheral.disconnect();
this.log('Disconnected from Mini, parsing data');
// This is the matching format for the binary data for unpacking.
const format = "<HHHHHHxxxx";
const unpacked = bufferpack.unpack(format, sensorData);
// Sensordata unpacked looks like this:
// (humidity, light, sh_rad, lo_rad, temp, pressure, co2, voc)
// Example:
// [ 81, 0, 10, 0, 2387, 48689, 366, 116 ]
// Some of the values requires minor math to get correct values
let sensorValues = {
humidity: unpacked[3] / 100,
light: unpacked[0],
temperature: (unpacked[1] / 100) - 273.15, //In Kelvin on Mini
pressure: unpacked[2] / 50,
voc: unpacked[4],
rssi: rssi
}
resolve(sensorValues)
} catch (error) {
reject(error)
}
})
}
sleep(ms){
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}
discoverWaveDevices(driver) {
return new Promise(async(resolve, reject) => {
this.log("Searching for Airthings Wave devices...")
const timeout = 29000;
const ble = Homey.ManagerBLE;
try {
let devices = [];
// Wave : b42e1f6eade711e489d3123b93f75cba
const foundDevices = await ble.discover(['b42e1f6eade711e489d3123b93f75cba'], timeout);
foundDevices.forEach(device => {
const format = "<xxIxx";
devices.push({
name: "Airthings Wave (" + bufferpack.unpack(format, device.manufacturerData)[0] + ")",
data: {
id: device.id,
uuid: device.uuid,
address: device.address
}
})
});
resolve(devices)
} catch (error) {
reject(error)
}
});
}
discoverWavePlusDevices(driver) {
return new Promise(async(resolve, reject) => {
this.log("Searching for Airthings Wave Plus devices...")
const timeout = 29000;
const ble = Homey.ManagerBLE;
try {
let devices = [];
// Wave + : b42e1c08ade711e489d3123b93f75cba
const foundDevices = await ble.discover(['b42e1c08ade711e489d3123b93f75cba'], timeout);
foundDevices.forEach(device => {
const format = "<xxIxx";
devices.push({
name: "Airthings Wave + (" + bufferpack.unpack(format, device.manufacturerData)[0] + ")",
data: {
id: device.id,
uuid: device.uuid,
address: device.address
}
})
});
resolve(devices)
} catch (error) {
reject(error)
}
});
}
discoverWaveMiniDevices(driver) {
return new Promise(async(resolve, reject) => {
this.log("Searching for Airthings Wave Mini devices...")
const timeout = 29000;
const ble = Homey.ManagerBLE;
try {
let devices = [];
// Wave + : b42e3882ade711e489d3123b93f75cba
const foundDevices = await ble.discover(['b42e3882ade711e489d3123b93f75cba'], timeout);
foundDevices.forEach(device => {
const format = "<xxIxx";
devices.push({
name: "Airthings Wave Mini (" + bufferpack.unpack(format, device.manufacturerData)[0] + ")",
data: {
id: device.id,
uuid: device.uuid,
address: device.address
}
})
});
resolve(devices)
} catch (error) {
reject(error)
}
});
}
}
module.exports = AirthingsApp;