activityAccessory.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 activityAccessory extends accessory {
  7. private service: Service
  8. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  9. return new Promise((resolve, reject) => {
  10. let activityDevices: device[] = []
  11. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/activity')
  12. .then((response) => {
  13. let devices = JSON.parse(response.body)
  14. for (const device of devices) {
  15. if (device.enabled == "true") {
  16. let object: device = {
  17. type: "Activity",
  18. typeId: device.id,
  19. uniqueId: "Activity." + device.id,
  20. deviceName: device.name,
  21. switchable: "false",
  22. dimmable: "false",
  23. detailedType: ""
  24. }
  25. activityDevices.push(object)
  26. }
  27. }
  28. resolve(activityDevices)
  29. })
  30. .catch((error) => {
  31. reject('activityAccessory::discoverDevices Error ->' + error)
  32. })
  33. })
  34. }
  35. constructor(
  36. private readonly platform: domoticaPlatform,
  37. private readonly accessory: PlatformAccessory,
  38. ) {
  39. super("Activity", accessory.context.device.typeId)
  40. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  41. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Activity')
  42. .setCharacteristic(this.platform.Characteristic.Model, 'Domotica-Activity')
  43. .setCharacteristic(this.platform.Characteristic.SerialNumber, '08000' + accessory.context.device.typeId)
  44. this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch)
  45. this.service.getCharacteristic(this.platform.Characteristic.On)
  46. .onGet(this.getOn.bind(this))
  47. .onSet(this.setOn.bind(this));
  48. this.setName(accessory.context.device.deviceName)
  49. }
  50. setName(name) {
  51. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  52. }
  53. setValue(key, value) {
  54. this.service.updateCharacteristic(this.platform.Characteristic.On, false)
  55. }
  56. update() {
  57. }
  58. getOn(): CharacteristicValue {
  59. return false;
  60. }
  61. async setOn(value: CharacteristicValue) {
  62. // http://$domoticaIP/external/WebUpdate.php?data=Activity/$id/
  63. httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=Activity/' + this.accessory.context.device.typeId + '/')
  64. .catch((error) => {
  65. this.platform.log.debug('activityAccessory::setOn Error ->' + error)
  66. })
  67. }
  68. }