-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·306 lines (252 loc) · 8.83 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
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
"use strict";
var Service, Characteristic;
var airQualitySensorService;
var temperatureSensorService;
var humiditySensorService;
var request = require('request');
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-smogomierz", "Smogomierz", SmogomierzSensor);
};
const DataType = {
PM2_5: 'pm2_5Type',
PM10: 'pm10Type',
AIRQUALITY: 'airQualityType',
TEMPERATURE: 'temperatureType',
HUMIDITY: 'humidityType'
};
class SmogomierzRepository {
constructor(log, config) {
this.log = log
// URL to Smogomierz sensor
this.url = config['url'];
if (!this.url) throw new Error("Smogomierz - you must provide a URL adress.");
this.log.info("URL set: " + this.url)
this.lastupdate = 0;
this.cache = undefined;
this.isFetching = false
this.callbackQueue = []
this.cacheExpiryTime = Number(config['cacheExpiryTime'])
if (isNaN(this.cacheExpiryTime)) {
this.cacheExpiryTime = 10
this.log.error("Wrong config 'cacheExpiryTime' parameter. Set to default.")
} else if (this.cacheExpiryTime < 1) {
this.cacheExpiryTime = 10
this.log.error("'cacheExpiryTime' lower then 1. Set to default.")
}
this.log.info("cacheExpiryTime set: " + this.cacheExpiryTime.toString())
}
/**
* Get all Smogomierz data from ESP8266
*/
getData(callback) {
var self = this;
var url = this.url + 'api';
if (self.isFetching) {
self.callbackQueue.push(callback)
return
}
if (this._shouldUpdate()) {
self.isFetching = true
request({
url: url,
json: true,
}, function (err, response, data) {
self.isFetching = false
let callbackQueue = self.callbackQueue
self.callbackQueue = []
// If no errors
if (!err && response.statusCode === 200) {
self.cache = data
self.lastupdate = new Date().getTime() / 1000;
callback(null, data, 'Fetch');
for (let c of callbackQueue) {
c(null, data, 'Cache');
}
// If error
} else {
self.log.error("Smogomierz doesn't work or Unknown Error.");
callback(err, null, null);
for (let c of callbackQueue) {
c(err, null, null);
}
}
});
// Return cached data
} else {
callback(null, self.cache, 'Cache');
}
}
_shouldUpdate() {
let intervalBetweenUpdates = this.cacheExpiryTime * 60
return this.lastupdate === 0 ||
this.lastupdate + intervalBetweenUpdates < (new Date().getTime() / 1000) ||
this.cache === undefined
}
}
/**
* Smogomierz Accessory
*/
function SmogomierzSensor(log, config) {
this.log = log;
this.servicesNames = config['servicesNames'] || {}
this.smogomierzRepo = new SmogomierzRepository(log, config)
this.log.info("Smogomierz setuped");
}
SmogomierzSensor.prototype = {
getAirQuality: function(next) {
var self = this
self._getData(
airQualitySensorService,
DataType.AIRQUALITY,
next
)
},
getPm2_5: function(next) {
var self = this
self._getData(
airQualitySensorService,
DataType.PM2_5,
next
)
},
getPm10: function(next) {
var self = this
self._getData(
airQualitySensorService,
DataType.PM10,
next
)
},
getTemperature: function(next) {
var self = this
self._getData(
temperatureSensorService,
DataType.TEMPERATURE,
next
)
},
getHumidity: function(next) {
var self = this
self._getData(
humiditySensorService,
DataType.HUMIDITY,
next
)
},
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function () {
var services = [];
/**
* Informations
*/
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Hackerspace Silesia")
.setCharacteristic(Characteristic.Model, "Software")
.setCharacteristic(Characteristic.SerialNumber, "123-456");
services.push(informationService);
/**
* airQualitySensorService
*/
let airQualityName = this.servicesNames['airQuality'] || "Air Quality"
airQualitySensorService = new Service.AirQualitySensor(airQualityName);
airQualitySensorService
.getCharacteristic(Characteristic.AirQuality)
.on('get', this.getAirQuality.bind(this));
airQualitySensorService
.getCharacteristic(Characteristic.PM2_5Density)
.on('get', this.getPm2_5.bind(this));
airQualitySensorService
.getCharacteristic(Characteristic.PM10Density)
.on('get', this.getPm10.bind(this));
services.push(airQualitySensorService);
/**
* temperatureSensorService
*/
let temperatureName = this.servicesNames['temperature'] || "Temperature"
temperatureSensorService = new Service.TemperatureSensor(temperatureName)
temperatureSensorService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
minValue: -100,
maxValue: 100
})
.on('get', this.getTemperature.bind(this));
services.push(temperatureSensorService);
/**
* humiditySensorService
*/
let humidityName = this.servicesNames['humidity'] || "Humidity"
humiditySensorService = new Service.HumiditySensor(humidityName)
humiditySensorService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getHumidity.bind(this));
services.push(humiditySensorService);
return services;
},
// PRIAVTE
_getData: function(service, type, next) {
var self = this
self.smogomierzRepo.getData(function (error, data, source) {
if (error) {
service.setCharacteristic(Characteristic.StatusFault, 1);
self.log(error.message);
return next(error, null);
}
service.setCharacteristic(Characteristic.StatusFault, 0);
let typeName = null
let value = null
switch (type) {
case DataType.AIRQUALITY:
typeName = "AirQuality"
value = self._transformPM25ToAirQuality(data.pm25)
break;
case DataType.PM2_5:
typeName = "PM2.5"
value = data.pm25
break;
case DataType.PM10:
typeName = "PM10"
value = data.pm10
break;
case DataType.TEMPERATURE:
typeName = "Temperature"
value = data.temperature
break;
case DataType.HUMIDITY:
typeName = "Humidity"
value = data.humidity
break;
default:
let error = new Error("Unknown data type: " + type)
self.log(error.message);
return next(error, null);
}
self.log.info("Update %s: %s from [%s].", typeName, value.toString(), source);
return next(null, value);
})
},
// Based on Index level for PM2.5 http://www.eea.europa.eu/themes/air/air-quality-index
_transformPM25ToAirQuality: function (PM25) {
if (isNaN(PM25) || PM25 === null || PM25 === "" || PM25 === undefined ) {
return (0); // Error or unknown response
} else if (PM25 <= 10) {
return (1); // Return EXCELLENT
} else if (PM25 > 10 && PM25 <= 20) {
return (2); // Return GOOD
} else if (PM25 > 20 && PM25 <= 25) {
return (3); // Return FAIR
} else if (PM25 > 25 && PM25 <= 50) {
return (4); // Return INFERIOR
} else if (PM25 > 50) {
return (5); // Return POOR (Homekit only goes to cat 5).
} else {
return (0); // Error or unknown response.
}
}
};