Skip to content

Commit

Permalink
fix: lgtm alerts (#458)
Browse files Browse the repository at this point in the history
* fix: lgtm alerts

* revert const and cleaner switch statement
  • Loading branch information
robertsLando committed May 7, 2020
1 parent ae6acf4 commit ac44aed
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 77 deletions.
36 changes: 17 additions & 19 deletions app.js
@@ -1,26 +1,24 @@
var express = require('express'),
reqlib = require('app-root-path').require,
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
app = express(),
fs = require('fs'),
SerialPort = require('serialport'),
jsonStore = reqlib('/lib/jsonStore.js'),
cors = require('cors'),
ZWaveClient = reqlib('/lib/ZwaveClient'),
MqttClient = reqlib('/lib/MqttClient'),
Gateway = reqlib('/lib/Gateway'),
store = reqlib('config/store.js'),
config = reqlib('config/app.js'),
debug = reqlib('/lib/debug')('App'),
history = require('connect-history-api-fallback'),
utils = reqlib('/lib/utils.js');
var express = require('express')
var reqlib = require('app-root-path').require
var logger = require('morgan')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var app = express()
var SerialPort = require('serialport')
var jsonStore = reqlib('/lib/jsonStore.js')
var cors = require('cors')
var ZWaveClient = reqlib('/lib/ZwaveClient')
var MqttClient = reqlib('/lib/MqttClient')
var Gateway = reqlib('/lib/Gateway')
var store = reqlib('config/store.js')
var debug = reqlib('/lib/debug')('App')
var history = require('connect-history-api-fallback')
var utils = reqlib('/lib/utils.js')

var gw; //the gateway instance
let io;

debug("Application path:" + utils.getPath(true));
debug('Application path:' + utils.getPath(true))

// view engine setup
app.set('views', utils.joinPath(false, 'views'));
Expand Down
37 changes: 4 additions & 33 deletions lib/Gateway.js
Expand Up @@ -400,35 +400,6 @@ function copy (obj) {
return JSON.parse(JSON.stringify(obj))
}

/**
* Get Hass configurations based on valueID units
*
* @param {String} units The valueID units
* @returns An array with compatible configurations
*/
// eslint-disable-next-line no-unused-vars
function typeByUnits (units) {
var cfg = null

// TODO: Support more units: https://github.com/OpenZWave/open-zwave/blob/master/config/SensorMultiLevelCCTypes.xml

if (/\b(%)\b/gi.test(units)) {
cfg = ['sensor_illuminance', 'sensor_humidity', 'sensor_gas_density']
} else if (/\b(m\/s|mph|km\/h|kmh)\b/gi.test(units)) {
cfg = ['sensor_speed']
} else if (/\b(°)\b/gi.test(units)) {
cfg = ['sensor_angle']
} else if (/\b(c|f)\b/gi.test(units)) {
cfg = ['sensor_temperature']
} else if (/\b(w|v|watt|volt|kw|kwh|kw\/h)\b/gi.test(units)) {
cfg = ['sensor_electricity']
} else if (/\b(bar|pa|g\/m3|mmhg)\b/gi.test(units)) {
cfg = ['sensor_humidity']
}

return cfg
}

/**
* Get the device Object to send in discovery payload
*
Expand Down Expand Up @@ -551,10 +522,10 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
let op = valueConf.postOperation

// revert operation to write
if (op.includes('/')) op = op.replace('/', '*')
else if (op.includes('*')) op = op.replace('*', '/')
else if (op.includes('+')) op = op.replace('+', '-')
else if (op.includes('-')) op = op.replace('-', '+')
if (op.includes('/')) op = op.replace(/\//, '*')
else if (op.includes('*')) op = op.replace(/\*/g, '/')
else if (op.includes('+')) op = op.replace(/\+/, '-')
else if (op.includes('-')) op = op.replace(/-/, '+')

payload = eval(payload + op)
}
Expand Down
11 changes: 5 additions & 6 deletions lib/MqttClient.js
Expand Up @@ -222,11 +222,10 @@ function onMessageReceived (topic, payload) {
*/
MqttClient.prototype.getClientTopic = function (...devices) {
var subTopic = ''
if (devices) {
for (var i = 0; i < devices.length; i++) {
var name = this.cleanName(devices[i])
subTopic += '/' + DEVICES_PREFIX + '/' + name
}

for (var i = 0; i < devices.length; i++) {
var name = this.cleanName(devices[i])
subTopic += '/' + DEVICES_PREFIX + '/' + name
}

return this.config.prefix + '/' + CLIENTS_PREFIX + '/' + this.clientID + subTopic + '/status'
Expand All @@ -236,7 +235,7 @@ MqttClient.prototype.cleanName = function (name) {
if (!isNaN(name)) return name

name = name.replace(/\s/g, '_')
return name.replace(/[+*#\\.''``!?^=(),""%[\]:;{}]+/g, '')
return name.replace(/[+*#\\.'`!?^=(),"%[\]:;{}]+/g, '')
}

/**
Expand Down
21 changes: 3 additions & 18 deletions lib/ZwaveClient.js
Expand Up @@ -24,8 +24,6 @@ const ZWAVE_STATUS = {
6: 'closed'
}

// eslint-disable-next-line no-unused-vars
const nop = () => { }
const ZWAVE_LOG_FILE = utils.joinPath(storeDir, 'OZW_Log.txt')
const readFile = (path) => new Promise((resolve, reject) => fs.readFile(path, 'utf8', (err, data) => err ? reject(err) : resolve(data)))

Expand Down Expand Up @@ -521,25 +519,16 @@ function sceneEvent (nodeid, sceneCode) {
}

function notification (nodeid, notif, help) {
// eslint-disable-next-line no-unused-vars
var msg
var ozwnode = this.nodes[nodeid]
switch (notif) {
case 0:
msg = 'node' + nodeid + ': message complete'
break
case 1:
msg = 'node' + nodeid + ': timeout'
break
case 2:
msg = 'node' + nodeid + ': nop'
case 0: // message complete
case 1: // timeout
case 2: // nop
break
case 3: // awake
case 4: // sleep
case 5: // dead
case 6: // alive
msg = 'node' + nodeid + ': node ' + NODE_STATUS[notif]

// eslint-disable-next-line no-case-declarations
const ready = notif !== 5
// eslint-disable-next-line no-case-declarations
Expand All @@ -555,10 +544,6 @@ function notification (nodeid, notif, help) {

this.emit('nodeStatus', ozwnode)
}

break
default:
msg = 'Unknown notification code ' + notif
}

debug('Notification from node %d: %s (%s)', nodeid, help, notif)
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonStore.js
Expand Up @@ -72,4 +72,4 @@ StorageHelper.prototype.put = function (model, data) {
}

// eslint-disable-next-line camelcase
var storage_helper = module.exports = exports = new StorageHelper()
var storage_helper = module.exports = new StorageHelper()

0 comments on commit ac44aed

Please sign in to comment.