Commit 951d4479 authored by Michael Ott's avatar Michael Ott
Browse files

Move actual query from dcdbquery to libdcdb in new class Sensor

parent 0093452f
......@@ -9,6 +9,7 @@ CXXFLAGS = -O2 -ggdb -Wall -Werror \
# List of object files to build and the derived list of corresponding source files
OBJS = src/connection.o \
src/sensor.o \
src/sensordatastore.o \
src/timestamp.o \
src/sensorconfig.o \
......
/*
* Sensor.h
*
* Created on: 28 Apr 2017
* Author: ottmi
*/
#ifndef LIB_INCLUDE_DCDB_SENSOR_H_
#define LIB_INCLUDE_DCDB_SENSOR_H_
#include <string>
#include <list>
#include "dcdb/sensordatastore.h"
#include "dcdb/timestamp.h"
namespace DCDB {
class Sensor {
public:
Sensor(DCDB::Connection* connection, std::string publicName);
virtual ~Sensor();
void query(std::list<SensorDataStoreReading>& reading, TimeStamp& start, TimeStamp& end);
private:
Connection* connection;
std::string publicName;
};
} /* namespace DCDB */
#endif /* LIB_INCLUDE_DCDB_SENSOR_H_ */
/*
* Sensor.cpp
*
* Created on: 28 Apr 2017
* Author: ottmi
*/
#include <iostream>
#include <dcdb/sensor.h>
#include <dcdb/sensorconfig.h>
#include <dcdb/virtualsensor.h>
namespace DCDB {
Sensor::Sensor(DCDB::Connection* connection, std::string publicName) {
this->connection = connection;
this->publicName = publicName;
}
Sensor::~Sensor() {
// TODO Auto-generated destructor stub
}
void Sensor::query(std::list<SensorDataStoreReading>& result, TimeStamp& start, TimeStamp& end) {
/* Initialize the SensorConfig interface */
DCDB::SensorConfig sensorConfig(connection);
DCDB::SensorDataStore sensorDataStore(connection);
DCDB::PublicSensor publicSensor;
switch (sensorConfig.getPublicSensorByName(publicSensor, publicName.c_str())) {
case DCDB::SC_OK:
break;
case DCDB::SC_INVALIDSESSION:
std::cout << "Invalid session." << std::endl;
return;
case DCDB::SC_UNKNOWNSENSOR:
std::cout << "Unknown sensor: " << publicName << std::endl;
return;
default:
std::cout << "Unknown error." << std::endl;
return;
}
if (publicSensor.is_virtual) {
DCDB::VSensor vSen(connection, publicSensor);
vSen.query(result, start, end);
}
else {
/* Expand the pattern into a list of existing sensors in the time range */
std::list<DCDB::SensorId> sensorIds;
switch (sensorConfig.getSensorListForPattern(sensorIds, publicSensor.pattern, start, end)) {
case DCDB::SC_OK:
break;
case DCDB::SC_INVALIDPATTERN:
std::cout << "Invalid pattern." << std::endl;
return;
default:
std::cout << "Unknown error." << std::endl;
return;
}
/* Iterate over the expanded list of sensorIds and output the results in CSV format */
for (std::list<DCDB::SensorId>::iterator sit = sensorIds.begin(); sit != sensorIds.end(); sit++) {
sensorDataStore.query(result, *sit, start, end);
}
}
}
} /* namespace DCDB */
......@@ -128,14 +128,6 @@ void DCDBQuery::genOutput(DCDB::SensorDataStoreReading& reading)
}
}
void DCDBQuery::queryCallback(DCDB::SensorDataStoreReading& reading, void* userData)
{
DCDBQuery* self = (DCDBQuery*)userData;
/* Generate output for the reading */
self->genOutput(reading);
}
void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DCDB::TimeStamp start, DCDB::TimeStamp end)
{
/* Create a new connection to the database */
......@@ -148,7 +140,6 @@ void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DC
/* Initialize the SensorConfig interface */
DCDB::SensorConfig sensorConfig(connection);
DCDB::SensorDataStore sensorDataStore(connection);
/* Print the CSV header */
std::cout << "Sensor,Time,Value" << std::endl;
......@@ -185,47 +176,12 @@ void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DC
}
}
/* Lookup the sensor in the published sensors table */
DCDB::PublicSensor publicSensor;
switch (sensorConfig.getPublicSensorByName(publicSensor, it->c_str())) {
case DCDB::SC_OK:
break;
case DCDB::SC_INVALIDSESSION:
std::cout << "Invalid session." << std::endl;
return;
case DCDB::SC_UNKNOWNSENSOR:
std::cout << "Unknown sensor: " << *it << std::endl;
return;
default:
std::cout << "Unknown error." << std::endl;
return;
}
/* If this is a virtual sensor... */
if (publicSensor.is_virtual) {
sensorName = *it;
DCDB::VSensor vSen(connection, publicSensor);
vSen.queryCB(queryCallback, this, start, end);
}
else {
/* Expand the pattern into a list of existing sensors in the time range */
std::list<DCDB::SensorId> sensorIds;
switch (sensorConfig.getSensorListForPattern(sensorIds, publicSensor.pattern, start, end)) {
case DCDB::SC_OK:
break;
case DCDB::SC_INVALIDPATTERN:
std::cout << "Invalid pattern." << std::endl;
return;
default:
std::cout << "Unknown error." << std::endl;
return;
}
/* Iterate over the expanded list of sensorIds and output the results in CSV format */
for (std::list<DCDB::SensorId>::iterator sit = sensorIds.begin(); sit != sensorIds.end(); sit++) {
sensorName = *it;
sensorDataStore.queryCB(queryCallback, this, *sit, start, end);
}
std::list<DCDB::SensorDataStoreReading> results;
sensorName = *it;
DCDB::Sensor sensor(connection, sensorName);
sensor.query(results, start, end);
for (std::list<DCDB::SensorDataStoreReading>::iterator reading = results.begin(); reading != results.end(); reading++) {
genOutput(*reading);
}
}
......
......@@ -35,6 +35,7 @@
#include "dcdb/sensorconfig.h"
#include "dcdb/unitconv.h"
#include "dcdb/virtualsensor.h"
#include "dcdb/sensor.h"
#ifndef QUERY_H
#define QUERY_H
......
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