This commit is contained in:
Neeflix
2018-06-15 00:17:54 +02:00
9 changed files with 156 additions and 134 deletions

View File

@@ -12,8 +12,7 @@
#include "../utilities/config.h"
namespace Input
{
using namespace Input;
Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map<char, Event> 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;
}
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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<IInputDriver> 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";
}

View File

@@ -25,25 +25,9 @@ class EventHandler;
class EventHandler : public IEventHandler
{
public:
EventHandler(std::shared_ptr<IInputDriver> 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<IInputDriver> input_driver);
virtual ~EventHandler();
virtual void handle(Event& event);
private:
std::shared_ptr<IInputDriver> input_driver;

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -6,5 +6,79 @@
*/
#include "InputDriverFactory.h"
#include "Detector.h"
using namespace Input;
static shared_ptr<IInputDriver*> 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<InputDriver*>(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<char, Event> input_events = this->create_input_events(matrix_config);
return new Detector(input_gpio_interface, input_events, input_notifier);
}
static std::map<char, Event> InputDriverFactory::create_input_events(json matrix_config)
{
std::map<char, Event> 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);
}
};
}

View File

@@ -29,75 +29,10 @@ class InputFactory
{
public:
static shared_ptr<IInputDriver*> 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<InputDriver*>(new InputDriver(event_notifier, detector));
}
static shared_ptr<IInputDriver*> 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<char, Event> input_events = this->create_input_events(matrix_config);
return new Detector(input_gpio_interface, input_events, input_notifier);
}
static std::map<char, Event> create_input_events(json matrix_config)
{
std::map<char, Event> 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<char, Event> create_input_events(json matrix_config);
static void ConfigureLogger();
#endif /* INPUTFACTORY_H_ */