diff --git a/FlippR-Driver/cli/PrintHandler.h b/FlippR-Driver/cli/PrintHandler.h index 3ba31cb..b5d3bc2 100644 --- a/FlippR-Driver/cli/PrintHandler.h +++ b/FlippR-Driver/cli/PrintHandler.h @@ -6,7 +6,7 @@ #define flippR_driver_PRINTHANDLER_H #include -#include "input/EventHandler.h" +#include "../src/input/EventHandler.h" #include "input/IInputDriver.h" class PrintHandler : public flippR_driver::input::EventHandler diff --git a/FlippR-Driver/include/input/Event.h b/FlippR-Driver/include/input/Event.h index 3a69f6d..0af04c8 100644 --- a/FlippR-Driver/include/input/Event.h +++ b/FlippR-Driver/include/input/Event.h @@ -30,6 +30,8 @@ namespace flippR_driver { std::string name; char address; int priority; + std::chrono::time_point last_activation; + }; bool operator==(const Event &left, const Event &right); diff --git a/FlippR-Driver/src/input/DistributingEvent.h b/FlippR-Driver/src/input/DistributingEvent.h index 69c38f3..6a5814f 100644 --- a/FlippR-Driver/src/input/DistributingEvent.h +++ b/FlippR-Driver/src/input/DistributingEvent.h @@ -37,7 +37,6 @@ private: ACTIVATED }; std::shared_ptr event_notifier; - std::chrono::time_point last_activation; ActivationState activation_state; diff --git a/FlippR-Driver/include/input/EventHandler.h b/FlippR-Driver/src/input/EventHandler.h similarity index 90% rename from FlippR-Driver/include/input/EventHandler.h rename to FlippR-Driver/src/input/EventHandler.h index 1f334df..c085236 100644 --- a/FlippR-Driver/include/input/EventHandler.h +++ b/FlippR-Driver/src/input/EventHandler.h @@ -12,9 +12,9 @@ #ifndef INPUTEVENTHANDLER_H_ #define INPUTEVENTHANDLER_H_ -#include "IInputDriver.h" +#include "input/IInputDriver.h" -#include "IEventHandler.h" +#include "input/IEventHandler.h" #include "input/Event.h" namespace flippR_driver diff --git a/FlippR-Driver/src/utility/InputSocketHandler.cpp b/FlippR-Driver/src/utility/InputSocketHandler.cpp new file mode 100644 index 0000000..6387e0d --- /dev/null +++ b/FlippR-Driver/src/utility/InputSocketHandler.cpp @@ -0,0 +1,42 @@ +// +// Created by rhetenor on 27.11.18. +// + +#include "InputSocketHandler.h" + +#include "boost/date_time/posix_time/posix_time.hpp" +#include + +namespace flippR_driver +{ +namespace utility +{ +using namespace nlohmann; + +InputSocketHandler::InputSocketHandler(boost::asio::io_service &service, std::string socket_file) : SocketHandler(service, socket_file) {} + +void InputSocketHandler::handle(flippR_driver::input::Event &event) +{ + json event_serialization = serialize_event(event); + + write_to_socket(event_serialization); +} + +json InputSocketHandler::serialize_event(input::Event &event) +{ + json serialized_event = json("event"); + + serialized_event["name"] = event.name; + + std::time_t activation_time = std::chrono::system_clock::to_time_t(event.last_activation); + boost::posix_time::ptime posix_time = boost::posix_time::from_time_t(activation_time); + serialized_event["activation_time"] = boost::posix_time::to_simple_string(posix_time); // todo learn to write time right + + serialized_event["priority"] = event.priority; + + return serialized_event; +} + + +} +} diff --git a/FlippR-Driver/src/utility/InputSocketHandler.h b/FlippR-Driver/src/utility/InputSocketHandler.h new file mode 100644 index 0000000..99c5de4 --- /dev/null +++ b/FlippR-Driver/src/utility/InputSocketHandler.h @@ -0,0 +1,32 @@ +// +// Created by rhetenor on 27.11.18. +// + +#ifndef FLIPPR_DRIVER_INPUTSOCKETHANDLER_H +#define FLIPPR_DRIVER_INPUTSOCKETHANDLER_H + +#include "SocketHandler.h" + +#include "input/IEventHandler.h" + +#include +namespace flippR_driver +{ +namespace utility +{ +class InputSocketHandler : public SocketHandler, IEventHandler +{ +public: + InputSocketHandler(boost::asio::io_service &service, std::string socket_file = "/var/run/user/" + std::to_string(getuid()) + + "flippR/S.flippR_input"); + + void handle(input::Event &event) override; + +private: + nlohmann::json serialize_event(input::Event &event); + +}; +} +} + +#endif //FLIPPR_DRIVER_INPUTSOCKETHANDLER_H diff --git a/FlippR-Driver/src/utility/OutputGPIOInterface.h b/FlippR-Driver/src/utility/OutputGPIOInterface.h index 18299b5..3522b3b 100644 --- a/FlippR-Driver/src/utility/OutputGPIOInterface.h +++ b/FlippR-Driver/src/utility/OutputGPIOInterface.h @@ -62,9 +62,9 @@ private: private: std::mutex output_item_mutex; - std::map pins_driver_board {{"i2c_address", 0}, {"pin_base", 0}, {"data", 0}, {"CL", 0}, {"latch-select-A", "0"}, {"latch-select-B", "0"}, {"latch-select-C", 0}, {"mux1", 0}, {"mux2", 0}, {"pin-select-A", 0}, {"pin-select-B", 0}, {"pin-select-C", 0}}; - std::map pins_sound {{"i2c_address", 0}, {"pin_base", 0}, {"fire", 0}, {"A", 0}, {"B", "0"}, {"C", "0"}, {"D", 0}, {"E", 0}, {"F", 0}, {"G", 0}}; - std::map pins_display {{"i2c_address", 0}, {"pin_base", 0}, {"fire", 0}, {"A", 0}, {"B", "0"}, {"C", "0"}, {"D", 0}, {"E", 0}, {"F", 0}, {"G", 0}}; + std::map pins_driver_board;// {{"i2c_address", 0}, {"pin_base", 0}, {"data", 0}, {"CL", 0}, {"latch-select-A", "0"}, {"latch-select-B", "0"}, {"latch-select-C", 0}, {"mux1", 0}, {"mux2", 0}, {"pin-select-A", 0}, {"pin-select-B", 0}, {"pin-select-C", 0}}; + std::map pins_sound;// {{"i2c_address", 0}, {"pin_base", 0}, {"fire", 0}, {"A", 0}, {"B", "0"}, {"C", "0"}, {"D", 0}, {"E", 0}, {"F", 0}, {"G", 0}}; + std::map pins_display;// {{"i2c_address", 0}, {"pin_base", 0}, {"fire", 0}, {"A", 0}, {"B", "0"}, {"C", "0"}, {"D", 0}, {"E", 0}, {"F", 0}, {"G", 0}}; }; } diff --git a/FlippR-Driver/src/utility/SocketHandler.cpp b/FlippR-Driver/src/utility/SocketHandler.cpp new file mode 100644 index 0000000..e939934 --- /dev/null +++ b/FlippR-Driver/src/utility/SocketHandler.cpp @@ -0,0 +1,26 @@ +// +// Created by rhetenor on 27.11.18. +// + +#include "SocketHandler.h" + +namespace flippR_driver +{ +namespace utility +{ +using namespace nlohmann; + +SocketHandler::SocketHandler(boost::asio::io_service &service, std::string socket_file) : socket(service) +{ + this->socket.connect(socket_file); +} + +void SocketHandler::write_to_socket(json &json) +{ + std::string json_data = json.dump(); + + boost::asio::write(this->socket, boost::asio::buffer(json_data, json_data.length())); +} + +} +} \ No newline at end of file diff --git a/FlippR-Driver/src/utility/SocketHandler.h b/FlippR-Driver/src/utility/SocketHandler.h new file mode 100644 index 0000000..b1ccf50 --- /dev/null +++ b/FlippR-Driver/src/utility/SocketHandler.h @@ -0,0 +1,32 @@ +// +// Created by rhetenor on 27.11.18. +// + +#ifndef FLIPPR_DRIVER_SOCKETHANDLER_H +#define FLIPPR_DRIVER_SOCKETHANDLER_H + +#include + +#include "json/json.hpp" + +namespace flippR_driver +{ +namespace utility +{ +class SocketHandler +{ +public: + SocketHandler(boost::asio::io_service &service, std::string socket_file); + +protected: + boost::asio::local::stream_protocol::socket socket; + +protected: + void write_to_socket(nlohmann::json &data); + +}; +} +} + + +#endif //FLIPPR_DRIVER_SOCKETHANDLER_H