UNPKG

domotz-remote-pawn

Version:

Domotz Agent

98 lines (88 loc) 3.28 kB
/** This file is part of Domotz Agent. * Copyright (C) 2021 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 Alessandro Pagiaro <a.pagiaro@domotz.com> on 06/07/2021. */ var factory = function (resourceLocator, pkgInfo) { var host = resourceLocator.discovery.host; var registerRequest = resourceLocator.registerRequest; var myConsole = resourceLocator.log.decorateLogs(); var fs = resourceLocator.fs; function formattedInterfacesList(intf) { var formattedIntf = []; Object.keys(intf).forEach(function (key) { formattedIntf.push({ ip: intf[key].ip, mac: intf[key].mac, }); }); return formattedIntf; } function getCustomInfo(env) { try { const customInfoPath = env.CUSTOM_INFO_PATH; if (!fs.existsSync(customInfoPath)) { return undefined; } var customInfo = fs.readFileSync(customInfoPath, { encoding: 'ascii', flag: 'r' }); return JSON.parse(customInfo); } catch (error) { myConsole.error(error); return undefined; } } function create() { var env = process.env; var body = { device: env.DPLATFORM, architecture: env.DARCHITECTURE, toolchain: env.DTOOLCHAIN, pkg_version: env.DVERSION, agent_version: pkgInfo.version, mac_address: host.getMAC(), platform: resourceLocator.utils.platform.getFullPlatform(), interfaces: JSON.stringify(formattedInterfacesList(host.getInterfacesList())), }; var customInfo = getCustomInfo(env); if (customInfo) { body.custom_info = customInfo; } return registerRequest.sendWithPromise('not-configured-agent', 'POST', body, function (response) { return response.headers.location; }); } function update(ipAddress, listeningPort) { var body = { private_ip: ipAddress, port: listeningPort, }; return registerRequest.sendWithPromise('not-configured-agent/' + host.getMAC(), 'PUT', body); } function destroy() { return registerRequest.sendWithPromise('not-configured-agent/' + host.getMAC(), 'DELETE', {}).catch(function (err) { myConsole.debug('Error while deleting the not-configured-agent %s', err); }); } return { create: create, update: update, delete: destroy, get_custom_info: getCustomInfo, }; }; module.exports.factory = factory;