aboutsummaryrefslogtreecommitdiff
path: root/src/mqtt
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-10-20 23:18:16 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-10-20 23:18:16 +0200
commit0266bdd60cb9cccf20a5ded3eba72ea833bee72d (patch)
treed727bad80aeaef673f48bbbc171fb4e9297b72fc /src/mqtt
parent73d272ffe8954b3169901eda74428bad3d2740fe (diff)
downloaddiller-server-0266bdd60cb9cccf20a5ded3eba72ea833bee72d.tar.gz
diller-server-0266bdd60cb9cccf20a5ded3eba72ea833bee72d.tar.bz2
diller-server-0266bdd60cb9cccf20a5ded3eba72ea833bee72d.tar.xz
diller-server-0266bdd60cb9cccf20a5ded3eba72ea833bee72d.zip
o Adding a webapp.
o Using di.js as dependency injection framework.
Diffstat (limited to 'src/mqtt')
-rw-r--r--src/mqtt/DillerMqtt.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/mqtt/DillerMqtt.js b/src/mqtt/DillerMqtt.js
new file mode 100644
index 0000000..3fe43dc
--- /dev/null
+++ b/src/mqtt/DillerMqtt.js
@@ -0,0 +1,38 @@
+var di = require('di');
+var mqtt = require('mqtt');
+
+function DillerMqtt(config, diller) {
+ var log = config.log();
+
+ function run() {
+ log.info('Connecting to ' + config.mqttUrl);
+ var mqttClient = mqtt.connect(config.mqttUrl);
+
+ mqttClient.on('offline', function () {
+ log.info('offline');
+ });
+
+ mqttClient.on('error', function (error) {
+ log.info('error', {error: error});
+ });
+
+ mqttClient.on('connect', function () {
+ log.info('Connected');
+ mqttClient.subscribe('/diller/#');
+ });
+
+ mqttClient.on('message', function (topic, message, payload) {
+ diller.onMessage(topic, message, payload);
+ });
+ }
+
+ return {
+ run: run
+ };
+}
+var Diller = require('../Diller');
+var DillerConfig = require('../DillerConfig');
+
+di.annotate(DillerMqtt, new di.Inject(DillerConfig, Diller));
+
+module.exports = DillerMqtt;