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'; API_SITES_URL = 'functions/getSites';
// #region logging // #region logging
const debug = ''; let debug = '';
const getLogger = () => { const getLogger = () => {
const consoleLogger = msg => console.log('plejd-api', msg); const consoleLogger = msg => console.log('plejd-api', msg);
@ -34,6 +34,15 @@ class PlejdApi extends EventEmitter {
this.site = null; this.site = null;
} }
updateSettings(settings) {
if (settings.debug) {
debug = 'console';
}
else {
debug = '';
}
}
login() { login() {
logger('login()'); logger('login()');
const self = this; const self = this;

View file

@ -71,6 +71,15 @@ class PlejdService extends EventEmitter {
this.wireEvents(); this.wireEvents();
} }
updateSettings(settings) {
if (settings.debug) {
debug = 'console';
}
else {
debug = '';
}
}
turnOn(id, command) { turnOn(id, command) {
logger('turning on ' + id + ' at brightness ' + (!command.brightness ? 255 : command.brightness)); logger('turning on ' + id + ' at brightness ' + (!command.brightness ? 255 : command.brightness));
const brightness = command.brightness ? command.brightness : 0; const brightness = command.brightness ? command.brightness : 0;
@ -83,7 +92,7 @@ class PlejdService extends EventEmitter {
let i = 0; let i = 0;
const transitionRef = setInterval(() => { const transitionRef = setInterval(() => {
this._turnOn(id, (brightnessStep * i) + 1); this._turnOn(id, parseInt((brightnessStep * i) + 1));
if (i >= steps) { if (i >= steps) {
clearInterval(transitionRef); clearInterval(transitionRef);
@ -124,19 +133,16 @@ class PlejdService extends EventEmitter {
let i = 0; let i = 0;
const transitionRef = setInterval(() => { const transitionRef = setInterval(() => {
currentBrightness = initialBrightness - (brightnessStep * i); currentBrightness = parseInt(initialBrightness - (brightnessStep * i));
if (currentBrightness <= 0) { if (currentBrightness <= 0 || i >= steps) {
clearInterval(transitionRef);
}
this._turnOn(id, currentBrightness);
if (i >= steps) {
clearInterval(transitionRef); clearInterval(transitionRef);
// finally, we turn it off // finally, we turn it off
this._turnOff(id); this._turnOff(id);
return;
} }
this._turnOn(id, currentBrightness);
i++; i++;
}, 500); }, 500);

View file

@ -45,6 +45,18 @@ async function main() {
plejd.turnOff(deviceId, command); 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'; const startTopic = 'hass/status';
// #region logging // #region logging
const debug = ''; let debug = '';
const getLogger = () => { const getLogger = () => {
const consoleLogger = msg => console.log('plejd-mqtt', msg); 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 getStateTopic = plug => `${getPath(plug)}/state`;
const getCommandTopic = plug => `${getPath(plug)}/set`; const getCommandTopic = plug => `${getPath(plug)}/set`;
const getSceneEventTopic = () => `plejd/event/scene`; const getSceneEventTopic = () => `plejd/event/scene`;
const getSettingsTopic = () => `plejd/settings`;
const getDiscoveryPayload = device => ({ const getDiscoveryPayload = device => ({
schema: 'json', schema: 'json',
@ -78,6 +79,12 @@ class MqttClient extends EventEmitter {
logger('error: unable to subscribe to control topics'); 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', () => { this.client.on('close', () => {
@ -92,6 +99,9 @@ class MqttClient extends EventEmitter {
logger('home assistant has started. lets do discovery.'); logger('home assistant has started. lets do discovery.');
self.emit('connected'); self.emit('connected');
} }
else if (topic === getSettingsTopic()) {
self.emit('settingsChanged', command);
}
if (_.includes(topic, 'set')) { if (_.includes(topic, 'set')) {
const device = self.devices.find(x => getCommandTopic(x) === topic); 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() { reconnect() {
this.client.reconnect(); this.client.reconnect();
} }