UNPKG

domotz-remote-pawn

Version:

Domotz Agent

67 lines (59 loc) 2.71 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 counter = 9; var numberOfFastDiscoveryCycle = 10; var bonjour2Period = 4; // around 2 minutes var handler = function (resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); counter++; var bonjourDelay = resourceLocator.settingsManager.getSetting('configuration.settings.discovery.bonjour_delay', 10000); var piggybackDiscovery = resourceLocator.lodash.get(resourceLocator, 'configuration.settings.discovery.bonjour.piggyback_discovery', false); if (resourceLocator.settingsManager.isBroadcastDiscoveryDisabled()) { myConsole.info('Skipping bonjour discovery'); return; } if (resourceLocator.settingsManager.isBonjour2Enabled()) { if (counter >= bonjour2Period) { counter = 0; myConsole.info('Using Bonjour discovery v2 algorithm'); resourceLocator.discovery.bonjourListener.startOrRefresh(); if (piggybackDiscovery) { return; } setTimeout(function () { var servicesByMac = resourceLocator.discovery.bonjourListener.getServicesByMac(); resourceLocator.lodash.forOwn(servicesByMac, function (value, key) { resourceLocator.discoverySenderFactory(resourceLocator).sendAndCacheDiscoveryResult(key, value, { getProtocolName: function () { return 'bonjour'; }, }); }); }, bonjourDelay); } else { myConsole.info('Skipped, next run in %s discovery cycles', bonjour2Period - counter); } } else { myConsole.info('Called: ' + counter + ' times, starting at ' + numberOfFastDiscoveryCycle + ' )'); if (counter === numberOfFastDiscoveryCycle) { counter = 0; resourceLocator.discovery.protocols_orchestrator.discover('bonjour'); } } }; module.exports.handler = handler;