domotz-remote-pawn
Version:
Domotz Agent
81 lines (71 loc) • 2.89 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 Tommaso Latini <tommaso@domotz.com> on 16/07/15.
*/
const AGENT_CREATE_TIMEOUT = 180000;
const DEFAULT_CONTEXT = 'office';
var factory = function (random, resourceLocator) {
var web = resourceLocator.utils.web;
var lodash = resourceLocator.lodash;
var hostInfo = resourceLocator.discovery.host;
var configuration = resourceLocator.configuration;
var userRequest = resourceLocator.userRequest;
var platform = resourceLocator.utils.platform;
var mainAgentProperties = ['display_name', 'id', 'licence_id', 'status', 'versions', 'timezone', 'location', 'country_code'];
var myConsole = resourceLocator.log.decorateLogs();
function activateOnPublicApi(endpoint, apiKey, requestBody) {
var method = 'POST';
var resource = endpoint + '/agent';
var name = requestBody.name || hostInfo.getMAC();
var body = {
display_name: name,
mac_address: hostInfo.getMAC(),
platform: platform.getFullPlatform(),
architecture: process.env.DARCHITECTURE,
context: DEFAULT_CONTEXT,
deny_all_interfaces: Boolean(requestBody.deny_all_interfaces) || false,
};
delete requestBody.name;
delete requestBody.deny_all_interfaces;
delete requestBody.mac_address;
delete requestBody.platform;
delete requestBody.context;
delete requestBody.architecture;
lodash.extend(body, requestBody);
myConsole.debug('Public api %s call to create a new agent %s (%s)', endpoint, name, hostInfo.getMAC());
return userRequest.sendWithPromise(resource, method, body, web.extractLocation, true, {
timeout: AGENT_CREATE_TIMEOUT,
headers: {
'X-API-Key': apiKey,
},
doNotFollow204: true,
});
}
function get() {
var data = {};
mainAgentProperties.forEach(function (val) {
data[val] = configuration[val];
});
return data;
}
return {
activateOnPublicApi: activateOnPublicApi,
get: get,
};
};
module.exports.factory = factory;