mqtt_server/lib/mqtt/mqttClient.js

199 lines
5.5 KiB
JavaScript
Raw Permalink Normal View History

2017-01-09 22:46:39 +00:00
'use strict';
const mqtt = require('mqtt');
const request = require('request');
2017-11-08 15:52:59 +00:00
const util = require('util');
2016-11-23 19:19:03 +00:00
const logger = require('log4js').getLogger();
2016-06-28 14:02:11 +00:00
2017-01-07 18:21:24 +00:00
// Var db = require('../server/db-connector').dbConnection;
2016-11-13 22:21:18 +00:00
// 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);
2016-06-28 14:02:11 +00:00
let globalMode = 'FanOff';
2016-06-28 14:02:11 +00:00
let lastDispatch = 0;
2016-06-28 14:02:11 +00:00
2017-01-07 18:21:24 +00:00
const mqttClient = function(events) {
2016-11-23 19:19:03 +00:00
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 = {
2017-11-08 15:52:59 +00:00
'temp': 0,
'last': 0,
'data': []
2016-11-13 22:21:18 +00:00
};
2017-01-15 00:03:24 +00:00
this.ranges = {
2017-11-08 15:52:59 +00:00
'day': { 'low': 19.5, 'high': 22.5, 'max': 22.7 },
'night': { 'low': 18.5, 'high': 20.5, 'max': 20.7 }
2017-01-15 00:03:24 +00:00
};
2017-01-06 16:59:39 +00:00
this.first = false;
2017-01-08 01:18:17 +00:00
this.maxLength = 540;
2017-01-06 16:59:39 +00:00
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
2017-01-09 22:46:39 +00:00
events.on('sendIFTTT', function(mode) {
let d;
2016-11-23 19:19:03 +00:00
const url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
if (mode !== globalMode) {
2017-02-28 23:49:17 +00:00
logger.info('Go for change state', mode);
globalMode = mode;
2017-01-07 22:48:51 +00:00
logger.debug('Global Mode now A:', globalMode);
2017-11-08 15:52:59 +00:00
2017-01-07 18:21:24 +00:00
/* Request(url, function (error, response, body) {
logger.debug(response, body);
2017-01-07 18:21:24 +00:00
}); */
2017-11-08 15:52:59 +00:00
events.emit('changeState', mode === 'FanOn' ? 1 : 0);
d = new Date();
2016-11-23 19:19:03 +00:00
lastDispatch = d.getTime();
}
2017-11-08 15:52:59 +00:00
else
logger.warn('Not sent...');
});
2016-11-23 19:19:03 +00:00
2017-02-28 23:02:52 +00:00
this.updateGlobal = function(newStatus) {
2017-11-08 15:52:59 +00:00
globalMode = (newStatus === 0) ? 'FanOff' : 'FanOn';
logger.info('updateGlobal changed to: ', globalMode);
};
2017-02-28 23:02:52 +00:00
2017-01-07 18:21:24 +00:00
this.storeData = function(data, alt) {
2017-01-06 16:59:39 +00:00
if (!this.first) {
this.first = true;
2017-11-08 15:52:59 +00:00
2017-01-06 16:59:39 +00:00
return [];
}
let target = alt || this.livingRoom.data;
2017-11-08 15:52:59 +00:00
if (target.length === this.maxLength)
2017-01-06 16:59:39 +00:00
target = target.slice(1);
2017-11-08 15:52:59 +00:00
2017-01-06 16:59:39 +00:00
target.push(data);
2017-11-08 15:52:59 +00:00
if (alt)
2017-01-06 16:59:39 +00:00
return target;
2017-11-08 15:52:59 +00:00
2017-01-06 16:59:39 +00:00
this.livingRoom.data = target;
};
2017-01-07 18:21:24 +00:00
this.logTemp = function() {
2017-01-06 16:59:39 +00:00
const now = new Date;
const mod = 60000 - (now.getTime() % 60000);
this.storeData(this.livingRoom.temp);
2017-11-08 15:52:59 +00:00
const data = { 'id': 'graph', 'data': this.livingRoom.data };
2017-01-06 16:59:39 +00:00
events.emit('sendSocket', data);
setTimeout(this.logTemp.bind(this), mod + 10);
};
2017-01-07 18:21:24 +00:00
this.fanTimer = function() {
let n;
2016-11-23 19:19:03 +00:00
const now = new Date;
2017-01-07 22:58:18 +00:00
const mod = 900000 - (now.getTime() % 900000);
const day = now.getDay();
2017-11-08 15:52:59 +00:00
const daytimeLimits = { 'low': 27900000, 'high': 63000000 };
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
2017-01-15 00:03:24 +00:00
const curRange = (nowMS < 25200000) ? this.ranges.night : this.ranges.day;
2016-06-24 13:44:21 +00:00
2016-11-15 07:56:41 +00:00
if (globalMode === 'FanOff') {
2017-01-15 00:03:24 +00:00
logger.info(`Fans off, temp should be <= ${curRange.low}`);
mode = (parseFloat(this.livingRoom.temp) <= curRange.low) ? 'FanOn' : 'FanOff';
2017-11-08 15:52:59 +00:00
}
else {
2017-01-15 00:03:24 +00:00
logger.info(`Fans on, temp should not be less than ${curRange.high}`);
mode = (parseFloat(this.livingRoom.temp) <= curRange.high) ? 'FanOn' : 'FanOff';
2016-11-13 22:21:18 +00:00
}
2016-06-24 13:44:21 +00:00
2017-01-06 16:59:39 +00:00
if ((globalMode !== 'FanOff' || mode !== 'FanOff') && ((day >= 1 && day <= 5) && (nowMS >= daytimeLimits.low && nowMS <= daytimeLimits.high))) {
logger.info('Week day');
2017-11-08 15:52:59 +00:00
mode = 'FanOff';
}
n = now.getTime() - this.lastMsg;
logger.info('Last msg', n);
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
if (n >= 600000) {
logger.error('No message received for over 10 minutes');
2016-11-13 22:21:18 +00:00
mode = 'FanOff';
logger.warn('Setting quit for 15 seconds.');
2017-01-06 16:59:39 +00:00
setTimeout(() => {
throw new error('Ejecting for restart');
}, 15000);
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);
2017-11-08 15:52:59 +00:00
const data = { 'id': 'temperature', 'data': { 'mode': globalMode, 'temp': this.livingRoom.temp } };
2016-06-24 13:44:21 +00:00
2017-01-15 00:03:24 +00:00
if (this.livingRoom.temp !== 0) {
2017-01-07 22:51:57 +00:00
events.emit('sendIFTTT', mode);
2017-01-15 00:03:24 +00:00
events.emit('sendSocket', data);
2017-01-07 22:51:57 +00:00
}
2017-01-15 00:03:24 +00:00
setTimeout(this.fanTimer.bind(this), mod + 500);
2016-11-13 22:21:18 +00:00
};
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 = {
2017-11-08 15:52:59 +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
2017-11-08 15:52:59 +00:00
//this.client = mqtt.connect(`mqtt://${ orgId }silvrtree.co.uk`, options);
this.client = mqtt.connect(`mqtt://192.16.1.150`, options);
2016-06-22 15:07:56 +00:00
2017-01-09 22:46:39 +00:00
this.client.on('connect', function() {
this.connected = true;
2017-11-08 15:52:59 +00:00
logger.info('Connected to Aida Broker');
2016-11-13 22:21:18 +00:00
this.fanTimer();
2017-01-06 16:59:39 +00:00
this.logTemp();
2017-01-09 22:46:39 +00:00
}.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
2017-02-28 22:31:04 +00:00
this.client.on('updateState', function(newStatus) {
globalMode = (newStatus === 0) ? 'FanOff' : 'FanOn';
2017-02-28 22:34:35 +00:00
logger.info('updateState changed to: ', globalMode);
2017-02-28 22:31:04 +00:00
});
2017-01-09 22:46:39 +00:00
this.client.on('message', function(topic, message) {
2017-01-15 00:03:24 +00:00
const now = new Date;
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
const curRange = (nowMS < 25200000) ? this.ranges.night : this.ranges.day;
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);
2017-01-15 00:03:24 +00:00
logger.info('lr:', this.livingRoom.temp, this.livingRoom.temp >= curRange.max);
2017-01-15 00:03:24 +00:00
if (this.livingRoom.temp >= curRange.max) {
logger.warn('Max temp reached, turn off');
2016-11-23 19:19:03 +00:00
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
2017-11-08 15:52:59 +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);
2017-01-09 22:46:39 +00:00
}.bind(this));
2016-06-24 13:44:21 +00:00
2017-01-07 18:21:24 +00:00
this.isConnected = function() {
2016-11-13 22:21:18 +00:00
return this.connected;
};
};
2016-06-22 15:07:56 +00:00
2017-01-09 22:46:39 +00:00
module.exports.mqttClient = mqttClient;