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

Add support for plugin/sensor discovery to REST server

parent 017d5e8f
......@@ -102,9 +102,42 @@ Formats of the other sensor-specific config-files are explained in the correspon
## <a name="restApi">REST API</a>
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).
Dcdbpusher runs a HTTPS server which provides some functionality to be controlled over a RESTful API. The API is by default hosted at port 8000 on the localhost but the address can be changed in the [`global.conf`](#GC).
A HTTPS request to dcdbpusher should have the following format: `[GET|PUT] host:port[ressource]?[queries]`.
Tables with allowed ressources sorted by REST methods can be found below. A query consists of a key-value pair of the format `key=value`. For every request at least the authentication token has to be appended as query. Multiple queries are separated by semicolons(';').
### Table of queries
| Key | Value | Explanation |
|:--- |:----- |:----------- |
| authkey | Your authentication token | The authentication token is required to verify that you are allowed to make this particular request. The authkey query must be included by every request
| interval | Time-value in [s] | Only for GET request of sensor readings average. One can (optionally) specify a custom time interval for which the average of sensor readings is calculated. By default, every sensor reading in the cache is used to calculate the average.
### Ressources for GET request
| Ressource | Explanation |
|:--------- |:----------- |
| /help | Responds with a small cheatsheet which presents all currently supported ressources.
| /plugins | (Discovery) Returns a list of all currently loaded plugins.
| /[plugin]/sensors | (Discovery) Returns a list of all sensors which belong to [plugin]. To find out which plugins are available one can request the `/plugins` ressource.
| /[plugin]/[sensor]/avg | Calculates and returns the average of the last sensor readings. Can be combined with the interval query.
### Ressources for PUT request
| Ressource | Explanation |
|:--------- |:----------- |
| /[plugin]/[action] | One can request to do a action on the specified plugin. Currently supported actions are `start` and `stop` which start or stop the polling of the sensors of the plugin.
### Examples
Two examples for HTTPS requests:
* `GET https://localhost:8000/sysfs/freq1/avg?authkey=myToken;interval=15`
* `PUT https://localhost:8000/bacnet/stop?authkey=myToken`
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
......
......@@ -122,15 +122,12 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
"least the query ?authkey=[token] at the end. Multiple queries\n"
"need to be separated by semicolons(';')\n";
} else if (pathStrs[0] == "plugins") {
/* TODO
boost::property_tree::ptree root, plugins;
boost::property_tree::write_info(data, root);
for(auto& p : _httpsServer._plugins) {
plugins.put(p.id, "");
}
root.add_child("plugins", plugins);
*/
boost::property_tree::write_info(data, root);
} else {
//do some prior checks
......@@ -141,7 +138,23 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
}
if (pathStrs[1] == "sensors") {
//TODO make (JSON) list of sensors
response = "Plugin not found!";
connection->set_status(server::connection::not_found);
for(auto& p : _httpsServer._plugins) {
if (p.id == pathStrs[0]) {
boost::property_tree::ptree root, sensors;
for(auto s : p.configurator->getSensors()) {
sensors.put(s->getName(), "");
}
root.add_child(p.id, sensors);
boost::property_tree::write_info(data, root);
response = "";
connection->set_status(server::connection::ok);
break;
}
}
} else {
if (pathStrs.size() < 3) {
......
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