UNPKG

domotz-remote-pawn

Version:

Domotz Agent

94 lines (86 loc) 3.53 kB
/** 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 18/09/15. */ var previousIp = null, previousPort = null, agentDeleted = false; var updatePkgInfo = function (resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); var engineRequest = resourceLocator.engineRequest; var pkgJson = resourceLocator.pkg.json; var webapp = resourceLocator.webapp; var status = resourceLocator.status; var notConfiguredAgent = resourceLocator.entities.notConfiguredAgent; var host = resourceLocator.discovery.host; var platform = resourceLocator.utils.platform; switch (status.get()) { case status.RESTRICTED: { var ip = host.getIP(); var port = webapp.get('port'); if (ip === previousIp && port === previousPort) { myConsole.info("Private IP [%s] and port [%s] didn't change." + ' No need to notify engine', ip, port); return; } notConfiguredAgent .create() .then(function (location) { myConsole.info('Location for not-configured-agent: %s', location); }) .then(function () { return notConfiguredAgent.update(ip, port); }) .then(function () { myConsole.info('Update private IP [%s] and port [%s]', ip, port); previousIp = ip; previousPort = port; }); break; } case status.CONFIGURED: { var pkgVersion = process.env.DVERSION; var agentVersion = pkgJson.version; var architecture = process.env.DARCHITECTURE; var fullPlatform = platform.getFullPlatform(); if (!agentDeleted) { notConfiguredAgent.delete().then(function () { agentDeleted = true; myConsole.info('Delete Unconfigured Agent.'); }); } else { myConsole.info('Agent already deleted.'); } var version = { package: pkgVersion, agent: agentVersion, }; var confVersion = resourceLocator.lodash.get(resourceLocator.configuration, 'conf_version', null); if (confVersion) { version.conf = confVersion; } engineRequest.cachedSendWithPromise('', 'PUT', { version: version, architecture: architecture, platform: fullPlatform, }); break; } case status.UPDATING: { myConsole.warn('Pending update. Stop standard operation'); } } }; module.exports.handler = updatePkgInfo;