Files
flippr-code/FlippR-Driver/src/input/InputDriverFactory.cpp
2018-09-21 00:41:13 +02:00

77 lines
2.5 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/config.h"
#include "utilities/LoggerFactory.h"
#include "InputDriver.h"
#include "EventNotifier.h"
using namespace nlohmann;
namespace FlippR_Driver
{
namespace Input
{
std::shared_ptr<IInputDriver> InputDriverFactory::get_InputDriver(std::istream &input_config_stream, std::istream &matrix_config_stream)
{
LoggerFactory::CreateInputLogger();
IBlockingQueue<Event> *event_queue = new BlockingQueue<Event>;
json matrix_config;
matrix_config_stream >> matrix_config;
std::map<char, std::shared_ptr<Event>> address_event_map;
std::map<std::string, std::shared_ptr<Event>> name_event_map;
create_input_events(matrix_config, address_event_map, name_event_map);
std::shared_ptr<IEventNotifier> event_notifier(new EventNotifier(event_queue));
std::unique_ptr<IInputGPIOInterface> input_gpio_interface(new InputGPIOInterface(input_config_stream));
std::unique_ptr<IDetector> detector(
new Detector(std::move(input_gpio_interface), address_event_map, event_notifier));
return std::shared_ptr<InputDriver>(new InputDriver(event_notifier, std::move(detector), name_event_map));
}
void InputDriverFactory::create_input_events(json matrix_config, std::map<char, std::shared_ptr<Event>>& address_event_map,
std::map<std::string, std::shared_ptr<Event>>& name_event_map,
std::shared_ptr<IEventNotifier> event_notifier)
{
auto& event_array = matrix_config.at("input_matrix");
for (auto &json_event : event_array)
{
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>();
int deactivation_time = json_event.at("deactivation_time_milli").get<json::number_integer_t>();
std::shared_ptr<Event> event_ptr(new DistributingEvent(address, priority, name,
std::chrono::milliseconds(deactivation_time), event_notifier));
address_event_map.emplace(address, event_ptr);
name_event_map.emplace(name, event_ptr);
}
catch (json::exception &e)
{
CLOG(ERROR, INPUT_LOGGER) << "Matrix config-file corrupted: " << e.what();
exit(EXIT_FAILURE);
}
}
}
}
}