Read log level from configuration and update readme
- Break out reading of settings in js and remove it from shell scripts - Pick up config from Logger via Configuration class
This commit is contained in:
parent
4176cfb714
commit
1b55cabf63
7 changed files with 90 additions and 34 deletions
15
plejd/Configuration.js
Normal file
15
plejd/Configuration.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
const fs = require('fs');
|
||||
|
||||
class Configuration {
|
||||
static _config = null;
|
||||
|
||||
static getConfiguration() {
|
||||
if (!Configuration._config) {
|
||||
const rawData = fs.readFileSync('/data/options.json');
|
||||
Configuration._config = JSON.parse(rawData);
|
||||
}
|
||||
return Configuration._config;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Configuration;
|
||||
|
|
@ -10,6 +10,7 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"]
|
|||
COPY ./api.js /plejd/
|
||||
COPY ./ble.bluez.js /plejd/
|
||||
COPY ./config.json /plejd/
|
||||
COPY ./Configuration.js /plejd/
|
||||
COPY ./Logger.js /plejd/
|
||||
COPY ./main.js /plejd/
|
||||
COPY ./mqtt.js /plejd/
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
const winston = require("winston");
|
||||
const { colorize, combine, label, printf, timestamp } = winston.format;
|
||||
|
||||
const Configuration = require("./Configuration");
|
||||
|
||||
const LEVELS = ["error", "warn", "info", "debug", "verbose", "silly"];
|
||||
|
||||
const logFormat = printf(info => {
|
||||
if(info.stack) {
|
||||
return `${info.timestamp} ${info.level} [${info.label}] ${info.message}\n${info.stack}`;
|
||||
|
|
@ -19,11 +23,17 @@ class Logger {
|
|||
* - swap debug/verbose levels and omit http to mimic HA standard
|
||||
* Levels (in order): error, warn, info, debug, verbose, silly
|
||||
* */
|
||||
static getLogger(moduleName, level="verbose") {
|
||||
static getLogger(moduleName) {
|
||||
const config = Configuration.getConfiguration();
|
||||
const level = (config.logLevel && LEVELS.find(l => l.startsWith(config.logLevel[0].toLowerCase()))) || "info";
|
||||
|
||||
const logger = winston.createLogger({
|
||||
format: combine(
|
||||
winston.format(info => {
|
||||
switch (info.level) {
|
||||
case "warning":
|
||||
info.level = "WRN";
|
||||
break;
|
||||
case "verbose":
|
||||
info.level = "VRB";
|
||||
break;
|
||||
|
|
@ -48,6 +58,12 @@ class Logger {
|
|||
]
|
||||
});
|
||||
winston.addColors(Logger.logLevels().colors);
|
||||
|
||||
|
||||
if (moduleName === "plejd-main") {
|
||||
logger.log(level, `Log level set to ${level}`);
|
||||
}
|
||||
|
||||
return logger;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
"mqttUsername": "",
|
||||
"mqttPassword": "",
|
||||
"includeRoomsAsLights": false,
|
||||
"logLevel": "info",
|
||||
"connectionTimeout": 2,
|
||||
"writeQueueWaitTime": 400
|
||||
},
|
||||
|
|
@ -35,6 +36,7 @@
|
|||
"mqttUsername": "str",
|
||||
"mqttPassword": "str",
|
||||
"includeRoomsAsLights": "bool",
|
||||
"logLevel": "str",
|
||||
"connectionTimeout": "int",
|
||||
"writeQueueWaitTime": "int"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
const api = require('./api');
|
||||
const mqtt = require('./mqtt');
|
||||
const fs = require('fs');
|
||||
|
||||
const Logger = require('./Logger');
|
||||
const PlejdService = require('./ble.bluez');
|
||||
const SceneManager = require('./scene.manager');
|
||||
const Configuration = require("./Configuration");
|
||||
|
||||
const logger = Logger.getLogger("plejd-main");
|
||||
|
||||
|
|
@ -14,8 +14,7 @@ const version = "0.4.8";
|
|||
async function main() {
|
||||
logger.info(`Starting Plejd add-on v. ${version}`);
|
||||
|
||||
const rawData = fs.readFileSync('/data/plejd.json');
|
||||
const config = JSON.parse(rawData);
|
||||
const config = Configuration.getConfiguration();
|
||||
|
||||
if (!config.connectionTimeout) {
|
||||
config.connectionTimeout = 2;
|
||||
|
|
|
|||
|
|
@ -1,33 +1,4 @@
|
|||
#!/usr/bin/with-contenv bashio
|
||||
|
||||
CONFIG_PATH=/data/options.json
|
||||
|
||||
SITE=$(jq --raw-output ".site" $CONFIG_PATH)
|
||||
USERNAME=$(jq --raw-output ".username" $CONFIG_PATH)
|
||||
PASSWORD=$(jq --raw-output ".password" $CONFIG_PATH)
|
||||
MQTTBROKER=$(jq --raw-output ".mqttBroker" $CONFIG_PATH)
|
||||
MQTTUSERNAME=$(jq --raw-output ".mqttUsername" $CONFIG_PATH)
|
||||
MQTTPASSWORD=$(jq --raw-output ".mqttPassword" $CONFIG_PATH)
|
||||
INCLUDEROOMSASLIGHTS=$(jq --raw-output ".includeRoomsAsLights" $CONFIG_PATH)
|
||||
CONNECTIONTIMEOUT=$(jq --raw-output ".connectionTimeout" $CONFIG_PATH)
|
||||
WRITEQUEUEWAITTIME=$(jq --raw-output ".writeQueueWaitTime" $CONFIG_PATH)
|
||||
|
||||
PLEJD_PATH=/data/plejd.json
|
||||
PLEJD_CONFIG="{
|
||||
\"site\": \"$SITE\",
|
||||
\"username\": \"$USERNAME\",
|
||||
\"password\": \"$PASSWORD\",
|
||||
\"mqttBroker\": \"$MQTTBROKER\",
|
||||
\"mqttUsername\": \"$MQTTUSERNAME\",
|
||||
\"mqttPassword\": \"$MQTTPASSWORD\",
|
||||
\"includeRoomsAsLights\": \"$INCLUDEROOMSASLIGHTS\",
|
||||
\"connectionTimeout\": \"$CONNECTIONTIMEOUT\",
|
||||
\"writeQueueWaitTime\": \"$WRITEQUEUEWAITTIME\"
|
||||
}
|
||||
"
|
||||
|
||||
bashio::log.info 'Wrote plejd.json'
|
||||
echo "$PLEJD_CONFIG" > $PLEJD_PATH
|
||||
|
||||
bashio::log.info 'Running add-on'
|
||||
exec node /plejd/main.js
|
||||
exec node /plejd/main.js
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue