From ab8587f65a7d502f1353730d7a0fdbb3f7ad9526 Mon Sep 17 00:00:00 2001 From: "St. John Johnson" Date: Mon, 15 Feb 2016 10:04:14 -0800 Subject: [PATCH] Make it much more Node friendly! - Bin file for running the service - Name is correct - Dockerfile installs JUST the production packages - Port is customizable --- .gitignore | 1 + Dockerfile | 16 +++++++++++++++- README.md | 8 ++++++-- _config.yml | 3 +++ bin/smartthings-mqtt-bridge | 3 +++ package.json | 5 ++++- server.js | 12 +++++++++--- 7 files changed, 41 insertions(+), 7 deletions(-) create mode 100755 bin/smartthings-mqtt-bridge diff --git a/.gitignore b/.gitignore index 5f81419..0f6ad93 100644 --- a/.gitignore +++ b/.gitignore @@ -35,5 +35,6 @@ node_modules *.log subscription.json config.yml +state.json artifacts diff --git a/Dockerfile b/Dockerfile index 4186c66..7768fc5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,17 @@ -FROM node:4-onbuild +FROM node:4 MAINTAINER St. John Johnson and Jeremiah Wuenschel +# Create our application direcory +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +# Copy and install dependencies +COPY package.json /usr/src/app/ +RUN npm install --production + +# Copy everything else +COPY . /usr/src/app + # Expose Configuration Volume VOLUME /config @@ -9,3 +20,6 @@ ENV CONFIG_DIR=/config # Expose the web service port EXPOSE 8080 + +# Run the service +CMD [ "npm", "start" ] diff --git a/README.md b/README.md index 6f2c798..afdb36b 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ $ mqtt pub -t 'smartthings/Fireplace Lights/switch' -m 'off' # Configuration -The bridge has one yaml file for configuration. Currently we only have two items you can set: +The bridge has one yaml file for configuration. Currently we only have three items you can set: ``` --- @@ -48,9 +48,13 @@ mqtt: host: mqtt # Preface for the topics $PREFACE/$DEVICE_NAME/$PROPERTY preface: smartthings + +# Port number to listen on +port: 8080 + ``` -We'll be adding additional fields as this service progresses (port, username, password, etc). +We'll be adding additional fields as this service progresses (mqtt port, username, password, etc). # Usage diff --git a/_config.yml b/_config.yml index c715893..80ffce3 100644 --- a/_config.yml +++ b/_config.yml @@ -4,3 +4,6 @@ mqtt: host: mqtt # Preface for the topics $PREFACE/$DEVICE_NAME/$PROPERTY preface: smartthings + +# Port number to listen on +port: 8080 diff --git a/bin/smartthings-mqtt-bridge b/bin/smartthings-mqtt-bridge new file mode 100755 index 0000000..ba6c42a --- /dev/null +++ b/bin/smartthings-mqtt-bridge @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require('../server.js'); diff --git a/package.json b/package.json index 1ffde08..8191ee9 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,11 @@ { - "name": "MQTTBridge", + "name": "smartthings-mqtt-bridge", "version": "1.1.2", "description": "Bridge between SmartThings and an MQTT broker", "main": "server.js", + "bin": { + "smartthings-mqtt-bridge": "./bin/smartthings-mqtt-bridge" + }, "scripts": { "pretest": "npm run hint && npm run style", "test": "jenkins-mocha", diff --git a/server.js b/server.js index d97c42b..25cc6bc 100644 --- a/server.js +++ b/server.js @@ -100,6 +100,11 @@ function migrateState (version) { config.mqtt.preface = '/smartthings'; } + // Default port + if (!config.port) { + config.port = 8080; + } + // Stuff was previously in subscription.json, load that and migrate it var SUBSCRIPTION_FILE = path.join(CONFIG_DIR, 'subscription.json'); if (semver.lt(version, '1.1.0') && fs.existsSync(SUBSCRIPTION_FILE)) { @@ -241,6 +246,7 @@ async.series([ function loadFromDisk (next) { var state; + winston.info('Starting SmartThings MQTT Bridge - v%s', CURRENT_VERSION); winston.info('Loading configuration'); config = loadConfiguration(); @@ -256,7 +262,7 @@ async.series([ process.nextTick(next); }, function connectToMQTT (next) { - winston.info('Connecting to MQTT'); + winston.info('Connecting to MQTT at mqtt://%s', config.mqtt.host); client = mqtt.connect('mqtt://' + config.mqtt.host); client.on('message', parseMQTTMessage); @@ -332,11 +338,11 @@ async.series([ } }); - app.listen(8080, next); + app.listen(config.port, next); } ], function (error) { if (error) { return winston.error(error); } - winston.info('Listening at http://localhost:8080'); + winston.info('Listening at http://localhost:%s', config.port); });