var di = require('di'); var mqtt = require('mqtt'); function DillerMqtt(config, tx) { 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) { tx(function (pg, dao, diller) { return diller.onMessage(topic, message, payload); }); }); } return { run: run }; } var DillerTx = require('../DillerTx'); var DillerConfig = require('../DillerConfig'); di.annotate(DillerMqtt, new di.Inject(DillerConfig, DillerTx)); module.exports = DillerMqtt;