mqtt_server/lib/mqtt/mqttClient.js
2016-11-23 20:56:11 +00:00

137 lines
3.4 KiB
JavaScript

const mqtt = require('mqtt');
const request = require('request');
let util = require('util');
const logger = require('log4js').getLogger();
const EventEmitter = require('events');
let 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');
let db_name = 'mqtt';
//var dbCouch = nano.use(db_name);
var globalMode = 'FanOff';
var lastDispatch = 0;
const mqttClient = function (events) {
const orgId = '';
const userName = '';
const appKey = 'bob';
this.livingRoom = {
temp: 0,
last: 0
};
let mode = 'FanOff';
const d = new Date();
this.lastMsg = d.getTime();
events.on('sendIFTTT', function (mode) {
const url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
if (mode !== globalMode)
{
console.log('Sending..');
globalMode = mode;
request(url, function (error, response, body) {
// if (!error && response.statusCode === 200) {
console.log(response, body);
// }
});
var d = new Date();
lastDispatch = d.getTime();
} else {
console.log('Not sent...');
}
});
this.fanTimer = function () {
'use strict';
const now = new Date;
const mod = 900000 - (now.getTime() % 900000);
if (globalMode === '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';
}
let 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';
throw new error('Ejecting for restart');
}
logger.info('LR temp:', this.livingRoom.temp);
events.emit('sendIFTTT', mode);
const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}};
events.emit('sendSocket', data);
setTimeout(this.fanTimer.bind(this), mod + 10);
};
this.lighting = 'lights';
this.connected = false;
const 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) {
const json = JSON.parse(message.toString());
logger.debug(json);
logger.debug(json.temp);
this.livingRoom.temp = parseFloat(json.temp);
console.log('lr:',this.livingRoom.temp, this.livingRoom.temp >= 22.6);
if (this.livingRoom.temp >= 22.7) {
console.log('Max temp reached, turn off');
events.emit('sendIFTTT', 'FanOff');
}
const d = new Date();
this.lastMsg = d.getTime();
const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}};
events.emit('sendSocket', data);
}.bind(this));
this.isConnected = function () {
return this.connected;
};
};
module.exports.mqttClient = mqttClient;