refactored inputfactory

This commit is contained in:
Jonas Zeunert
2018-12-06 13:15:29 +01:00
parent 8557a22607
commit 1467bc6ddd
2 changed files with 53 additions and 35 deletions

View File

@@ -22,64 +22,77 @@ namespace flippR_driver
{ {
namespace input namespace input
{ {
std::shared_ptr<IInputDriver> InputDriverFactory::get_InputDriver(std::istream &input_config_stream, std::istream &matrix_config_stream) namespace InputDriverFactory
{
std::shared_ptr<IInputDriver>
get_InputDriver(std::istream &input_config_stream, std::istream &matrix_config_stream)
{ {
LoggerFactory::CreateInputLogger(); LoggerFactory::CreateInputLogger();
IBlockingQueue<Event> *event_queue = new BlockingQueue<Event>; IBlockingQueue<Event> *event_queue = new BlockingQueue<Event>;
json matrix_config;
matrix_config_stream >> matrix_config;
std::shared_ptr<IEventNotifier> event_notifier(new EventNotifier(event_queue)); std::shared_ptr<IEventNotifier> event_notifier(new EventNotifier(event_queue));
std::vector<std::shared_ptr<DistributingEvent>> events; std::vector<std::shared_ptr<DistributingEvent>> events;
std::map<std::string, std::shared_ptr<Event>> name_event_map; std::map<std::string, std::shared_ptr<Event>> name_event_map;
json matrix_config;
matrix_config_stream >> matrix_config;
create_input_events(matrix_config, events, name_event_map, event_notifier); create_input_events(matrix_config, events, name_event_map, event_notifier);
std::unique_ptr<IInputGPIOInterface> input_gpio_interface(new InputGPIOInterface(input_config_stream)); std::unique_ptr<IInputGPIOInterface> input_gpio_interface(new InputGPIOInterface(input_config_stream));
std::unique_ptr<IDetector> detector( std::unique_ptr<IDetector> detector(new Detector(std::move(input_gpio_interface), events));
new Detector(std::move(input_gpio_interface), events));
return std::shared_ptr<InputDriver>(new InputDriver(event_notifier, std::move(detector), name_event_map)); return std::shared_ptr<InputDriver>(new InputDriver(event_notifier, std::move(detector), name_event_map));
} }
namespace
void InputDriverFactory::create_input_events(json matrix_config, std::vector<std::shared_ptr<DistributingEvent>>& events,
std::map<std::string, std::shared_ptr<Event>>& name_event_map,
std::shared_ptr<IEventNotifier> event_notifier)
{ {
int global_bounce_time = matrix_config.at("global_bounce_time").get<json::number_integer_t>(); void create_input_events(json matrix_config, std::vector<std::shared_ptr<DistributingEvent>> &events,
auto& event_array = matrix_config.at("input_matrix"); std::map<std::string, std::shared_ptr<Event>> &name_event_map,
for (auto &json_event : event_array) std::shared_ptr<IEventNotifier> event_notifier)
{
int global_bounce_time = matrix_config.at("global_bounce_time").get<json::number_integer_t>();
auto &json_events = matrix_config.at("input_matrix");
for(auto &json_event : json_events)
{
std::shared_ptr<DistributingEvent> event = create_event(json_event, event_notifier, global_bounce_time);
events.push_back(event);
name_event_map.emplace(event->name, event);
}
}
static std::shared_ptr<DistributingEvent> create_event(json &json_event, std::shared_ptr<IEventNotifier> event_notifier, int bounce_time)
{ {
try try
{ {
std::string name = json_event.at("name"); std::string name = json_event.at("name");
char address = json_event.at("address").get<json::number_integer_t>(); char address = json_event.at("address").get<json::number_integer_t>();
int priority = json_event.at("priority").get<json::number_integer_t>(); int priority = json_event.at("priority").get<json::number_integer_t>();
int bounce_time = global_bounce_time;
auto it_bounce_time = json_event.find("bounce_time"); set_individual_bounce_time(json_event, bounce_time);
if (it_bounce_time != json_event.end())
{
bounce_time = it_bounce_time->get<json::number_integer_t>();
}
std::shared_ptr<DistributingEvent> event_ptr(new DistributingEvent(address, priority, name, return std::make_shared<DistributingEvent>(address, priority, name, std::chrono::milliseconds(bounce_time), event_notifier);
std::chrono::milliseconds(bounce_time), event_notifier));
events.push_back(event_ptr);
name_event_map.emplace(name, event_ptr);
} }
catch (json::exception &e) catch(json::exception &e)
{ {
CLOG(ERROR, INPUT_LOGGER) << "Matrix config-file corrupted: " << e.what(); CLOG(ERROR, INPUT_LOGGER) << "Matrix config-file corrupted: " << e.what();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
}
static void set_individual_bounce_time(json &json_event, int &bounce_time)
{
auto it_bounce_time = json_event.find("bounce_time");
if(it_bounce_time != json_event.end())
{
bounce_time = it_bounce_time->get<json::number_integer_t>();
}
}
} }
} }
}
}
}

View File

@@ -11,6 +11,8 @@
#include <istream> #include <istream>
#include <atomic> #include <atomic>
#include <boost/optional.hpp>
#include "Detector.h" #include "Detector.h"
#include "input/IInputDriver.h" #include "input/IInputDriver.h"
@@ -22,17 +24,20 @@ namespace flippR_driver
{ {
namespace input namespace input
{ {
class InputDriverFactory namespace InputDriverFactory
{ {
public:
static std::shared_ptr<IInputDriver> get_InputDriver(std::istream& input_config_stream, std::istream& matrix_config_stream); static std::shared_ptr<IInputDriver> get_InputDriver(std::istream& input_config_stream, std::istream& matrix_config_stream);
private: namespace
{
static void create_input_events(nlohmann::json matrix_config, static void create_input_events(nlohmann::json matrix_config,
std::vector<std::shared_ptr<DistributingEvent>>& events, std::vector<std::shared_ptr<DistributingEvent>> &events,
std::map<std::string, std::shared_ptr<Event>>& name_event_map, std::map<std::string, std::shared_ptr<Event>> &name_event_map,
std::shared_ptr<IEventNotifier> event_notifier); std::shared_ptr<IEventNotifier> event_notifier);
static std::shared_ptr<DistributingEvent> create_event(nlohmann::json &json_event, std::shared_ptr<IEventNotifier> event_notifier, int bounce_time);
static void set_individual_bounce_time(nlohmann::json &json_event, int &bounce_time);
}
}; };
} }
} }