UNPKG

domotz-remote-pawn

Version:

Domotz Agent

84 lines (72 loc) 3.44 kB
/** This file is part of Domotz Agent. * Copyright (C) 2022 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/>. **/ const LOCK_ID = 'BONJOUR_RPC'; const LOCK_INDEX = 1; var bonjourScanCommand = function (message, resourceLocator) { var cid = message.correlationId; var payload = message.payload; var myConsole = resourceLocator.log.decorateLogs(); var lodash = resourceLocator.lodash; var timeout = lodash.get(payload, 'bonjour_timeout_ms', 8000); var blocking = lodash.get(payload, 'blocking', 1); var bonjourDeviceDiscovery = resourceLocator.discovery.bonjour_discovery; var q = resourceLocator.q; var mutex = resourceLocator.utils.mutex; return { execute: function (callback) { if (!resourceLocator.settingsManager.isBonjour2Enabled()) { return callback({ error: 'bonjour2 not enabled', data: null }); } if (mutex.isLocked(LOCK_ID)) { return callback({ error: 'scan in progress', data: null }); } if (resourceLocator.settingsManager.isBroadcastDiscoveryDisabled()) { myConsole.info('Broadcast discovery disabled'); return callback({ error: 'broadcast discovery disabled', data: null }); } function processScanResults() { var bonjourDiscoveryData = resourceLocator.discovery.accumulator.fetchAllByProtocol('bonjour'); var reportToEnginePromises = resourceLocator.discovery.protocols_orchestrator.sendResultToEngine( bonjourDiscoveryData, bonjourDeviceDiscovery, blocking, false ); q.all(reportToEnginePromises) .finally(function () { myConsole.info( 'Bonjour scan completed, closing RPC, reporting data for %s devices', Object.keys(bonjourDiscoveryData).length ); mutex.unlock(LOCK_ID, LOCK_INDEX); callback({ error: null, data: bonjourDiscoveryData }); }) .catch(function (err) { myConsole.error('Error while reporting bonjour scan results to engine: %s', err.toString()); }); } myConsole.info('%s - Executing command bonjour, scan timeout is %s', cid, timeout); resourceLocator.discovery.bonjourListener.startOrRefresh(); mutex.lock(LOCK_ID, LOCK_INDEX); setTimeout(processScanResults, timeout); }, describe: function () { return cid + ' - bonjour message:'; }, }; }; module.exports.command = bonjourScanCommand;