UNPKG

domotz-remote-pawn

Version:

Domotz Agent

97 lines (89 loc) 3.92 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 LOG_EVENT = 'HANDLER - (updatePkgInfo)'; var previousIp = null, previousPort = null, agentDeleted = false, previousPkgVersion = null, previousAgentVersion = null; var updatePkgInfo = function (resourceLocator) { 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; switch (status.get()) { case (status.RESTRICTED): { var ip = host.getIP(); var port = webapp.get('port'); if (ip === previousIp && port === previousPort) { console.info("%s: Private IP [%s] and port [%s] didn't change." + " No need to notify engine", LOG_EVENT, ip, port); return; } notConfiguredAgent.create() .then(function(location) { console.info('%s: Location for not-configured-agent: %s', LOG_EVENT, location); }) .then(function () { return notConfiguredAgent.update(ip, port); }) .then(function() { console.info("%s: Update private IP [%s] and port [%s]", LOG_EVENT, ip, port); previousIp = ip; previousPort = port; }); break; } case (status.CONFIGURED): { var pkgVersion = process.env.DVERSION; var agentVersion = pkgJson.version; if (!agentDeleted) { notConfiguredAgent.delete() .then(function () { agentDeleted = true; console.info("%s: Delete Unconfigured Agent.", LOG_EVENT); }); } else { console.info("%s: Agent already deleted.", LOG_EVENT); } if (previousAgentVersion !== agentVersion || previousPkgVersion !== pkgVersion) { engineRequest.send('', 'PUT', {version: {package: pkgVersion, agent: agentVersion}}, function () { console.info('%s: Sent package info: Agent Version [%s], ' + 'Package Version [%s]', LOG_EVENT, agentVersion, pkgVersion); previousAgentVersion = agentVersion; previousPkgVersion = pkgVersion; } ); } else { console.info("%s: Agent Version [%s] and Package Version [%s]" + " didn't change. No need to notify engine", LOG_EVENT, agentVersion, pkgVersion); } break; } case (status.UPDATING): { console.warn('%s: Pending update. Stop standard operation', LOG_EVENT); } } }; module.exports.handler = updatePkgInfo;