changed structure

This commit is contained in:
Johannes Wendel
2019-04-24 19:22:55 +02:00
parent 8785c701be
commit 218cb65dd4
54 changed files with 1348 additions and 5278 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

@@ -0,0 +1,43 @@
//
// Created by rhetenor on 27.11.18.
//
#include "InputSocketHandler.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include "json.hpp"
namespace flippR_driver
{
namespace utility
{
using namespace nlohmann;
InputSocketHandler::InputSocketHandler(boost::asio::io_service &service, std::string socket_file) : SocketHandler(service, std::move(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 "input/EventHandler.h"
#include <Poco/JSON/JSON.h>
#include <string>
namespace flippR_driver
{
namespace utility
{
class InputSocketHandler : public input::EventHandler
{
public:
InputSocketHandler();
void handle(input::Event &event) override;
private:
std::string serialize_event(input::Event &event);
};
}
}
#endif //FLIPPR_DRIVER_INPUTSOCKETHANDLER_H

View File

@@ -0,0 +1,21 @@
//
// Created by rhetenor on 4/15/19.
//
#include <Poco/Net/HTTPServerParams.h>
#include "OutputHTTPServer.h"
#include "OutputRequestHandlerFactory.h"
namespace flippR_driver
{
namespace utility
{
using namespace Poco::Net;
OutputHTTPServer::OutputHTTPServer(std::shared_ptr<output::OutputDriver> output_driver, Socket &socket) :
HTTPServer(new OutputRequestHandlerFactory(output_driver), socket, new HTTPServerParams())
{}
}
}

View File

@@ -0,0 +1,28 @@
//
// Created by rhetenor on 4/15/19.
//
#ifndef FLIPPR_CODE_OUTPUTHTTPSERVER_H
#define FLIPPR_CODE_OUTPUTHTTPSERVER_H
#include <Poco/Net/HTTPServer.h>
#include <Poco/Net/Socket.h>
#include "output/OutputDriver.h"
namespace flippR_driver
{
namespace utility
{
class OutputHTTPServer : public Poco::Net::HTTPServer
{
public:
explicit OutputHTTPServer(std::shared_ptr<output::OutputDriver> output_driver, Poco::Net::Socket &socket);
};
}
}
#endif //FLIPPR_CODE_OUTPUTHTTPSERVER_H

View File

@@ -0,0 +1,189 @@
//
// Created by rhetenor on 3/6/19.
//
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Exception.h>
#include <string>
#include "OutputRequestHandler.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.getURI()));
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(const NotFoundException &e)
{
response.setStatusAndReason(HTTPServerResponse::HTTP_NOT_FOUND, e.displayText());
}
catch(const Poco::InvalidArgumentException &e)
{
response.setStatusAndReason(HTTPServerResponse::HTTP_REASON_BAD_REQUEST, 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);
}
else
{
throw new Poco::NotFoundException("No item type called " + item_type);
}
}
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")
{
try
{
unsigned int score = std::stoi(score);
display->write_score(score);
}
catch(std::invalid_argument &e)
{
throw new Poco::InvalidArgumentException("Could not convert " + score + " to a number!\n" + e.what());
}
}
else
{
throw new Poco::NotFoundException("No Action with name \"" + action + "\" on sounds!");
}
}
std::vector<std::string> OutputRequestHandler::getPathSegments(Poco::URI uri)
{
std::vector<std::string> path_segments;
uri.getPathSegments(path_segments);
return path_segments;
}
}
}

View File

@@ -0,0 +1,46 @@
//
// Created by rhetenor on 3/6/19.
//
#ifndef FLIPPR_CODE_OUTPUTREQUESTHANDLER_H
#define FLIPPR_CODE_OUTPUTREQUESTHANDLER_H
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/URI.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);
std::vector<std::string> getPathSegments(Poco::URI uri);
private:
std::shared_ptr<output::OutputDriver> output_driver;
};
}
}
#endif //FLIPPR_CODE_OUTPUTREQUESTHANDLER_H

View File

@@ -0,0 +1,24 @@
//
// Created by rhetenor on 4/15/19.
//
#include "OutputRequestHandlerFactory.h"
#include "OutputRequestHandler.h"
namespace flippR_driver
{
namespace utility
{
using namespace Poco::Net;
OutputRequestHandlerFactory::OutputRequestHandlerFactory(std::shared_ptr<output::OutputDriver> output_driver) :
output_driver(output_driver)
{}
HTTPRequestHandler *OutputRequestHandlerFactory::createRequestHandler(const HTTPServerRequest &request)
{
return new OutputRequestHandler(this->output_driver);
}
}
}

View File

@@ -0,0 +1,31 @@
//
// Created by rhetenor on 4/15/19.
//
#ifndef FLIPPR_CODE_OUTPUTREQUESTHANDLERFACTORY_H
#define FLIPPR_CODE_OUTPUTREQUESTHANDLERFACTORY_H
#include <Poco/Net/HTTPRequestHandlerFactory.h>
#include <memory>
#include "output/OutputDriver.h"
namespace flippR_driver
{
namespace utility
{
class OutputRequestHandlerFactory :: public Poco::Net::HTTPRequestHandlerFactory
{
public:
explicit OutputRequestHandlerFactory(std::shared_ptr<output::OutputDriver> output_driver);
Poco::Net::HTTPRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest& request) override;
private:
std::shared_ptr<output::OutputDriver> output_driver;
};
}
}
#endif //FLIPPR_CODE_OUTPUTREQUESTHANDLERFACTORY_H

View File

@@ -0,0 +1,5 @@
//
// Created by rhetenor on 11.12.18.
//
#include "OutputSocketHandler.h"

View File

@@ -0,0 +1,25 @@
//
// Created by rhetenor on 11.12.18.
//
#ifndef FLIPPR_DRIVER_OUTPUTSOCKETHANDLER_H
#define FLIPPR_DRIVER_OUTPUTSOCKETHANDLER_H
#include "SocketHandler.h"
#include "output/OutputDriver.h"
namespace flippR_driver
{
namespace utility
{
class OutputSocketHandler : public SocketHandler
{
OutputSocketHandler(std::unique_ptr<output::OutputDriver> output_driver);
};
}
}
#endif //FLIPPR_DRIVER_OUTPUTSOCKETHANDLER_H