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

Add support for avg-interval parameter for GET requests

parent 837091a9
......@@ -104,7 +104,7 @@ Formats of the other sensor-specific config-files are explained in the correspon
Dcdbpusher runs a HTTPS server which 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 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`.
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[;interval=timeInS]`. Specification of an interval (the part in square brackets) is completely optional. If one wishes, a interval in seconds can be specified there, to only get the average of the last [interval] seconds.
## MQTT topic
......
......@@ -6,6 +6,7 @@
*/
#include "HttpsServer.h"
#include "timestamp.h"
#include <iostream>
#include <memory>
......@@ -85,6 +86,7 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
std::string plugin = pathStrs[0];
//select code depending on request
/* ##################################################### GET ##################################################### */
if (method == "GET") {
//do some prior checks
......@@ -95,6 +97,15 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
} else {
std::string sensor = pathStrs[1];
std::string action = pathStrs[2];
uint64_t time = 0;
for (auto& p : queries) {
if (p.first == "interval") {
time = getTimestamp();
time -= S_TO_NS(std::stoul(p.second));
}
}
//process actual request
//check if query and action are valid values
//check if authkey is valid
......@@ -111,12 +122,17 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
const reading_t * const cache = s->getCache();
unsigned size = s->getCacheSize();
unsigned count = 0;
for(unsigned i = 0; i < size; i++) {
avg += cache[i].value;
if (cache[i].timestamp > time) {
avg += cache[i].value;
count++;
}
}
avg /= size;
if (count > 0)
avg /= count;
response = plugin + "::" + sensor + " Average of last " + std::to_string(size) + " values is " + std::to_string(avg);
response = plugin + "::" + sensor + " Average of last " + std::to_string(count) + " values is " + std::to_string(avg);
connection->set_status(server::connection::ok);
}
}
......@@ -127,6 +143,7 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
connection->set_status(server::connection::unauthorized);
}
}
/* ##################################################### PUT ##################################################### */
} else if (method == "PUT") {
std::string action = pathStrs[1];
......
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