UNPKG

domotz-remote-pawn

Version:

Domotz Agent

140 lines (127 loc) 5.86 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/>. **/ /** * * Created by Iacopo Papalini <ipapalini@domotz.com> on 08/09/15. */ // Key: Handler (the name of handler is the same of the module in events/ // Value: Event that triggers that particular handler const EVENTS = { amqpConnectionClose: ['amqpConnectionClose'], autoWakeOnLan: ['autoWakeOnLan'], avahiResolve: ['endFastDeviceDiscovery', 'avahiResolve'], baselineLatencyMeasurement: ['baselineLatencyMeasurement'], bigNetworksScan: ['bigNetworksScan'], bonjourDiscovery: ['endFastDeviceDiscovery', 'bonjourDiscovery'], connectionCheck: ['onListeningWebapp', 'sanityCheck', 'connectionCheck'], dhcpRequestsReport: ['dhcpRequestsReport'], diagnostic: ['endFastDeviceDiscovery', 'diagnostic'], fastDeviceDiscovery: ['onListeningWebapp', 'fastDeviceDiscovery'], fetchSpeedTestServers: ['onListeningWebapp', 'fetchSpeedTestServers'], flushLog: ['flushLog'], heartBeat: ['heartBeat'], igdDiscovery: ['endFastDeviceDiscovery', 'igdDiscovery'], interfaceListUpdater: ['endFastDeviceDiscovery', 'interfaceListUpdater'], invalidateCache: ['invalidateCache'], invalidateEyesList: ['invalidateEyesList'], ipConflictDetection: ['ipConflictDetection'], logFolderCleaner: ['logFolderCleaner'], mtr: ['mtr'], netbiosDiscovery: ['endFastDeviceDiscovery', 'netbiosDiscovery'], netgearUpdateFirewallRules: ['endFastDeviceDiscovery', 'netgearUpdateFirewallRules'], networkInfo: ['networkInfo'], onvifDeviceDiscovery: ['endFastDeviceDiscovery', 'onvifDeviceDiscovery'], osDataDiscovery: ['endFastDeviceDiscovery', 'osDataDiscovery'], osInfo: ['osInfo'], rtdScan: ['rtdScan', 'endFastDeviceDiscovery'], scheduledRestart: ['onListeningWebapp', 'scheduledRestart'], snmpBasicDiscovery: ['endFastDeviceDiscovery', 'snmpBasicDiscovery'], snmpCheckStatusDiscovery: ['endFastDeviceDiscovery', 'snmpCheckStatusDiscovery'], snmpCustomDiscovery: ['endFastDeviceDiscovery', 'snmpCustomDiscovery'], snmpInterfaceDiscovery: ['endFastDeviceDiscovery', 'snmpInterfaceDiscovery'], snmpMibDiscovery: ['endFastDeviceDiscovery', 'snmpMibDiscovery'], snmpOverlayDB: ['endFastDeviceDiscovery', 'snmpOverlayDB'], snmpOverlayDeviceDB: ['endFastDeviceDiscovery', 'snmpOverlayDeviceDB'], snmpOverlayDiscovery: ['endFastDeviceDiscovery', 'snmpOverlayDiscovery'], speedTest: ['speedTest'], tcpPortScan: ['endFastDeviceDiscovery', 'tcpPortScan'], tcpServicesMonitoring: ['endFastDeviceDiscovery', 'tcpServicesMonitoring'], udpPortScan: ['endFastDeviceDiscovery', 'udpPortScan'], updateHttpListeningPort: ['endFastDeviceDiscovery', 'updateHttpListeningPort'], updateJammedDevicesList: ['updateJammedDevicesList'], updateModuleInfo: ['updatePkgInfo', 'updateModuleInfo'], updatePkgInfo: ['updatePkgInfo'], updateSettings: ['updateSettings'], upnpDeviceDiscovery: ['endFastDeviceDiscovery', 'upnpDeviceDiscovery'], wsServerStart: ['onListeningWebapp', 'wsServerStart'], }; if (process.env.DPLATFORM === 'win') { EVENTS.processCleaner = ['processCleaner']; } var factory = function (events, resourceLocator) { var agentEventEmitter = new events.EventEmitter(); var handlers = Object.keys(EVENTS); var myConsole = resourceLocator.log.decorateLogs(); agentEventEmitter.setMaxListeners(handlers.length); var ret = {}; function emitter(e) { return function () { myConsole.debug('Emitting event: ' + JSON.stringify(e)); agentEventEmitter.emit(e, resourceLocator); }; } function handlerWrapper(handler, handlerName, resourceLocator) { if (resourceLocator.settingsManager) { var enabled = resourceLocator.settingsManager.isEventHandlerEnabled(handlerName); if (!enabled) { return; } } handler(resourceLocator); } function bindEvent(event, handler, handlerName) { agentEventEmitter[event] = event; agentEventEmitter.on(event, handlerWrapper.bind(null, handler, handlerName)); ret[event] = emitter(event); myConsole.info('Bound event: ' + event); } function nop(event) { 'use strict'; return function () { myConsole.debug('nop for event %s', event); }; } handlers.forEach(function (handlerName) { var eventList = EVENTS[handlerName]; var module = require('./handler/' + handlerName + '.js'); myConsole.info('Setup handler [%s] for event:', handlerName); eventList.forEach(function (event) { if (resourceLocator.status.isPassive()) { myConsole.info('Since in passive mode, triggers only its handler'); if (handlerName === event) { bindEvent(event, module.handler, handlerName); } else { bindEvent(event, nop(event), handlerName); } } else { bindEvent(event, module.handler, handlerName); } }); }); return ret; }; module.exports.factory = factory;