Commit 84f185bc authored by Axel Auweter's avatar Axel Auweter
Browse files

Refactor DCDBLib to introduce DCDB namespace & prepare DB schema for virtual sensor support.

parent 4fd356ec
...@@ -30,7 +30,7 @@ int keepRunning; ...@@ -30,7 +30,7 @@ int keepRunning;
bool statistics; bool statistics;
uint64_t msgCtr; uint64_t msgCtr;
uint64_t pmsgCtr; uint64_t pmsgCtr;
SensorDataStore *mySensorDataStore; DCDB::SensorDataStore *mySensorDataStore;
std::string listenHost, cassandraHost, ttl; std::string listenHost, cassandraHost, ttl;
/* Normal termination (SIGINT, CTRL+C) */ /* Normal termination (SIGINT, CTRL+C) */
...@@ -93,7 +93,7 @@ void mqttCallback(SimpleMQTTMessage *msg) ...@@ -93,7 +93,7 @@ void mqttCallback(SimpleMQTTMessage *msg)
* into a valid SensorId. If successful, store * into a valid SensorId. If successful, store
* the record in the database. * the record in the database.
*/ */
SensorId sid; DCDB::SensorId sid;
if (sid.mqttTopicConvert(msg->getTopic())) { if (sid.mqttTopicConvert(msg->getTopic())) {
#if 0 #if 0
cout << "Topic decode successful:" cout << "Topic decode successful:"
...@@ -185,8 +185,8 @@ int main(int argc, char* const argv[]) { ...@@ -185,8 +185,8 @@ int main(int argc, char* const argv[]) {
* Allocate and initialize connection to Cassandra. * Allocate and initialize connection to Cassandra.
*/ */
std::string sdHost = cassandraHost; std::string sdHost = cassandraHost;
DCDBConnection* dcdbConn; DCDB::Connection* dcdbConn;
dcdbConn = new DCDBConnection(sdHost, 9042); dcdbConn = new DCDB::Connection(sdHost, 9042);
if (!dcdbConn->connect()) { if (!dcdbConn->connect()) {
std::cout << "Cannot connect to Cassandra!" << std::endl; std::cout << "Cannot connect to Cassandra!" << std::endl;
...@@ -201,7 +201,7 @@ int main(int argc, char* const argv[]) { ...@@ -201,7 +201,7 @@ int main(int argc, char* const argv[]) {
/* /*
* Allocate the SensorDataStore. * Allocate the SensorDataStore.
*/ */
mySensorDataStore = new SensorDataStore(dcdbConn); mySensorDataStore = new DCDB::SensorDataStore(dcdbConn);
/* /*
* Set TTL for data store inserts if TTL > 0. * Set TTL for data store inserts if TTL > 0.
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* @file * @file
* @brief This file contains parts of the public API for the * @brief This file contains parts of the public API for the
* DCDBLib library. * DCDBLib library.
* It contains the class definition of the DCDBConnection class, * It contains the class definition of the Connection class,
* that handles connections to the data store and schema * that handles connections to the data store and schema
* initialization. * initialization.
*/ */
...@@ -19,16 +19,18 @@ ...@@ -19,16 +19,18 @@
#include "cassandra.h" #include "cassandra.h"
#ifndef CONNECTION_H #ifndef DCDB_CONNECTION_H
#define CONNECTION_H #define DCDB_CONNECTION_H
namespace DCDB {
/* Forward-declaration of the implementation-internal classes */ /* Forward-declaration of the implementation-internal classes */
class DCDBConnectionImpl; class ConnectionImpl;
class DCDBConnection class Connection
{ {
private: private:
DCDBConnectionImpl* impl; /**< The object which implements the core functionality of this class */ ConnectionImpl* impl; /**< The object which implements the core functionality of this class */
public: public:
/** /**
...@@ -99,21 +101,23 @@ public: ...@@ -99,21 +101,23 @@ public:
bool initSchema(); bool initSchema();
/** /**
* @brief Standard constructor for DCDBConnections. * @brief Standard constructor for Connections.
* *
* If not set explicitly, hostname and port will default to localhost and 9042. * If not set explicitly, hostname and port will default to localhost and 9042.
*/ */
DCDBConnection(); Connection();
/** /**
* @brief Construct a DCDBConnection to the specific host and port. * @brief Construct a Connection to the specific host and port.
*/ */
DCDBConnection(std::string hostname, uint16_t port); Connection(std::string hostname, uint16_t port);
/** /**
* @brief Standard destructor for DCDBConnections. * @brief Standard destructor for Connections.
*/ */
virtual ~DCDBConnection(); virtual ~Connection();
}; };
#endif /* CONNECTION_H */ } /* End of namespace DCDB */
#endif /* DCDB_CONNECTION_H */
...@@ -22,12 +22,14 @@ ...@@ -22,12 +22,14 @@
#include "cassandra.h" #include "cassandra.h"
#ifndef SENSORCONFIG_H #ifndef DCDB_SENSORCONFIG_H
#define SENSORCONFIG_H #define DCDB_SENSORCONFIG_H
namespace DCDB {
class SensorConfigImpl; class SensorConfigImpl;
class DCDBPublicSensor class PublicSensor
{ {
public: public:
std::string name; std::string name;
...@@ -36,8 +38,8 @@ public: ...@@ -36,8 +38,8 @@ public:
std::string unit; std::string unit;
bool integrable; bool integrable;
DCDBPublicSensor(); PublicSensor();
DCDBPublicSensor(const DCDBPublicSensor &copy); PublicSensor(const PublicSensor &copy);
}; };
typedef enum { typedef enum {
...@@ -58,21 +60,22 @@ public: ...@@ -58,21 +60,22 @@ public:
SCError publishSensor(const char* publicName, const char* sensorPattern); SCError publishSensor(const char* publicName, const char* sensorPattern);
SCError unPublishSensor(const char* publicName); SCError unPublishSensor(const char* publicName);
SCError getPublicSensorNames(std::list<std::string>& publicSensors); SCError getPublicSensorNames(std::list<std::string>& publicSensors);
SCError getPublicSensorsVerbose(std::list<DCDBPublicSensor>& publicSensors); SCError getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors);
SCError getPublicSensorByName(DCDBPublicSensor& sensor, const char* publicName); SCError getPublicSensorByName(PublicSensor& sensor, const char* publicName);
SCError getSensorPattern(std::string& pattern, std::string publicName); SCError getSensorPattern(std::string& pattern, std::string publicName);
SCError getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern); SCError getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern);
SCError getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, DCDBTimeStamp start, DCDBTimeStamp end); SCError getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, TimeStamp start, TimeStamp end);
SCError setSensorScalingFactor(std::string publicName, double scalingFactor); SCError setSensorScalingFactor(std::string publicName, double scalingFactor);
SCError setSensorUnit(std::string publicName, std::string unit); SCError setSensorUnit(std::string publicName, std::string unit);
SCError setSensorIntegrable(std::string publicName, bool integrable); SCError setSensorIntegrable(std::string publicName, bool integrable);
SensorConfig(DCDBConnection* conn); SensorConfig(Connection* conn);
virtual ~SensorConfig(); virtual ~SensorConfig();
}; };
} /* End of namespace DCDB */
#endif /* CONFIG_H */ #endif /* DCDB_SENSORCONFIG_H */
...@@ -21,8 +21,10 @@ ...@@ -21,8 +21,10 @@
#include "timestamp.h" #include "timestamp.h"
#include "connection.h" #include "connection.h"
#ifndef SENSORDATASTORE_H_ #ifndef DCDB_SENSORDATASTORE_H
#define SENSORDATASTORE_H_ #define DCDB_SENSORDATASTORE_H
namespace DCDB {
typedef enum { typedef enum {
SDS_OK, SDS_OK,
...@@ -40,7 +42,7 @@ class SensorDataStoreReading ...@@ -40,7 +42,7 @@ class SensorDataStoreReading
{ {
public: public:
SensorId sensorId; SensorId sensorId;
DCDBTimeStamp timeStamp; TimeStamp timeStamp;
int64_t value; int64_t value;
#if 0 #if 0
...@@ -85,7 +87,7 @@ public: ...@@ -85,7 +87,7 @@ public:
* @param start Start of the time series. * @param start Start of the time series.
* @param end End of the time series. * @param end End of the time series.
*/ */
void query(std::list<SensorDataStoreReading>& result, SensorId& sid, DCDBTimeStamp& start, DCDBTimeStamp& end); void query(std::list<SensorDataStoreReading>& result, SensorId& sid, TimeStamp& start, TimeStamp& end);
/** /**
* @brief This function queries the integrated value (val * sec) * @brief This function queries the integrated value (val * sec)
...@@ -96,7 +98,7 @@ public: ...@@ -96,7 +98,7 @@ public:
* @param end End of the time series. * @param end End of the time series.
* @return SDS_OK if ok, SDS_EMPTYSET if not at least 2 readings in interval. * @return SDS_OK if ok, SDS_EMPTYSET if not at least 2 readings in interval.
*/ */
SDSQueryResult querySum(int64_t& result, SensorId& sid, DCDBTimeStamp& start, DCDBTimeStamp& end); SDSQueryResult querySum(int64_t& result, SensorId& sid, TimeStamp& start, TimeStamp& end);
/** /**
* @brief This function truncates all sensor data that is older than * @brief This function truncates all sensor data that is older than
...@@ -109,10 +111,10 @@ public: ...@@ -109,10 +111,10 @@ public:
* @brief A shortcut constructor for a SensorDataStore object * @brief A shortcut constructor for a SensorDataStore object
* that allows accessing the data store through a * that allows accessing the data store through a
* connection that is already established. * connection that is already established.
* @param conn The DCDBConnection object of an established * @param conn The Connection object of an established
* connection to Cassandra. * connection to Cassandra.
*/ */
SensorDataStore(DCDBConnection* conn); SensorDataStore(Connection* conn);
/** /**
* @brief The standard destructor for a SensorDatStore object. * @brief The standard destructor for a SensorDatStore object.
...@@ -120,5 +122,6 @@ public: ...@@ -120,5 +122,6 @@ public:
virtual ~SensorDataStore(); virtual ~SensorDataStore();
}; };
} /* End of namespace DCDB */
#endif /* SENSORDATASTORE_H_ */ #endif /* DCDB_SENSORDATASTORE_H */
...@@ -8,8 +8,10 @@ ...@@ -8,8 +8,10 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#ifndef SENSORID_H #ifndef DCDB_SENSORID_H
#define SENSORID_H #define DCDB_SENSORID_H
namespace DCDB {
/* Ensure that the unions and structs are created without padding */ /* Ensure that the unions and structs are created without padding */
#pragma pack(push,1) #pragma pack(push,1)
...@@ -122,4 +124,6 @@ public: ...@@ -122,4 +124,6 @@ public:
#pragma pack(pop) #pragma pack(pop)
#endif /* SENSORID_H */ } /* End of namespace DCDB */
#endif /* DCDB_SENSORID_H */
/* /*
* dcdbtimestamp.h * timestamp.h
* *
* Created on: Feb 18, 2015 * Created on: Feb 18, 2015
* Author: Axel Auweter * Author: Axel Auweter
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
/** /**
* @file * @file
* @brief This file is a companion to the sensordatastore API. * @brief This file is a companion to the sensordatastore API.
* It contains the DCDBTimeStamp class definition that helps in * It contains the TimeStamp class definition that helps in
* creating and modifying timestamps in the SensorDataStore. * creating and modifying timestamps in the SensorDataStore.
*/ */
...@@ -17,26 +17,28 @@ ...@@ -17,26 +17,28 @@
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#ifndef DCDBTIMESTAMP_H #ifndef DCDB_TIMESTAMP_H
#define DCDBTIMESTAMP_H #define DCDB_TIMESTAMP_H
class DCDBTimeStampConversionException : public std::runtime_error namespace DCDB {
class TimeStampConversionException : public std::runtime_error
{ {
public: public:
DCDBTimeStampConversionException():runtime_error("Time stamp conversion error.") {} TimeStampConversionException():runtime_error("Time stamp conversion error.") {}
}; };
/** /**
* @brief The DCDBTimeStamp class contains a single TimeStamp. * @brief The TimeStamp class contains a single TimeStamp.
*/ */
class DCDBTimeStamp class TimeStamp
{ {
protected: protected:
uint64_t raw; /**< The raw timestamp data (nanoseconds since Unix Epoch) */ uint64_t raw; /**< The raw timestamp data (nanoseconds since Unix Epoch) */
/** /**
* @brief Parses a string and tries to derive the time from it by * @brief Parses a string and tries to derive the time from it by
* guessing the format. Throws DCDBTimeStampException on failure. * guessing the format. Throws TimeStampException on failure.
* @param timestr A string containing a representation of time * @param timestr A string containing a representation of time
* @param localTime Denotes if the timestr contains local time instead of UTC * @param localTime Denotes if the timestr contains local time instead of UTC
*/ */
...@@ -47,27 +49,27 @@ public: ...@@ -47,27 +49,27 @@ public:
/** /**
* @brief Standard constructor. Initializes the object with the current time. * @brief Standard constructor. Initializes the object with the current time.
*/ */
DCDBTimeStamp(); TimeStamp();
/** /**
* @brief Raw constructor. Initializes the object with an existing raw time. * @brief Raw constructor. Initializes the object with an existing raw time.
*/ */
DCDBTimeStamp(uint64_t ts) {raw = ts;} TimeStamp(uint64_t ts) {raw = ts;}
/** /**
* @brief String constructor. Initializes the object by best guess from a time string. * @brief String constructor. Initializes the object by best guess from a time string.
*/ */
DCDBTimeStamp(std::string ts, bool localTime = false); TimeStamp(std::string ts, bool localTime = false);
/** /**
* @brief Time_t constructor. Initializes the object from a time_t struct. * @brief Time_t constructor. Initializes the object from a time_t struct.
*/ */
DCDBTimeStamp(time_t ts); TimeStamp(time_t ts);
/** /**
* @brief Standard destructor. * @brief Standard destructor.
*/ */
virtual ~DCDBTimeStamp(); virtual ~TimeStamp();
/** /**
* @brief Sets the object's value to the current time. * @brief Sets the object's value to the current time.
...@@ -103,13 +105,14 @@ public: ...@@ -103,13 +105,14 @@ public:
uint16_t getWeekstamp(void); uint16_t getWeekstamp(void);
/* Overloaded operators (compare raw values) */ /* Overloaded operators (compare raw values) */
inline bool operator == (const DCDBTimeStamp& rhs) const {return raw == rhs.raw;} inline bool operator == (const TimeStamp& rhs) const {return raw == rhs.raw;}
inline bool operator != (const DCDBTimeStamp& rhs) const {return raw != rhs.raw;} inline bool operator != (const TimeStamp& rhs) const {return raw != rhs.raw;}
inline bool operator < (const DCDBTimeStamp& rhs) const {return raw < rhs.raw;} inline bool operator < (const TimeStamp& rhs) const {return raw < rhs.raw;}
inline bool operator > (const DCDBTimeStamp& rhs) const {return raw > rhs.raw;} inline bool operator > (const TimeStamp& rhs) const {return raw > rhs.raw;}
inline bool operator <= (const DCDBTimeStamp& rhs) const {return raw <= rhs.raw;} inline bool operator <= (const TimeStamp& rhs) const {return raw <= rhs.raw;}
inline bool operator >= (const DCDBTimeStamp& rhs) const {return raw >= rhs.raw;} inline bool operator >= (const TimeStamp& rhs) const {return raw >= rhs.raw;}
}; };
} /* End of namespace DCDB */
#endif /* DCDBTIMESTAMP_H */ #endif /* DCDB_TIMESTAMP_H */
...@@ -5,68 +5,72 @@ ...@@ -5,68 +5,72 @@
* Author: Axel Auweter * Author: Axel Auweter
*/ */
#ifndef UNITCONV_H
#define UNITCONV_H
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#ifndef DCDB_UNITCONV_H
#define DCDB_UNITCONV_H
namespace DCDB {
typedef enum { typedef enum {
/* Undefined */ /* Undefined */
DCDBUnit_None, Unit_None,
/* Base units */ /* Base units */
DCDBUnit_Meter, Unit_Meter,
DCDBUnit_Second, Unit_Second,
DCDBUnit_Ampere, Unit_Ampere,
DCDBUnit_Kelvin, Unit_Kelvin,
DCDBUnit_Watt, Unit_Watt,
DCDBUnit_Volt, Unit_Volt,
DCDBUnit_Hertz, Unit_Hertz,
/* Others */ /* Others */
DCDBUnit_Celsius, Unit_Celsius,
DCDBUnit_Fahrenheit, Unit_Fahrenheit,
/* 1e3 */ /* 1e3 */
DCDBUnit_KiloHertz, Unit_KiloHertz,
/* 1e6 */ /* 1e6 */
DCDBUnit_MegaHertz, Unit_MegaHertz,
/* 1e9 */ /* 1e9 */
DCDBUnit_GigaHertz, Unit_GigaHertz,
/* 1e-2 */ /* 1e-2 */
DCDBUnit_CentiMeter, Unit_CentiMeter,
DCDBUnit_CentiCelsius, Unit_CentiCelsius,
/* 1e-3 */ /* 1e-3 */
DCDBUnit_MilliMeter, Unit_MilliMeter,
DCDBUnit_MilliSecond, Unit_MilliSecond,
DCDBUnit_MilliAmpere, Unit_MilliAmpere,
DCDBUnit_MilliKelvin, Unit_MilliKelvin,
DCDBUnit_MilliWatt, Unit_MilliWatt,
DCDBUnit_MilliVolt, Unit_MilliVolt,
DCDBUnit_MilliCelsius, Unit_MilliCelsius,
/* 1e-6 */ /* 1e-6 */
DCDBUnit_MicroMeter, Unit_MicroMeter,
DCDBUnit_MicroSecond, Unit_MicroSecond,
DCDBUnit_MicroAmpere, Unit_MicroAmpere,
DCDBUnit_MicroKelvin, Unit_MicroKelvin,
DCDBUnit_MicroWatt, Unit_MicroWatt,
DCDBUnit_MicroVolt, Unit_MicroVolt,
DCDBUnit_MicroCelsius, Unit_MicroCelsius,
} DCDBUnit; } Unit;
class UnitConv class UnitConv
{ {
public: public:
static DCDBUnit fromString(std::string unit); static Unit fromString(std::string unit);
static std::string toString(DCDBUnit unit); static std::string toString(Unit unit);
static bool convert(int64_t& value, DCDBUnit from, DCDBUnit to); static bool convert(int64_t& value, Unit from, Unit to);
static bool convert(double& value, DCDBUnit from, DCDBUnit to); static bool convert(double& value, Unit from, Unit to);
}; };
#endif } /* End of namespace DCDB */
#endif /* DCDB_UNITCONV_H */
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
/* /*
* @file * @file
* @brief This file contains the internal functions of the * @brief This file contains the internal functions of the
* DCDBConnection which are provided by the * Connection which are provided by the
* DCDBConnectionImpl class. * ConnectionImpl class.
*/ */
#include <string> #include <string>
...@@ -17,10 +17,12 @@ ...@@ -17,10 +17,12 @@
#include "connection.h" #include "connection.h"