domotz-remote-pawn
Version:
Domotz Agent
123 lines (109 loc) • 4.4 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 15/06/15.
*
*/
var factory = function (resourceLocator) {
var q = resourceLocator.q;
var request = resourceLocator.request;
var fileUtils = resourceLocator.utils.file;
var webUtils = resourceLocator.utils.web;
var path = resourceLocator.path;
var env = process.env;
var configFile = env.DOMOTZ_JSON;
var configUri;
var hubGeoLocation;
function removeOtherSettingsFiles() {
['agent_settings.json', 'jammed.txt', 'devices_to_exclude.conf', 'custom_interfaces.conf', 'ookla_blacklist.conf', 'device_map.conf'].forEach(
function (filename) {
fileUtils.deleteFile(path.join(process.env.DOMOTZ_CONF_DIR, filename), false);
}
);
}
function setConfigurationUrl(location) {
configUri = location;
}
function getConfiguredAgentId() {
if (configUri) {
var uriArray = configUri.split('/');
return uriArray[uriArray.length - 1];
}
}
function loadConfiguration() {
console.debug(' Loading configuration from %s', configFile);
return fileUtils.readFile(configFile, JSON.parse);
}
function _logUrl() {
return configUri.replace(/^(https?:\/\/[^:]+:)[^@]+(@.*?\/[^/]+)$/, '$1*****$2');
}
function getConfiguration(credentials) {
var deferred = q.defer();
if (configUri) {
console.info('CONFIGURATION - Recover Agent configuration from: ' + _logUrl());
var callback = function (error, response, body) {
if (error) {
deferred.reject(error);
} else if (response.statusCode > 299 || response.statusCode < 200) {
deferred.reject({
message: body,
status: response.statusCode,
stack: new Error().stack,
});
} else {
try {
var configData = JSON.parse(body);
var pwd = credentials ? credentials.password : webUtils.parseUrl(configUri).password;
console.info(' HUB configuration read. Agent ID: %s', configData.id);
configData.credentials.password = pwd;
deferred.resolve(configData);
} catch (err) {
deferred.reject(err);
}
}
};
var options = { uri: configUri };
if (credentials) {
options.auth = credentials;
}
request.get(options, callback);
} else {
deferred.reject(new Error('No URI for configuration'));
}
return deferred.promise;
}
function writeConfiguration(configData) {
var fileName = configFile;
var fileContents = JSON.stringify(configData, null, ' ');
console.info('CONFIGURATION - Writing configuration for hub in file: ' + fileName);
fileUtils.writeFile(fileName, fileContents, true);
return configData;
}
function deleteConfiguration() {
fileUtils.deleteFile(configFile);
}
return {
setConfigurationUrl: setConfigurationUrl,
getConfiguredAgentId: getConfiguredAgentId,
loadConfiguration: loadConfiguration,
getConfiguration: getConfiguration,
writeConfiguration: writeConfiguration,
deleteConfiguration: deleteConfiguration,
removeOtherSettingsFiles: removeOtherSettingsFiles,
};
};
module.exports.factory = factory;