added distribution events
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace FlippR_Driver {
|
||||
namespace Input {
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace FlippR_Driver
|
||||
namespace Input
|
||||
{
|
||||
|
||||
Detector::Detector(std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::map<char, std::shared_ptr<Event>> events, std::shared_ptr<IEventNotifier> event_notifier) :
|
||||
Detector::Detector(std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::map<char, std::shared_ptr<DistributingEvent>> events, std::shared_ptr<IEventNotifier> event_notifier) :
|
||||
input_gpio_interface(std::move(input_gpio_interface)), events(events), is_running(true), event_notifier(event_notifier)
|
||||
{
|
||||
this->detect_thread = std::thread(&Detector::detect, this);
|
||||
@@ -44,7 +44,8 @@ void Detector::detect()
|
||||
{
|
||||
auto event = events.at(address);
|
||||
CLOG(DEBUG, INPUT_LOGGER) << "Found event " << event->name << "(" << std::to_string(event->address) << ").";
|
||||
event_notifier->distribute_event(event);
|
||||
|
||||
event->distribute();
|
||||
}
|
||||
catch(std::out_of_range& e)
|
||||
{
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "utilities/IInputGPIOInterface.h"
|
||||
|
||||
#include "IDetector.h"
|
||||
#include "Event.h"
|
||||
#include "DistributingEvent.h"
|
||||
#include "IEventNotifier.h"
|
||||
|
||||
namespace FlippR_Driver
|
||||
@@ -33,7 +33,7 @@ class Detector : public IDetector
|
||||
{
|
||||
|
||||
public:
|
||||
Detector(std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::map<char, std::shared_ptr<Event>> events, std::shared_ptr<IEventNotifier> event_notifier);
|
||||
Detector(std::unique_ptr<IInputGPIOInterface> input_gpio_interface, std::map<char, std::shared_ptr<DistributingEvent>> events, std::shared_ptr<IEventNotifier> event_notifier);
|
||||
~Detector();
|
||||
|
||||
private:
|
||||
@@ -43,7 +43,7 @@ private:
|
||||
private:
|
||||
std::unique_ptr<IInputGPIOInterface> input_gpio_interface;
|
||||
|
||||
std::map<char, std::shared_ptr<Event>> events;
|
||||
std::map<char, std::shared_ptr<DistributingEvent>> events;
|
||||
|
||||
std::shared_ptr<IEventNotifier> event_notifier;
|
||||
|
||||
|
||||
19
FlippR-Driver/src/input/DistributingEvent.cpp
Normal file
19
FlippR-Driver/src/input/DistributingEvent.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by rhetenor on 21.09.18.
|
||||
//
|
||||
|
||||
#include "DistributingEvent.h"
|
||||
|
||||
void FlippR_Driver::Input::DistributingEvent::distribute()
|
||||
{
|
||||
std::chrono::milliseconds elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - last_activation);
|
||||
if(elapsed_time > deactivation_time)
|
||||
{
|
||||
event_notifier->distribute_event(std::shared_ptr<Event>(this)); // todo why new shared ptr?
|
||||
}
|
||||
}
|
||||
|
||||
FlippR_Driver::Input::DistributingEvent::DistributingEvent(char address, int priority, std::string name,
|
||||
std::chrono::milliseconds deactivation_time, std::shared_ptr<IEventNotifier> event_notifier)
|
||||
: Event(address, priority, name), deactivation_time(deactivation_time), event_notifier(event_notifier)
|
||||
{}
|
||||
35
FlippR-Driver/src/input/DistributingEvent.h
Normal file
35
FlippR-Driver/src/input/DistributingEvent.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by rhetenor on 21.09.18.
|
||||
//
|
||||
|
||||
#ifndef FLIPPR_DRIVER_DISTRIBUTINGEVENT_H
|
||||
#define FLIPPR_DRIVER_DISTRIBUTINGEVENT_H
|
||||
|
||||
#include "Event.h"
|
||||
#include "IEventNotifier.h"
|
||||
|
||||
namespace FlippR_Driver
|
||||
{
|
||||
namespace Input
|
||||
{
|
||||
class DistributingEvent : public Event
|
||||
{
|
||||
public:
|
||||
DistributingEvent(char address, int priority, std::string name,
|
||||
std::chrono::milliseconds deactivation_time, std::shared_ptr<IEventNotifier> event_notifier);
|
||||
|
||||
void distribute();
|
||||
|
||||
public:
|
||||
std::chrono::milliseconds deactivation_time;
|
||||
|
||||
private:
|
||||
std::shared_ptr<IEventNotifier> event_notifier;
|
||||
std::chrono::time_point<std::chrono::high_resolution_clock> last_activation;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif //FLIPPR_DRIVER_DISTRIBUTINGEVENT_H
|
||||
@@ -45,7 +45,8 @@ std::shared_ptr<IInputDriver> InputDriverFactory::get_InputDriver(std::istream &
|
||||
|
||||
|
||||
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::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)
|
||||
@@ -56,8 +57,10 @@ void InputDriverFactory::create_input_events(json matrix_config, std::map<char,
|
||||
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 Event(address, priority, name));
|
||||
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);
|
||||
|
||||
@@ -29,7 +29,10 @@ public:
|
||||
static std::shared_ptr<IInputDriver> get_InputDriver(std::istream& input_config_stream, std::istream& matrix_config_stream);
|
||||
|
||||
private:
|
||||
static void create_input_events(nlohmann::json matrix_config, std::map<char, std::shared_ptr<Event>>& address_event_map, std::map<std::string, std::shared_ptr<Event>>& name_event_map);
|
||||
static void create_input_events(nlohmann::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);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user