forked from iler/homebridge-airthings-waveplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
120 lines (99 loc) · 3.36 KB
/
index.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
const WavePlus = require('node-airthings-waveplus');
let Service;
let Characteristic;
const waitingDevices = {};
const devices = {};
module.exports = (homebridge) => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-wave-plus', 'Airthings WavePlus', AirthingsWaveplus, true);
};
class AirthingsWaveplus {
constructor (log, config) {
this.log = log;
this.config = config;
this.name = this.config.name;
this.serialNumber = this.config.serialNumber;
this.updatedAt = 0;
this.manufacturer="Airthings";
this.model="Wave Plus";
var wavePlus
WavePlus.on('found', wavePlus => {
devices[wavePlus.serialNumber] = wavePlus;
log('found', wavePlus.id, 'serial number', wavePlus.serialNumber);
log('address', wavePlus.address);
log('connectable', wavePlus.connectable);
waitingDevices[wavePlus.serialNumber] && waitingDevices[wavePlus.serialNumber](wavePlus);
delete waitingDevices[wavePlus.serialNumber];
});
if (!this.config.disableTemp) {
this.tempService = new Service.TemperatureSensor(this.name);
this.tempService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({ minValue: -200, maxValue: 200, minStep: 0.01 });
}
if (!this.config.disableHumidity) {
this.humidityService = new Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({ minValue: 0, maxValue: 100, minStep: 0.5 });
}
var listenTo;
listenTo = (wavePlus) => {
log('listening...');
wavePlus.on('updated', (data) => {
log('Updated data:', data);
this.update(wavePlus, data);
});
}
if (wavePlus) {
listenTo(wavePlus);
} else {
waitingDevices[this.serialNumber] = (wavePlus) => {
listenTo(wavePlus);
};
}
}
update (wavePlus, data) {
const { config } = this;
const { temperature, humidity } = data;
wavePlus.previousValues = data;
const now = Date.now();
if ((config.frequency == null) || ((now - this.updatedAt) > config.frequency * 1000)) {
this.updatedAt = now;
if (!config.disableTemp) {
if (temperature !== this.temperature) {
this.temperature = temperature;
this.tempService
.getCharacteristic(Characteristic.CurrentTemperature)
.updateValue(temperature);
}
}
if (!config.disableHumidity) {
if (humidity !== this.humidity) {
this.humidity = humidity;
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.updateValue(humidity);
}
}
}
}
getServices () {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serialNumber)
const services = [informationService]
if (this.tempService) {
services.push(this.tempService);
}
if (this.humidityService) {
services.push(this.humidityService);
}
return services;
}
}
function hypotenuse (a, b, c = 0) {
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2) + Math.pow(c, 2));
}