added get item functionality to http server

This commit is contained in:
Jonas Zeunert
2019-04-30 13:12:53 +02:00
parent 97f4e0fb7f
commit 2ae71c2f53
3 changed files with 165 additions and 93 deletions

View File

@@ -22,11 +22,28 @@ OutputRequestHandler::OutputRequestHandler(std::shared_ptr<output::OutputDriver>
output_driver(output_driver)
{}
/**
* Handles a REST request with a URI form of:
*
* address/{item_type}/[item_name]/[action]/[score]
*
* Where
* {item_type} is either solenoids, lamps, sounds, displays
* [item_name] is the string name of an item (optional if you want to get the list of all available items)
* [action] is the particular action for the item:
* Solenoids: trigger
* lamps: activate, deactivate, status
* sounds: play
* displays: write_score (for this is the additional optional attribute [score]
*
* @param request
* @param response
*/
void OutputRequestHandler::handleRequest(HTTPServerRequest &request,
HTTPServerResponse &response)
{
auto path_segments = getPathSegments(URI(request.getURI()));
// ToDo catch exception
std::string item_type = path_segments.at(0);
std::string item_name = path_segments.at(1);
std::string action = path_segments.at(2);
@@ -42,7 +59,12 @@ void OutputRequestHandler::handleRequest(HTTPServerRequest &request,
try
{
parseRequest(item_type, item_name, action, score);
boost::optional<Poco::JSON::Object> json_response = parseRequest(item_type, item_name, action, score);
if(json_response)
{
std::ostream& ostr = response.send();
json_response->stringify(ostr);
}
}
catch(const NotFoundException &e)
{
@@ -54,23 +76,23 @@ void OutputRequestHandler::handleRequest(HTTPServerRequest &request,
}
}
void OutputRequestHandler::parseRequest(const std::string& item_type, const std::string& item_name, const std::string& action, const std::string& score)
boost::optional<Poco::JSON::Object> 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);
return parseSolenoid(item_name, action);
}
else if(item_type == "lamps")
{
parseLamp(item_name, action);
return parseLamp(item_name, action);
}
else if(item_type == "sounds")
{
parseSound(item_name, action);
return parseSound(item_name, action);
}
else if(item_type == "displays")
{
parseDisplay(item_name, action, score);
return parseDisplay(item_name, action, score);
}
else
{
@@ -78,8 +100,15 @@ void OutputRequestHandler::parseRequest(const std::string& item_type, const std:
}
}
void OutputRequestHandler::parseSolenoid(const std::string& item_name, const std::string& action)
boost::optional<Poco::JSON::Object> OutputRequestHandler::parseSolenoid(const std::string& item_name, const std::string& action)
{
if(item_name == "")
{
Poco::JSON::Object response;
response.set("solenoids", output_driver->get_solenoids());
return response;
}
auto opt_solenoid = this->output_driver->get_solenoid(item_name);
if(!opt_solenoid)
@@ -97,10 +126,19 @@ void OutputRequestHandler::parseSolenoid(const std::string& item_name, const std
{
throw new Poco::NotFoundException("No action with name \"" + action + "\" on solenoids!");
}
return {};
}
void OutputRequestHandler::parseLamp(const std::string& item_name, const std::string& action)
boost::optional<Poco::JSON::Object> OutputRequestHandler::parseLamp(const std::string& item_name, const std::string& action)
{
if(item_name == "")
{
Poco::JSON::Object response;
response.set("lamps", output_driver->get_lamps());
return response;
}
auto opt_lamp = this->output_driver->get_lamp(item_name);
if(!opt_lamp)
@@ -122,10 +160,19 @@ void OutputRequestHandler::parseLamp(const std::string& item_name, const std::st
{
throw new Poco::NotFoundException("No action with name \"" + action + "\" on lamps!");
}
return {};
}
void OutputRequestHandler::parseSound(const std::string& item_name, const std::string& action)
boost::optional<Poco::JSON::Object> OutputRequestHandler::parseSound(const std::string& item_name, const std::string& action)
{
if(item_name == "")
{
Poco::JSON::Object response;
response.set("sounds", output_driver->get_lamps());
return response;
}
auto opt_sound = this->output_driver->get_sound(item_name);
if(!opt_sound)
@@ -143,10 +190,18 @@ void OutputRequestHandler::parseSound(const std::string& item_name, const std::s
{
throw new Poco::NotFoundException("No action with name \"" + action + "\" on sounds!");
}
return {};
}
void OutputRequestHandler::parseDisplay(const std::string& item_name, const std::string& action, const std::string& score)
boost::optional<Poco::JSON::Object> OutputRequestHandler::parseDisplay(const std::string& item_name, const std::string& action, const std::string& score)
{
if(item_name == "")
{
Poco::JSON::Object response;
response.set("displays", output_driver->get_displays());
return response;
}
uint8_t display_number = std::stoi(item_name);
auto opt_display = this->output_driver->get_display(display_number);
@@ -162,7 +217,6 @@ void OutputRequestHandler::parseDisplay(const std::string& item_name, const std:
{
try
{
//TODO not compiling
unsigned int numerical_score = std::stoi(score);
display->write_score(numerical_score);
}
@@ -175,6 +229,8 @@ void OutputRequestHandler::parseDisplay(const std::string& item_name, const std:
{
throw new Poco::NotFoundException("No Action with name \"" + action + "\" on sounds!");
}
return {};
}
std::vector<std::string> OutputRequestHandler::getPathSegments(Poco::URI uri)

View File

@@ -7,8 +7,10 @@
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/URI.h>
#include <Poco/JSON/JSON.h>
#include <memory>
#include <boost/optional.hpp>
#include <Poco/JSON/Object.h>
#include "output/OutputDriver.h"
@@ -25,12 +27,12 @@ public:
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);
boost::optional<Poco::JSON::Object> 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);
boost::optional<Poco::JSON::Object> parseSolenoid(const std::string& item_name, const std::string& action);
boost::optional<Poco::JSON::Object> parseLamp(const std::string& item_name, const std::string& action);
boost::optional<Poco::JSON::Object> parseSound(const std::string& item_name, const std::string& action);
boost::optional<Poco::JSON::Object> parseDisplay(const std::string& item_name, const std::string& action, const std::string& score);
std::vector<std::string> getPathSegments(Poco::URI uri);