Files
flippr-code/FlippR-Driver/src/input/InputDriverFactory.cpp
2018-07-11 22:17:49 +02:00

80 lines
2.1 KiB
C++

/*
* InputDriverFactory.cpp
*
* Created on: Jun 14, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#include "InputDriverFactory.h"
#include "utilities/LoggerFactory.hpp"
#include "EventNotifier.h"
using namespace nlohmann;
namespace Input
{
std::shared_ptr<InputDriver> InputDriverFactory::get_InputDriver(std::istream& input_config_stream, std::istream& matrix_config_stream)
{
ConfigureLogger();
IBlockingQueue<Event>* event_queue = new BlockingQueue<Event>;
std::shared_ptr<IEventNotifier> event_notifier = std::make_shared<EventNotifier>(event_queue);
std::unique_ptr<IInputGPIOInterface> input_gpio_interface = std::make_unique<InputGPIOInterface>(input_config_stream);
std::unique_ptr<IDetector> detector(std::move(get_detector(matrix_config_stream, std::move(input_gpio_interface), event_notifier)));
return std::shared_ptr<InputDriver>(new InputDriver(event_notifier, std::move(detector)));
}
std::unique_ptr<IDetector> InputDriverFactory::get_detector(std::istream& matrix_config_stream, std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::shared_ptr<IEventNotifier> event_notifier)
{
json matrix_config;
matrix_config_stream >> matrix_config;
std::map<char, Event> input_events = create_input_events(matrix_config);
return std::unique_ptr<Detector>(new Detector(std::move(input_gpio_interface), input_events, event_notifier));
}
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").get<json::number_integer_t>();
int priority = json_event.at("priority").get<json::number_integer_t>();
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;
}
void InputDriverFactory::ConfigureLogger()
{
LoggerFactory::CreateInputLogger();
}
}