added couch logging

This commit is contained in:
Martin Donnelly 2016-06-28 15:02:11 +01:00
parent da39b810b6
commit 199a5d1cd5
3 changed files with 65 additions and 4 deletions

View File

@ -1,5 +1,38 @@
var mqtt = require('mqtt'); var mqtt = require('mqtt');
var request = require('request'); var request = require('request');
var util = require('util');
var logger = require('log4js').getLogger();
var EventEmitter = require('events');
var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
//var nano = require('nano')('http://localhost:5984');
var busEmitter = new EventEmitter();
var db_name = 'mqtt';
var dbCouch = nano.use(db_name);
function insertEntry(obj) {
logger.debug('Inserting into couch...');
logger.info(util.inspect(obj));
dbCouch.insert(obj, function(err, body, header) {
if (err) {
logger.error('Error inserting into couch');
return;
}
});
logger.debug('Insert done..');
}
var doInsertEntry = (obj) => {
// Logger.info('sendSocket: ' + JSON.stringify(obj));
insertEntry(obj);
};
var mqttClient = function() { var mqttClient = function() {
@ -21,7 +54,7 @@ var mqttClient = function() {
var url = ['https://maker.ifttt.com/trigger/',mode,'/with/key/cWvECkeiyAPwmiOPBkXL2D'].join(''); var url = ['https://maker.ifttt.com/trigger/',mode,'/with/key/cWvECkeiyAPwmiOPBkXL2D'].join('');
console.log('LR temp:', this.livingRoom.temp); logger.info('LR temp:', this.livingRoom.temp);
request(url, function(error, response, body) { request(url, function(error, response, body) {
if (!error && response.statusCode === 200) { if (!error && response.statusCode === 200) {
@ -49,7 +82,7 @@ var mqttClient = function() {
this.client.on('connect', function() { this.client.on('connect', function() {
connected = true; connected = true;
console.log('Connected to SIlvr Broker'); logger.info('Connected to SIlvr Broker');
this.fanTimer(); this.fanTimer();
}.bind(this)); }.bind(this));
@ -57,11 +90,13 @@ var mqttClient = function() {
this.client.on('message', function(topic, message) { this.client.on('message', function(topic, message) {
var json = JSON.parse(message.toString()); var json = JSON.parse(message.toString());
console.log(json); logger.debug(json);
console.log(json.temp); logger.debug(json.temp);
this.livingRoom.temp = json.temp; this.livingRoom.temp = json.temp;
busEmitter.emit('saveData', json);
}.bind(this)); }.bind(this));
this.isConnected = function() { this.isConnected = function() {
@ -69,6 +104,10 @@ var mqttClient = function() {
}; };
busEmitter.on('saveData', doInsertEntry);
}; };
/* /*

20
maker.js Normal file
View File

@ -0,0 +1,20 @@
/**
*
* User: Martin Donnelly
* Date: 2016-06-28
* Time: 14:50
*
*/
var nano = require('nano')('http://localhost:5984');
var db_name = 'mqtt';
// Clean up the database we created previously
nano.db.destroy(db_name, function() {
// Create a new database
nano.db.create(db_name, function() {
// Specify the database we are going to use
var newDB = nano.use(db_name);
});
});

View File

@ -33,6 +33,7 @@
"clone": "^1.0.2", "clone": "^1.0.2",
"del": "^2.2.0", "del": "^2.2.0",
"elapsed": "0.0.7", "elapsed": "0.0.7",
"events": "^1.1.1",
"gulp": "^3.9.1", "gulp": "^3.9.1",
"gulp-autoprefixer": "^3.1.0", "gulp-autoprefixer": "^3.1.0",
"gulp-cache": "^0.4.5", "gulp-cache": "^0.4.5",
@ -57,6 +58,7 @@
"log4js": "^0.6.36", "log4js": "^0.6.36",
"mocha": "^2.4.5", "mocha": "^2.4.5",
"mqtt-ws": "^0.2.0", "mqtt-ws": "^0.2.0",
"nano": "^6.2.0",
"node-cron": "^1.1.1", "node-cron": "^1.1.1",
"require-dir": "^0.3.0", "require-dir": "^0.3.0",
"should": "^8.3.1", "should": "^8.3.1",