put some time limiting stuff in and tidied up

This commit is contained in:
martind2000 2017-01-05 13:53:42 +00:00
parent daf022614a
commit 0d7099f22d
6 changed files with 46 additions and 37 deletions

View File

@ -1,21 +1,20 @@
'use strict';
const mqtt = require('mqtt'); const mqtt = require('mqtt');
const request = require('request'); const request = require('request');
let util = require('util'); let util = require('util');
const logger = require('log4js').getLogger(); const logger = require('log4js').getLogger();
const EventEmitter = require('events');
let Minibus = require('minibus');
// var db = require('../server/db-connector').dbConnection; // var db = require('../server/db-connector').dbConnection;
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984'); // var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
//var nano = require('nano')('http://localhost:5984'); // var nano = require('nano')('http://localhost:5984');
let db_name = 'mqtt'; // let db_name = 'mqtt';
//var dbCouch = nano.use(db_name); // var dbCouch = nano.use(db_name);
var globalMode = 'FanOff'; let globalMode = 'FanOff';
var lastDispatch = 0;
let lastDispatch = 0;
const mqttClient = function (events) { const mqttClient = function (events) {
@ -34,46 +33,54 @@ const mqttClient = function (events) {
this.lastMsg = d.getTime(); this.lastMsg = d.getTime();
events.on('sendIFTTT', function (mode) { events.on('sendIFTTT', function (mode) {
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('');
if (mode !== globalMode) if (mode !== globalMode) {
{ logger.info('Sending..');
console.log('Sending..'); globalMode = mode;
globalMode = mode; request(url, function (error, response, body) {
request(url, function (error, response, body) { logger.debug(response, body);
// if (!error && response.statusCode === 200) {
console.log(response, body);
// }
});
var d = new Date();
lastDispatch = d.getTime();
} else {
console.log('Not sent...');
}
}); });
d = new Date();
lastDispatch = d.getTime();
} else {
logger.warn('Not sent...');
}
});
this.fanTimer = function () { this.fanTimer = function () {
'use strict';
let n;
const now = new Date; const now = new Date;
const mod = 900000 - (now.getTime() % 900000); const mod = 900000 - (now.getTime() % 900000);
const day = now.getDay();
const daytimeLimits = {low:27900000,high:63000000};
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
if (globalMode === 'FanOff') { if (globalMode === 'FanOff') {
console.log('Fans off, temp should be <= 19.5'); logger.info('Fans off, temp should be <= 19.5');
mode = (parseFloat(this.livingRoom.temp) <= 19.5) ? 'FanOn' : 'FanOff'; mode = (parseFloat(this.livingRoom.temp) <= 19.5) ? 'FanOn' : 'FanOff';
} else { } else {
console.log('Fans on, temp should not be less than 22.5'); logger.info('Fans on, temp should not be less than 22.5');
mode = (parseFloat(this.livingRoom.temp) <= 22.5) ? 'FanOn' : 'FanOff'; mode = (parseFloat(this.livingRoom.temp) <= 22.5) ? 'FanOn' : 'FanOff';
} }
let n = new Date(); if ((globalMode !== 'FanOff' || mode !== 'FanOff') && ((day >=1 && day <=5) && (nowMS >= daytimeLimits.low && nowMS <= daytimeLimits.high))) {
n = n.getTime() - this.lastMsg; logger.info('Week day');
console.log('Last msg', n); mode = 'FanOff'
}
n = now.getTime() - this.lastMsg;
logger.info('Last msg', n);
if (n >= 600000) { if (n >= 600000) {
console.error('No message received for over 10 minutes'); logger.error('No message received for over 10 minutes');
mode = 'FanOff'; mode = 'FanOff';
throw new error('Ejecting for restart'); logger.warn('Setting quit for 15 seconds.');
setTimeout(()=>{throw new error('Ejecting for restart');}, 15000);
} }
logger.info('LR temp:', this.livingRoom.temp); logger.info('LR temp:', this.livingRoom.temp);
@ -100,8 +107,8 @@ const mqttClient = function (events) {
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', function () {
connected = true; this.connected = true;
logger.info('Connected to SIlvr Broker'); logger.info('Connected to Silvr Broker');
this.fanTimer(); this.fanTimer();
}.bind(this)); }.bind(this));
@ -114,9 +121,10 @@ const mqttClient = function (events) {
logger.debug(json.temp); logger.debug(json.temp);
this.livingRoom.temp = parseFloat(json.temp); this.livingRoom.temp = parseFloat(json.temp);
console.log('lr:',this.livingRoom.temp, this.livingRoom.temp >= 22.6); logger.info('lr:', this.livingRoom.temp, this.livingRoom.temp >= 22.6);
if (this.livingRoom.temp >= 22.7) { if (this.livingRoom.temp >= 22.7) {
console.log('Max temp reached, turn off'); logger.warn('Max temp reached, turn off');
events.emit('sendIFTTT', 'FanOff'); events.emit('sendIFTTT', 'FanOff');
} }
const d = new Date(); const d = new Date();

View File

View File

@ -36,7 +36,8 @@
"sugar-date": "^2.0.2", "sugar-date": "^2.0.2",
"uuid-pure": "^1.0.10", "uuid-pure": "^1.0.10",
"websocket": "^1.0.23", "websocket": "^1.0.23",
"ws": "^1.1.1" "ws": "^1.1.1",
"better-npm-run": "0.0.13"
}, },
"devDependencies": { "devDependencies": {
"after": "^0.8.1", "after": "^0.8.1",