Commit aead8d55 authored by Axel Auweter's avatar Axel Auweter
Browse files

Merge branch 'ticket-38'

parents f9b575af b4dea9a0
......@@ -11,6 +11,9 @@ install/
.settings
*.xcworkspace/
## Ignore debug symbol directories
*.dSYM
## Ignore directory indexing tools
cscope.out
.DS_Store
......
include ../config.mk
CXXFLAGS = -O2 -g --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
CXXFLAGS = -O2 -g --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-deprecated-declarations -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
OBJS = collectagent.o \
simplemqttserver.o \
simplemqttserverthread.o \
......
......@@ -30,7 +30,7 @@ int keepRunning;
bool statistics;
uint64_t msgCtr;
uint64_t pmsgCtr;
SensorDataStore *mySensorDataStore;
DCDB::SensorDataStore *mySensorDataStore;
std::string listenHost, cassandraHost, ttl;
/* Normal termination (SIGINT, CTRL+C) */
......@@ -93,7 +93,7 @@ void mqttCallback(SimpleMQTTMessage *msg)
* into a valid SensorId. If successful, store
* the record in the database.
*/
SensorId sid;
DCDB::SensorId sid;
if (sid.mqttTopicConvert(msg->getTopic())) {
#if 0
cout << "Topic decode successful:"
......@@ -185,8 +185,8 @@ int main(int argc, char* const argv[]) {
* Allocate and initialize connection to Cassandra.
*/
std::string sdHost = cassandraHost;
DCDBConnection* dcdbConn;
dcdbConn = new DCDBConnection(sdHost, 9042);
DCDB::Connection* dcdbConn;
dcdbConn = new DCDB::Connection(sdHost, 9042);
if (!dcdbConn->connect()) {
std::cout << "Cannot connect to Cassandra!" << std::endl;
......@@ -201,7 +201,7 @@ int main(int argc, char* const argv[]) {
/*
* Allocate the SensorDataStore.
*/
mySensorDataStore = new SensorDataStore(dcdbConn);
mySensorDataStore = new DCDB::SensorDataStore(dcdbConn);
/*
* Set TTL for data store inserts if TTL > 0.
......
......@@ -14,6 +14,7 @@ OBJS = src/connection.o \
src/sensorconfig.o \
src/sensorid.o \
src/unitconv.o \
src/virtualsensor.o \
src/c_api.o
# List of public header files necessary to use this libray
......@@ -42,7 +43,7 @@ P = $(shell cd $(DCDBDEPLOYPATH)/lib/ && pwd)
.PHONY : check-target-env all clean install
# Main Library Target
$(TARGET): $(OBJS)
$(TARGET): $(OBJS) .header-check
@if [ "$(OS)" = "Darwin" ]; then \
echo "Linking library in Mac OS style: $(TARGET)"; \
$(CXX) $(CXXFLAGS) $(DLFLAGS) -o $(TARGET) $(OBJS) $(LIBS); \
......@@ -67,6 +68,11 @@ check-target-env:
printf "\n******************************************\nPlease type the following line before running:\n export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$P\n******************************************\n"; \
fi; \
fi
# Header check
.header-check: $(PUBHEADERS) $(PRIVHEADERS)
@touch .header-check
@touch $(OBJS:.o=.cpp)
# Build the documentation
docs: $(PUBHEADERS) $(PRIVHEADERS) $(SRC)
......@@ -75,6 +81,7 @@ docs: $(PUBHEADERS) $(PRIVHEADERS) $(SRC)
# Clean everything
clean: clean-docs
rm -f .header-check
rm -f $(OBJS) $(TARGET)
# Clean the documentation
......
......@@ -9,7 +9,7 @@
* @file
* @brief This file contains parts of the public API for the
* 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
* initialization.
*/
......@@ -19,16 +19,18 @@
#include "cassandra.h"
#ifndef CONNECTION_H
#define CONNECTION_H
#ifndef DCDB_CONNECTION_H
#define DCDB_CONNECTION_H
namespace DCDB {
/* Forward-declaration of the implementation-internal classes */
class DCDBConnectionImpl;
class ConnectionImpl;
class DCDBConnection
class Connection
{
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:
/**
......@@ -99,21 +101,23 @@ public:
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.
*/
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,22 +22,29 @@
#include "cassandra.h"
#ifndef SENSORCONFIG_H
#define SENSORCONFIG_H
#ifndef DCDB_SENSORCONFIG_H
#define DCDB_SENSORCONFIG_H
namespace DCDB {
class SensorConfigImpl;
class DCDBPublicSensor
class PublicSensor
{
public:
std::string name;
bool is_virtual;
std::string pattern;
double scaling_factor;
std::string unit;
bool integrable;
std::string expression;
std::string v_sensorid;
uint64_t t_zero;
uint64_t frequency;
DCDBPublicSensor();
DCDBPublicSensor(const DCDBPublicSensor &copy);
PublicSensor();
PublicSensor(const PublicSensor &copy);
};
typedef enum {
......@@ -45,6 +52,10 @@ typedef enum {
SC_INVALIDSESSION,
SC_INVALIDPATTERN,
SC_INVALIDPUBLICNAME,
SC_INVALIDEXPRESSION,
SC_EXPRESSIONSELFREF,
SC_INVALIDVSENSORID,
SC_WRONGTYPE,
SC_UNKNOWNSENSOR,
SC_UNKNOWNERROR
} SCError;
......@@ -56,23 +67,31 @@ protected:
public:
SCError publishSensor(const char* publicName, const char* sensorPattern);
SCError publishVirtualSensor(const char* publicName, const char* vSensorExpression, const char * vSensorId, TimeStamp tZero, uint64_t frequency);
SCError unPublishSensor(const char* publicName);
SCError getPublicSensorNames(std::list<std::string>& publicSensors);
SCError getPublicSensorsVerbose(std::list<DCDBPublicSensor>& publicSensors);
SCError getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors);
SCError getPublicSensorByName(PublicSensor& sensor, const char* publicName);
SCError getPublicSensorByName(DCDBPublicSensor& sensor, const char* publicName);
SCError isVirtual(bool& isVirtual, 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, 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 setSensorUnit(std::string publicName, std::string unit);
SCError setSensorIntegrable(std::string publicName, bool integrable);
SensorConfig(DCDBConnection* conn);
SCError setVirtualSensorExpression(std::string publicName, std::string expression);
SCError setVirtualSensorTZero(std::string publicName, TimeStamp tZero);
SCError setVirtualSensorFrequency(std::string publicName, uint64_t frequency);
SensorConfig(Connection* conn);
virtual ~SensorConfig();
};
} /* End of namespace DCDB */
#endif /* CONFIG_H */
#endif /* DCDB_SENSORCONFIG_H */
......@@ -21,8 +21,10 @@
#include "timestamp.h"
#include "connection.h"
#ifndef SENSORDATASTORE_H_
#define SENSORDATASTORE_H_
#ifndef DCDB_SENSORDATASTORE_H
#define DCDB_SENSORDATASTORE_H
namespace DCDB {
typedef enum {
SDS_OK,
......@@ -40,7 +42,7 @@ class SensorDataStoreReading
{
public:
SensorId sensorId;
DCDBTimeStamp timeStamp;
TimeStamp timeStamp;
int64_t value;
#if 0
......@@ -85,7 +87,19 @@ public:
* @param start Start 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);
typedef void (*QueryCbFunc)(SensorDataStoreReading& reading, void* userData);
/**
* @brief This function queries a sensor's values in
* the given time range.
* @param cbFunc A function to called for each reading.
* @param userData Pointer to user data that will be returned in the callback.
* @param sid The SensorId to query.
* @param start Start of the time series.
* @param end End of the time series.
*/
void queryCB(QueryCbFunc cbFunc, void* userData, SensorId& sid, TimeStamp& start, TimeStamp& end);
/**
* @brief This function queries the integrated value (val * sec)
......@@ -96,7 +110,7 @@ public:
* @param end End of the time series.
* @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
......@@ -109,10 +123,10 @@ public:
* @brief A shortcut constructor for a SensorDataStore object
* that allows accessing the data store through a
* connection that is already established.
* @param conn The DCDBConnection object of an established
* @param conn The Connection object of an established
* connection to Cassandra.
*/
SensorDataStore(DCDBConnection* conn);
SensorDataStore(Connection* conn);
/**
* @brief The standard destructor for a SensorDatStore object.
......@@ -120,5 +134,6 @@ public:
virtual ~SensorDataStore();
};
} /* End of namespace DCDB */
#endif /* SENSORDATASTORE_H_ */
#endif /* DCDB_SENSORDATASTORE_H */
......@@ -8,8 +8,10 @@
#include <cstdint>
#include <string>
#ifndef SENSORID_H
#define SENSORID_H
#ifndef DCDB_SENSORID_H
#define DCDB_SENSORID_H
namespace DCDB {
/* Ensure that the unions and structs are created without padding */
#pragma pack(push,1)
......@@ -122,4 +124,6 @@ public:
#pragma pack(pop)
#endif /* SENSORID_H */
} /* End of namespace DCDB */
#endif /* DCDB_SENSORID_H */
/*
* dcdbtimestamp.h
* timestamp.h
*
* Created on: Feb 18, 2015
* Author: Axel Auweter
......@@ -8,7 +8,7 @@
/**
* @file
* @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.
*/
......@@ -17,26 +17,28 @@
#include <string>
#include <stdexcept>
#ifndef DCDBTIMESTAMP_H
#define DCDBTIMESTAMP_H
#ifndef DCDB_TIMESTAMP_H
#define DCDB_TIMESTAMP_H
class DCDBTimeStampConversionException : public std::runtime_error
namespace DCDB {
class TimeStampConversionException : public std::runtime_error
{
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:
uint64_t raw; /**< The raw timestamp data (nanoseconds since Unix Epoch) */
/**
* @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 localTime Denotes if the timestr contains local time instead of UTC
*/
......@@ -47,27 +49,27 @@ public:
/**
* @brief Standard constructor. Initializes the object with the current time.
*/
DCDBTimeStamp();
TimeStamp();
/**
* @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.
*/
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.
*/
DCDBTimeStamp(time_t ts);
TimeStamp(time_t ts);
/**
* @brief Standard destructor.
*/
virtual ~DCDBTimeStamp();
virtual ~TimeStamp();
/**
* @brief Sets the object's value to the current time.
......@@ -88,28 +90,29 @@ public:
* @brief Returns the raw time stamp value.
* @return The object's value as uint64_t.
*/
uint64_t getRaw(void);
uint64_t getRaw(void) const;
/**
* @brief Returns the time stamp's value as human readable string
* @return The object's value as std::string.
*/
std::string getString(void);
std::string getString(void) const;
/**
* @brief Returns the "weekstamp" corresponding to the object's value
* @return The week number of the timestamp.
*/
uint16_t getWeekstamp(void);
uint16_t getWeekstamp(void) const;
/* Overloaded operators (compare raw values) */
inline bool operator == (const DCDBTimeStamp& rhs) const {return raw == rhs.raw;}
inline bool operator != (const DCDBTimeStamp& rhs) const {return raw != rhs.raw;}
inline bool operator < (const DCDBTimeStamp& rhs) const {return raw < rhs.raw;}
inline bool operator > (const DCDBTimeStamp& rhs) const {return raw > rhs.raw;}
inline bool operator <= (const DCDBTimeStamp& 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 TimeStamp& rhs) const {return raw != rhs.raw;}
inline bool operator < (const TimeStamp& rhs) const {return raw < rhs.raw;}
inline bool operator > (const TimeStamp& rhs) const {return raw > rhs.raw;}
inline bool operator <= (const TimeStamp& 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 @@
* Author: Axel Auweter
*/
#ifndef UNITCONV_H
#define UNITCONV_H
#include <cstdint>
#include <string>
#ifndef DCDB_UNITCONV_H
#define DCDB_UNITCONV_H
namespace DCDB {
typedef enum {
/* Undefined */
DCDBUnit_None,
Unit_None,
/* Base units */
DCDBUnit_Meter,
DCDBUnit_Second,
DCDBUnit_Ampere,
DCDBUnit_Kelvin,
DCDBUnit_Watt,
DCDBUnit_Volt,
DCDBUnit_Hertz,
Unit_Meter,
Unit_Second,
Unit_Ampere,
Unit_Kelvin,
Unit_Watt,
Unit_Volt,
Unit_Hertz,
/* Others */
DCDBUnit_Celsius,
DCDBUnit_Fahrenheit,
Unit_Celsius,
Unit_Fahrenheit,
/* 1e3 */
DCDBUnit_KiloHertz,
Unit_KiloHertz,
/* 1e6 */
DCDBUnit_MegaHertz,
Unit_MegaHertz,
/* 1e9 */
DCDBUnit_GigaHertz,
Unit_GigaHertz,
/* 1e-2 */
DCDBUnit_CentiMeter,
DCDBUnit_CentiCelsius,
Unit_CentiMeter,
Unit_CentiCelsius,
/* 1e-3 */
DCDBUnit_MilliMeter,
DCDBUnit_MilliSecond,
DCDBUnit_MilliAmpere,
DCDBUnit_MilliKelvin,
DCDBUnit_MilliWatt,
DCDBUnit_MilliVolt,
DCDBUnit_MilliCelsius,
Unit_MilliMeter,
Unit_MilliSecond,
Unit_MilliAmpere,
Unit_MilliKelvin,
Unit_MilliWatt,
Unit_MilliVolt,
Unit_MilliCelsius,
/* 1e-6 */
DCDBUnit_MicroMeter,
DCDBUnit_MicroSecond,
DCDBUnit_MicroAmpere,
DCDBUnit_MicroKelvin,
DCDBUnit_MicroWatt,
DCDBUnit_MicroVolt,
DCDBUnit_MicroCelsius,
} DCDBUnit;
Unit_MicroMeter,
Unit_MicroSecond,
Unit_MicroAmpere,
Unit_MicroKelvin,
Unit_MicroWatt,
Unit_MicroVolt,
Unit_MicroCelsius,
} Unit;
class UnitConv
{
public:
static DCDBUnit fromString(std::string unit);
static std::string toString(DCDBUnit unit);
static bool convert(int64_t& value, DCDBUnit from, DCDBUnit to);
static bool convert(double& value, DCDBUnit from, DCDBUnit to);
static Unit fromString(std::string unit);
static std::string toString(Unit unit);
static bool convert(int64_t& value, Unit from, Unit to);
static bool convert(double& value, Unit from, Unit to);
};
#endif
} /* End of namespace DCDB */
#endif /* DCDB_UNITCONV_H */
/*
* virtualsensor.h
*
* Created on: Jan 15, 2016
* Author: Axel Auweter
*/
/**
* @file
* @brief This file contains classes for handling virtual sensors.
*/
#include <stdexcept>
#include <string>
#include <unordered_set>
#include "connection.h"
#include "sensorconfig.h"
#include "sensordatastore.h"
#ifndef DCDB_VIRTUAL_SENSOR_H
#define DCDB_VIRTUAL_SENSOR_H
namespace DCDB {
/**
* @brief Exception class for parsing Virtual Sensor Expressions
*
* Exceptions of this type are thrown whenever the parser for virtual
* sensor expressions encounters a syntax error. What() tries to
* provide the user with a hint on the location where the error was
* encountered while processing the expression string.
*/
class VSExpressionParserException : public std::runtime_error
{
public:
VSExpressionParserException(const std::string& where) : runtime_error("Error parsing expression at: "), where_(where) {}
virtual const char* what() const throw();
private:
static std::string msg_;
std::string where_;
};
/* Forward declare VirtualSensor::VSensorExpressionImpl and VirtualSensor::VSensorImpl */
namespace VirtualSensor{
class VSensorExpressionImpl;
class VSensorImpl;
}
/**
* @brief Public class for evaluating Virtual Sensors expressions
*
* This class forwards to the library internal VSensorExpressionImpl class.
*/
class VSensorExpression
{
protected:
VirtualSensor::VSensorExpressionImpl *impl;
public:
void getInputs(std::unordered_set<std::string>& inputSet);
void getInputsRecursive(std::unordered_set<std::string>& inputSet, bool virtualOnly = true);
VSensorExpression(Connection* conn, std::string expr);