-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
110 lines (90 loc) · 2.78 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
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('airthings-waveplus', 'Aithings Waveplus', AirthingsWaveplus, true);
};
class AirthingsWaveplus {
constructor (log, config) {
this.log = log;
this.name = config.name;
this.serialNumber = config.serialNumber;
this.config = config;
this.updatedAt = 0;
WavePlus.on('found', wavePlus => {
devices[wavePlus.id] = wavePlus;
waitingDevices[wavePlus.id] && waitingDevices[wavePlus.id](wavePlus);
delete waitingDevices[wavePlus.id];
debug('found', wavePlus.id);
});
if (!config.disableTemp) {
this.tempService = new Service.TemperatureSensor(this.name);
this.tempService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({ minValue: -200, maxValue: 200, minStep: 0.01 });
}
if (!config.disableHumidity) {
this.humidityService = new Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({ minValue: 0, maxValue: 100, minStep: 0.5 });
}
var wavePlus;
var listenTo;
listenTo = (wavePlus) => {
wavePlus.on('updated', (data) => {
this.update(wavePlus, data);
});
}
if (wavePlus) {
listenTo(wavePlus);
} else {
waitingDevices[this.id] = (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);
}
}
}
debug(data);
}
getServices () {
const services = [];
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));
}