Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alesf committed Jan 16, 2021
1 parent 8c9bf87 commit 66d21fd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 63 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,41 @@ HTTP Air quality accessory for Homebridge

## Sample config

Minimal:

{
"accessory": "HttpAirQuality",
"url": "http://localhost/airquality",
}

Full:

{
"accessory": "HttpAirQuality",
"name": "Air Quality",
"airQualityIndexName": "Air Quality",
"url": "http://localhost/airquality",
"httpMethod": "GET",
"pollingInterval": 300
}

## Sample data

{
air_quality: 1,
pm10: 32,
pm25: 26,
so2: 3,
o3: 25,
no2: 21
}

If you do not provide air_quality it will be calculated from other values.
All values are optional. Only provided values will be used.

## Air Quality value meaning

0: Characteristic.AirQuality.EXCELLENT,
1: Characteristic.AirQuality.GOOD,
2: Characteristic.AirQuality.FAIR,
3: Characteristic.AirQuality.INFERIOR,
4: Characteristic.AirQuality.POOR
117 changes: 55 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,20 @@ let Service, Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;

homebridge.registerAccessory("homebridge-http-air-quality", "HttpAirQuality", HttpAirQuality);
};

/**
* Air Accessory
*/
function HttpAirQuality(log, config) {
this.log = log;

this.pollingInterval = config.pollingInterval || 300;

this.name = config.name || 'Air Quality';
this.url = config.url;
this.httpMethod = config.httpMethod || 'GET';

this.name = config.name || 'Air Quality';
this.airQualityIndexName = config.airQualityIndexName || 'Air Quality';

this.lastUpdate = 0;
this.sensors = {};

this.data = undefined;

// excelent, good, fair, inferior, poor
Expand All @@ -48,6 +42,24 @@ function HttpAirQuality(log, config) {

HttpAirQuality.prototype = {

// fetch new data
fetchData: function (params) {
let self = this;

request(this.httpMethod, this.url)
.set("Accept", "application/json")
.end(function (err, response, data) {
if (!err && response.statusCode === 200) {
self.data = data;
self.lastUpdate = new Date().getTime() / 1000;
self.updateData(params);
} else {
self.log("fetchData error");
}
self.fetchInProgress = false;
});
},

// wrapper for updateData method (new data/cache)
setData: function (params) {
if (this.lastUpdate === 0 || this.lastUpdate + this.pollingInterval < (new Date().getTime() / 1000) || this.data === undefined) {
Expand All @@ -60,60 +72,42 @@ HttpAirQuality.prototype = {

// update sensors data
updateData: function (params) {
let self = this;

if (params['key'] in self.data) {
let widget = self.sensors[params['key']];

widget.setCharacteristic(Characteristic.StatusFault, 0);
let aqi_key = undefined;
params['characteristics'].forEach(function (characteristic) {
if (self.data[characteristic.key]) {
let value = characteristic.formatter(self.data[characteristic.key]);
self.log.debug(characteristic.key + ' = ' + value);
if (!isNaN(value)) {
widget.setCharacteristic(characteristic.characteristic, value);
self.limits[characteristic.key].forEach(function (limit, key) {
if (value > limit) {
aqi_key = key;
}
});
}
this.AQISensorService.setCharacteristic(Characteristic.StatusFault, 0);

let self = this;
let aqi_key = undefined;
params['characteristics'].forEach(function (c) {
if (self.data[c.key]) {
let value = c.formatter(self.data[c.key]);
self.log(c.key + ' = ' + value);
if (!isNaN(value)) {
self.AQISensorService.setCharacteristic(c.characteristic, value);
self.limits[c.key].forEach(function (limit, key) {
if (value > limit && aqi_key < key) {
aqi_key = key;
}
});
}
});
let AQI = aqi_key !== undefined ? self.levels[aqi_key] : Characteristic.AirQuality.UNKNOWN;
self.log.debug(params['key'] + ' = ' + AQI);
params.callback(null, AQI);
} else {
this.sensors[params['key']].setCharacteristic(Characteristic.StatusFault, 1);
self.log.debug(params['key'] + ' = no value');
params.callback(null);
}
});

if ('air_quality' in self.data) {
aqi_key = self.data['air_quality'];
}
},

// fetch new data from Airly
fetchData: function (params) {
let self = this;
let AQI = self.levels[aqi_key] || Characteristic.AirQuality.UNKNOWN;

this.AQISensorService.setCharacteristic(Characteristic.AirQuality, AQI);
this.log('AQI = ' + AQI);
params.callback(null, AQI);

request(this.httpMethod, this.url)
.set("Accept", "application/json")
.end(function (err, response, data) {
if (!err && response.statusCode === 200) {
self.data = data;
self.lastUpdate = new Date().getTime() / 1000;
self.updateData(params);
} else {
self.log("fetchData error");
}
self.fetchInProgress = false;
});
},

updateAirQualityIndex: function (callback) {
updateAQI: function (callback) {
this.setData({
'callback': callback,
'key': 'air_quality',
'characteristics': [
callback: callback,
characteristics: [
{
'key': 'pm25',
'characteristic': Characteristic.PM2_5Density,
Expand Down Expand Up @@ -151,18 +145,17 @@ HttpAirQuality.prototype = {
identify: callback => callback(),

getServices: function () {
let informationService = new Service.AccessoryInformation();
informationService
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "Generic")
.setCharacteristic(Characteristic.Model, "HTTP(S)")
.setCharacteristic(Characteristic.SerialNumber, "0000-0000-0000");
this.sensors['information'] = informationService;


let airQualityIndexSensorService = new Service.AirQualitySensor(this.airQualityIndexName);
airQualityIndexSensorService.getCharacteristic(Characteristic.AirQuality).on('get', this.updateAirQualityIndex.bind(this));
this.sensors['air_quality'] = airQualityIndexSensorService;
this.AQISensorService = new Service.AirQualitySensor(this.name);
this.AQISensorService
.getCharacteristic(Characteristic.AirQuality)
.on('get', this.updateAQI.bind(this));

return Object.values(this.sensors);
return [this.informationService, this.AQISensorService];
}
};

0 comments on commit 66d21fd

Please sign in to comment.