Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/code style #150

Merged
merged 8 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or the definition README at
// https://github.com/microsoft/vscode-dev-containers/tree/master/containers/javascript-node-12
{
"name": "Node.js 12",
"dockerFile": "Dockerfile",
"name": "Node.js 12",
"dockerFile": "Dockerfile",

// Use 'settings' to set *default* container specific settings.json values on container create.
// You can edit these settings after create using File > Preferences > Settings > Remote.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
// Use 'settings' to set *default* container specific settings.json values on container create.
// You can edit these settings after create using File > Preferences > Settings > Remote.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},

// Use 'appPort' to create a container with published ports. If the port isn't working, be sure
// your server accepts connections from all interfaces (0.0.0.0 or '*'), not just localhost.
// "appPort": [],
// Use 'appPort' to create a container with published ports. If the port isn't working, be sure
// your server accepts connections from all interfaces (0.0.0.0 or '*'), not just localhost.
// "appPort": [],

// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "yarn install",
// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "yarn install",

// Uncomment the next line to have VS Code connect as an existing non-root user in the container.
// On Linux, by default, the container user's UID/GID will be updated to match your local user. See
// https://aka.ms/vscode-remote/containers/non-root for details on adding a non-root user if none exist.
// "remoteUser": "node",
// Uncomment the next line to have VS Code connect as an existing non-root user in the container.
// On Linux, by default, the container user's UID/GID will be updated to match your local user. See
// https://aka.ms/vscode-remote/containers/non-root for details on adding a non-root user if none exist.
// "remoteUser": "node",

// Add the IDs of extensions you want installed when the container is created in the array below.
"extensions": [
"dbaeumer.vscode-eslint"
]
// Add the IDs of extensions you want installed when the container is created in the array below.
"extensions": ["dbaeumer.vscode-eslint"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ build/Release

# Dependency directories
node_modules/
# npm install should not be done on dev machines, only on installation in HA
package-lock.json
jspm_packages/

# TypeScript v1 declaration files
Expand Down
16 changes: 16 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
endOfLine: 'lf',
printWidth: 100,
semi: true,
singleQuote: true,
tabWidth: 2,
trailingComma: 'all',
overrides: [
{
files: ['*.js'],
options: {
trailingComma: 'all',
},
},
],
};
279 changes: 191 additions & 88 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions plejd/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
39 changes: 39 additions & 0 deletions plejd/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// const path = require('path');

// {
// "extends": ["airbnb-base", "plugin:prettier/recommended"],
// "plugins": ["prettier"],
// "rules": {
// "prettier/prettier": "error"
// }
// }

// eslint-disable-next-line no-undef
module.exports = {
root: true,
extends: [
'airbnb-base',
// 'prettier',
// 'plugin:prettier/recommended'
],
parser: 'babel-eslint',
// plugins: ['prettier'],
rules: getRules(),
};

function getRules() {
return {
// Allows modification of properties passed to functions.
// Notably used in array.forEach(e => {e.prop = val;})
'no-param-reassign': ['error', { props: false }],
// ++ operator widely used
'no-plusplus': ['off'],
// Hassio-Plejd team feals _ prefix is great for "private" variables.
// They will still be available for use from the outside
'no-underscore-dangle': ['off'],
// Allow function hoisting to improve code readability
'no-use-before-define': ['error', { functions: false, classes: true, variables: true }],
// Allow direct indexing of arrays only (array[0])
'prefer-destructuring': ['error', { array: false, object: true }],
};
}
15 changes: 15 additions & 0 deletions plejd/Configuration.js
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 8 additions & 4 deletions plejd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ ENV LANG C.UTF-8
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Copy data for add-on
COPY ./api.js /plejd/
COPY ./config.json /plejd/
COPY ./Configuration.js /plejd/
COPY ./Logger.js /plejd/
COPY ./main.js /plejd/
COPY ./mqtt.js /plejd/
COPY ./MqttClient.js /plejd/
COPY ./package.json /plejd/
COPY ./ble.bluez.js /plejd/
COPY ./scene.manager.js /plejd/
COPY ./PlejdApi.js /plejd/
COPY ./PlejdService.js /plejd/
COPY ./Scene.js /plejd/
COPY ./SceneManager.js /plejd/
COPY ./SceneStep.js /plejd/

ARG BUILD_ARCH

Expand Down
111 changes: 111 additions & 0 deletions plejd/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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 LEVELS_LOOKUP = {
error: 'ERR',
warn: 'WRN',
info: 'INF',
debug: 'DBG',
verbose: 'VRB',
silly: 'SLY',
};

const logFormat = printf((info) => {
if (info.stack) {
return `${info.timestamp} ${info.level} [${info.label}] ${info.message}\n${info.stack}`;
}
return `${info.timestamp} ${info.level} [${info.label}] ${info.message}`;
});

/** Winston-based logger */
class Logger {
constructor() {
throw new Error('Please call createLogger instead');
}

/** Created logger will follow Winston createLogger, but
* - add module name to logger
* - swap debug/verbose levels and omit http to mimic HA standard
* Levels (in order): error, warn, info, debug, verbose, silly
* */
static getLogger(moduleName) {
const config = Configuration.getConfiguration();
// eslint-disable-next-line max-len
const level = (config.logLevel && LEVELS.find((l) => l.startsWith(config.logLevel[0].toLowerCase())))
|| 'info';

const logger = winston.createLogger({
format: combine(
winston.format((info) => {
info.level = LEVELS_LOOKUP[info.level] || '???';
return info;
})(),
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
label({ label: moduleName }),
colorize(),
logFormat,
),
level,
levels: Logger.logLevels().levels,
transports: [new winston.transports.Console()],
});
winston.addColors(Logger.logLevels().colors);

if (moduleName === 'plejd-main') {
logger.log(level, `Log level set to ${level}`);
}

return logger;
}

static logLevels() {
// Default (npm) levels
// levels = {
// error: 0,
// warn: 1,
// info: 2,
// http: 3,
// verbose: 4,
// debug: 5,
// silly: 6
// }
// colors = {
// error: 'red',
// warn: 'yellow',
// info: 'green',
// http: 'green',
// verbose: 'cyan',
// debug: 'blue',
// silly: 'magenta'
// };

// Mimic HA standard below
// Debug/verbose swapped compared to npm levels, http omitted
return {
levels: {
error: 0,
warn: 1,
info: 2,
debug: 3,
verbose: 4,
silly: 6,
},
colors: {
error: 'red',
warn: 'yellow',
info: 'green',
debug: 'cyan',
verbose: 'blue',
silly: 'magenta',
},
};
}
}

module.exports = Logger;
Loading