Skip to content

Commit

Permalink
Merge pull request #16 from stjohnjohnson/MoreNodeFriendly
Browse files Browse the repository at this point in the history
Make it much more Node friendly!
  • Loading branch information
stjohnjohnson committed Feb 17, 2016
2 parents a2d431c + ab8587f commit 5ef2d4c
Showing 7 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -35,5 +35,6 @@ node_modules
*.log
subscription.json
config.yml
state.json

artifacts
16 changes: 15 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
FROM node:4-onbuild
FROM node:4
MAINTAINER St. John Johnson <st.john.johnson@gmail.com> and Jeremiah Wuenschel <jeremiah.wuenschel@gmail.com>

# 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" ]
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -4,3 +4,6 @@ mqtt:
host: mqtt
# Preface for the topics $PREFACE/$DEVICE_NAME/$PROPERTY
preface: smartthings

# Port number to listen on
port: 8080
3 changes: 3 additions & 0 deletions bin/smartthings-mqtt-bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../server.js');
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
12 changes: 9 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 5ef2d4c

Please sign in to comment.