Converted to pure ES6 using https://lebab.io/try-it
This commit is contained in:
parent
6d83d048d4
commit
65bc6987d4
@ -1,7 +1,4 @@
|
|||||||
'use strict';
|
import mqtt from 'mqtt';
|
||||||
const mqtt = require('mqtt');
|
|
||||||
const request = require('request');
|
|
||||||
let util = require('util');
|
|
||||||
const logger = require('log4js').getLogger();
|
const logger = require('log4js').getLogger();
|
||||||
|
|
||||||
// Var db = require('../server/db-connector').dbConnection;
|
// Var db = require('../server/db-connector').dbConnection;
|
||||||
@ -37,7 +34,7 @@ const mqttClient = function(events) {
|
|||||||
const d = new Date();
|
const d = new Date();
|
||||||
this.lastMsg = d.getTime();
|
this.lastMsg = d.getTime();
|
||||||
|
|
||||||
events.on('sendIFTTT', function(mode) {
|
events.on('sendIFTTT', mode => {
|
||||||
let d;
|
let d;
|
||||||
const url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
|
const url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
|
||||||
|
|
||||||
@ -138,24 +135,24 @@ const mqttClient = function(events) {
|
|||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
keepalive: 3600,
|
keepalive: 3600,
|
||||||
clientId: 'a:' + orgId + ':' + Date.now(),
|
clientId: `a:${orgId}:${Date.now()}`,
|
||||||
username: userName,
|
username: userName,
|
||||||
password: new Buffer(appKey)
|
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;
|
this.connected = true;
|
||||||
logger.info('Connected to Silvr Broker');
|
logger.info('Connected to Silvr Broker');
|
||||||
this.fanTimer();
|
this.fanTimer();
|
||||||
this.logTemp();
|
this.logTemp();
|
||||||
}.bind(this));
|
});
|
||||||
|
|
||||||
this.client.subscribe('livingroomTemp');
|
this.client.subscribe('livingroomTemp');
|
||||||
|
|
||||||
this.client.on('message', function(topic, message) {
|
this.client.on('message', (topic, message) => {
|
||||||
|
|
||||||
const json = JSON.parse(message.toString());
|
const json = JSON.parse(message.toString());
|
||||||
logger.debug(json);
|
logger.debug(json);
|
||||||
@ -174,7 +171,7 @@ const mqttClient = function(events) {
|
|||||||
const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}};
|
const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}};
|
||||||
events.emit('sendSocket', data);
|
events.emit('sendSocket', data);
|
||||||
|
|
||||||
}.bind(this));
|
});
|
||||||
|
|
||||||
this.isConnected = function() {
|
this.isConnected = function() {
|
||||||
return this.connected;
|
return this.connected;
|
||||||
@ -182,4 +179,4 @@ const mqttClient = function(events) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.mqttClient = mqttClient;
|
export {mqttClient};
|
||||||
|
Loading…
Reference in New Issue
Block a user