Commit 1ee72f08 authored by Alessio Netti's avatar Alessio Netti
Browse files

REST API fixes for topics

- By convention, users should supply '.' characters instead of '/' when
querying sensor topics over the REST API to avoid conflicts with URL
parsing; the characters are then replaced automatically in the server
parent bb671e05
......@@ -323,7 +323,7 @@ restResponse_t AnalyticsManager::REST(const vector<string>& pathStrs, const vect
for (auto &s : u->getBaseOutputs())
// Explicitly adding nodes to the ptree as to prevent BOOST from performing
// parsing on the node names
group.push_back(boost::property_tree::ptree::value_type(s->getName(), boost::property_tree::ptree(s->getMqtt())));
group.push_back(boost::property_tree::ptree::value_type("", boost::property_tree::ptree(s->getMqtt())));
sensors.add_child(a->getName(), group);
}
root.add_child(p.id, sensors);
......@@ -334,7 +334,7 @@ restResponse_t AnalyticsManager::REST(const vector<string>& pathStrs, const vect
found = true;
for (auto &u : a->getUnits())
for (auto &s : u->getBaseOutputs())
data << a->getName() << "." << s->getName() << " " << s->getMqtt() << "\n";
data << a->getName() << "::" << s->getMqtt() << "\n";
}
}
reply.data = data.str();
......@@ -356,7 +356,7 @@ restResponse_t AnalyticsManager::REST(const vector<string>& pathStrs, const vect
found = true;
boost::property_tree::ptree group;
for (auto &u : a->getUnits())
group.push_back(boost::property_tree::ptree::value_type(u->getName(), boost::property_tree::ptree()));
group.push_back(boost::property_tree::ptree::value_type("", boost::property_tree::ptree(u->getName())));
units.add_child(a->getName(), group);
}
root.add_child(p.id, units);
......@@ -366,7 +366,7 @@ restResponse_t AnalyticsManager::REST(const vector<string>& pathStrs, const vect
if (a->getStreaming() && (analyzer == "" || analyzer == a->getName())) {
found = true;
for (auto &u : a->getUnits())
data << a->getName() << "." << u->getName() << "\n";
data << a->getName() << "::" << u->getName() << "\n";
}
}
reply.data = data.str();
......@@ -437,7 +437,7 @@ restResponse_t AnalyticsManager::REST(const vector<string>& pathStrs, const vect
string unit = SensorNavigator::rootKey;
for (auto& p : queries)
if (p.first == "unit")
unit = p.second;
unit = MQTTChecker::convertTopic(p.second);
bool found=false, unitFound=false;
for (auto &p : _plugins)
......
......@@ -264,9 +264,10 @@ struct httpHandler_t {
//try getting the latest value
try {
int64_t val = mySensorCache.getSensor(pathStrs[0], (uint64_t) time * 1000000000);
std::string sensor = MQTTChecker::convertTopic(pathStrs[0]);
int64_t val = mySensorCache.getSensor(sensor, (uint64_t) time * 1000000000);
connection->set_status(httpServer_t::connection::ok);
response = "collectagent::" + pathStrs[0] + " Average of last " +
response = "collectagent::" + sensor + " Average of last " +
std::to_string(time) + " seconds is " + std::to_string(val);
//data << val << "\n";
//data << "Sid : " << sid.toString() << ", Value: " << val << "." << std::endl;
......
......@@ -6,6 +6,7 @@
#define PROJECT_MQTTCHECKER_H
#include <set>
#include <algorithm>
#include "logging.h"
#define MQTT_SEP '/'
......@@ -33,6 +34,20 @@ public:
return m;
}
/**
* @brief Replaces all characters in a MQTT topics matching a token and sanitizes the result
*
* @param topic The topic or suffix to be processed
* @param tok The token character to be replaced with forward slashes
* @return The processed MQTT topic
*/
static std::string convertTopic(const std::string& topic, const char tok='.') {
if(topic.empty()) return topic;
std::string newTopic = topic;
std::replace(newTopic.begin(), newTopic.end(), tok, MQTT_SEP);
return newTopic;
}
/**
* @brief Sanitizes and formats a MQTT topic or suffix
*
......
......@@ -212,7 +212,7 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
goto error;
}
std::string sensor = pathStrs[1];
std::string sensor = MQTTChecker::convertTopic(pathStrs[1]);
std::string action = pathStrs[2];
uint64_t time = 0;
......
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