added outputrequesthandler

This commit is contained in:
Jonas Zeunert
2019-03-08 20:46:46 +01:00
parent 4bf96cb2df
commit 865772214b
14 changed files with 310 additions and 32 deletions

View File

@@ -0,0 +1,5 @@
//
// Created by rhetenor on 3/6/19.
//
#include "InputSocketConnection.h"

View File

@@ -0,0 +1,18 @@
//
// Created by rhetenor on 3/6/19.
//
#ifndef FLIPPR_CODE_INPUTSOCKETCONNECTION_H
#define FLIPPR_CODE_INPUTSOCKETCONNECTION_H
#include <Poco/Net/TCPServerConnection.h>
class InputSocketConnection : public Poco::Net::TCPServerConnection
{
};
#endif //FLIPPR_CODE_INPUTSOCKETCONNECTION_H

View File

@@ -5,26 +5,25 @@
#ifndef FLIPPR_DRIVER_INPUTSOCKETHANDLER_H
#define FLIPPR_DRIVER_INPUTSOCKETHANDLER_H
#include "SocketHandler.h"
#include "input/EventHandler.h"
#include <Poco/JSON/JSON.h>
#include <string>
namespace flippR_driver
{
namespace utility
{
class InputSocketHandler : public SocketHandler, public input::EventHandler
class InputSocketHandler : public input::EventHandler
{
public:
explicit InputSocketHandler(boost::asio::io_service &service, std::string socket_file = "/var/run/user/" + std::to_string(getuid())
+ "flippR/S.flippR_input");
InputSocketHandler();
void handle(input::Event &event) override;
private:
nlohmann::json serialize_event(input::Event &event);
std::string serialize_event(input::Event &event);
};
}

View File

@@ -0,0 +1,168 @@
//
// Created by rhetenor on 3/6/19.
//
#include "OutputRequestHandler.h"
#include <Poco/URI.h>
#include <Poco/Exception.h>
namespace flippR_driver
{
namespace utility
{
using namespace Poco;
using namespace Poco::Net;
OutputRequestHandler::OutputRequestHandler(std::shared_ptr<output::OutputDriver> output_driver) :
output_driver(output_driver)
{}
void OutputRequestHandler::handleRequest(HTTPServerRequest &request,
HTTPServerResponse &response)
{
auto& path_segments = getPathSegments(URI(request.getPathSegments()));
std::string item_type = path_segments.at(0);
std::string item_name = path_segments.at(1);
std::string action = path_segments.at(2);
std::string score = "";
if(item_type == "displays")
{
score = path_segments.at(3);
}
response.setContentType("text/json");
response.setStatus(HTTPServerResponse::HTTP_OK);
try
{
parseRequest(item_type, item_name, action, score);
}
catch(NotFoundException &e)
{
response.setStatusAndReason(HTTPServerResponse::HTTP_NOT_FOUND, e.displayText());
}
}
void OutputRequestHandler::parseRequest(const std::string& item_type, const std::string& item_name, const std::string& action, const std::string& score)
{
if(item_type == "solenoids")
{
parseSolenoid(item_name, action);
}
else if(item_type == "lamps")
{
parseLamp(item_name, action);
}
else if(item_type == "sounds")
{
parseSound(item_name, action);
}
else if(item_type == "displays")
{
parseDisplay(item_name, action, score);
}
}
void OutputRequestHandler::parseSolenoid(const std::string& item_name, const std::string& action)
{
auto opt_solenoid = this->output_driver->get_solenoid(item_name);
if(!opt_solenoid)
{
throw new Poco::NotFoundException("No solenoid with name \"" + item_name + "\"!");
}
auto solenoid = opt_solenoid->get();
if(action == "trigger")
{
solenoid->trigger();
}
else
{
throw new Poco::NotFoundException("No action with name \"" + action + "\" on solenoids!");
}
}
void OutputRequestHandler::parseLamp(const std::string& item_name, const std::string& action)
{
auto opt_lamp = this->output_driver->get_lamp(item_name);
if(!opt_lamp)
{
throw new Poco::NotFoundException("No lamp with name \"" + item_name + "\"!");
}
auto lamp = opt_lamp->get();
if(action == "activate")
{
lamp->activate();
}
else if(action == "deactivate")
{
lamp->deactivate();
}
else
{
throw new Poco::NotFoundException("No action with name \"" + action + "\" on lamps!");
}
}
void OutputRequestHandler::parseSound(const std::string& item_name, const std::string& action)
{
auto opt_sound = this->output_driver->get_sound(item_name);
if(!opt_sound)
{
throw new Poco::NotFoundException("No sound with name \"" + item_name + "\"!");
}
auto sound = opt_sound->get();
if(action == "play")
{
sound->play();
}
else
{
throw new Poco::NotFoundException("No action with name \"" + action + "\" on sounds!");
}
}
void OutputRequestHandler::parseDisplay(const std::string& item_name, const std::string& action, const std::string& score)
{
uint8_t display_number = std::stoi(item_name);
auto opt_display = this->output_driver->get_display(display_number);
if(!opt_display)
{
throw new Poco::NotFoundException("No display with number \"" + item_name + "\"!");
}
auto display = opt_display->get();
if(action == "write_score")
{
unsigned int score = std::stoi(score);
display->write_score(score);
}
else
{
throw new Poco::NotFoundException("No Action with name \"" + action + "\" on sounds!");
}
}
std::vector<std::string> OutputRequestHandler::getPathSegements(Poco::URI uri)
{
std::vector<std::string> path_segments;
uri.getPathSegments(path_segments);
return path_segments;
}
}
}

View File

@@ -0,0 +1,39 @@
//
// Created by rhetenor on 3/6/19.
//
#ifndef FLIPPR_CODE_OUTPUTREQUESTHANDLER_H
#define FLIPPR_CODE_OUTPUTREQUESTHANDLER_H
#include <Poco/Net/HTTPRequestHandler.h>
#include <memory>
#include <output/OutputDriver.h>
namespace flippR_driver
{
namespace utility
{
class OutputRequestHandler : public Poco::Net::HTTPRequestHandler
{
public:
OutputRequestHandler(std::shared_ptr<output::OutputDriver> output_driver);
void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) override;
private:
void parseRequest(const std::string& item_type, const std::string& item_name, const std::string& action, const std::string& score = 0);
void parseSolenoid(const std::string& item_name, const std::string& action);
void parseLamp(const std::string& item_name, const std::string& action);
void parseSound(const std::string& item_name, const std::string& action);
void parseDisplay(const std::string& item_name, const std::string& action, const std::string& score);
private:
std::shared_ptr<output::OutputDriver> output_driver;
};
}
}
#endif //FLIPPR_CODE_OUTPUTREQUESTHANDLER_H

