From 199a5d1cd5aa78d8260bb93f4d0ee5cefd1393ac Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Tue, 28 Jun 2016 15:02:11 +0100 Subject: [PATCH] added couch logging --- lib/mqtt/mqttClient.js | 47 ++++++++++++++++++++++++++++++++++++++---- maker.js | 20 ++++++++++++++++++ package.json | 2 ++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 maker.js diff --git a/lib/mqtt/mqttClient.js b/lib/mqtt/mqttClient.js index 0e2b0d8..8b67793 100644 --- a/lib/mqtt/mqttClient.js +++ b/lib/mqtt/mqttClient.js @@ -1,5 +1,38 @@ var mqtt = require('mqtt'); 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() { @@ -21,7 +54,7 @@ var mqttClient = function() { 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) { if (!error && response.statusCode === 200) { @@ -49,7 +82,7 @@ var mqttClient = function() { this.client.on('connect', function() { connected = true; - console.log('Connected to SIlvr Broker'); + logger.info('Connected to SIlvr Broker'); this.fanTimer(); }.bind(this)); @@ -57,11 +90,13 @@ var mqttClient = function() { this.client.on('message', function(topic, message) { var json = JSON.parse(message.toString()); - console.log(json); - console.log(json.temp); + logger.debug(json); + logger.debug(json.temp); this.livingRoom.temp = json.temp; + busEmitter.emit('saveData', json); + }.bind(this)); this.isConnected = function() { @@ -69,6 +104,10 @@ var mqttClient = function() { }; + + busEmitter.on('saveData', doInsertEntry); + + }; /* diff --git a/maker.js b/maker.js new file mode 100644 index 0000000..3393f6a --- /dev/null +++ b/maker.js @@ -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); + + }); + }); diff --git a/package.json b/package.json index 8aefa69..578964d 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "clone": "^1.0.2", "del": "^2.2.0", "elapsed": "0.0.7", + "events": "^1.1.1", "gulp": "^3.9.1", "gulp-autoprefixer": "^3.1.0", "gulp-cache": "^0.4.5", @@ -57,6 +58,7 @@ "log4js": "^0.6.36", "mocha": "^2.4.5", "mqtt-ws": "^0.2.0", + "nano": "^6.2.0", "node-cron": "^1.1.1", "require-dir": "^0.3.0", "should": "^8.3.1",