domotz-remote-pawn
Version:
Domotz Agent
153 lines (133 loc) • 5.36 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 (q, request, fileUtils, webUtils) {
var env = process.env;
var configFile = env.DOMOTZ_JSON;
var logDir = env.DOMOTZ_LOG_DIR;
var configUri, hubGeoLocation;
function isOpenWrt() {
return env.DPLATFORM === 'openwrt';
}
function setConfigurationUrl(location) {
configUri = location;
}
function loadConfiguration() {
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("CONFIGURATION - HUB configuration read. " +
"Agent ID: " + 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);
}
function setGeographicalCoordinates(geoLocation) {
hubGeoLocation = {
location: {
latitude: geoLocation.latitude,
longitude: geoLocation.longitude
},
timezone: geoLocation.time_zone,
country: geoLocation.country_code
};
}
function sendGeographicalCoordinates() {
var deferred = q.defer();
if (hubGeoLocation && configUri) {
console.info('CONFIGURATION - Send PUT request to endpoint: ' + _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 {
var geoLocation = hubGeoLocation.location;
console.info("CONFIGURATION - Reset agent geographical location (%s, %s)",
geoLocation.latitude, geoLocation.longitude);
deferred.resolve();
}
};
request.put({json: hubGeoLocation, uri: configUri}, callback);
} else {
deferred.reject(new Error("Missing agent location or GEO coordinates"));
}
return deferred.promise;
}
return {
setConfigurationUrl: setConfigurationUrl,
loadConfiguration: loadConfiguration,
getConfiguration: getConfiguration,
writeConfiguration: writeConfiguration,
deleteConfiguration: deleteConfiguration,
setGeographicalCoordinates: setGeographicalCoordinates,
sendGeographicalCoordinates: sendGeographicalCoordinates
};
};
module.exports.factory = factory;