From 865772214b95085db2fa777df9e4a8f14be0495c Mon Sep 17 00:00:00 2001 From: Jonas Zeunert Date: Fri, 8 Mar 2019 20:46:46 +0100 Subject: [PATCH] added outputrequesthandler --- .idea/codeStyles/codeStyleConfig.xml | 5 + FlippR-Driver/CMakeLists.txt | 19 +- FlippR-Driver/cli/PrintHandler.h | 2 +- FlippR-Driver/cli/main.cpp | 36 ++-- .../json_example/output/Sound_Config.json | 5 + .../json_example/output_socket_solenoid.json | 10 ++ .../src/utility/InputSocketConnection.cpp | 5 + .../src/utility/InputSocketConnection.h | 18 ++ .../src/utility/InputSocketHandler.h | 11 +- .../src/utility/OutputRequestHandler.cpp | 168 ++++++++++++++++++ .../src/utility/OutputRequestHandler.h | 39 ++++ .../src/utility/OutputSocketHandler.h | 2 + FlippR-Driver/src/utility/SocketHandler.cpp | 12 +- FlippR-Driver/src/utility/SocketHandler.h | 10 +- 14 files changed, 310 insertions(+), 32 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 FlippR-Driver/contrib/json_example/output_socket_solenoid.json create mode 100644 FlippR-Driver/src/utility/InputSocketConnection.cpp create mode 100644 FlippR-Driver/src/utility/InputSocketConnection.h create mode 100644 FlippR-Driver/src/utility/OutputRequestHandler.cpp create mode 100644 FlippR-Driver/src/utility/OutputRequestHandler.h diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/FlippR-Driver/CMakeLists.txt b/FlippR-Driver/CMakeLists.txt index 3de33b2..90d042b 100644 --- a/FlippR-Driver/CMakeLists.txt +++ b/FlippR-Driver/CMakeLists.txt @@ -96,6 +96,9 @@ endif(BUILD_SHARED_LIB) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src) 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 ######################### find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS}) if(Boost_FOUND) @@ -107,9 +110,6 @@ else() message (FATAL_ERROR "Can't find Boost.") endif(Boost_FOUND) -# Set libraries include path -target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/${LIB_DIR}) - ##################### WIRING_PI ########################## find_library(wiringPi_LIB wiringPi) if(NOT wiringPi_LIB) @@ -122,25 +122,32 @@ if(NOT crypt_LIB) message(FATAL_ERROR "Could not find crypt library") endif() target_link_libraries(${PROJECT_NAME} PUBLIC ${crypt_LIB}) -## EASYLOGGING + +##################### EASYLOGGING ######################### set(EASYLOGGING_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${LIB_DIR}/easylogging) add_library(Easylogging STATIC ${EASYLOGGING_INCLUDE_DIR}/easylogging++.cc) target_include_directories(${PROJECT_NAME} PUBLIC ${EASYLOGGING_INCLUDE_DIR}) target_link_libraries(${PROJECT_NAME} PUBLIC Easylogging) -## CATCH +######################## CATCH ############################ set(CATCH_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/tests) add_library(Catch INTERFACE) target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}) -## THREAD +####################### THREAD ############################ find_package(Threads REQUIRED) if(NOT CMAKE_THREAD_LIBS_INIT) message(FATAL_ERROR, "Could not find libthread") endif() 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) add_subdirectory(tests) endif(ENABLE_TESTING) diff --git a/FlippR-Driver/cli/PrintHandler.h b/FlippR-Driver/cli/PrintHandler.h index a19097f..36355d0 100644 --- a/FlippR-Driver/cli/PrintHandler.h +++ b/FlippR-Driver/cli/PrintHandler.h @@ -6,7 +6,7 @@ #define flippR_driver_PRINTHANDLER_H #include -#include "../src/input/implementation/EventHandler.h" +#include "input/EventHandler.h" #include "input/InputDriver.h" class PrintHandler : public flippR_driver::input::EventHandler diff --git a/FlippR-Driver/cli/main.cpp b/FlippR-Driver/cli/main.cpp index c459a23..3cbc723 100644 --- a/FlippR-Driver/cli/main.cpp +++ b/FlippR-Driver/cli/main.cpp @@ -16,6 +16,8 @@ using namespace flippR_driver; namespace po = boost::program_options; +PrintHandler* print_handler = nullptr; + void __sigint_handler(int param) { printf("Caught SIGINT... aborting!\n"); @@ -29,17 +31,9 @@ static void show_usage(const std::string &name) << "-osolc= -osc=]"; } -static void register_program_options(po::options_description &po_desc) +static void register_program_options(po::options_description &po_desc, boost::optional &input_pin_config_file, std::string &input_matrix_config_file, + boost::optional &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 input_pin_config_file; - std::string input_matrix_config_file; - - boost::optional 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() ("help", "print_help_message") ("input_pin_config,ipc", po::value>(&input_pin_config_file), "input pin config file") @@ -83,11 +77,29 @@ int main (int argc, char *argv[]) // registering sigint signal(SIGINT, __sigint_handler); - // todo work with boost program options po::options_description po_desc("Options"); - register_program_options(po_desc); + boost::optional input_pin_config_file; + std::string input_matrix_config_file; + boost::optional 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); return 0; diff --git a/FlippR-Driver/contrib/json_example/output/Sound_Config.json b/FlippR-Driver/contrib/json_example/output/Sound_Config.json index e3122ff..8b8f1ab 100644 --- a/FlippR-Driver/contrib/json_example/output/Sound_Config.json +++ b/FlippR-Driver/contrib/json_example/output/Sound_Config.json @@ -2,6 +2,11 @@ "deactivation_time_milliseconds" : 10, "sounds" : [ + { + "id" : -1, //todo should be 0 + "address" : 0, + "description" : "Deactivate Sound" + }, { "id" : 0, "address" : "22", diff --git a/FlippR-Driver/contrib/json_example/output_socket_solenoid.json b/FlippR-Driver/contrib/json_example/output_socket_solenoid.json new file mode 100644 index 0000000..bd4aa12 --- /dev/null +++ b/FlippR-Driver/contrib/json_example/output_socket_solenoid.json @@ -0,0 +1,10 @@ +{ + "type" : "Solenoid", + "name" : "Right flap", + "functions" : + { + "trigger" : false + } +} +string function; +lamp \ No newline at end of file diff --git a/FlippR-Driver/src/utility/InputSocketConnection.cpp b/FlippR-Driver/src/utility/InputSocketConnection.cpp new file mode 100644 index 0000000..242b3a2 --- /dev/null +++ b/FlippR-Driver/src/utility/InputSocketConnection.cpp @@ -0,0 +1,5 @@ +// +// Created by rhetenor on 3/6/19. +// + +#include "InputSocketConnection.h" diff --git a/FlippR-Driver/src/utility/InputSocketConnection.h b/FlippR-Driver/src/utility/InputSocketConnection.h new file mode 100644 index 0000000..d9119f9 --- /dev/null +++ b/FlippR-Driver/src/utility/InputSocketConnection.h @@ -0,0 +1,18 @@ +// +// Created by rhetenor on 3/6/19. +// + +#ifndef FLIPPR_CODE_INPUTSOCKETCONNECTION_H +#define FLIPPR_CODE_INPUTSOCKETCONNECTION_H + + +#include + +class InputSocketConnection : public Poco::Net::TCPServerConnection +{ + + +}; + + +#endif //FLIPPR_CODE_INPUTSOCKETCONNECTION_H diff --git a/FlippR-Driver/src/utility/InputSocketHandler.h b/FlippR-Driver/src/utility/InputSocketHandler.h index b4f3629..80098ca 100644 --- a/FlippR-Driver/src/utility/InputSocketHandler.h +++ b/FlippR-Driver/src/utility/InputSocketHandler.h @@ -5,26 +5,25 @@ #ifndef FLIPPR_DRIVER_INPUTSOCKETHANDLER_H #define FLIPPR_DRIVER_INPUTSOCKETHANDLER_H -#include "SocketHandler.h" - #include "input/EventHandler.h" +#include + #include 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); }; } diff --git a/FlippR-Driver/src/utility/OutputRequestHandler.cpp b/FlippR-Driver/src/utility/OutputRequestHandler.cpp new file mode 100644 index 0000000..1feefbc --- /dev/null +++ b/FlippR-Driver/src/utility/OutputRequestHandler.cpp @@ -0,0 +1,168 @@ +// +// Created by rhetenor on 3/6/19. +// + +#include "OutputRequestHandler.h" + +#include +#include +namespace flippR_driver +{ +namespace utility +{ +using namespace Poco; +using namespace Poco::Net; + +OutputRequestHandler::OutputRequestHandler(std::shared_ptr 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 OutputRequestHandler::getPathSegements(Poco::URI uri) +{ + std::vector path_segments; + uri.getPathSegments(path_segments); + return path_segments; +} + + +} +} + diff --git a/FlippR-Driver/src/utility/OutputRequestHandler.h b/FlippR-Driver/src/utility/OutputRequestHandler.h new file mode 100644 index 0000000..eeaa323 --- /dev/null +++ b/FlippR-Driver/src/utility/OutputRequestHandler.h @@ -0,0 +1,39 @@ +// +// Created by rhetenor on 3/6/19. +// + +#ifndef FLIPPR_CODE_OUTPUTREQUESTHANDLER_H +#define FLIPPR_CODE_OUTPUTREQUESTHANDLER_H + +#include +#include +#include +namespace flippR_driver +{ +namespace utility +{ + +class OutputRequestHandler : public Poco::Net::HTTPRequestHandler +{ +public: + OutputRequestHandler(std::shared_ptr 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_driver; + +}; + +} +} + + +#endif //FLIPPR_CODE_OUTPUTREQUESTHANDLER_H diff --git a/FlippR-Driver/src/utility/OutputSocketHandler.h b/FlippR-Driver/src/utility/OutputSocketHandler.h index 09be765..9e8c9b0 100644 --- a/FlippR-Driver/src/utility/OutputSocketHandler.h +++ b/FlippR-Driver/src/utility/OutputSocketHandler.h @@ -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_driver); }; } diff --git a/FlippR-Driver/src/utility/SocketHandler.cpp b/FlippR-Driver/src/utility/SocketHandler.cpp index e939934..c580841 100644 --- a/FlippR-Driver/src/utility/SocketHandler.cpp +++ b/FlippR-Driver/src/utility/SocketHandler.cpp @@ -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(); diff --git a/FlippR-Driver/src/utility/SocketHandler.h b/FlippR-Driver/src/utility/SocketHandler.h index b1ccf50..5f49819 100644 --- a/FlippR-Driver/src/utility/SocketHandler.h +++ b/FlippR-Driver/src/utility/SocketHandler.h @@ -5,9 +5,8 @@ #ifndef FLIPPR_DRIVER_SOCKETHANDLER_H #define FLIPPR_DRIVER_SOCKETHANDLER_H -#include - -#include "json/json.hpp" +#include +#include 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); }; } }