| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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 powerSocketAccessory extends accessory {
- private service: Service
- private state = {
- On: false
- }
- static async discoverDevices(platform: domoticaPlatform): Promise<device[]> {
- return new Promise((resolve, reject) => {
- let powersocketDevices: device[] = []
- httpRequest("http://" + platform.config.hostname + ':' + platform.config.port + '/API/inventory/powersocket')
- .then((response) => {
- let devices = JSON.parse(response.body)
- for (const device of devices) {
- if (device.enabled == "true" &&
- device.switchable == "true") {
- let object: device = {
- type: "PowerSocket",
- typeId: device.id,
- uniqueId: "PowerSocket." + device.id,
- deviceName: device.name,
- switchable: device.switchable,
- dimmable: "false",
- detailedType: device.type
- }
- powersocketDevices.push(object)
- }
- }
-
- resolve(powersocketDevices)
- })
- .catch((error) => {
- reject('powerSocketAccessory::discoverDevices Error ->' + error)
- })
- })
- }
- constructor(
- private readonly platform: domoticaPlatform,
- private readonly accessory: PlatformAccessory,
- ) {
- super("PowerSocket", accessory.context.device.typeId)
- this.accessory.getService(this.platform.Service.AccessoryInformation)!
- .setCharacteristic(this.platform.Characteristic.Manufacturer, 'PowerSocket')
- .setCharacteristic(this.platform.Characteristic.Model, 'BW-SHP6')
- .setCharacteristic(this.platform.Characteristic.SerialNumber, '02000' + accessory.context.device.typeId)
- if (accessory.context.device.detailedType == "light") {
- this.service = this.accessory.getService(this.platform.Service.Lightbulb) || this.accessory.addService(this.platform.Service.Lightbulb)
- } else { // powersocket
- this.service = this.accessory.getService(this.platform.Service.Outlet) || this.accessory.addService(this.platform.Service.Outlet)
- }
- if (accessory.context.device.switchable == "true") {
- this.service.getCharacteristic(this.platform.Characteristic.On)
- .onSet(this.setOn.bind(this))
- .onGet(this.getOn.bind(this))
- } else {
- this.service.getCharacteristic(this.platform.Characteristic.On)
- .onGet(this.getOn.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 != "") {
- this.state.On = (value == "1")
- this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On)
- }
- }
- update() {
- const data = JSON.stringify([{
- type: 'powersocket',
- 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('powersocket')) {
- for (const device of devices.powersocket) {
- if (device.id == this.accessory.context.device.typeId) {
- if (device.state == 'on') {
- this.state.On = true
- } else {
- this.state.On = false
- }
- this.service.updateCharacteristic(this.platform.Characteristic.On, this.state.On)
- break
- }
- }
- }
- })
- .catch((error) => {
- this.platform.log.debug('powerSocketAccessory::update Error ->' + error)
- })
- }
- async setOn(value: CharacteristicValue) {
- this.state.On = value as boolean
- // http://$domoticaIP/external/WebUpdate.php?data=PowerSocket/$id/$state/
- httpRequest("http://" + 'administration-container:8080' + '/external/WebUpdate.php?data=PowerSocket/' + this.accessory.context.device.typeId + '/' + (this.state.On? '1': '0') + '/')
- .catch((error) => {
- this.platform.log.debug('powerSocketAccessory::setOn Error ->' + error)
- })
- }
- getOn(): CharacteristicValue {
- return this.state.On
- }
- }
|