mirror of
https://gitlab.silvrtree.co.uk/martind2000/mqttArchiver.git
synced 2025-03-13 10:00:01 +00:00
168 lines
3.8 KiB
JavaScript
168 lines
3.8 KiB
JavaScript
var mqtt = require('mqtt');
|
|
var logger = require('log4js').getLogger();
|
|
|
|
var EventEmitter = require('events');
|
|
|
|
var busEmitter = new EventEmitter();
|
|
|
|
var requestify = require('requestify');
|
|
|
|
var db = require('../server/db-connector').dbConnection;
|
|
|
|
var dbSave = require('../server/db-save')(db);
|
|
|
|
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
|
|
var nano = require('nano')('http://localhost:5984');
|
|
var db_name = 'mqttarchive';
|
|
var dbCouch = nano.use(db_name);
|
|
|
|
|
|
function dataBuilder(obj) {
|
|
'use strict';
|
|
var now = new Date();
|
|
var newObj = {device_type: 'mDot', evt_type: 'update', timestamp: {}, evt: {}};
|
|
|
|
newObj.device_id = obj.topic.split('/')[4];
|
|
|
|
newObj.evt = obj;
|
|
newObj.timestamp['$date'] = now.getTime();
|
|
|
|
return newObj;
|
|
}
|
|
|
|
|
|
function saveToDB(data) {
|
|
// logger.debug('Inserting into couch...');
|
|
// Logger.info(util.inspect(obj));
|
|
dbCouch.insert(data, function(err, body, header) {
|
|
if (err) {
|
|
logger.error('Error inserting into couch');
|
|
logger.error(err);
|
|
return;
|
|
}
|
|
});
|
|
}
|
|
|
|
var doSendMessage = (obj) => {
|
|
'use strict';
|
|
requestify.post('http://localhost:3011/apiv2/message', obj)
|
|
//requestify.post('http://mdotserver.mybluemix.net/apiv2/message', obj)
|
|
.then(function(response) {
|
|
// Get the response body
|
|
// logger.debug(response.getBody());
|
|
})
|
|
.catch(function(e) {
|
|
logger.error('doSendMessage',e);
|
|
});
|
|
};
|
|
|
|
|
|
var doInsertEntry = (obj) => {
|
|
// Logger.info('sendSocket: ' + JSON.stringify(obj));
|
|
|
|
// insertEntry(obj);
|
|
|
|
dbSave.addNewEvent(obj)
|
|
.then(function(d) {
|
|
'use strict';
|
|
//logger.info('Finished - Raw',d);
|
|
saveToDB(obj);
|
|
if (obj.type === 'mDot') {
|
|
busEmitter.emit('sendMessage', obj);
|
|
}
|
|
})
|
|
.catch(function(e) {
|
|
'use strict';
|
|
logger.error(e);
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
var mqttClient = function() {
|
|
var count=0;
|
|
var subscribeTopic;
|
|
var orgId = 'qz0da4';
|
|
var userName = 'a-qz0da4-dfwwdkmkzr';
|
|
var address = '.messaging.internetofthings.ibmcloud.com';
|
|
var appKey = '9txJEf3Cjy7hkSOvkv';
|
|
|
|
//Var subscribeTopic = prefix + deviceId + '/evt/+/fmt/json';
|
|
|
|
|
|
this.connected = false;
|
|
|
|
var options = {
|
|
keepalive: 10,
|
|
clientId: 'a:' + orgId + ':' + Date.now(),
|
|
username: userName,
|
|
password: new Buffer(appKey),
|
|
reconnectPeriod:1000,
|
|
connectTimeout:30 * 1000
|
|
|
|
};
|
|
|
|
this.client = mqtt.connect('mqtt://' + orgId + address, options);
|
|
|
|
this.client.on('connect', function() {
|
|
connected = true;
|
|
logger.info('Connected to ', address);
|
|
}.bind(this));
|
|
|
|
this.client.on('connected', function() {
|
|
logger.debug('mqttConnect - doConnection - Connected');
|
|
|
|
});
|
|
this.client.on('close', function() {
|
|
logger.warn('mqttConnect - Connection closed');
|
|
});
|
|
|
|
this.client.on('offline', function() {
|
|
logger.warn('mqttConnect - OFFLINE!');
|
|
});
|
|
|
|
|
|
this.client.on('error', function(e) {
|
|
logger.error('mqttConnect - error');
|
|
logger.error(e);
|
|
});
|
|
|
|
this.client.on('reconnect', function() {
|
|
logger.warn('mqttConnect - Attempting to reconnect...');
|
|
|
|
});
|
|
|
|
subscribeTopic = 'iot-2/type/+/id/+/evt/+/fmt/json';
|
|
logger.info('Subscribing:', subscribeTopic);
|
|
this.client.subscribe(subscribeTopic);
|
|
|
|
this.client.on('message', function(topic, message) {
|
|
|
|
var json = JSON.parse(message.toString());
|
|
|
|
var topicArray = topic.split('/');
|
|
json.topic = topic;
|
|
json.type = topicArray[2];
|
|
json.device = topicArray[4];
|
|
json.event = topicArray[6];
|
|
|
|
busEmitter.emit('saveData', json);
|
|
count++;
|
|
|
|
}.bind(this));
|
|
|
|
busEmitter.on('saveData', doInsertEntry);
|
|
busEmitter.on('sendMessage', doSendMessage);
|
|
|
|
this.getCount = function() {
|
|
'use strict';
|
|
return count;
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.mqttClient = mqttClient;
|