-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·247 lines (211 loc) · 7.27 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
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
var Service, Characteristic;
var request = require("request");
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-davis", "davis", davis);
}
function davis(log, config) {
this.log = log;
// Config
this.url = config["url"];
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "NotSetByUser";
this.model = config["model"] || "NotSetByUser";
this.pollingIntervalSeconds = parseInt(config["pollingIntervalSeconds"] || 300);
this.temperatureUnitOfMeasure = (config["temperatureUnitOfMeasure"] || "C").toUpperCase();
this._timeoutID = -1;
this._cachedData = { "temperature": 0, "humidity": 0, "PM2p5:": 0, "PM10": 0};
this.serial = config["serial"] || "NotSetByUser";
this.getData(this.url);
this.txid = config["txid"] || 1;
this.useInternal = config ["useInternal"] || false;
this.sensorType = config ["sensorType"] || 1 ;
//defaults
this.temperature = 0;
this.humidity = 0;
//Specific to Airlink
this.PM2p5 = 0;
this.PM10 = 0;
}
function computeAqiFromPm(averagePM25, averagePM10) {
const limits25 = [15, 30, 55, 110]
const limits10 = [25, 50, 90, 180]
if (averagePM25 === 0 && averagePM10 === 0) {
return Characteristic.AirQuality.UNKNOWN;
}
if (averagePM25 <= limits25[0] && averagePM10 <= limits10[0]) {
return Characteristic.AirQuality.EXCELLENT;
}
if (averagePM25 <= limits25[1] && averagePM10 <= limits10[1]) {
return Characteristic.AirQuality.GOOD;
}
if (averagePM25 <= limits25[2] && averagePM10 <= limits10[2]) {
return Characteristic.AirQuality.FAIR;
}
if (averagePM25 <= limits25[3] && averagePM10 <= limits10[3]) {
return Characteristic.AirQuality.INFERIOR;
}
return Characteristic.AirQuality.POOR;
}
davis.prototype = {
httpRequest: function (url, body, method, callback) {
request({
url: url,
body: body,
method: method
},
function (error, response, body) {
callback(error, response, body)
})
},
getStateHumidity: function (callback) {
callback(null, this._cachedData.humidity);
},
getStateTemperature: function (callback) {
callback(null, this._cachedData.temperature);
},
getAirQuality: function (callback) {
callback(null, this._cachedData.airQuality);
},
getPM2p5: function (callback) {
callback(null, this._cachedData.PM2p5);
},
getPM10: function (callback) {
callback(null, this._cachedData.PM10);
},
getServices: function () {
var services = [],
informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
services.push(informationService);
this.temperatureService = new Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on("get", this.getStateTemperature.bind(this));
services.push(this.temperatureService);
this.humidityService = new Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({minValue: 0, maxValue: 100})
.on("get", this.getStateHumidity.bind(this));
services.push(this.humidityService);
if (this.sensorType == 3) {
this.airQualityService = new Service.AirQualitySensor(this.name);
this.airQualityService
.getCharacteristic(Characteristic.AirQuality)
.on("get", this.getAirQuality.bind(this));
this.airQualityService
.getCharacteristic(Characteristic.PM2_5Density)
.on("get", this.getPM2p5.bind(this));
this.airQualityService
.getCharacteristic(Characteristic.PM10Density)
.on("get", this.getPM10.bind(this));
services.push(this.airQualityService);
}
return services;
},
getData: function (url) {
this.httpRequest(url, "", "GET", function (error, response, responseBody) {
var queue = function () {
if (this._timeoutID > -1) {
clearTimeout(this._timeoutID);
this._timeoutID = -1;
}
this._timeoutID = setTimeout(function () {
this._timeoutID = -1;
this.getData(this.url);
}.bind(this), this.pollingIntervalSeconds * 1000);
}.bind(this);
if (error) {
this.log.error("Request to Davis API failed: %s (infrequent errors is normal)", error.message);
queue();
return;
}
this.log.debug("Request to Davis API succeeded!");
var jsonResponse = JSON.parse(responseBody);
if (jsonResponse.data && (!jsonResponse.data.conditions || jsonResponse.data.conditions.length == 0)) {
this.log.error("Response from Davis API doesn't contain expected result.");
queue();
return;
}
let weather = jsonResponse.data.conditions;
let length = weather.length;
if (this.useInternal) {
this.sensorType = 2;
}
for (let i = 0; i < length; i++) {
/* known to me data structure types:
* Vantage data: https://weatherlink.github.io/weatherlink-live-local-api/
* Airlink data: https://weatherlink.github.io/airlink-local-api/
*
* 1: Vantage External (at minimum)
* 2: Leaf/Soil Moisture records
* 3: Barometrics
* 4: Internal (Weatherlink Transmitter)
* 5: Airlink Old Format External
* 6: Airlink New Format External
*/
switch (weather[i].data_structure_type) {
case 1:
if (this.sensorType == 1) {
if (weather[i].txid == this.txid) {
this.temperature = weather[i].temp;
this.humidity = weather[i].hum;
}
}
break;
case 4:
if (this.sensorType == 2) {
this.temperature = weather[i].temp_in;
this.humidity = weather[i].hum_in;
}
break;
case 5:
if (this.sensorType == 3) {
this.temperature = weather[i].temp;
this.humidity = weather[i].hum;
this.PM10 = weather[i].pm_10p0;
this.PM2p5 = weather[i].pm_2p5;
}
break;
case 6:
if (this.sensorType == 3) {
this.temperature = weather[i].temp;
this.humidity = weather[i].hum;
this.PM10 = weather[i].pm_10;
this.PM2p5 = weather[i].pm_2p5;
this.log.debug("%s %s %s %s", this.temperature, this.humidity, this.PM10, this.PM2p5);
}
break;
default:
break;
}
}
this.log.debug("%s %s %s %s", this.temperature, this.humidity, this.PM10, this.PM2p5);
if (this.sensorType == 3) {
this._cachedData = {
"temperature": this.temperatureUnitOfMeasure == "F" ? this.convertFromFahrenheitToCelsius(this.temperature) : this.temperature,
"humidity": Math.round(this.humidity),
"PM2p5": this.PM2p5,
"PM10": this.PM10,
"airQuality": computeAqiFromPm(this.PM2p5, this.PM10),
}
} else {
this._cachedData = {
"temperature": this.temperatureUnitOfMeasure == "F" ? this.convertFromFahrenheitToCelsius(this.temperature) : this.temperature,
"humidity": Math.round(this.humidity),
}
};
this.log.debug("%s %s %s %s", this.temperature, this.humidity, this.PM2p5, this.PM10);
this.log.debug("%s %s %s %s", this._cachedData.temperature, this._cachedData.humidity, this.PM2p5, this.PM10);
queue();
}.bind(this));
},
convertFromFahrenheitToCelsius: function (f) { //MUST BE A NUMBER!
return parseFloat(((f - 32) * (5 / 9)).toFixed(1));
}
};