domotz-remote-pawn
Version:
Domotz Agent
72 lines (61 loc) • 2.48 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
/**
*
* Created by Iacopo Papalini <ipapalini@domotz.com> on 17/09/15.
*/
var factory = function (resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var amqplib = resourceLocator.amqplib;
var util = resourceLocator.util;
var configuration = resourceLocator.configuration;
var amqpServerConfiguration = configuration.message_broker;
var credentials = configuration.credentials;
function logConnection() {
myConsole.debug('Connection successful:');
Object.keys(amqpServerConfiguration).forEach(function (key) {
myConsole.debug(' --> ' + key + ': ' + amqpServerConfiguration[key]);
});
}
function createConnectionUrl() {
var user = credentials.user;
var password = credentials.password;
var protocol = amqpServerConfiguration.protocol || 'amqp';
var host = amqpServerConfiguration.host;
var port = amqpServerConfiguration.port;
var vhost = amqpServerConfiguration.virtual_host;
myConsole.info('Connecting to: %s://%s:***********@%s:%s/%s', protocol, user, host, port, vhost);
return util.format('%s://%s:%s@%s:%s/%s?heartbeat=60', protocol, user, password, host, port, vhost);
}
// USELESS!!! TODO: Remove when it disappears from listener.js
function setSignalHandler(connection) {
logConnection();
process.once('SIGINT', function () {
connection.close();
});
return connection;
}
function connect(url) {
return amqplib.connect(url);
}
return {
createConnectionUrl: createConnectionUrl,
connect: connect,
setSignalHandler: setSignalHandler,
};
};
module.exports.factory = factory;