-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #9 fix wlightboxs service type * tempSensor support * airSensor support
- Loading branch information
Showing
6 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters