diff --git a/lib/mqtt/mqttClient.js b/lib/mqtt/mqttClient.js index 8a78a1d..97fae72 100644 --- a/lib/mqtt/mqttClient.js +++ b/lib/mqtt/mqttClient.js @@ -1,7 +1,4 @@ -'use strict'; -const mqtt = require('mqtt'); -const request = require('request'); -let util = require('util'); +import mqtt from 'mqtt'; const logger = require('log4js').getLogger(); // Var db = require('../server/db-connector').dbConnection; @@ -37,7 +34,7 @@ const mqttClient = function(events) { const d = new Date(); this.lastMsg = d.getTime(); - events.on('sendIFTTT', function(mode) { + events.on('sendIFTTT', mode => { let d; const url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join(''); @@ -138,24 +135,24 @@ const mqttClient = function(events) { const options = { keepalive: 3600, - clientId: 'a:' + orgId + ':' + Date.now(), + clientId: `a:${orgId}:${Date.now()}`, username: userName, password: new Buffer(appKey) }; - this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options); + this.client = mqtt.connect(`mqtt://${orgId}silvrtree.co.uk`, options); - this.client.on('connect', function() { + this.client.on('connect', () => { this.connected = true; logger.info('Connected to Silvr Broker'); this.fanTimer(); this.logTemp(); - }.bind(this)); + }); this.client.subscribe('livingroomTemp'); - this.client.on('message', function(topic, message) { + this.client.on('message', (topic, message) => { const json = JSON.parse(message.toString()); logger.debug(json); @@ -174,7 +171,7 @@ const mqttClient = function(events) { const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}}; events.emit('sendSocket', data); - }.bind(this)); + }); this.isConnected = function() { return this.connected; @@ -182,4 +179,4 @@ const mqttClient = function(events) { }; -module.exports.mqttClient = mqttClient; +export {mqttClient};