| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 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<device[]> {
- 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)
- })
- }
- }
|