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

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

View File

@@ -96,6 +96,9 @@ endif(BUILD_SHARED_LIB)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/include) target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/include)
# Set libraries include path
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/${LIB_DIR})
######################### BOOST ######################### ######################### BOOST #########################
find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS}) find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})
if(Boost_FOUND) if(Boost_FOUND)
@@ -107,9 +110,6 @@ else()
message (FATAL_ERROR "Can't find Boost.") message (FATAL_ERROR "Can't find Boost.")
endif(Boost_FOUND) endif(Boost_FOUND)
# Set libraries include path
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/${LIB_DIR})
##################### WIRING_PI ########################## ##################### WIRING_PI ##########################
find_library(wiringPi_LIB wiringPi) find_library(wiringPi_LIB wiringPi)
if(NOT wiringPi_LIB) if(NOT wiringPi_LIB)
@@ -122,25 +122,32 @@ if(NOT crypt_LIB)
message(FATAL_ERROR "Could not find crypt library") message(FATAL_ERROR "Could not find crypt library")
endif() endif()
target_link_libraries(${PROJECT_NAME} PUBLIC ${crypt_LIB}) target_link_libraries(${PROJECT_NAME} PUBLIC ${crypt_LIB})
## EASYLOGGING
##################### EASYLOGGING #########################
set(EASYLOGGING_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${LIB_DIR}/easylogging) set(EASYLOGGING_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${LIB_DIR}/easylogging)
add_library(Easylogging STATIC ${EASYLOGGING_INCLUDE_DIR}/easylogging++.cc) add_library(Easylogging STATIC ${EASYLOGGING_INCLUDE_DIR}/easylogging++.cc)
target_include_directories(${PROJECT_NAME} PUBLIC ${EASYLOGGING_INCLUDE_DIR}) target_include_directories(${PROJECT_NAME} PUBLIC ${EASYLOGGING_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC Easylogging) target_link_libraries(${PROJECT_NAME} PUBLIC Easylogging)
## CATCH ######################## CATCH ############################
set(CATCH_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/tests) set(CATCH_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/tests)
add_library(Catch INTERFACE) add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}) target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
## THREAD ####################### THREAD ############################
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
if(NOT CMAKE_THREAD_LIBS_INIT) if(NOT CMAKE_THREAD_LIBS_INIT)
message(FATAL_ERROR, "Could not find libthread") message(FATAL_ERROR, "Could not find libthread")
endif() endif()
target_link_libraries(${PROJECT_NAME} PRIVATE ${Threads_LIBRARIES}) target_link_libraries(${PROJECT_NAME} PRIVATE ${Threads_LIBRARIES})
####################### POCO ##############################
find_package(Poco REQUIRED COMPONENTS Foundation Net JSON )
if(NOT Poco_FOUND)
message(FATAL_ERROR, "Could not find libPoco")
endif()
target_link_libraries(${PROJECT_NAME} PUBLIC Poco::Foundation Poco::Net Poco::JSON)
if(ENABLE_TESTING) if(ENABLE_TESTING)
add_subdirectory(tests) add_subdirectory(tests)
endif(ENABLE_TESTING) endif(ENABLE_TESTING)

View File

@@ -6,7 +6,7 @@
#define flippR_driver_PRINTHANDLER_H #define flippR_driver_PRINTHANDLER_H
#include <memory> #include <memory>
#include "../src/input/implementation/EventHandler.h" #include "input/EventHandler.h"
#include "input/InputDriver.h" #include "input/InputDriver.h"
class PrintHandler : public flippR_driver::input::EventHandler class PrintHandler : public flippR_driver::input::EventHandler

View File

