Skip to content

Commit

Permalink
Merge 84ad357 into 5a9a1d1
Browse files Browse the repository at this point in the history
  • Loading branch information
dersimn committed Jan 10, 2019
2 parents 5a9a1d1 + 84ad357 commit 94271a3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 42 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
@@ -0,0 +1,8 @@
FROM node:slim

COPY . /node

RUN cd /node && \
npm install

ENTRYPOINT [ "node", "/node/index.js" ]
8 changes: 8 additions & 0 deletions Dockerfile.armhf
@@ -0,0 +1,8 @@
FROM arm32v7/node:slim

COPY . /node

RUN cd /node && \
npm install

ENTRYPOINT [ "node", "/node/index.js" ]
33 changes: 17 additions & 16 deletions config.js
@@ -1,13 +1,14 @@
const config = require('yargs')
.env('MQTTSCRIPTS')
.usage('Usage: $0 [options]')
.describe('v', 'possible values: "error", "warn", "info", "debug"')
.describe('n', 'instance name. used as mqtt client id and as prefix for connected topic')
.describe('s', 'topic prefix for $ substitution (shorthand for variables, see docs)')
.describe('t', 'disable variable feedback (see docs)')
.describe('u', 'mqtt broker url. See https://github.com/mqttjs/MQTT.js#connect-using-a-url')
.describe('h', 'show help')
.describe('d', 'directory to scan for .js and .coffee files. can be used multiple times.')
.describe('w', 'disable file watching (don\'t exit process on file changes)')
.describe('verbosity', 'possible values: "error", "warn", "info", "debug"')
.describe('name', 'instance name. used as mqtt client id and as prefix for connected topic')
.describe('variable-prefix', 'topic prefix for $ substitution (shorthand for variables, see docs)')
.describe('disable-variables', 'disable variable feedback (see docs)')
.describe('url', 'mqtt broker url. See https://github.com/mqttjs/MQTT.js#connect-using-a-url')
.describe('help', 'show help')
.describe('dir', 'directory to scan for .js and .coffee files. can be used multiple times.')
.describe('disable-watch', 'disable file watching (don\'t exit process on file changes)')
.alias({
c: 'config',
d: 'dir',
Expand All @@ -23,14 +24,14 @@ const config = require('yargs')

})
.default({
u: 'mqtt://127.0.0.1',
l: 48.7408,
m: 9.1778,
n: 'logic',
s: 'var',
v: 'info',
t: false,
w: false
url: 'mqtt://127.0.0.1',
latitude: 48.7408,
longitude: 9.1778,
name: 'logic',
'variable-prefix': 'var',
verbosity: 'info',
'disable-variables': false,
'disable-watch': false
})
.config('config')
.version()
Expand Down
72 changes: 57 additions & 15 deletions doc/README.header.md
Expand Up @@ -46,30 +46,72 @@ echo "log.info 'get ma a coffee' > /opt/mqtt-smarthome/scripts/test1.coffee
```mqtt-scripts -d /opt/mqtt-smarthome/scripts```


### Run with Docker

To run with Docker, use either a [pre-build image](https://hub.docker.com/r/dersimn/mqtt-scripts) or build one your own. Either way, just substitude the node-command you would have used by the Docker command, for e.g.:

```mqtt-scripts --help```

becomes

```docker run dersimn/mqtt-scripts --help```

An example for a productive configuration would be:

```
docker run -d --restart=always --name=logic \
-e "TZ=Europe/Berlin" \
-v /opt/hma/etc/scripts:/scripts:ro \
dersimn/mqtt-scripts \
--url mqtt://10.1.1.50 \
--dir /scripts
```

Configure via `MQTTSCRIPTS_` env variables when using Docker Compose.


#### Build

To build a Docker image yourself use the following workflow:

```
git clone https://github.com/hobbyquaker/mqtt-scripts.git
cd mqtt-scripts
docker build -t mqtt-scripts .
```

To build for the Raspberry Pi, run:

```
docker build -t mqtt-scripts:armhf -f Dockerfile.armhf .
```


# Command Line Options

<pre>
Usage: mqtt-scripts [options]

Options:
-v, --verbosity possible values: "error", "warn", "info", "debug"
[default: "info"]
-n, --name instance name. used as mqtt client id and as prefix
for connected topic [default: "logic"]
-s, --variable-prefix topic prefix for $ substitution (shorthand for
variables, see docs) [default: "var"]
-t, --disable-variables disable variable feedback (see docs)
[default: false]
-u, --url mqtt broker url. See https://github.com/mqttjs/MQTT.
js#connect-using-a-url [default: "mqtt://127.0.0.1"]
-h, --help Show help
--version Show version number [boolean]
-c, --config Path to JSON config file
-d, --dir directory to scan for .js and .coffee files. can be
used multiple times.
-h, --help Show help [boolean]
-s, --variable-prefix topic prefix for $ substitution (shorthand for
variables, see docs) [default: "var"]
-t, --disable-variables disable variable feedback (see docs) [default: false]
-n, --name instance name. used as mqtt client id and as prefix
for connected topic [default: "logic"]
-u, --url mqtt broker url. See
https://github.com/mqttjs/MQTT.js#connect-using-a-url
[default: "mqtt://127.0.0.1"]
-v, --verbosity possible values: "error", "warn", "info", "debug"
[default: "info"]
-w, --disable-watch disable file watching (don't exit process on file
changes)
--version Show version number
-l, --latitude Coordinates are needed for the sunSchedule method
-m, --longitude
changes) [default: false]
-l, --latitude [default: 48.7408]
-m, --longitude [default: 9.1778]
</pre>

