Skip to content

Commit ecb31f9

Browse files
committed
Use more readable variable names in code
1 parent 84f3b0c commit ecb31f9

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

config.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const config = require('yargs')
22
.env('MQTTSCRIPTS')
33
.usage('Usage: $0 [options]')
4-
.describe('v', 'possible values: "error", "warn", "info", "debug"')
5-
.describe('n', 'instance name. used as mqtt client id and as prefix for connected topic')
6-
.describe('s', 'topic prefix for $ substitution (shorthand for variables, see docs)')
7-
.describe('t', 'disable variable feedback (see docs)')
8-
.describe('u', 'mqtt broker url. See https://github.com/mqttjs/MQTT.js#connect-using-a-url')
9-
.describe('h', 'show help')
10-
.describe('d', 'directory to scan for .js and .coffee files. can be used multiple times.')
11-
.describe('w', 'disable file watching (don\'t exit process on file changes)')
4+
.describe('verbosity', 'possible values: "error", "warn", "info", "debug"')
5+
.describe('name', 'instance name. used as mqtt client id and as prefix for connected topic')
6+
.describe('variable-prefix', 'topic prefix for $ substitution (shorthand for variables, see docs)')
7+
.describe('disable-variables', 'disable variable feedback (see docs)')
8+
.describe('url', 'mqtt broker url. See https://github.com/mqttjs/MQTT.js#connect-using-a-url')
9+
.describe('help', 'show help')
10+
.describe('dir', 'directory to scan for .js and .coffee files. can be used multiple times.')
11+
.describe('disable-watch', 'disable file watching (don\'t exit process on file changes)')
1212
.alias({
1313
c: 'config',
1414
d: 'dir',
@@ -24,14 +24,14 @@ const config = require('yargs')
2424

2525
})
2626
.default({
27-
u: 'mqtt://127.0.0.1',
28-
l: 48.7408,
29-
m: 9.1778,
30-
n: 'logic',
31-
s: 'var',
32-
v: 'info',
33-
t: false,
34-
w: false
27+
url: 'mqtt://127.0.0.1',
28+
latitude: 48.7408,
29+
longitude: 9.1778,
30+
name: 'logic',
31+
'variable-prefix': 'var',
32+
verbosity: 'info',
33+
'disable-variables': false,
34+
'disable-watch': false
3535
})
3636
.config('config')
3737
.version()

index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ function calculateSunTimes() {
5555
const yesterday = new Date(today.getTime() - 86400000); // (24 * 60 * 60 * 1000));
5656
const tomorrow = new Date(today.getTime() + 86400000); // (24 * 60 * 60 * 1000));
5757
sunTimes = [
58-
suncalc.getTimes(yesterday, config.l, config.m),
59-
suncalc.getTimes(today, config.l, config.m),
60-
suncalc.getTimes(tomorrow, config.l, config.m)
58+
suncalc.getTimes(yesterday, config.latitude, config.longitude),
59+
suncalc.getTimes(today, config.latitude, config.longitude),
60+
suncalc.getTimes(tomorrow, config.latitude, config.longitude)
6161
];
6262
}
6363

@@ -187,7 +187,7 @@ mqtt.on('message', (topic, payload, msg) => {
187187
const topicArr = topic.split('/');
188188
let oldState;
189189

190-
if (topicArr[0] === config.s && topicArr[1] === 'set' && !config.t) {
190+
if (topicArr[0] === config.variablePrefix && topicArr[1] === 'set' && !config.disableVariables) {
191191
topicArr[1] = 'status';
192192
topic = topicArr.join('/');
193193
oldState = status[topic] || {};
@@ -415,7 +415,7 @@ function runScript(script, name) {
415415
}
416416

417417
if (typeof topic === 'string') {
418-
topic = topic.replace(/^\$/, config.s + '/status/');
418+
topic = topic.replace(/^\$/, config.variablePrefix + '/status/');
419419
topic = topic.replace(/^([^/]+)\/\//, '$1/status/');
420420

421421
if (typeof options.condition === 'string') {
@@ -606,10 +606,10 @@ function runScript(script, name) {
606606

607607
let changed;
608608

609-
topic = topic.replace(/^\$/, config.s + '//');
609+
topic = topic.replace(/^\$/, config.variablePrefix + '//');
610610

611611
const tmp = topic.split('/');
612-
if (tmp[0] === config.s && !config.t) {
612+
if (tmp[0] === config.variablePrefix && !config.disableVariables) {
613613
// Variable
614614

615615
tmp[1] = 'status';
@@ -631,7 +631,7 @@ function runScript(script, name) {
631631
Sandbox.publish(topic, val, {retain: true});
632632
}
633633
/* istanbul ignore next */ // TODO tests!
634-
} else if (tmp[0] === config.s && config.t) {
634+
} else if (tmp[0] === config.variablePrefix && config.disableVariables) {
635635
/* istanbul ignore next */
636636
tmp[1] = 'status';
637637
topic = tmp.join('/');
@@ -653,7 +653,7 @@ function runScript(script, name) {
653653
* @returns {mixed} the topics value
654654
*/
655655
getValue: function Sandbox_getValue(topic) {
656-
topic = topic.replace(/^\$/, config.s + '/status/');
656+
topic = topic.replace(/^\$/, config.variablePrefix + '/status/');
657657
topic = topic.replace(/^([^/]+)\/\/(.+)$/, '$1/status/$2');
658658
return status[topic] && status[topic].val;
659659
},
@@ -781,7 +781,7 @@ function loadSandbox(callback) {
781781
}
782782
});
783783

784-
if (!config['disable-watch']) {
784+
if (!config.disableWatch) {
785785
watch.watchTree(dir, {
786786
filter(path) {
787787
return path.match(/\.js$/);
@@ -818,7 +818,7 @@ function loadDir(dir) {
818818
}
819819
});
820820

821-
if (!config['disable-watch']) {
821+
if (!config.disableWatch) {
822822
watch.watchTree(dir, {
823823
filter(path) {
824824
return path.match(/\.(js|coffee)$/);

0 commit comments

Comments
 (0)