Aida-Server/lib/recipes.js
2017-12-15 23:32:30 +00:00

206 lines
6.3 KiB
JavaScript

const em = require('events').EventEmitter;
const util = require('util');
const logger = require('log4js').getLogger('recipes');
logger.level = 'debug';
const BedroomHeaterV2 = function(devices) {
const _this = this;
this.mqtt = devices.mqtt;
this.wemo = devices.wemo;
this.globalMode = 'FanOff';
this.bedroom = {
'temp': 0,
'last': 0,
'data': []
};
this.ranges = {
'day': { 'low': 20.2, 'high': 22.5, 'max': 22.7 },
'night': { 'low': 20.2, 'high': 22.0, 'max': 22.7 }
};
this.startTimer = clearTimeout();
console.log('this.starttimer', this.startTimer);
this.setMode = function() {
let n;
let mode = 'FanOff';
const onTime = 900000;
const now = new Date;
const mod = onTime - (now.getTime() % onTime);
const day = now.getDay();
const daytimeLimits = { 'low': 27900000, 'high': 63000000 };
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
const curRange = (nowMS < 25200000) ? this.ranges.night : this.ranges.day;
if (this.globalMode === 'FanOff') {
logger.info(`Fans off, temp (${this.bedroom.temp}) should be <= ${curRange.low}`);
mode = (parseFloat(this.bedroom.temp) <= curRange.low) ? 'FanOn' : 'FanOff';
}
else {
logger.info(`Fans on, temp (${this.bedroom.temp}) should be less than ${curRange.high}`);
mode = (parseFloat(this.bedroom.temp) <= curRange.high) ? 'FanOn' : 'FanOff';
}
if ((this.globalMode !== 'FanOff' || mode !== 'FanOff') && ((day >= 1 && day <= 5) && (nowMS >= daytimeLimits.low && nowMS <= daytimeLimits.high))) {
logger.info('Week day, during the day');
mode = 'FanOff';
this.startTimer = clearTimeout();
}
if (!this.startTimer && mode === 'FanOn' && this.globalMode === 'FanOff') {
logger.debug('1 Minute timer started...');
this.startTimer = setTimeout((() => {
_this.wemo.emit('wemo', 'FanOn');
}).bind(_this), 60000);
}
if (this.bedroom.temp >= curRange.max && this.globalMode == 'FanOn') {
this.startTimer = clearTimeout();
logger.warn('Max temp reached, turn off');
_this.wemo.emit('wemo', 'FanOff');
}
};
const updateTemp = (temp) => {
logger.debug(temp);
const now = new Date;
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
const curRange = (nowMS < 25200000) ? this.ranges.night : this.ranges.day;
this.bedroom.temp = parseFloat(temp);
this.setMode();
// logger.info(this.bedroom.temp, this.bedroom.temp >= curRange.max);
/* if (this.bedroom.temp >= curRange.max && this.globalMode == 'FanOn') {
this.startTimer = clearTimeout();
logger.warn('Max temp reached, turn off');
_this.wemo.emit('wemo', 'fanOff');
}*/
};
this.mqtt.on('bedroomTemp', (d) => {
updateTemp(d);
});
this.wemo.on('changeState', (d) => {
logger.debug('changeState', d);
logger.debug('fix', ((parseInt(d, 10) === 0) ? 'FanOff' : 'FanOn'));
this.globalMode = (parseInt(d, 10) === 0) ? 'FanOff' : 'FanOn';
logger.debug('this.globalMode', this.globalMode);
});
};
const BedroomRecipe = function(devices) {
const _this = this;
this.mqtt = devices.mqtt;
this.wemo = devices.wemo;
this.globalMode = 'FanOff';
this.bedroom = {
'temp': 0,
'last': 0,
'data': []
};
this.ranges = {
'day': { 'low': 20.2, 'high': 22.0, 'max': 22.7 },
'night': { 'low': 20.2, 'high': 22.0, 'max': 22.7 }
};
logger.debug('Setting up bedroom recipe');
this.fanTimer = function() {
let n;
let mode = 'FanOff';
const onTime = 900000;
const now = new Date;
const mod = onTime - (now.getTime() % onTime);
const day = now.getDay();
const daytimeLimits = { 'low': 27900000, 'high': 63000000 };
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
const curRange = (nowMS < 25200000) ? this.ranges.night : this.ranges.day;
if (this.globalMode === 'FanOff') {
logger.info(`Fans off, temp (${this.bedroom.temp}) should be <= ${curRange.low}`);
mode = (parseFloat(this.bedroom.temp) <= curRange.low) ? 'FanOn' : 'FanOff';
}
else {
logger.info(`Fans on, temp (${this.bedroom.temp}) should not be less than ${curRange.high}`);
mode = (parseFloat(this.bedroom.temp) <= curRange.high) ? 'FanOn' : 'FanOff';
}
if ((this.globalMode !== 'FanOff' || mode !== 'FanOff') && ((day >= 1 && day <= 5) && (nowMS >= daytimeLimits.low && nowMS <= daytimeLimits.high))) {
logger.info('Week day, during the day');
mode = 'FanOff';
}
/* if (n >= 600000) {
logger.error('No message received for over 10 minutes');
mode = 'FanOff';
logger.warn('Setting quit for 15 seconds.');
setTimeout(() => {
throw new error('Ejecting for restart');
}, 15000);
}*/
// const data = { 'id': 'temperature', 'data': { 'mode': this.globalMode, 'temp': this.bedroom.temp } };
if (this.bedroom.temp !== 0)
_this.wemo.emit('wemo', mode);
setTimeout(this.fanTimer.bind(this), mod + 500);
};
const updateTemp = (temp) => {
const now = new Date;
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
const curRange = (nowMS < 25200000) ? this.ranges.night : this.ranges.day;
this.bedroom.temp = parseFloat(temp);
// logger.info(this.bedroom.temp, this.bedroom.temp >= curRange.max);
if (this.bedroom.temp >= curRange.max) {
logger.warn('Max temp reached, turn off');
_this.wemo.emit('wemo', 'FanOff');
}
};
this.mqtt.on('bedroomTemp', (d) => {
updateTemp(d);
});
setTimeout(this.fanTimer.bind(this), 10000);
};
const BedroomLightMorning = function(devices) {
const _this = this;
this.lights = devices.lights;
this.lights.on('Bedroom', (d) => {
const now = new Date;
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
const dimLight = (nowMS < 28800000);
const device = {
'id': d.id,
'color' : { 'hue': 0, 'saturation': 0, 'brightness': 25, 'kelvin': 3200 }
};
if (dimLight && d.status === 'on') {
logger.info('Too bright, dim it..');
_this.lights.emit('setColour', device);
}
});
// Bedroom on
};
util.inherits(BedroomRecipe, em);
util.inherits(BedroomLightMorning, em);
util.inherits(BedroomHeaterV2, em);
module.exports = { BedroomRecipe, BedroomLightMorning, BedroomHeaterV2 };