working on big refactor

This commit is contained in:
Jonas Zeunert
2018-12-14 01:09:54 +01:00
parent f3100004b9
commit 2aee0f4f9d
67 changed files with 914 additions and 619 deletions

View File

@@ -12,9 +12,9 @@
#include "utility/LoggerFactory.h"
#include "InputDriver.h"
#include "EventNotifier.h"
#include "Detector.h"
#include "input/impl/InputDriver.h"
#include "input/impl/EventNotifier.h"
#include "input/impl/Detector.h"
namespace flippR_driver
{
@@ -26,63 +26,64 @@ namespace InputDriverFactory
using namespace nlohmann;
using namespace flippR_driver::utility;
std::shared_ptr<IInputDriver> get_InputDriver(std::istream &input_pin_stream, std::istream &matrix_config_stream)
std::shared_ptr<InputDriver> get_InputDriver(std::istream &input_pin_stream, std::istream &matrix_config_stream)
{
LoggerFactory::CreateInputLogger();
IBlockingQueue<Event> *event_queue = new BlockingQueue<Event>;
std::shared_ptr<IEventNotifier> event_notifier(new EventNotifier(event_queue));
std::shared_ptr<EventNotifier> event_notifier(new impl::EventNotifier(event_queue));
std::vector<std::shared_ptr<DistributingEvent>> events;
std::map<std::string, std::shared_ptr<Event>> name_event_map;
create_events(matrix_config_stream, events, name_event_map, event_notifier);
std::unique_ptr<IInputGPIOInterface> input_gpio_interface(new InputGPIOInterface(create_pin_map(input_pin_stream)));
std::unique_ptr<IDetector> detector(new Detector(std::move(input_gpio_interface), events));
std::unique_ptr<InputPinController> input_gpio_interface(new impl::InputPinController(create_pin_map(input_pin_stream)));
std::unique_ptr<Detector> detector(new impl::Detector(std::move(input_gpio_interface), events));
return std::make_shared<InputDriver>(event_notifier, std::move(detector), name_event_map);
return std::make_shared<impl::InputDriver>(event_notifier, std::move(detector), name_event_map);
}
namespace
{
void create_events(std::istream &matrix_config_stream, std::vector<std::shared_ptr<DistributingEvent>> &events,
std::map<std::string, std::shared_ptr<Event>> &name_event_map,
std::shared_ptr<IEventNotifier> event_notifier)
std::shared_ptr<EventNotifier> event_notifier)
{
json matrix_config;
matrix_config_stream >> matrix_config;
int global_bounce_time = matrix_config.at("global_bounce_time").get<uint8_t>();
auto &json_events = matrix_config.at("input_matrix");
for(auto &json_event : json_events)
try
{
int global_bounce_time = matrix_config.at("global_bounce_time").get<uint8_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);
}
}
std::shared_ptr<DistributingEvent> create_event(json &json_event, std::shared_ptr<IEventNotifier> event_notifier, int bounce_time)
{
try
{
std::string name = json_event.at("name");
char address = json_event.at("address").get<uint8_t>();
int priority = json_event.at("priority").get<uint8_t>();
set_individual_bounce_time(json_event, bounce_time);
return std::make_shared<DistributingEvent>(address, priority, name, std::chrono::milliseconds(bounce_time), event_notifier);
}
}
catch(json::exception &e)
{
CLOG(ERROR, INPUT_LOGGER) << "Matrix config-file corrupted: " << e.what();
CLOG(ERROR, INPUT_LOGGER) << "Event matrix config file corrupted: " << e.what();
exit(EXIT_FAILURE);
}
}
std::shared_ptr<DistributingEvent> create_event(json &json_event, std::shared_ptr<EventNotifier> event_notifier, int bounce_time)
{
std::string name = json_event.at("name");
char address = json_event.at("address").get<uint8_t>();
int priority = json_event.at("priority").get<uint8_t>();
set_individual_bounce_time(json_event, bounce_time);
return std::make_shared<DistributingEvent>(address, priority, name, std::chrono::milliseconds(bounce_time), event_notifier);
}
void set_individual_bounce_time(json &json_event, int &bounce_time)
{
auto it_bounce_time = json_event.find("bounce_time");
@@ -114,14 +115,9 @@ namespace
input_pin_map["data_address"] = pin_config.at("data").get<uint8_t>();
}
catch(json::type_error &e)
catch(json::exception &e)
{
CLOG(ERROR, INPUT_LOGGER) << "Input json corrupted! " << e.what();
exit(EXIT_FAILURE);
}
catch(json::out_of_range &e)
{
CLOG(ERROR, INPUT_LOGGER) << "Input json corrupted! " << e.what();
CLOG(ERROR, INPUT_LOGGER) << "Input pin config file corrupted! " << e.what();
exit(EXIT_FAILURE);
}