UNPKG

homebridge-redmond-robot

Version:
110 lines 6.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwitchAccessory = void 0; const vacuum = require('./lib/vacuum.js'); class SwitchAccessory { constructor(platform, accessory, log) { this.platform = platform; this.accessory = accessory; this.log = log; this.state = { On: false, BatteryLevel: 100, Charging: true, }; const redmond = accessory.context.redmond; this.redmond = redmond; this.accessory.getService(this.platform.Service.AccessoryInformation) .setCharacteristic(this.platform.Characteristic.Manufacturer, 'RedmondRobot') .setCharacteristic(this.platform.Characteristic.Model, accessory.context.device.thingName) .setCharacteristic(this.platform.Characteristic.SerialNumber, accessory.context.device.attributes.mac) .setCharacteristic(this.platform.Characteristic.FirmwareRevision, accessory.context.device.attributes.firmware_version); this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch); this.service.setCharacteristic(this.platform.Characteristic.Name, accessory.context.nickname); this.service.getCharacteristic(this.platform.Characteristic.On) .onSet(this.setOn.bind(this)) .onGet(this.getOn.bind(this)); const batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery); batteryService.getCharacteristic(this.platform.Characteristic.StatusLowBattery) .onGet(this.handleStatusLowBatteryGet.bind(this)); batteryService.getCharacteristic(this.platform.Characteristic.BatteryLevel) .onGet(this.handleBatteryLevelGet.bind(this)); this.redmond.getConnection().then((client) => { this.client = client; const deviceTopic = '$aws/things/' + accessory.context.device.thingName + '/shadow/update/delta'; const getTopic = '$aws/things/' + accessory.context.device.thingName + '/shadow/get'; const getTopicAccepted = '$aws/things/' + accessory.context.device.thingName + '/shadow/get/accepted'; client.subscribe(deviceTopic); client.subscribe(getTopicAccepted); client.publish(getTopic, ''); client.on('message', (topic, msg) => { if (!topic.includes(accessory.context.device.thingName)) return; if (!Object.prototype.hasOwnProperty.call(JSON.parse(msg.toString()), 'state')) return; const message = JSON.parse(msg.toString()); if (Object.prototype.hasOwnProperty.call(message.state, 'working_status')) { const mode = message.state.working_status; const state = vacuum.isCleaning(mode); const isCharging = vacuum.isCharging(mode); this.state.Charging = isCharging; this.state.On = state; this.log.debug(accessory.context.nickname, mode); this.service.updateCharacteristic(this.platform.Characteristic.On, state); batteryService.updateCharacteristic(this.platform.Characteristic.ChargingState, isCharging); } if (!Object.prototype.hasOwnProperty.call(JSON.parse(msg.toString()).state, 'reported')) return; if (Object.prototype.hasOwnProperty.call(message.state.reported, 'working_status')) { const mode = message.state.reported.working_status; const state = vacuum.isCleaning(mode); const isCharging = vacuum.isCharging(mode); this.state.Charging = isCharging; this.state.On = state; this.log.debug(accessory.context.nickname, mode); this.service.updateCharacteristic(this.platform.Characteristic.On, state); batteryService.updateCharacteristic(this.platform.Characteristic.ChargingState, isCharging); } if (Object.prototype.hasOwnProperty.call(message.state.reported, 'battery_level')) { const battery_level = message.state.reported.battery_level; this.state.BatteryLevel = battery_level; this.log.debug(accessory.context.nickname, battery_level); batteryService.updateCharacteristic(this.platform.Characteristic.BatteryLevel, battery_level); } }); }); } async setOn(value) { this.state.On = value; const mode = this.state.On ? this.accessory.context.modes.startMode : this.accessory.context.modes.stopMode; const topic = '$aws/things/' + this.accessory.context.device.thingName + '/shadow/update'; const payload = { state: { desired: { working_status: mode }, }, }; this.client.publish(topic, JSON.stringify(payload)); this.platform.log.debug('Set Characteristic On ->', value); } async getOn() { const isOn = this.state.On; this.platform.log.debug('Get Characteristic On ->', isOn); const topic = '$aws/things/' + this.accessory.context.device.thingName + '/shadow/get'; this.client.publish(topic, ''); return isOn; } async handleStatusLowBatteryGet() { const batteryLevel = this.state.BatteryLevel; let state = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL; if (batteryLevel < 20) { state = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW; } return state; } async handleBatteryLevelGet() { const batteryLevel = this.state.BatteryLevel; return batteryLevel; } } exports.SwitchAccessory = SwitchAccessory; //# sourceMappingURL=switchAccessory.js.map