mqtt_server/lib/mqtt/mqttClient.js

137 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-11-23 19:19:03 +00:00
const mqtt = require('mqtt');
const request = require('request');
let util = require('util');
const logger = require('log4js').getLogger();
2016-06-28 14:02:11 +00:00
2016-11-23 19:19:03 +00:00
const EventEmitter = require('events');
let Minibus = require('minibus');
2016-06-28 14:02:11 +00:00
2016-11-13 22:21:18 +00:00
// var db = require('../server/db-connector').dbConnection;
2016-07-03 23:40:25 +00:00
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
2016-06-28 14:02:11 +00:00
//var nano = require('nano')('http://localhost:5984');
2016-11-23 19:19:03 +00:00
let db_name = 'mqtt';
2016-07-03 23:40:25 +00:00
//var dbCouch = nano.use(db_name);
2016-06-28 14:02:11 +00:00
2016-11-15 07:56:41 +00:00
var globalMode = 'FanOff';
2016-11-16 22:15:26 +00:00
var lastDispatch = 0;
2016-06-28 14:02:11 +00:00
2016-11-23 19:19:03 +00:00
const mqttClient = function (events) {
const orgId = '';
const userName = '';
const appKey = 'bob';
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
this.livingRoom = {
temp: 0,
last: 0
};
2016-11-23 19:19:03 +00:00
let mode = 'FanOff';
2016-11-13 21:21:11 +00:00
2016-11-23 19:19:03 +00:00
const d = new Date();
2016-11-13 22:21:18 +00:00
this.lastMsg = d.getTime();
2016-11-13 21:21:11 +00:00
2016-11-23 19:19:03 +00:00
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...');
}
});
2016-11-13 22:21:18 +00:00
this.fanTimer = function () {
'use strict';
2016-11-23 19:19:03 +00:00
const now = new Date;
const mod = 900000 - (now.getTime() % 900000);
2016-06-24 13:44:21 +00:00
2016-11-15 07:56:41 +00:00
if (globalMode === 'FanOff') {
2016-11-13 22:21:18 +00:00
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';
}
2016-06-24 13:44:21 +00:00
2016-11-23 19:19:03 +00:00
let n = new Date();
2016-11-13 22:21:18 +00:00
n = n.getTime() - this.lastMsg;
console.log('Last msg', n);
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
if (n >= 600000) {
console.error('No message received for over 10 minutes');
mode = 'FanOff';
2016-11-16 22:15:26 +00:00
throw new error('Ejecting for restart');
2016-11-13 22:21:18 +00:00
}
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
logger.info('LR temp:', this.livingRoom.temp);
2016-06-24 13:44:21 +00:00
2016-11-23 19:19:03 +00:00
events.emit('sendIFTTT', mode);
const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}};
2016-11-23 19:19:03 +00:00
events.emit('sendSocket', data);
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
setTimeout(this.fanTimer.bind(this), mod + 10);
};
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
this.lighting = 'lights';
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
this.connected = false;
2016-06-22 15:07:56 +00:00
2016-11-23 19:19:03 +00:00
const options = {
2016-11-13 22:21:18 +00:00
keepalive: 3600,
clientId: 'a:' + orgId + ':' + Date.now(),
username: userName,
password: new Buffer(appKey)
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
};
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
this.client.on('connect', function () {
connected = true;
logger.info('Connected to SIlvr Broker');
this.fanTimer();
}.bind(this));
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
this.client.subscribe('livingroomTemp');
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
this.client.on('message', function (topic, message) {
2016-06-24 13:44:21 +00:00
2016-11-23 19:19:03 +00:00
const json = JSON.parse(message.toString());
2016-11-13 22:21:18 +00:00
logger.debug(json);
logger.debug(json.temp);
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
this.livingRoom.temp = parseFloat(json.temp);
2016-11-23 19:19:03 +00:00
console.log('lr:',this.livingRoom.temp, this.livingRoom.temp >= 22.6);
2016-11-23 19:05:21 +00:00
if (this.livingRoom.temp >= 22.7) {
2016-11-23 19:19:03 +00:00
console.log('Max temp reached, turn off');
events.emit('sendIFTTT', 'FanOff');
2016-11-13 22:21:18 +00:00
}
2016-11-23 19:19:03 +00:00
const d = new Date();
2016-11-13 22:21:18 +00:00
this.lastMsg = d.getTime();
2016-06-24 13:44:21 +00:00
const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}};
2016-11-23 19:19:03 +00:00
events.emit('sendSocket', data);
2016-11-13 22:21:18 +00:00
}.bind(this));
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
this.isConnected = function () {
return this.connected;
};
2016-06-28 14:02:11 +00:00
2016-11-13 22:21:18 +00:00
};
2016-06-22 15:07:56 +00:00
module.exports.mqttClient = mqttClient;