domotz-remote-pawn
Version:
Domotz Agent
62 lines (53 loc) • 2.1 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/>.
**/
/**
* This command tries a set of free services in order to find the location of the Agent
* Created by Iacopo Papalini <ipapalini@domotz.com> on 14/09/15.
*/
var getLocation = function (message, resourceLocator) {
var getLocationFromService = resourceLocator.networkTools.geolocation.getLocationFromService;
var results = {
stats: {
start: new Date(),
end: null,
elapsed_milliseconds: null,
provider: null
},
location: {}
};
return {
describe: function () {
return message.correlationId + " - Get geographical location; ";
},
execute: function (callback) {
function finalize(location) {
results.location = location;
results.stats.end = new Date();
results.stats.elapsed_milliseconds = results.stats.end.getTime() - results.stats.start.getTime();
callback(results);
return null;
}
var providers = ['maxmind', 'freegeoip', 'default'];
function fetchData(){
var provider = providers.shift();
getLocationFromService(provider, resourceLocator, fetchData, finalize);
}
fetchData();
}
};
};
module.exports.command = getLocation;