added support to toggle logging through mqtt

This commit is contained in:
icanos 2019-12-22 18:20:20 +00:00
parent a9567517ef
commit c8dae032a9
4 changed files with 57 additions and 11 deletions

View file

@ -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;

View file

@ -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,20 +133,17 @@ 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);
}

View file

@ -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);
}
});
});
});

View file

@ -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();
}