@@ -16,6 +16,8 @@
using namespace flippR_driver; using namespace flippR_driver;
namespace po = boost::program_options; namespace po = boost::program_options;
PrintHandler* print_handler = nullptr;
void __sigint_handler(int param) void __sigint_handler(int param)
{ {
printf("Caught SIGINT... aborting!\n"); printf("Caught SIGINT... aborting!\n");
@@ -29,17 +31,9 @@ static void show_usage(const std::string &name)
<< "-osolc=<output_solenoid_config_file> -osc=<output_sound_config_file>]"; << "-osolc=<output_solenoid_config_file> -osc=<output_sound_config_file>]";
} }
static void register_program_options(po::options_description &po_desc) static void register_program_options(po::options_description &po_desc, boost::optional<std::string> &input_pin_config_file, std::string &input_matrix_config_file,
boost::optional<std::string> &output_pin_config_file, std::string &output_display_config_file, std::string &output_lamp_config_file, std::string &output_solenoid_config_file, std::string &output_sound_config_file)
{ {
boost::optional<std::string> input_pin_config_file;
std::string input_matrix_config_file;
boost::optional<std::string> output_pin_config_file;
std::string output_display_config_file;
std::string output_lamp_config_file;
std::string output_solenoid_config_file;
std::string output_sound_config_file;
po_desc.add_options() po_desc.add_options()
("help", "print_help_message") ("help", "print_help_message")
("input_pin_config,ipc", po::value<boost::optional<std::string>>(&input_pin_config_file), "input pin config file") ("input_pin_config,ipc", po::value<boost::optional<std::string>>(&input_pin_config_file), "input pin config file")
@@ -83,11 +77,29 @@ int main (int argc, char *argv[])
// registering sigint // registering sigint
signal(SIGINT, __sigint_handler); signal(SIGINT, __sigint_handler);
// todo work with boost program options
po::options_description po_desc("Options"); po::options_description po_desc("Options");
register_program_options(po_desc); boost::optional<std::string> input_pin_config_file;
std::string input_matrix_config_file;
boost::optional<std::string> output_pin_config_file;
std::string output_display_config_file;
std::string output_lamp_config_file;
std::string output_solenoid_config_file;
std::string output_sound_config_file;
register_program_options(po_desc, input_pin_config_file, input_matrix_config_file, output_pin_config_file, output_display_config_file, output_lamp_config_file, output_solenoid_config_file, output_sound_config_file);
// todo parse
if(input_pin_config_file)
{
print_handler = start_print_handler(*input_pin_config_file, input_matrix_config_file);
}
if(output_pin_config_file)
{
start_output_driver(*output_pin_config_file, )
}
while(1); while(1);
return 0; return 0;

View File

@@ -2,6 +2,11 @@
"deactivation_time_milliseconds" : 10, "deactivation_time_milliseconds" : 10,
"sounds" : "sounds" :
[ [
{
"id" : -1, //todo should be 0
"address" : 0,
"description" : "Deactivate Sound"
},
{ {
"id" : 0, "id" : 0,
"address" : "22", "address" : "22",

View File

@@ -0,0 +1,10 @@
{
"type" : "Solenoid",
"name" : "Right flap",
"functions" :
{
"trigger" : false
}
}
string function;
lamp

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 #ifndef FLIPPR_DRIVER_INPUTSOCKETHANDLER_H
#define FLIPPR_DRIVER_INPUTSOCKETHANDLER_H #define FLIPPR_DRIVER_INPUTSOCKETHANDLER_H
#include "SocketHandler.h"
#include "input/EventHandler.h" #include "input/EventHandler.h"
#include <Poco/JSON/JSON.h>
#include <string> #include <string>
namespace flippR_driver namespace flippR_driver
{ {
namespace utility namespace utility
{ {
class InputSocketHandler : public SocketHandler, public input::EventHandler class InputSocketHandler : public input::EventHandler
{ {
public: public:
explicit InputSocketHandler(boost::asio::io_service &service, std::string socket_file = "/var/run/user/" + std::to_string(getuid()) InputSocketHandler();
+ "flippR/S.flippR_input");
void handle(input::Event &event) override; void handle(input::Event &event) override;
private: 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 #define FLIPPR_DRIVER_OUTPUTSOCKETHANDLER_H
#include "SocketHandler.h" #include "SocketHandler.h"
#include "output/OutputDriver.h"
namespace flippR_driver namespace flippR_driver
{ {
@@ -15,6 +16,7 @@ namespace utility
class OutputSocketHandler : public SocketHandler class OutputSocketHandler : public SocketHandler
{ {
OutputSocketHandler(std::unique_ptr<output::OutputDriver> output_driver);
}; };
} }

View File

@@ -10,11 +10,21 @@ namespace utility
{ {
using namespace nlohmann; 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); 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) void SocketHandler::write_to_socket(json &json)
{ {
std::string json_data = json.dump(); std::string json_data = json.dump();

View File

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