mqtt_server/lib/mqtt/mqttClient.js

120 lines
2.6 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-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-06-22 15:07:56 +00:00
var mqttClient = function() {
var orgId = '';
var userName = '';
var appKey = 'bob';
2016-06-24 13:44:21 +00:00
this.livingRoom = {
temp: 0,
last: 0
};
2016-06-22 15:07:56 +00:00
2016-06-24 13:44:21 +00:00
this.fanTimer = function() {
'use strict';
var now = new Date;
var mod = 300000 - (now.getTime() % 300000);
var mode = (this.livingRoom.temp > 22.0) ? 'FanOn' : 'FanOff';
var url = ['https://maker.ifttt.com/trigger/',mode,'/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
2016-06-28 14:02:11 +00:00
logger.info('LR temp:', this.livingRoom.temp);
2016-06-24 13:44:21 +00:00
request(url, function(error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
});
setTimeout(this.fanTimer.bind(this),mod + 10);
};
2016-06-22 15:07:56 +00:00
this.lighting = 'lights';
this.connected = false;
var options = {
keepalive: 3600,
clientId: 'a:' + orgId + ':' + Date.now(),
username: userName,
password: new Buffer(appKey)
};
this.client = mqtt.connect('mqtt://' + orgId + 'silvrtree.co.uk', options);
this.client.on('connect', function() {
connected = true;
2016-06-28 14:02:11 +00:00
logger.info('Connected to SIlvr Broker');
2016-06-24 13:44:21 +00:00
this.fanTimer();
}.bind(this));
2016-06-22 15:07:56 +00:00
2016-06-24 13:44:21 +00:00
this.client.subscribe('livingroomTemp');
2016-06-22 15:07:56 +00:00
this.client.on('message', function(topic, message) {
2016-06-24 13:44:21 +00:00
var json = JSON.parse(message.toString());
2016-06-28 14:02:11 +00:00
logger.debug(json);
logger.debug(json.temp);
2016-06-24 13:44:21 +00:00
this.livingRoom.temp = json.temp;
2016-07-03 23:40:25 +00:00
}.bind(this));
2016-06-22 15:07:56 +00:00
this.isConnected = function() {
return this.connected;
};
2016-06-24 13:44:21 +00:00
2016-06-28 14:02:11 +00:00
2016-06-22 15:07:56 +00:00
};
/*
Client = mqtt.connect();
client.subscribe('presence');
client.on('message', function(topic, message) {
console.log(message);
});
*/
mqttClient.prototype.lightsIn = function() {
var destinationName = 'lightsIn';
this.client.publish(destinationName, '0');
var destinationName = 'lightsIn';
this.client.publish(destinationName, '1');
};
mqttClient.prototype.lightsOut = function() {
var destinationName = 'lightsOut';
this.client.publish(destinationName, 'f');
destinationName = 'lightsOut';
this.client.publish(destinationName, 'g');
};
module.exports.mqttClient = mqttClient;