If you're running multiple instances of mqtt-scripts you have to decide which one should handle variables and disable
Expand Down
23 changes: 12 additions & 11 deletions index.js
Expand Up @@ -15,6 +15,7 @@ const pkg = require('./package.json');
/* istanbul ignore next */
log.setLevel(['debug', 'info', 'warn', 'error'].indexOf(config.verbosity) === -1 ? 'info' : config.verbosity);
log.info(pkg.name + ' ' + pkg.version + ' starting');
log.debug('loaded config: ', config);

const modules = {
fs: require('fs'),
Expand Down Expand Up @@ -54,9 +55,9 @@ function calculateSunTimes() {
const yesterday = new Date(today.getTime() - 86400000); // (24 * 60 * 60 * 1000));
const tomorrow = new Date(today.getTime() + 86400000); // (24 * 60 * 60 * 1000));
sunTimes = [
suncalc.getTimes(yesterday, config.l, config.m),
suncalc.getTimes(today, config.l, config.m),
suncalc.getTimes(tomorrow, config.l, config.m)
suncalc.getTimes(yesterday, config.latitude, config.longitude),
suncalc.getTimes(today, config.latitude, config.longitude),
suncalc.getTimes(tomorrow, config.latitude, config.longitude)
];
}

Expand Down Expand Up @@ -186,7 +187,7 @@ mqtt.on('message', (topic, payload, msg) => {
const topicArr = topic.split('/');
let oldState;

if (topicArr[0] === config.s && topicArr[1] === 'set' && !config.t) {
if (topicArr[0] === config.variablePrefix && topicArr[1] === 'set' && !config.disableVariables) {
topicArr[1] = 'status';
topic = topicArr.join('/');
oldState = status[topic] || {};
Expand Down Expand Up @@ -414,7 +415,7 @@ function runScript(script, name) {
}

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

if (typeof options.condition === 'string') {
Expand Down Expand Up @@ -605,10 +606,10 @@ function runScript(script, name) {

let changed;

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

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

tmp[1] = 'status';
Expand All @@ -630,7 +631,7 @@ function runScript(script, name) {
Sandbox.publish(topic, val, {retain: true});
}
/* istanbul ignore next */ // TODO tests!
} else if (tmp[0] === config.s && config.t) {
} else if (tmp[0] === config.variablePrefix && config.disableVariables) {
/* istanbul ignore next */
tmp[1] = 'status';
topic = tmp.join('/');
Expand All @@ -652,7 +653,7 @@ function runScript(script, name) {
* @returns {mixed} the topics value
*/
getValue: function Sandbox_getValue(topic) {
topic = topic.replace(/^\$/, config.s + '/status/');
topic = topic.replace(/^\$/, config.variablePrefix + '/status/');
topic = topic.replace(/^([^/]+)\/\/(.+)$/, '$1/status/$2');
return status[topic] && status[topic].val;
},
Expand Down Expand Up @@ -780,7 +781,7 @@ function loadSandbox(callback) {
}
});

if (!config['disable-watch']) {
if (!config.disableWatch) {
watch.watchTree(dir, {
filter(path) {
return path.match(/\.js$/);
Expand Down Expand Up @@ -817,7 +818,7 @@ function loadDir(dir) {
}
});

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

0 comments on commit 94271a3

Please sign in to comment.