UNPKG

domotz-remote-pawn

Version:

Domotz Agent

83 lines (73 loc) 3.1 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'], analyzeRequest: ['endFastDeviceDiscovery'], avahiResolve: ['endFastDeviceDiscovery'], connectionCheck: ['onListeningWebapp', 'sanityCheck'], fastDeviceDiscovery: ['onListeningWebapp', 'fastDeviceDiscovery'], fetchSpeedTestServers: ['onListeningWebapp'], flushLog: ['flushLog'], logFolderCleaner: ['logFolderCleaner'], heartBeat: ['heartBeat'], deviceStatusUpdate: ['deviceStatusUpdate'], mtr: ['mtr'], portMap: ['endFastDeviceDiscovery'], speedTest: ['speedTest'], suicide: ['onListeningWebapp'], updateAgent: ['onListeningWebapp', 'updateAgent'], updateHttpListeningPort: ['endFastDeviceDiscovery'], updatePkgInfo: ['updatePkgInfo'], autoWakeOnLan: ['autoWakeOnLan'], tcpServicesMonitoring: ['endFastDeviceDiscovery'], upnpDeviceDiscovery: ['endFastDeviceDiscovery'], bonjourDiscovery: ['endFastDeviceDiscovery'], snmpBasicDiscovery: ['endFastDeviceDiscovery'], snmpInterfaceDiscovery: ['endFastDeviceDiscovery'], updateJammedDevicesList: ['updateJammedDevicesList'], invalidateTcpMonitoredServicesList: ['invalidateTcpMonitoredServicesList'] }; var factory = function (events, resourceLocator) { var agentEventEmitter = new (events.EventEmitter)(); var handlers = Object.keys(EVENTS); var ret = {}; function emitter(e) { return function (arg1, arg2, arg3) { console.debug("EVENT - Emitting event: " + JSON.stringify(e)); agentEventEmitter.emit(e, resourceLocator, arg1, arg2, arg3); }; } handlers.forEach(function(handler) { var eventList = EVENTS[handler]; var module = require('./handler/' + handler + '.js'); console.info('EVENT - Setup handler [%s] for event:', handler); eventList.forEach(function(event) { agentEventEmitter[event] = event; agentEventEmitter.on(event, module.handler); ret[event] = emitter(event); console.info('EVENT - --> ' + event); }); }); return ret; }; module.exports.factory = factory;