diff --git a/FlippR-Driver/src/input/Detector.cpp b/FlippR-Driver/src/input/Detector.cpp index a147a70..24404e4 100644 --- a/FlippR-Driver/src/input/Detector.cpp +++ b/FlippR-Driver/src/input/Detector.cpp @@ -12,8 +12,7 @@ #include "../utilities/config.h" -namespace Input -{ +using namespace Input; Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map events, IEventNotifier* event_notifier) : input_gpio_interface(input_gpio_interface), events(events), is_running(true), event_notifier(event_notifier) @@ -69,5 +68,3 @@ bool Detector::check_inputs(char& address) } return false; } - -} diff --git a/FlippR-Driver/src/input/Event.cpp b/FlippR-Driver/src/input/Event.cpp index 3e7d0ac..2f0fb86 100644 --- a/FlippR-Driver/src/input/Event.cpp +++ b/FlippR-Driver/src/input/Event.cpp @@ -4,7 +4,22 @@ * Created on: Jun 15, 2018 * Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht */ +#include "Event.h" +using namespace Input; + Event::Event(char address, char priority, std::string name) : + address(address), priority(priority), name(name) + { + CLOG_IF(VLOG_IS_ON(HIGH_VERBOSITY), INFO, INPUT_LOGGER) << "Created event: " << name << ", address: " << address; + } + bool Event::operator==(const Event& other) + { + return this->name == other.name; + } + friend bool operator<(const Event& left, const Event& right) + { + return left.priority < right.priority; + } diff --git a/FlippR-Driver/src/input/Event.h b/FlippR-Driver/src/input/Event.h index ce55f6f..97221ba 100644 --- a/FlippR-Driver/src/input/Event.h +++ b/FlippR-Driver/src/input/Event.h @@ -18,25 +18,15 @@ namespace Input class Event { - public: - Event(char address, char priority, std::string name) : - address(address), priority(priority), name(name) - { - CLOG_IF(VLOG_IS_ON(HIGH_VERBOSITY), INFO, INPUT_LOGGER) << "Created event: " << name << ", address: " << address; - } + Event(char address, char priority, std::string name); + bool operator==(const Event& other); + friend bool operator<(const Event& left, const Event& right); - bool operator==(const Event& other) - { - return this->name == other.name; - } - - friend bool operator<(const Event& left, const Event& right) - { - return left.priority < right.priority; - } private: char address; + +public: char priority; std::string name; diff --git a/FlippR-Driver/src/input/EventHandler.cpp b/FlippR-Driver/src/input/EventHandler.cpp index a400050..b9ec332 100644 --- a/FlippR-Driver/src/input/EventHandler.cpp +++ b/FlippR-Driver/src/input/EventHandler.cpp @@ -4,7 +4,28 @@ * Created on: Jun 14, 2018 * Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht */ - - +#include "EventHandler.h" + +using namespace Input; + + EventHandler::EventHandler(std::shared_ptr input_driver) : + input_driver(input_driver) + { + this->input_driver->register_event_handler(this); + + CLOG(INFO, INPUT_LOGGER) << "Created EventHandler"; + } + + virtual EventHandler::~EventHandler() + { + this->input_driver->unregister_event_handler(this); + this->input_driver = NULL; + } + + // This function is intended to be non pure, if it is called when the derived class doesn't exist anymore + virtual void EventHandler::handle(Event& event) + { + CLOG(WARNING, INPUT_LOGGER) << "Called EventHandler parent class"; + } diff --git a/FlippR-Driver/src/input/EventHandler.h b/FlippR-Driver/src/input/EventHandler.h index 00af7dd..95760ae 100644 --- a/FlippR-Driver/src/input/EventHandler.h +++ b/FlippR-Driver/src/input/EventHandler.h @@ -25,25 +25,9 @@ class EventHandler; class EventHandler : public IEventHandler { public: - EventHandler(std::shared_ptr input_driver) : - input_driver(input_driver) - { - this->input_driver->register_event_handler(this); - - CLOG(INFO, INPUT_LOGGER) << "Created EventHandler"; - } - - virtual ~EventHandler() - { - this->input_driver->unregister_event_handler(this); - this->input_driver = NULL; - } - - // This function is intended to be non pure, if it is called when the derived class doesn't exist anymore - virtual void handle(Event& event) - { - CLOG(WARNING, INPUT_LOGGER) << "Called EventHandler parent class"; - } + EventHandler(std::shared_ptr input_driver); + virtual ~EventHandler(); + virtual void handle(Event& event); private: std::shared_ptr input_driver; diff --git a/FlippR-Driver/src/input/InputDriver.cpp b/FlippR-Driver/src/input/InputDriver.cpp index b082379..e5c4095 100644 --- a/FlippR-Driver/src/input/InputDriver.cpp +++ b/FlippR-Driver/src/input/InputDriver.cpp @@ -4,7 +4,32 @@ * Created on: Jun 15, 2018 * Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht */ - - +#include "InputDriver.h" + +using namespace Input; +InputDriver::InputDriver(IEventNotifier* event_notifier, IDetector* detector) : + event_notifier(event_notifier), detector(detector) +{ + CLOG(INFO, INPUT_LOGGER) << "Created InputDriver"; +} + +InputDriver::~InputDriver() +{ + delete event_notifier; + event_notifier = NULL; + + delete detector; + detector = NULL; +} + +void InputDriver::register_event_handler(IEventHandler* handler) +{ + event_notifier->register_event_handler(handler); +} + +void InputDriver::unregister_event_handler(IEventHandler* handler) +{ + event_notifier->unregister_event_handler(handler); +} diff --git a/FlippR-Driver/src/input/InputDriver.h b/FlippR-Driver/src/input/InputDriver.h index 886c374..b32e69b 100644 --- a/FlippR-Driver/src/input/InputDriver.h +++ b/FlippR-Driver/src/input/InputDriver.h @@ -19,30 +19,11 @@ class InputDriver : public IInputDriver { public: - InputDriver(IEventNotifier* event_notifier, IDetector* detector) : - event_notifier(event_notifier), detector(detector) - { - CLOG(INFO, INPUT_LOGGER) << "Created InputDriver"; - } - ~InputDriver() - { - delete event_notifier; - event_notifier = NULL; - - delete detector; - detector = NULL; - } - - void register_event_handler(IEventHandler* handler) - { - event_notifier->register_event_handler(handler); - } - - void unregister_event_handler(IEventHandler* handler) - { - event_notifier->unregister_event_handler(handler); - } + InputDriver(IEventNotifier* event_notifier, IDetector* detector); + ~InputDriver(); + void register_event_handler(IEventHandler* handler); + void unregister_event_handler(IEventHandler* handler); private: IEventNotifier* event_notifier; diff --git a/FlippR-Driver/src/input/InputDriverFactory.cpp b/FlippR-Driver/src/input/InputDriverFactory.cpp index 3074f0d..611aab4 100644 --- a/FlippR-Driver/src/input/InputDriverFactory.cpp +++ b/FlippR-Driver/src/input/InputDriverFactory.cpp @@ -6,5 +6,79 @@ */ +#include "InputDriverFactory.h" +#include "Detector.h" +using namespace Input; + + static shared_ptr InputDriverFactory::get_InputDriver(std::string& input_config_path, std::string& matrix_config_path) + { + this->ConfigureLogger(); + auto event_notifier = new EventNotifier(); + + auto detector = this->get_detector(input_config_path, matrix_config_path); + + return shared_ptr(new InputDriver(event_notifier, detector)); + } + + static IDetector* get_detector(std::string& input_config_path, std::string& matrix_config_path) + { + std::ifstream input_config_stream(input_config_path); + json input_config; + input_config << input_config_stream; + + std::ifstream matrix_config_stream(matrix_config_path); + json matrix_config; + matrix_config << matrix_config_stream; + + auto input_gpio_interface = new InputGPIOInterface(input_config); + auto input_notifier = new InputEventNotifier(); + + std::map input_events = this->create_input_events(matrix_config); + + return new Detector(input_gpio_interface, input_events, input_notifier); + } + + static std::map InputDriverFactory::create_input_events(json matrix_config) + { + std::map events; + + for(auto& json_event : matrix_config) + { + + try + { + std::string name = json_event.at("name"); + char address = json_event.at("address"); + int priority = json_event.at("priority"); + + Event event(address, priority, name); + + events.emplace(address, event); + } + catch(json::exception& e) + { + CLOG(ERROR, INPUT_LOGGER) << "Matrix config-file corrupted: " << e.what(); + exit(EXIT_FAILURE); + } + } + + return events; + } + + static void InputDriverFactory::ConfigureLogger() + { + el::Loggers::getLogger(INPUT_LOGGER); + + //TODO may configure? + el::Configurations conf; + conf.setToDefault(); + conf.set(el::Level::Global, el::ConfigurationType::Filename, DRIVER_LOG_FILE); + conf.set(el::Level::Global, el::ConfigurationType::Format, "%datetime [%level] [%func] : %msg"); + + el::Loggers::reconfigureLogger(INPUT_LOGGER, conf); + } + +}; +} diff --git a/FlippR-Driver/src/input/InputDriverFactory.h b/FlippR-Driver/src/input/InputDriverFactory.h index 8f8e465..4d89fec 100644 --- a/FlippR-Driver/src/input/InputDriverFactory.h +++ b/FlippR-Driver/src/input/InputDriverFactory.h @@ -29,75 +29,10 @@ class InputFactory { public: - static shared_ptr get_InputDriver(std::string& input_config_path, std::string& matrix_config_path) - { - this->ConfigureLogger(); - auto event_notifier = new EventNotifier(); - - auto detector = this->get_detector(input_config_path, matrix_config_path); - - return shared_ptr(new InputDriver(event_notifier, detector)); - } - + static shared_ptr get_InputDriver(std::string& input_config_path, std::string& matrix_config_path); private: - static IDetector* get_detector(std::string& input_config_path, std::string& matrix_config_path) - { - std::ifstream input_config_stream(input_config_path); - json input_config; - input_config << input_config_stream; - - std::ifstream matrix_config_stream(matrix_config_path); - json matrix_config; - matrix_config << matrix_config_stream; - - auto input_gpio_interface = new InputGPIOInterface(input_config); - auto input_notifier = new InputEventNotifier(); - - std::map input_events = this->create_input_events(matrix_config); - - return new Detector(input_gpio_interface, input_events, input_notifier); - } - - static std::map create_input_events(json matrix_config) - { - std::map events; - - for(auto& json_event : matrix_config) - { - try - { - std::string name = json_event.at("name"); - char address = json_event.at("address"); - int priority = json_event.at("priority"); - - Event event(address, priority, name); - - events.emplace(address, event); - } - catch(json::exception& e) - { - CLOG(ERROR, INPUT_LOGGER) << "Matrix config-file corrupted: " << e.what(); - exit(EXIT_FAILURE); - } - } - - return events; - } - - static void ConfigureLogger() - { - el::Loggers::getLogger(INPUT_LOGGER); - - //TODO may configure? - el::Configurations conf; - conf.setToDefault(); - conf.set(el::Level::Global, el::ConfigurationType::Filename, DRIVER_LOG_FILE); - conf.set(el::Level::Global, el::ConfigurationType::Format, "%datetime [%level] [%func] : %msg"); - - el::Loggers::reconfigureLogger(INPUT_LOGGER, conf); - } - -}; -} + static IDetector* get_detector(std::string& input_config_path, std::string& matrix_config_path); + static std::map create_input_events(json matrix_config); + static void ConfigureLogger(); #endif /* INPUTFACTORY_H_ */