domotz-remote-pawn
Version:
Domotz Agent
73 lines (64 loc) • 2.98 kB
JavaScript
/** 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 = 'UPNP_RPC';
const LOCK_INDEX = 1;
var upnp = function (message, resourceLocator) {
var cid = message.correlationId;
var payload = message.payload;
var myConsole = resourceLocator.log.decorateLogs();
var q = resourceLocator.q;
var lodash = resourceLocator.lodash;
var upnpDeviceDiscovery = resourceLocator.discovery.upnp_device_discovery;
var blocking = lodash.get(payload, 'blocking', 1);
var timeout = lodash.get(payload, 'upnp_timeout_ms', 8000);
var mutex = resourceLocator.utils.mutex;
return {
execute: function (callback) {
myConsole.info('%s - Executing command upnp, scan timeout is %s', cid, timeout);
if (resourceLocator.settingsManager.isBroadcastDiscoveryDisabled()) {
myConsole.info('Broadcast discovery disabled');
return callback({ error: 'broadcast discovery disabled', data: null });
}
if (mutex.isLocked(LOCK_ID)) {
return callback({ error: 'scan in progress', data: null });
}
function upnpCallback(upnpDiscoverySnapshotData) {
var reportToEnginePromises = resourceLocator.discovery.protocols_orchestrator.sendResultToEngine(
upnpDiscoverySnapshotData,
upnpDeviceDiscovery,
blocking,
false
);
q.all(reportToEnginePromises).finally(function () {
myConsole.info('UPNP scan completed, closing RPC, reporting data for %s devices', Object.keys(upnpDiscoverySnapshotData).length);
callback({ error: null, data: upnpDiscoverySnapshotData });
mutex.unlock(LOCK_ID, LOCK_INDEX);
});
}
function upnpErrorCallback(err) {
mutex.unlock(LOCK_ID, LOCK_INDEX);
callback({ error: err.toString(), data: null });
}
mutex.lock(LOCK_ID, LOCK_INDEX);
upnpDeviceDiscovery.discover(upnpCallback, upnpErrorCallback, timeout);
},
describe: function () {
return cid + ' - upnp message:' + payload;
},
};
};
module.exports.command = upnp;