domotz-remote-pawn
Version:
Domotz Agent
85 lines (71 loc) • 3.23 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz UK LLP
*
* 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 protocolDiscoveryOrchestratorFactory = function (resourceLocator, discoverySenderFactory) {
'use strict';
/***
* console overrides
*
**/
var discoveryConsole = resourceLocator.log.decorateLogs();
var discoverySender = discoverySenderFactory(resourceLocator);
var registeredProtocols = {
bonjour: resourceLocator.discovery.bonjour_discovery,
upnp: resourceLocator.discovery.upnp_device_discovery,
snmp: resourceLocator.discovery.snmp_basic_discovery,
};
function discover(protocolName) {
var protocolDriver = getProtocolDriver(protocolName);
if (!protocolDriver) {
discoveryConsole.error("Driver not found for protocol '%s'", protocolName);
return;
}
discoveryConsole.info('(%s): Discovery Started', protocolDriver.getProtocolNameUpperCase());
discoveryConsole.verbose('(%s): Got device maps, calling driver ...', protocolDriver.getProtocolNameUpperCase());
doProtocolDiscovery(protocolDriver);
}
function getProtocolDriver(protocolName) {
return registeredProtocols[protocolName];
}
function doProtocolDiscovery(protocolDriver) {
var prettyProtocolName = protocolDriver.getProtocolNameUpperCase();
var wrapperFunction = function (result) {
discoveryConsole.debug('(%s) Discovery terminated, analysing results ...', prettyProtocolName);
sendResultToEngine(result, protocolDriver);
};
if (!protocolDriver.isDiscoveryInProgress()) {
discoveryConsole.debug('(%s): Starting actual discovery ...', prettyProtocolName);
protocolDriver.discover(wrapperFunction, resourceLocator);
} else {
discoveryConsole.debug('(%s): discovery already in progress ...', prettyProtocolName);
}
}
function sendResultToEngine(response, protocolDriver, blocking, cached) {
cached = cached === undefined ? true : cached;
var promises = [];
for (var deviceMac in response) {
if (response.hasOwnProperty(deviceMac) && response[deviceMac]) {
promises.push(discoverySender.sendAndCacheDiscoveryResult(deviceMac, response[deviceMac], protocolDriver, blocking, cached));
}
}
return promises;
}
return {
discover: discover,
sendResultToEngine: sendResultToEngine,
};
};
module.exports.factory = protocolDiscoveryOrchestratorFactory;