mqtt_server/lib/mqtt/mqttClient.js
2016-11-13 22:33:03 +00:00

150 lines
3.3 KiB
JavaScript

var mqtt = require('mqtt');
var request = require('request');
var util = require('util');
var logger = require('log4js').getLogger();
var EventEmitter = require('events');
var Minibus = require('minibus');
// var db = require('../server/db-connector').dbConnection;
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
//var nano = require('nano')('http://localhost:5984');
var busEmitter = new EventEmitter();
var db_name = 'mqtt';
//var dbCouch = nano.use(db_name);
busEmitter.on('send', function (mode) {
var url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
request(url, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
});
});
var mqttClient = function () {
var orgId = '';
var userName = '';
var appKey = 'bob';
this.livingRoom = {
temp: 0,
last: 0
};
var mode = 'FanOff';
var d = new Date();
this.lastMsg = d.getTime();
this.fanTimer = function () {
'use strict';
var now = new Date;
var mod = 900000 - (now.getTime() % 900000);
// var
if (mode === 'FanOff') {
console.log('Fans off, temp should be <= 19.5');
mode = (parseFloat(this.livingRoom.temp) <= 19.5) ? 'FanOn' : 'FanOff';
} else {
console.log('Fans on, temp should not be less than 22.5');
mode = (parseFloat(this.livingRoom.temp) <= 22.5) ? 'FanOn' : 'FanOff';
}
var n = new Date();
n = n.getTime() - this.lastMsg;
console.log('Last msg', n);
if (n >= 600000) {
console.error('No message received for over 10 minutes');
mode = 'FanOff';
}
logger.info('LR temp:', this.livingRoom.temp);
busEmitter.emit('send', mode);
setTimeout(this.fanTimer.bind(this), mod + 10);
};
this.lighting = 'lights';
this.connected = false;
var options = {
keepalive: 3600,
clientId: 'a:' + orgId + ':' + Date.now(),
username: userName,
password: new Buffer(appKey)
};
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
this.client.on('connect', function () {
connected = true;
logger.info('Connected to SIlvr Broker');
this.fanTimer();
}.bind(this));
this.client.subscribe('livingroomTemp');
this.client.on('message', function (topic, message) {
var json = JSON.parse(message.toString());
logger.debug(json);
logger.debug(json.temp);
this.livingRoom.temp = parseFloat(json.temp);
if (this.livingRoom.temp >= 23) {
busEmitter.emit('send', 'FanOff');
}
var d = new Date();
this.lastMsg = d.getTime();
}.bind(this));
this.isConnected = function () {
return this.connected;
};
};
/*
Client = mqtt.connect();
client.subscribe('presence');
client.on('message', function(topic, message) {
console.log(message);
});
*/
mqttClient.prototype.lightsIn = function () {
var destinationName = 'lightsIn';
this.client.publish(destinationName, '0');
var destinationName = 'lightsIn';
this.client.publish(destinationName, '1');
};
mqttClient.prototype.lightsOut = function () {
var destinationName = 'lightsOut';
this.client.publish(destinationName, 'f');
destinationName = 'lightsOut';
this.client.publish(destinationName, 'g');
};
module.exports.mqttClient = mqttClient;