domotz-remote-pawn
Version:
Domotz Agent
69 lines (58 loc) • 2.29 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(amqplib, util, configuration) {
var amqpServerConfiguration = configuration.message_broker;
var credentials = configuration.credentials;
function logConnection() {
console.debug('AMQP - Connection successful:');
Object.keys(amqpServerConfiguration).forEach(function (key) {
console.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;
console.info('AMQP - 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);
}
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;