changed mqtt server address
This commit is contained in:
parent
353cc61416
commit
27b76026ce
@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const mqtt = require('mqtt');
|
const mqtt = require('mqtt');
|
||||||
const request = require('request');
|
const request = require('request');
|
||||||
let util = require('util');
|
const util = require('util');
|
||||||
const logger = require('log4js').getLogger();
|
const logger = require('log4js').getLogger();
|
||||||
|
|
||||||
// Var db = require('../server/db-connector').dbConnection;
|
// Var db = require('../server/db-connector').dbConnection;
|
||||||
@ -16,21 +16,19 @@ let globalMode = 'FanOff';
|
|||||||
|
|
||||||
let lastDispatch = 0;
|
let lastDispatch = 0;
|
||||||
|
|
||||||
|
|
||||||
const mqttClient = function(events) {
|
const mqttClient = function(events) {
|
||||||
|
|
||||||
const orgId = '';
|
const orgId = '';
|
||||||
const userName = '';
|
const userName = '';
|
||||||
const appKey = 'bob';
|
const appKey = 'bob';
|
||||||
|
|
||||||
this.livingRoom = {
|
this.livingRoom = {
|
||||||
temp: 0,
|
'temp': 0,
|
||||||
last: 0,
|
'last': 0,
|
||||||
data: []
|
'data': []
|
||||||
};
|
};
|
||||||
this.ranges = {
|
this.ranges = {
|
||||||
day: {low: 19.5, high: 22.5, max: 22.7},
|
'day': { 'low': 19.5, 'high': 22.5, 'max': 22.7 },
|
||||||
night: {low: 18.5, high: 20.5, max: 20.7}
|
'night': { 'low': 18.5, 'high': 20.5, 'max': 20.7 }
|
||||||
};
|
};
|
||||||
|
|
||||||
this.first = false;
|
this.first = false;
|
||||||
@ -49,37 +47,39 @@ const mqttClient = function(events) {
|
|||||||
logger.info('Go for change state', mode);
|
logger.info('Go for change state', mode);
|
||||||
globalMode = mode;
|
globalMode = mode;
|
||||||
logger.debug('Global Mode now A:', globalMode);
|
logger.debug('Global Mode now A:', globalMode);
|
||||||
|
|
||||||
/* Request(url, function (error, response, body) {
|
/* Request(url, function (error, response, body) {
|
||||||
logger.debug(response, body);
|
logger.debug(response, body);
|
||||||
}); */
|
}); */
|
||||||
|
|
||||||
events.emit('changeState', mode === 'FanOn' ? 1:0);
|
events.emit('changeState', mode === 'FanOn' ? 1 : 0);
|
||||||
|
|
||||||
d = new Date();
|
d = new Date();
|
||||||
lastDispatch = d.getTime();
|
lastDispatch = d.getTime();
|
||||||
} else {
|
|
||||||
logger.warn('Not sent...');
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
logger.warn('Not sent...');
|
||||||
});
|
});
|
||||||
|
|
||||||
this.updateGlobal = function(newStatus) {
|
this.updateGlobal = function(newStatus) {
|
||||||
globalMode = (newStatus === 0) ? 'FanOff' : 'FanOn';
|
globalMode = (newStatus === 0) ? 'FanOff' : 'FanOn';
|
||||||
logger.info('updateGlobal changed to: ', globalMode);
|
logger.info('updateGlobal changed to: ', globalMode);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.storeData = function(data, alt) {
|
this.storeData = function(data, alt) {
|
||||||
if (!this.first) {
|
if (!this.first) {
|
||||||
this.first = true;
|
this.first = true;
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
let target = alt || this.livingRoom.data;
|
let target = alt || this.livingRoom.data;
|
||||||
if (target.length === this.maxLength) {
|
if (target.length === this.maxLength)
|
||||||
target = target.slice(1);
|
target = target.slice(1);
|
||||||
}
|
|
||||||
target.push(data);
|
target.push(data);
|
||||||
if (alt) {
|
if (alt)
|
||||||
return target;
|
return target;
|
||||||
}
|
|
||||||
this.livingRoom.data = target;
|
this.livingRoom.data = target;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -87,33 +87,33 @@ const mqttClient = function(events) {
|
|||||||
const now = new Date;
|
const now = new Date;
|
||||||
const mod = 60000 - (now.getTime() % 60000);
|
const mod = 60000 - (now.getTime() % 60000);
|
||||||
this.storeData(this.livingRoom.temp);
|
this.storeData(this.livingRoom.temp);
|
||||||
const data = {id: 'graph', data: this.livingRoom.data};
|
const data = { 'id': 'graph', 'data': this.livingRoom.data };
|
||||||
events.emit('sendSocket', data);
|
events.emit('sendSocket', data);
|
||||||
|
|
||||||
setTimeout(this.logTemp.bind(this), mod + 10);
|
setTimeout(this.logTemp.bind(this), mod + 10);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.fanTimer = function() {
|
this.fanTimer = function() {
|
||||||
|
|
||||||
let n;
|
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 day = now.getDay();
|
||||||
const daytimeLimits = {low: 27900000, high: 63000000};
|
const daytimeLimits = { 'low': 27900000, 'high': 63000000 };
|
||||||
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
|
const nowMS = (now.getHours() * 3600000) + (now.getMinutes() * 60000);
|
||||||
const curRange = (nowMS < 25200000) ? this.ranges.night : this.ranges.day;
|
const curRange = (nowMS < 25200000) ? this.ranges.night : this.ranges.day;
|
||||||
|
|
||||||
if (globalMode === 'FanOff') {
|
if (globalMode === 'FanOff') {
|
||||||
logger.info(`Fans off, temp should be <= ${curRange.low}`);
|
logger.info(`Fans off, temp should be <= ${curRange.low}`);
|
||||||
mode = (parseFloat(this.livingRoom.temp) <= curRange.low) ? 'FanOn' : 'FanOff';
|
mode = (parseFloat(this.livingRoom.temp) <= curRange.low) ? 'FanOn' : 'FanOff';
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
logger.info(`Fans on, temp should not be less than ${curRange.high}`);
|
logger.info(`Fans on, temp should not be less than ${curRange.high}`);
|
||||||
mode = (parseFloat(this.livingRoom.temp) <= curRange.high) ? 'FanOn' : 'FanOff';
|
mode = (parseFloat(this.livingRoom.temp) <= curRange.high) ? 'FanOn' : 'FanOff';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((globalMode !== 'FanOff' || mode !== 'FanOff') && ((day >= 1 && day <= 5) && (nowMS >= daytimeLimits.low && nowMS <= daytimeLimits.high))) {
|
if ((globalMode !== 'FanOff' || mode !== 'FanOff') && ((day >= 1 && day <= 5) && (nowMS >= daytimeLimits.low && nowMS <= daytimeLimits.high))) {
|
||||||
logger.info('Week day');
|
logger.info('Week day');
|
||||||
mode = 'FanOff'
|
mode = 'FanOff';
|
||||||
}
|
}
|
||||||
|
|
||||||
n = now.getTime() - this.lastMsg;
|
n = now.getTime() - this.lastMsg;
|
||||||
@ -126,18 +126,16 @@ const mqttClient = function(events) {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
throw new error('Ejecting for restart');
|
throw new error('Ejecting for restart');
|
||||||
}, 15000);
|
}, 15000);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info('LR temp:', this.livingRoom.temp);
|
logger.info('LR temp:', this.livingRoom.temp);
|
||||||
const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}};
|
const data = { 'id': 'temperature', 'data': { 'mode': globalMode, 'temp': this.livingRoom.temp } };
|
||||||
|
|
||||||
if (this.livingRoom.temp !== 0) {
|
if (this.livingRoom.temp !== 0) {
|
||||||
events.emit('sendIFTTT', mode);
|
events.emit('sendIFTTT', mode);
|
||||||
events.emit('sendSocket', data);
|
events.emit('sendSocket', data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
setTimeout(this.fanTimer.bind(this), mod + 500);
|
setTimeout(this.fanTimer.bind(this), mod + 500);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -146,18 +144,19 @@ const mqttClient = function(events) {
|
|||||||
this.connected = false;
|
this.connected = false;
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
keepalive: 3600,
|
'keepalive': 3600,
|
||||||
clientId: 'a:' + orgId + ':' + Date.now(),
|
'clientId': `a:${ orgId }:${ Date.now()}`,
|
||||||
username: userName,
|
'username': userName,
|
||||||
password: new Buffer(appKey)
|
'password': new Buffer(appKey)
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
|
//this.client = mqtt.connect(`mqtt://${ orgId }silvrtree.co.uk`, options);
|
||||||
|
this.client = mqtt.connect(`mqtt://192.16.1.150`, options);
|
||||||
|
|
||||||
this.client.on('connect', function() {
|
this.client.on('connect', function() {
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
logger.info('Connected to Silvr Broker');
|
logger.info('Connected to Aida Broker');
|
||||||
this.fanTimer();
|
this.fanTimer();
|
||||||
this.logTemp();
|
this.logTemp();
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
@ -187,15 +186,13 @@ const mqttClient = function(events) {
|
|||||||
const d = new Date();
|
const d = new Date();
|
||||||
this.lastMsg = d.getTime();
|
this.lastMsg = d.getTime();
|
||||||
|
|
||||||
const data = {id: 'temperature', data: {mode: globalMode, temp: this.livingRoom.temp}};
|
const data = { 'id': 'temperature', 'data': { 'mode': globalMode, 'temp': this.livingRoom.temp } };
|
||||||
events.emit('sendSocket', data);
|
events.emit('sendSocket', data);
|
||||||
|
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
this.isConnected = function() {
|
this.isConnected = function() {
|
||||||
return this.connected;
|
return this.connected;
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.mqttClient = mqttClient;
|
module.exports.mqttClient = mqttClient;
|
||||||
|
Loading…
Reference in New Issue
Block a user