Commit 71b60c6b authored by Michael Ott's avatar Michael Ott
Browse files

Check regularly for modified published sensors

parent 9d109dfa
......@@ -250,10 +250,7 @@ int main(int argc, char *argv[])
_httpsServer = new RestAPI(restAPISettings, hierarchySettings, _cassandraConnection, io);
_configuration->readRestAPIUsers(_httpsServer);
LOG(info) << "Retrieving published sensor names and topics...";
if(!_httpsServer->buildTree()) {
throw std::runtime_error("Unable to initialize the sensor navigator!");
}
_httpsServer->checkPublishedSensorsAsync();
if (globalSettings.daemonize) {
//boost.log does not support forking officially.
......
......@@ -27,6 +27,7 @@
#include "RestAPI.h"
#include "dcdb/unitconv.h"
#include "timestamp.h"
#include <sstream>
#include <boost/property_tree/json_parser.hpp>
......@@ -44,6 +45,8 @@ RestAPI::RestAPI(serverSettings_t settings,
RESTHttpsServer(settings, io),
_connection(cassandraConnection),
_updating(false),
_timer(io, boost::posix_time::seconds(0)),
_publishedSensorsWritetime(0),
_hierarchySettings(hierarchySettings) {
//Configuring endpoints
......@@ -68,6 +71,8 @@ bool RestAPI::buildTree() {
//Spinlock to ensure that only one sensor navigator update runs at a time
while(_updating.exchange(true)) {}
LOG(info) << "Retrieving published sensor names and topics...";
//Get the list of all public sensors and topics.
if(!_sensorConfig || _sensorConfig->getPublicSensorsVerbose(publicSensors)!=DCDB::SC_OK) {
LOG(error) << "Unable to fetch list of public sensors!";
......@@ -79,8 +84,8 @@ bool RestAPI::buildTree() {
std::shared_ptr<MetadataStore> newMetadataStore = std::make_shared<MetadataStore>();
for(auto& s : publicSensors) {
topics.push_back(s.pattern);
newMetadataStore->store(s.pattern, DCDB::PublicSensor::publicSensorToMetadata(s));
topics.push_back(s.name);
newMetadataStore->store(s.name, DCDB::PublicSensor::publicSensorToMetadata(s));
}
//Build the tree navigator
......@@ -90,12 +95,26 @@ bool RestAPI::buildTree() {
//Replacing the old navigator and metadata store
_navigator = newNavigator;
_metadataStore = newMetadataStore;
_sensorConfig->getPublishedSensorsWritetime(_publishedSensorsWritetime);
_updating.store(false);
LOG(info) << "Built a sensor navigator of size " << std::to_string(_navigator->getTreeSize()) << " and depth " << std::to_string(_navigator->getTreeDepth()) << ".";
return true;
}
void RestAPI::checkPublishedSensorsAsync() {
uint64_t t;
if (_sensorConfig->getPublishedSensorsWritetime(t) == DCDB::SC_OK) {
if (t > _publishedSensorsWritetime) {
LOG(debug) << "Fetching updated sensors";
buildTree();
}
}
_timer.expires_at(timestamp2ptime(getTimestamp() + S_TO_NS(60)));
_timer.async_wait(std::bind(&RestAPI::checkPublishedSensorsAsync, this));
}
//Dummy GET request to create a datasource. All necessary checks that could be peformed here are
//already done by the RESTAPIServer (e.g., user credentials, connectivity to the DB,...).
void RestAPI::GET_datasource(endpointArgs) {
......
......@@ -74,6 +74,7 @@ public:
//Builds the internal sensor navigator
bool buildTree();
void checkPublishedSensorsAsync();
private:
......@@ -162,6 +163,8 @@ private:
DCDB::SensorConfig* _sensorConfig;
DCDB::SensorDataStore* _sensorDataStore;
atomic<bool> _updating;
boost::asio::deadline_timer _timer;
uint64_t _publishedSensorsWritetime;
std::string _separator;
hierarchySettings_t _hierarchySettings;
......
Markdown is supported
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