mqtt_server/lib/mqtt/mqttClient.js

150 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-06-22 15:07:56 +00:00
var mqtt = require('mqtt');
2016-06-24 13:44:21 +00:00
var request = require('request');
2016-06-28 14:02:11 +00:00
var util = require('util');
var logger = require('log4js').getLogger();
var EventEmitter = require('events');
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');
var busEmitter = new EventEmitter();
var 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-13 22:21:18 +00:00
var bus = Minibus.create();
2016-06-28 14:02:11 +00:00
2016-11-13 22:21:18 +00:00
bus.on('send', function (mode) {
var url = ['https://maker.ifttt.com/trigger/', mode, '/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
2016-06-28 14:02:11 +00:00
2016-11-13 22:21:18 +00:00
request(url, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
});
});
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
var mqttClient = function () {
2016-11-13 21:21:11 +00:00
2016-11-13 22:21:18 +00:00
var orgId = '';
var userName = '';
var appKey = 'bob';
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
this.livingRoom = {
temp: 0,
last: 0
};
var mode = 'FanOff';
2016-06-22 15:07:56 +00:00
2016-11-13 21:21:11 +00:00
2016-11-13 22:21:18 +00:00
var d = new Date();
this.lastMsg = d.getTime();
2016-11-13 21:21:11 +00:00
2016-11-13 22:21:18 +00:00
this.fanTimer = function () {
'use strict';
var now = new Date;
var mod = 900000 - (now.getTime() % 900000);
2016-11-13 21:21:11 +00:00
2016-11-13 22:21:18 +00:00
// var
2016-06-24 13:44:21 +00:00
2016-11-13 22:21:18 +00:00
if (mode === 'FanOff') {
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-13 22:21:18 +00:00
var n = new Date();
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-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-13 22:21:18 +00:00
bus.emit('send', mode);
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-13 22:21:18 +00:00
var options = {
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-13 22:21:18 +00:00
var json = JSON.parse(message.toString());
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-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
if (this.livingRoom.temp >= 23) {
bus.emit('send', 'FanOff');
}
var d = new Date();
this.lastMsg = d.getTime();
2016-06-24 13:44:21 +00:00
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
/*
2016-11-13 22:21:18 +00:00
Client = mqtt.connect();
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
client.subscribe('presence');
client.on('message', function(topic, message) {
console.log(message);
});
2016-06-22 15:07:56 +00:00
*/
2016-11-13 22:21:18 +00:00
mqttClient.prototype.lightsIn = function () {
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
var destinationName = 'lightsIn';
this.client.publish(destinationName, '0');
var destinationName = 'lightsIn';
this.client.publish(destinationName, '1');
};
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
mqttClient.prototype.lightsOut = function () {
2016-06-22 15:07:56 +00:00
2016-11-13 22:21:18 +00:00
var destinationName = 'lightsOut';
this.client.publish(destinationName, 'f');
destinationName = 'lightsOut';
this.client.publish(destinationName, 'g');
};
2016-06-22 15:07:56 +00:00
module.exports.mqttClient = mqttClient;