diff --git a/plejd/api.js b/plejd/api.js index a2a7b31..49823a5 100644 --- a/plejd/api.js +++ b/plejd/api.js @@ -8,7 +8,7 @@ API_LOGIN_URL = 'login'; API_SITES_URL = 'functions/getSites'; // #region logging -const debug = ''; +let debug = ''; const getLogger = () => { const consoleLogger = msg => console.log('plejd-api', msg); @@ -34,6 +34,15 @@ class PlejdApi extends EventEmitter { this.site = null; } + updateSettings(settings) { + if (settings.debug) { + debug = 'console'; + } + else { + debug = ''; + } + } + login() { logger('login()'); const self = this; diff --git a/plejd/ble.js b/plejd/ble.js index 05717ff..5a258d6 100644 --- a/plejd/ble.js +++ b/plejd/ble.js @@ -71,6 +71,15 @@ class PlejdService extends EventEmitter { this.wireEvents(); } + updateSettings(settings) { + if (settings.debug) { + debug = 'console'; + } + else { + debug = ''; + } + } + turnOn(id, command) { logger('turning on ' + id + ' at brightness ' + (!command.brightness ? 255 : command.brightness)); const brightness = command.brightness ? command.brightness : 0; @@ -83,7 +92,7 @@ class PlejdService extends EventEmitter { let i = 0; const transitionRef = setInterval(() => { - this._turnOn(id, (brightnessStep * i) + 1); + this._turnOn(id, parseInt((brightnessStep * i) + 1)); if (i >= steps) { clearInterval(transitionRef); @@ -124,19 +133,16 @@ class PlejdService extends EventEmitter { let i = 0; const transitionRef = setInterval(() => { - currentBrightness = initialBrightness - (brightnessStep * i); - if (currentBrightness <= 0) { - clearInterval(transitionRef); - } - - this._turnOn(id, currentBrightness); - - if (i >= steps) { + currentBrightness = parseInt(initialBrightness - (brightnessStep * i)); + if (currentBrightness <= 0 || i >= steps) { clearInterval(transitionRef); // finally, we turn it off this._turnOff(id); + return; } + + this._turnOn(id, currentBrightness); i++; }, 500); diff --git a/plejd/main.js b/plejd/main.js index 855037e..7b9856a 100644 --- a/plejd/main.js +++ b/plejd/main.js @@ -45,6 +45,18 @@ async function main() { plejd.turnOff(deviceId, command); } }); + + client.on('settingsChanged', (settings) => { + if (settings.module === 'mqtt') { + client.updateSettings(settings); + } + else if (settings.module === 'ble') { + plejd.updateSettings(settings); + } + else if (settings.module === 'api') { + plejdApi.updateSettings(settings); + } + }); }); }); diff --git a/plejd/mqtt.js b/plejd/mqtt.js index 40e6440..39afbb9 100644 --- a/plejd/mqtt.js +++ b/plejd/mqtt.js @@ -5,7 +5,7 @@ const _ = require('lodash'); const startTopic = 'hass/status'; // #region logging -const debug = ''; +let debug = ''; const getLogger = () => { const consoleLogger = msg => console.log('plejd-mqtt', msg); @@ -30,6 +30,7 @@ const getConfigPath = plug => `${getPath(plug)}/config`; const getStateTopic = plug => `${getPath(plug)}/state`; const getCommandTopic = plug => `${getPath(plug)}/set`; const getSceneEventTopic = () => `plejd/event/scene`; +const getSettingsTopic = () => `plejd/settings`; const getDiscoveryPayload = device => ({ schema: 'json', @@ -78,6 +79,12 @@ class MqttClient extends EventEmitter { logger('error: unable to subscribe to control topics'); } }); + + this.client.subscribe(getSettingsTopic(), (err) => { + if (err) { + console.log('error: could not subscribe to settings topic'); + } + }); }); this.client.on('close', () => { @@ -92,6 +99,9 @@ class MqttClient extends EventEmitter { logger('home assistant has started. lets do discovery.'); self.emit('connected'); } + else if (topic === getSettingsTopic()) { + self.emit('settingsChanged', command); + } if (_.includes(topic, 'set')) { const device = self.devices.find(x => getCommandTopic(x) === topic); @@ -100,6 +110,15 @@ class MqttClient extends EventEmitter { }); } + updateSettings(settings) { + if (settings.debug) { + debug = 'console'; + } + else { + debug = ''; + } + } + reconnect() { this.client.reconnect(); }