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 securitySystemAccessory extends accessory { private service: Service private state = { CurrentState: this.platform.Characteristic.SecuritySystemCurrentState.DISARMED, TargetState: this.platform.Characteristic.SecuritySystemCurrentState.DISARMED } static async discoverDevices(platform: domoticaPlatform): Promise { return new Promise((resolve, reject) => { let securitySystemDevices: device[] = [] httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/alarmsystem') .then((response) => { let devices = JSON.parse(response.body) for (const device of devices) { if (device.enabled == "true") { let object: device = { type: "SecuritySystem", typeId: device.id, uniqueId: "SecuritySystem." + device.id, deviceName: device.name, switchable: "false", dimmable: "false", detailedType: device.type } securitySystemDevices.push(object) } } resolve(securitySystemDevices) }) .catch((error) => { reject('securitySystemAccessory::discoverDevices Error ->' + error) }) }) } constructor( private readonly platform: domoticaPlatform, private readonly accessory: PlatformAccessory, ) { super("SecuritySystem", accessory.context.device.typeId) this.accessory.getService(this.platform.Service.AccessoryInformation)! .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Dierkse') .setCharacteristic(this.platform.Characteristic.Model, 'AlarmSystem') .setCharacteristic(this.platform.Characteristic.SerialNumber, '11000' + accessory.context.device.typeId) this.service = this.accessory.getService(this.platform.Service.SecuritySystem) || this.accessory.addService(this.platform.Service.SecuritySystem) this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState) .onGet(this.getSecuritySystemCurrentState.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemTargetState) .onGet(this.getSecuritySystemTargetState.bind(this)) .onSet(this.setSecuritySystemTargetState.bind(this)); this.setName(accessory.context.device.deviceName) this.update() } setName(name) { this.service.setCharacteristic(this.platform.Characteristic.Name, name) } setValue(key, value) { if (key == "state" && value != "") { if (value == "Disarmed") { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.DISARM } else if (value == "StayArmed") { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM } else if (value == "AwayArmed") { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM } else if (value == "NightArmed") { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM // } else if (value == "Triggered") { // this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED // this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED } this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState) this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState) } } update() { const data = JSON.stringify([{ type: 'alarmsystem', query: 'state' }]) httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data) .then((response) => { const devices = JSON.parse(response.body) if (devices.hasOwnProperty('alarmsystem')) { for (const device of devices.alarmsystem) { if (device.id == this.accessory.context.device.typeId) { if (device.state == "Disarmed") { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.DISARM } else if (device.state == "StayArmed") { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM } else if (device.state == "AwayArmed") { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM } else if (device.state == "NightArmed") { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM // } else if (device.state == "Triggered") { // this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED // this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED } this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState) this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState) break } } } }) .catch((error) => { this.platform.log.debug('securitySystemAccessory::update Error ->' + error) }) } getSecuritySystemCurrentState(): CharacteristicValue { return this.state.CurrentState } getSecuritySystemTargetState(): CharacteristicValue { return this.state.TargetState } async setSecuritySystemTargetState(value: CharacteristicValue) { let targetState: string = ""; this.state.TargetState = value as number // FIX if (value == this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM) { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM targetState = "StayArmed" } else if (value == this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM) { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM targetState = "AwayArmed" } else if (value == this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM) { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM targetState = "NightArmed" } else if (value == this.platform.Characteristic.SecuritySystemTargetState.DISARM) { this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED targetState = "Disarmed" } // else if (value == this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED) { // this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED // targetState = "Triggered" // } this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState) this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState) // http://$domoticaIP/external/WebUpdate.php?data=AlarmSystem/$id/$state/ //httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=AlarmSystem/' + this.accessory.context.device.typeId + '/' + (targetState? '1': '0') + '/') httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=AlarmSystem/' + 1 + '/' + targetState + '/') .catch((error) => { this.platform.log.debug('securitySystemAccessory::setSecuritySystemTargetState Error ->' + error) }) } }