Skip to content

Commit

Permalink
0.1.0 rc (#10)
Browse files Browse the repository at this point in the history
* #9 fix wlightboxs service type

* tempSensor support

* airSensor support
  • Loading branch information
Actardnes authored Nov 16, 2020
1 parent 30597ed commit fab0c51
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 2 deletions.
123 changes: 123 additions & 0 deletions blebox/airSensor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const _ = require("lodash");
const bleboxCommands = require("../common/bleboxCommands");
const AIRSENSOR_TYPE = require("../common/bleboxConst").BLEBOX_TYPE.AIRSENSOR;
const AbstractBoxWrapper = require("./abstractBox");

const PM_TYPES = {
PM2_5 : "pm2.5",
PM10: "pm10"
}

class AirSensorAccessoryWrapper extends AbstractBoxWrapper {
constructor(accessory, log, api, deviceInfo, stateInfo) {
super(accessory, log, api);

this.type = AIRSENSOR_TYPE;
this.checkStateCommand = bleboxCommands.getAirSensorState;

this.servicesDefList = [api.hap.Service.AirQualitySensor];

this.airQualityCharacteristic = api.hap.Characteristic.AirQuality;
this.pm2_5DensityCharacteristic = api.hap.Characteristic.PM2_5Density;
this.pm10DensityCharacteristic = api.hap.Characteristic.PM10Density;

this.init(deviceInfo, stateInfo);

this.assignCharacteristics();

this.startListening();
}

assignCharacteristics() {
super.assignCharacteristics();
const serviceNumber = 0;
const service = this.getService(serviceNumber);

service.getCharacteristic(this.airQualityCharacteristic)
.on('get', this.onGetCurrentAirQuality.bind(this));

service.getCharacteristic(this.pm2_5DensityCharacteristic)
.on('get', this.onGetPmDensity.bind(this, PM_TYPES.PM2_5));

service.getCharacteristic(this.pm10DensityCharacteristic)
.on('get', this.onGetPmDensity.bind(this, PM_TYPES.PM10));
}

updateStateInfoCharacteristics() {
const airSensor = this.getAirSensor();
if (airSensor) {
//update characteristics
const serviceNumber = 0;
const service = this.getService(serviceNumber);
service.updateCharacteristic(this.airQualityCharacteristic, this.getCurrentAirQualityValue());
service.updateCharacteristic(this.pm2_5DensityCharacteristic, this.getPmDensityValue(PM_TYPES.PM2_5));
service.updateCharacteristic(this.pm10DensityCharacteristic, this.getPmDensityValue(PM_TYPES.PM10));
}
};

updateStateInfo(stateInfo) {
if (stateInfo) {
this.accessory.context.blebox.airSensor = stateInfo.air || stateInfo;
this.updateStateInfoCharacteristics();
}
}

getAirSensor() {
return this.accessory.context.blebox.airSensor;
}

getCurrentAirQualityValue() {
let {sensors, airQualityLevel} = this.getAirSensor() || {};
if(_.isUndefined(airQualityLevel)){
airQualityLevel = (_.maxBy(sensors, function (sensor) {
return sensor.qualityLevel;
}) || {}).qualityLevel || this.airQualityCharacteristic.UNKNOWN;
}
return Math.max(this.airQualityCharacteristic.UNKNOWN, Math.min(airQualityLevel, this.airQualityCharacteristic.POOR));
};

getPmDensityValue(pmType){
const {sensors} = this.getAirSensor() || {};
const sensorForPmType = _.find(sensors, function (sensor){
return sensor && sensor.type === pmType;
}) || {};

return sensorForPmType.value || 0;
}

onGetCurrentAirQuality(callback) {
this.log("Getting 'Current air quality' characteristic ...");
const airSensor = this.getAirSensor();
if (this.isResponding() && airSensor) {
const airQualityValue = this.getCurrentAirQualityValue();
this.log("Current 'Current air quality' characteristic is %s", airQualityValue);
callback(null, airQualityValue);
} else {
this.log("Error getting 'Current air quality' characteristic. AirSensor: %s", airSensor);
callback(new Error("Error getting 'Current air quality'."));
}
};

onGetPmDensity(pmType, callback){
this.log("Getting 'Current pm %s density' characteristic ...", pmType);
const airSensor = this.getAirSensor();
if (this.isResponding() && airSensor) {
const pmDensityValue = this.getPmDensityValue(pmType);
this.log("Current 'Current pm %s density' characteristic is %s", pmType, pmDensityValue);
callback(null, pmDensityValue);
} else {
this.log("Error getting 'Current pm %s density' characteristic. AirSensor: %s", pmType, airSensor);
callback(new Error(`Error getting 'Current pm ${pmType} density'."`));
}
}
}

module.exports = {
type: AIRSENSOR_TYPE,
checkStateCommand: bleboxCommands.getAirSensorState,
create: function (accessory, log, api, deviceInfo, stateInfo) {
return new AirSensorAccessoryWrapper(accessory, log, api, deviceInfo, stateInfo);
}, restore: function (accessory, log, api) {
return new AirSensorAccessoryWrapper(accessory, log, api);
}
};
4 changes: 4 additions & 0 deletions blebox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const switchBox = require("./switchBox");
const switchBoxD = require("./switchBoxD");
const wLightBox = require("./wLightBox");
const wLightBoxS = require("./wLightBoxS");
const tempSensor = require("./tempSensor");
const airSensor = require("./airSensor");

const wrappers = {};
wrappers[dimmerBox.type] = dimmerBox;
Expand All @@ -18,5 +20,7 @@ wrappers[switchBox.type] = switchBox;
wrappers[switchBoxD.type] = switchBoxD;
wrappers[wLightBox.type] = wLightBox;
wrappers[wLightBoxS.type] = wLightBoxS;
wrappers[tempSensor.type] = tempSensor;
wrappers[airSensor.type] = airSensor;

module.exports = wrappers;
84 changes: 84 additions & 0 deletions blebox/tempSensor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const bleboxCommands = require("../common/bleboxCommands");
const TEMPSENSOR_TYPE = require("../common/bleboxConst").BLEBOX_TYPE.TEMPSENSOR;
const AbstractBoxWrapper = require("./abstractBox");

class TempSensorAccessoryWrapper extends AbstractBoxWrapper {
constructor(accessory, log, api, deviceInfo, stateInfo) {
super(accessory, log, api);

this.type = TEMPSENSOR_TYPE;
this.checkStateCommand = bleboxCommands.getTempSensorState;

this.servicesDefList = [api.hap.Service.TemperatureSensor];

this.currentTemperatureCharacteristic = api.hap.Characteristic.CurrentTemperature;

this.init(deviceInfo, stateInfo);

this.assignCharacteristics();

this.startListening();
}

assignCharacteristics() {
super.assignCharacteristics();
const serviceNumber = 0;
const service = this.getService(serviceNumber);

service.getCharacteristic(this.currentTemperatureCharacteristic)
.setProps({
maxValue: 125,
minValue: -125,
})
.on('get', this.onGetCurrentTemperature.bind(this));
}

updateStateInfoCharacteristics() {
const tempSensor = this.getTempSensor();
if (tempSensor) {
//update characteristics
const serviceNumber = 0;
const service = this.getService(serviceNumber);
service.updateCharacteristic(this.currentTemperatureCharacteristic, this.getCurrentTemperatureValue());
}
};

updateStateInfo(stateInfo) {
if (stateInfo) {
this.accessory.context.blebox.tempSensor = stateInfo.tempSensor || stateInfo;
this.updateStateInfoCharacteristics();
}
}

getTempSensor() {
return this.accessory.context.blebox.tempSensor;
}

getCurrentTemperatureValue() {
const {sensors: [{value: currentTemperature = 0} = {}] = []} = this.getTempSensor() || {};
return Number((currentTemperature / 100).toFixed(1)) || 0;
};

onGetCurrentTemperature(callback) {
this.log("Getting 'Current temperature' characteristic ...");
const tempSensor = this.getTempSensor();
if (this.isResponding() && tempSensor) {
const currentTemperatureValue = this.getCurrentTemperatureValue();
this.log("Current 'Current temperature' characteristic is %s", currentTemperatureValue);
callback(null, currentTemperatureValue);
} else {
this.log("Error getting 'Current temperature' characteristic. TempSensor: %s", tempSensor);
callback(new Error("Error getting 'Current temperature'."));
}
};
}

module.exports = {
type: TEMPSENSOR_TYPE,
checkStateCommand: bleboxCommands.getTempSensorState,
create: function (accessory, log, api, deviceInfo, stateInfo) {
return new TempSensorAccessoryWrapper(accessory, log, api, deviceInfo, stateInfo);
}, restore: function (accessory, log, api) {
return new TempSensorAccessoryWrapper(accessory, log, api);
}
};
2 changes: 1 addition & 1 deletion blebox/wLightBoxS.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WLightBoxSAccessoryWrapper extends AbstractBoxWrapper {
this.type = WLIGHTBOXS_TYPE;
this.checkStateCommand = bleboxCommands.Lightbulb;

this.servicesDefList = [api.hap.Service.Thermostat];
this.servicesDefList = [api.hap.Service.Lightbulb];

this.nameCharacteristic = api.hap.Characteristic.Name;
this.onCharacteristic = api.hap.Characteristic.On;
Expand Down
10 changes: 10 additions & 0 deletions common/bleboxCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,14 @@ module.exports = {
"method": "GET",
"url": "/s/t/{0}"
},
"getTempSensorState": {
"name": "getTempSensorState",
"method": "GET",
"url": "/api/tempsensor/state"
},
"getAirSensorState": {
"name": "getAirSensorState",
"method": "GET",
"url": "/api/air/state"
}
};
4 changes: 3 additions & 1 deletion common/bleboxConst.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports= {
WLIGHTBOX : "wlightbox",
WLIGHTBOXS : "wlightboxs",
SMARTWINDOWBOX : "smartwindowbox",
SAUNABOX : "saunabox"
SAUNABOX : "saunabox",
TEMPSENSOR : "tempsensor",
AIRSENSOR : "airsensor"
}
};

0 comments on commit fab0c51

Please sign in to comment.