securitySystemAccessory.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge'
  2. import { accessory } from './accessory'
  3. import { device } from './device'
  4. import { httpRequest, response } from './httpPromise'
  5. import { domoticaPlatform } from './platform'
  6. export class securitySystemAccessory extends accessory {
  7. private service: Service
  8. private state = {
  9. CurrentState: this.platform.Characteristic.SecuritySystemCurrentState.DISARMED,
  10. TargetState: this.platform.Characteristic.SecuritySystemCurrentState.DISARMED
  11. }
  12. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  13. return new Promise((resolve, reject) => {
  14. let securitySystemDevices: device[] = []
  15. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/alarmsystem')
  16. .then((response) => {
  17. let devices = JSON.parse(response.body)
  18. for (const device of devices) {
  19. if (device.enabled == "true") {
  20. let object: device = {
  21. type: "SecuritySystem",
  22. typeId: device.id,
  23. uniqueId: "SecuritySystem." + device.id,
  24. deviceName: device.name,
  25. switchable: "false",
  26. dimmable: "false",
  27. detailedType: device.type
  28. }
  29. securitySystemDevices.push(object)
  30. }
  31. }
  32. resolve(securitySystemDevices)
  33. })
  34. .catch((error) => {
  35. reject('securitySystemAccessory::discoverDevices Error ->' + error)
  36. })
  37. })
  38. }
  39. constructor(
  40. private readonly platform: domoticaPlatform,
  41. private readonly accessory: PlatformAccessory,
  42. ) {
  43. super("SecuritySystem", accessory.context.device.typeId)
  44. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  45. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Dierkse')
  46. .setCharacteristic(this.platform.Characteristic.Model, 'AlarmSystem')
  47. .setCharacteristic(this.platform.Characteristic.SerialNumber, '11000' + accessory.context.device.typeId)
  48. this.service = this.accessory.getService(this.platform.Service.SecuritySystem) || this.accessory.addService(this.platform.Service.SecuritySystem)
  49. this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState)
  50. .onGet(this.getSecuritySystemCurrentState.bind(this));
  51. this.service.getCharacteristic(this.platform.Characteristic.SecuritySystemTargetState)
  52. .onGet(this.getSecuritySystemTargetState.bind(this))
  53. .onSet(this.setSecuritySystemTargetState.bind(this));
  54. this.setName(accessory.context.device.deviceName)
  55. this.update()
  56. }
  57. setName(name) {
  58. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  59. }
  60. setValue(key, value) {
  61. if (key == "state" && value != "") {
  62. if (value == "Disarmed") {
  63. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED
  64. this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.DISARM
  65. } else if (value == "StayArmed") {
  66. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM
  67. this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM
  68. } else if (value == "AwayArmed") {
  69. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM
  70. this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM
  71. } else if (value == "NightArmed") {
  72. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM
  73. this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM
  74. // } else if (value == "Triggered") {
  75. // this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED
  76. // this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED
  77. }
  78. this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState)
  79. this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState)
  80. }
  81. }
  82. update() {
  83. const data = JSON.stringify([{
  84. type: 'alarmsystem',
  85. query: 'state'
  86. }])
  87. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  88. .then((response) => {
  89. const devices = JSON.parse(response.body)
  90. if (devices.hasOwnProperty('alarmsystem')) {
  91. for (const device of devices.alarmsystem) {
  92. if (device.id == this.accessory.context.device.typeId) {
  93. if (device.state == "Disarmed") {
  94. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED
  95. this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.DISARM
  96. } else if (device.state == "StayArmed") {
  97. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM
  98. this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM
  99. } else if (device.state == "AwayArmed") {
  100. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM
  101. this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM
  102. } else if (device.state == "NightArmed") {
  103. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM
  104. this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM
  105. // } else if (device.state == "Triggered") {
  106. // this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED
  107. // this.state.TargetState = this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED
  108. }
  109. this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState)
  110. this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState)
  111. break
  112. }
  113. }
  114. }
  115. })
  116. .catch((error) => {
  117. this.platform.log.debug('securitySystemAccessory::update Error ->' + error)
  118. })
  119. }
  120. getSecuritySystemCurrentState(): CharacteristicValue {
  121. return this.state.CurrentState
  122. }
  123. getSecuritySystemTargetState(): CharacteristicValue {
  124. return this.state.TargetState
  125. }
  126. async setSecuritySystemTargetState(value: CharacteristicValue) {
  127. let targetState: string = "";
  128. this.state.TargetState = value as number
  129. // FIX
  130. if (value == this.platform.Characteristic.SecuritySystemTargetState.STAY_ARM) {
  131. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.STAY_ARM
  132. targetState = "StayArmed"
  133. }
  134. else if (value == this.platform.Characteristic.SecuritySystemTargetState.AWAY_ARM) {
  135. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.AWAY_ARM
  136. targetState = "AwayArmed"
  137. }
  138. else if (value == this.platform.Characteristic.SecuritySystemTargetState.NIGHT_ARM) {
  139. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.NIGHT_ARM
  140. targetState = "NightArmed"
  141. }
  142. else if (value == this.platform.Characteristic.SecuritySystemTargetState.DISARM) {
  143. this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.DISARMED
  144. targetState = "Disarmed"
  145. }
  146. // else if (value == this.platform.Characteristic.SecuritySystemTargetState.TRIGGERED) {
  147. // this.state.CurrentState = this.platform.Characteristic.SecuritySystemCurrentState.TRIGGERED
  148. // targetState = "Triggered"
  149. // }
  150. this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemCurrentState, this.state.CurrentState)
  151. this.service.updateCharacteristic(this.platform.Characteristic.SecuritySystemTargetState, this.state.TargetState)
  152. // http://$domoticaIP/external/WebUpdate.php?data=AlarmSystem/$id/$state/
  153. //httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=AlarmSystem/' + this.accessory.context.device.typeId + '/' + (targetState? '1': '0') + '/')
  154. httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=AlarmSystem/' + 1 + '/' + targetState + '/')
  155. .catch((error) => {
  156. this.platform.log.debug('securitySystemAccessory::setSecuritySystemTargetState Error ->' + error)
  157. })
  158. }
  159. }