96 lines
3.1 KiB
TypeScript
96 lines
3.1 KiB
TypeScript
import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge'
|
|
|
|
import { accessory } from './accessory'
|
|
import { device } from './device'
|
|
import { httpRequest, response } from './httpPromise'
|
|
import { domoticaPlatform } from './platform'
|
|
|
|
export class activityAccessory extends accessory {
|
|
private service: Service
|
|
|
|
static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
|
|
return new Promise((resolve, reject) => {
|
|
let activityDevices: device[] = []
|
|
|
|
httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/activity')
|
|
.then((response) => {
|
|
let devices = JSON.parse(response.body)
|
|
|
|
for (const device of devices) {
|
|
if (device.enabled == "true") {
|
|
let object: device = {
|
|
type: "Activity",
|
|
typeId: device.id,
|
|
uniqueId: "Activity." + device.id,
|
|
deviceName: device.name,
|
|
switchable: device.switchable,
|
|
dimmable: "true",
|
|
detailedType: device.type
|
|
}
|
|
activityDevices.push(object)
|
|
}
|
|
}
|
|
|
|
resolve(activityDevices)
|
|
})
|
|
.catch((error) => {
|
|
reject('activityAccessory::discoverDevices Error ->' + error)
|
|
})
|
|
})
|
|
}
|
|
|
|
constructor(
|
|
private readonly platform: domoticaPlatform,
|
|
private readonly accessory: PlatformAccessory,
|
|
) {
|
|
super("Activity", accessory.context.device.typeId)
|
|
this.accessory.getService(this.platform.Service.AccessoryInformation)!
|
|
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Activity')
|
|
.setCharacteristic(this.platform.Characteristic.Model, 'Domotica-Activity')
|
|
.setCharacteristic(this.platform.Characteristic.SerialNumber, '08000' + accessory.context.device.typeId)
|
|
|
|
//*
|
|
this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch)
|
|
|
|
this.service.getCharacteristic(this.platform.Characteristic.On)
|
|
.onGet(this.getOn.bind(this))
|
|
.onSet(this.setOn.bind(this));
|
|
/*/
|
|
this.service = this.accessory.getService(this.platform.Service.StatefulProgrammableSwitch) || this.accessory.addService(this.platform.Service.StatefulProgrammableSwitch)
|
|
|
|
this.service.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent)
|
|
.onGet(this.getOn.bind(this));
|
|
|
|
this.service.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState)
|
|
.onGet(this.getOn.bind(this))
|
|
.onSet(this.setOn.bind(this));
|
|
//*/
|
|
this.setName(accessory.context.device.deviceName)
|
|
}
|
|
|
|
setName(name) {
|
|
this.service.setCharacteristic(this.platform.Characteristic.Name, name)
|
|
}
|
|
|
|
setValue(key, value) {
|
|
this.service.updateCharacteristic(this.platform.Characteristic.On, false)
|
|
}
|
|
|
|
update() {
|
|
}
|
|
|
|
getOn(): CharacteristicValue {
|
|
return false;
|
|
}
|
|
|
|
async setOn(value: CharacteristicValue) {
|
|
// http://$domoticaIP/external/WebUpdate.php?data=Activity/$id/
|
|
httpRequest("http://" + this.platform.config.hostname + '/external/WebUpdate.php?data=Activity/' + this.accessory.context.device.typeId + '/')
|
|
.catch((error) => {
|
|
this.platform.log.debug('activityAccessory::setOn Error ->' + error)
|
|
})
|
|
|
|
|
|
}
|
|
}
|