From 0266bdd60cb9cccf20a5ded3eba72ea833bee72d Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Tue, 20 Oct 2015 23:18:16 +0200
Subject: o Adding a webapp. o Using di.js as dependency injection framework.

---
 src/mqtt/DillerMqtt.js | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
 create mode 100644 src/mqtt/DillerMqtt.js

(limited to 'src/mqtt')

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;
-- 
cgit v1.2.3