aboutsummaryrefslogtreecommitdiff
path: root/src/mqtt
diff options
context:
space:
mode:
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;