Skip to content

Commit d722b88

Browse files
committed
Adds mqtt support
1 parent 4ae989c commit d722b88

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

app.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var chaconEmitter = require('./chaconEmitter');var chaconEmitter = require('./chaconEmitter');
1+
var chaconEmitter = require('./chaconEmitter');
22
var express = require('express');
33
var path = require('path');
44

@@ -16,6 +16,10 @@ function sendOffCommand(emitterId, deviceId) {
1616
chaconEmitter.transmit(chaconEmitter.buildOrder(emitterId, deviceId, false));
1717
}
1818

19+
function sendDimCommand(emitterId, deviceId, dimValue) {
20+
chaconEmitter.transmit(chaconEmitter.buildDimOrder(emitterId, deviceId, dimValue), true);
21+
}
22+
1923
app.get('/switch/:deviceId/:emitterId/:state', function (req, res) {
2024
var deviceId = parseInt(req.params.deviceId);
2125
var emitterId = parseInt(req.params.emitterId);
@@ -46,7 +50,7 @@ app.get('/switch/:deviceId/:emitterId/:state', function (req, res) {
4650
// console.log('dimValue:%s', dimValue);
4751
if (0 <= dimValue && dimValue <= 100) {
4852
// console.log('dimming!');
49-
chaconEmitter.transmit(chaconEmitter.buildDimOrder(emitterId, deviceId, dimValue), true);
53+
sendDimCommand(emitterId, deviceId, dimValue);
5054
}
5155
}
5256
}

mqtt-client.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var mqtt = require('mqtt');
2+
var client = mqtt.connect('mqtt://mosquitto');
3+
var chaconEmitter = require('./chaconEmitter');
4+
5+
chaconEmitter.init();
6+
7+
var switchTopic = 'chacon/switch';
8+
var dimmerTopic = 'chacon/dimmer';
9+
var emitterId = 12325261;
10+
11+
client.subscribe(switchTopic);
12+
client.subscribe(dimmerTopic);
13+
14+
client.on('message', function (topic, message) {
15+
16+
var command = JSON.parse(message.toString());
17+
var deviceId = command.deviceId;
18+
if (topic === switchTopic) {
19+
if (command.value === 'ON') {
20+
sendOnCommand(emitterId, deviceId);
21+
}
22+
else if (command.value === 'OFF') {
23+
sendOffCommand(emitterId, deviceId);
24+
}
25+
}
26+
else if (topic === dimmerTopic) {
27+
if (command.value === 'ON') {
28+
sendOnCommand(emitterId, deviceId);
29+
}
30+
else if (command.value === 'OFF') {
31+
sendOffCommand(emitterId, deviceId);
32+
}
33+
else {
34+
var dimValue = parseInt(command.value);
35+
if (0 <= dimValue && dimValue <= 100) {
36+
sendDimCommand(emitterId, deviceId, dimValue);
37+
}
38+
}
39+
}
40+
});
41+
42+
function sendOnCommand(emitterId, deviceId) {
43+
chaconEmitter.transmit(chaconEmitter.buildOrder(emitterId, deviceId, true));
44+
}
45+
46+
function sendOffCommand(emitterId, deviceId) {
47+
chaconEmitter.transmit(chaconEmitter.buildOrder(emitterId, deviceId, false));
48+
}
49+
50+
function sendDimCommand(emitterId, deviceId, dimValue) {
51+
chaconEmitter.transmit(chaconEmitter.buildDimOrder(emitterId, deviceId, dimValue), true);
52+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"debug": "~2.6.9",
1010
"express": "~4.15.5",
1111
"wiringpi-node": "^2.4.4"
12+
"mqtt": "^2.14.0"
1213
}
1314
}

0 commit comments

Comments
 (0)