cameraAccessory.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 cameraAccessory extends accessory {
  7. private service: Service
  8. private state = {
  9. Motion: false
  10. }
  11. static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
  12. return new Promise((resolve, reject) => {
  13. let cameraDevices: device[] = []
  14. httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/camera')
  15. .then((response) => {
  16. let devices = JSON.parse(response.body)
  17. for (const device of devices) {
  18. if (device.enabled == "true" &&
  19. device.brand == "RTSP" &&
  20. device.motiondetect == "true") {
  21. let object: device = {
  22. type: "Camera",
  23. typeId: device.id,
  24. uniqueId: "Camera." + device.id,
  25. deviceName: device.name,
  26. switchable: "false",
  27. dimmable: "false",
  28. detailedType: device.brand
  29. }
  30. cameraDevices.push(object)
  31. }
  32. }
  33. resolve(cameraDevices)
  34. })
  35. .catch((error) => {
  36. reject('cameraAccessory::discoverDevices Error ->' + error)
  37. })
  38. })
  39. }
  40. constructor(
  41. private readonly platform: domoticaPlatform,
  42. private readonly accessory: PlatformAccessory,
  43. ) {
  44. super("Camera", accessory.context.device.typeId)
  45. this.accessory.getService(this.platform.Service.AccessoryInformation)!
  46. .setCharacteristic(this.platform.Characteristic.Manufacturer, 'Camera')
  47. .setCharacteristic(this.platform.Characteristic.Model, 'ONVIF')
  48. .setCharacteristic(this.platform.Characteristic.SerialNumber, '07000' + accessory.context.device.typeId)
  49. this.service = this.accessory.getService(this.platform.Service.MotionSensor) || this.accessory.addService(this.platform.Service.MotionSensor)
  50. this.service.getCharacteristic(this.platform.Characteristic.MotionDetected)
  51. .onGet(this.getState.bind(this));
  52. this.setName(accessory.context.device.deviceName)
  53. this.update()
  54. }
  55. setName(name) {
  56. this.service.setCharacteristic(this.platform.Characteristic.Name, name)
  57. }
  58. setValue(key, value) {
  59. if (key == "state" && value != "") {
  60. if (value == "1") {
  61. this.state.Motion = true
  62. } else {
  63. this.state.Motion = false
  64. }
  65. this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.state.Motion)
  66. }
  67. }
  68. update() {
  69. const data = JSON.stringify([{
  70. type: 'camera',
  71. query: 'state'
  72. }])
  73. httpRequest("http://" + this.platform.config.hostname + ':' + this.platform.config.port + '/API/Status', data)
  74. .then((response) => {
  75. const devices = JSON.parse(response.body)
  76. if (devices.hasOwnProperty('camera')) {
  77. for (const device of devices.camera) {
  78. if (device.id == this.accessory.context.device.typeId) {
  79. if (device.motionstate == 'absent') {
  80. this.state.Motion = false
  81. } else { // present
  82. this.state.Motion = true
  83. }
  84. return this.state.Motion
  85. this.service.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.state.Motion)
  86. break
  87. }
  88. }
  89. }
  90. })
  91. .catch((error) => {
  92. this.platform.log.debug('cameraAccessory::update Error ->' + error)
  93. })
  94. }
  95. getState(): CharacteristicValue {
  96. return this.state.Motion
  97. }
  98. }