implemented socket handler

This commit is contained in:
Jonas Zeunert
2018-11-27 02:07:25 +01:00
parent 82ced6ec3c
commit 6b6b185f8e
9 changed files with 140 additions and 7 deletions

View File

@@ -0,0 +1,42 @@
//
// Created by rhetenor on 27.11.18.
//
#include "InputSocketHandler.h"
#include "boost/date_time/posix_time/posix_time.hpp"
#include <chrono>
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;
}
}
}

View File

@@ -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 <string>
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

View File

@@ -62,9 +62,9 @@ private:
private:
std::mutex output_item_mutex;
std::map<std::string, uint8_t> 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<std::string, uint8_t> 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<std::string, uint8_t> 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<std::string, uint8_t> 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<std::string, uint8_t> 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<std::string, uint8_t> pins_display;// {{"i2c_address", 0}, {"pin_base", 0}, {"fire", 0}, {"A", 0}, {"B", "0"}, {"C", "0"}, {"D", 0}, {"E", 0}, {"F", 0}, {"G", 0}};
};
}

View File

@@ -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()));
}
}
}

View File

@@ -0,0 +1,32 @@
//
// Created by rhetenor on 27.11.18.
//
#ifndef FLIPPR_DRIVER_SOCKETHANDLER_H
#define FLIPPR_DRIVER_SOCKETHANDLER_H
#include <boost/asio.hpp>
#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