View File

@@ -6,6 +6,7 @@
#define FLIPPR_DRIVER_OUTPUTSOCKETHANDLER_H
#include "SocketHandler.h"
#include "output/OutputDriver.h"
namespace flippR_driver
{
@@ -15,6 +16,7 @@ namespace utility
class OutputSocketHandler : public SocketHandler
{
OutputSocketHandler(std::unique_ptr<output::OutputDriver> output_driver);
};
}

View File

@@ -10,11 +10,21 @@ namespace utility
{
using namespace nlohmann;
SocketHandler::SocketHandler(boost::asio::io_service &service, std::string socket_file) : socket(service)
SocketHandler::SocketHandler(boost::asio::io_service &service, std::string socket_file) : socket(boost::asio::io_service{})
{
this->socket.connect(socket_file);
}
SocketHandler::create_client(const std::string& socket_file)
{
this->socket.connect(socket_file);
}
SocketHandler::create_server(const std::string& socket_file)
{
}
void SocketHandler::write_to_socket(json &json)
{
std::string json_data = json.dump();

View File

@@ -5,9 +5,8 @@
#ifndef FLIPPR_DRIVER_SOCKETHANDLER_H
#define FLIPPR_DRIVER_SOCKETHANDLER_H
#include <boost/asio.hpp>
#include "json/json.hpp"
#include <Poco/Net/Socket.h>
#include <Poco/JSON/JSON.h>
namespace flippR_driver
{
@@ -16,14 +15,13 @@ namespace utility
class SocketHandler
{
public:
SocketHandler(boost::asio::io_service &service, std::string socket_file);
SocketHandler(std::string socket_file);
protected:
boost::asio::local::stream_protocol::socket socket;
protected:
void write_to_socket(nlohmann::json &data);
void write_to_socket(std::string &data);
};
}
}