Commit bb55483c authored by Micha Mueller's avatar Micha Mueller
Browse files

Add HttpsServer support for GET requests of last sensor readings average

parent 19e3ff9c
......@@ -85,7 +85,7 @@ Formats of the other sensor-specific config-files are explained in the correspon
Dcdbpusher provides some functionality to be controlled over a REST API. The API is by default hosted at port 8000 on the localhost but the address can be changed in the [`global.conf`](#GC).
Currently dcdbpusher allows to switch plugins on or off via the REST API. To do so, one has to send a `PUT` request of the form `host:port/pluginName/start|stop?authkey=YourToken` to the API.
Currently dcdbpusher allows to switch plugins on or off or to get the sensor reading average of the last values via the REST API. To switch plugins on or off, one has to send a `PUT` request of the form `host:port/pluginName/start|stop?authkey=YourToken` to the API. Requesting an average is done by sending a `GET` of the form `host:port/pluginName/sensorName/avg?authkey=YourToken`.
## MQTT topic
......
......@@ -23,15 +23,104 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
LOG(info) << "HttpsServer: " << ip << ":" << port << " connected";
boost::network::uri::uri uri("https://" + request.destination);
server::string_type path = uri.path();
server::string_type query = uri.query();
//select code depending on request
if(method == "PUT") {
LOG(info) << "HttpsServer: PUT request to " << request.destination << " was made";
if (method == "GET") {
LOG(info) << "HttpsServer: GET request of " << request.destination << " was made";
std::string response = "";
boost::network::uri::uri uri("https://" + request.destination);
server::string_type path = uri.path();
server::string_type query = uri.query();
std::string plugin = path;
std::string sensor = "";
std::string action = "";
std::string auth_key = query;
std::string auth_value = "";
//do some string processing
//split up query into key and value
size_t pos = auth_key.find("=");
if (pos != std::string::npos) {
auth_value = auth_key.substr(pos+1);
auth_key.erase(pos);
}
//split up path into plugin, sensor and action
if (plugin.size() >= 2) {
if (plugin[0] == '/') {
plugin.erase(0,1);
}
if (plugin[plugin.size() -1] == '/') {
plugin.erase(plugin.size() -1);
}
pos = plugin.find("/");
if (pos != std::string::npos) {
sensor = plugin.substr(pos+1);
plugin.erase(pos);
}
pos = sensor.find("/");
if (pos != std::string::npos) {
action = sensor.substr(pos+1);
sensor.erase(pos);
}
}
//finished string processing
LOG(info) << "Plugin " << plugin << " Sensor " << sensor << " Action " << action;
//process actual request
//check if query and action are valid values
if (auth_key == "authkey" && (action == "avg")) {
//check if authkey is valid
if (check_authkey(auth_value)) {
response = "Plugin not found!";
connection->set_status(server::connection::not_found);
for(auto& p : _httpsServer._plugins) {
if (p.id == plugin) {
response = "Sensor not found!";
for(auto s : p.configurator->getSensors()) {
if (s->getName() == sensor) {
uint64_t avg = 0;
const reading_t * const cache = s->getCache();
for(int i = 0; i < CACHE_SIZE; i++) {
avg += cache[i].value;
}
avg /= CACHE_SIZE;
std::stringstream sstream;
sstream << plugin << "::" << sensor << ": Average of last " << CACHE_SIZE << " values is " << avg;
response = sstream.str();
connection->set_status(server::connection::ok);
goto end;
}
}
}
}
} else {
response = "Invalid authentication key!";
connection->set_status(server::connection::unauthorized);
}
} else {
response = "GET requests should be of the form \"host:port/[plugin]/[sensor]/[action]?authkey=[token]\"\n"
"Where [plugin] names a plugin, [sensor] a sensor within this plugin, [action] is 'avg' and [token] is your authentication key";
connection->set_status(server::connection::bad_request);
}
end:
LOG(info) << "HttpsServer: Responding: " << response;
data << response << std::endl;
} else if (method == "PUT") {
LOG(info) << "HttpsServer: PUT request to " << request.destination << " was made";
std::string response = "";
std::string plugin = path;
std::string action = "";
......@@ -92,6 +181,7 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
response = "Invalid authentication key!";
connection->set_status(server::connection::unauthorized);
}
} else {
response = "PUT requests should be of the form \"host:port/[plugin]/[action]?authkey=[token]\"\n"
"Where [plugin] names a plugin, [action] is either \"start\" or \"stop\" and [token] is your authentication key";
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment