UNPKG

domotz-remote-pawn

Version:

Domotz Agent

67 lines (59 loc) 2.66 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/>. **/ var previousUpdateHash = null; var updateModuleInfo = function (resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); var engineRequest = resourceLocator.engineRequest; var random = resourceLocator.utils.random; var status = resourceLocator.status; var modules = resourceLocator.utils.modules; switch (status.get()) { case status.RESTRICTED: { myConsole.warn('Not configured agent'); break; } case status.CONFIGURED: { modules.getAvailableModules(resourceLocator, function (result) { var body = result; myConsole.info('Body', JSON.stringify(body)); var bodyHash = random.md5(JSON.stringify(body)); if (bodyHash !== previousUpdateHash) { engineRequest .sendWithPromise('/module', 'PUT', body) .then(function () { myConsole.info('Sent modules to engine. Hash: %s', bodyHash); previousUpdateHash = bodyHash; }) .catch(function (err) { myConsole.error('Error while uploading results: %s', err.message); previousUpdateHash = null; }); } else { myConsole.info("Agent modules didn't change, no need to notify engine. Hash: %s", previousUpdateHash); } // save available modules resourceLocator.availableModules = result; myConsole.info('Available modules', JSON.stringify(resourceLocator.availableModules)); }); break; } case status.UPDATING: { myConsole.warn('Pending update. Stop standard operation'); } } }; module.exports.handler = updateModuleInfo;