Commit 67d39235 authored by Axel Auweter's avatar Axel Auweter
Browse files

Add additional vSensorId field to virtual sensor definition.

parent d835d1ea
......@@ -39,6 +39,7 @@ public:
std::string unit;
bool integrable;
std::string expression;
std::string v_sensorid;
uint64_t t_zero;
uint64_t frequency;
......@@ -51,6 +52,8 @@ typedef enum {
SC_INVALIDSESSION,
SC_INVALIDPATTERN,
SC_INVALIDPUBLICNAME,
SC_INVALIDEXPRESSION,
SC_INVALIDVSENSORID,
SC_WRONGTYPE,
SC_UNKNOWNSENSOR,
SC_UNKNOWNERROR
......@@ -63,7 +66,7 @@ protected:
public:
SCError publishSensor(const char* publicName, const char* sensorPattern);
SCError publishVirtualSensor(const char* publicName, const char* vSensorExpression, TimeStamp tZero, uint64_t frequency);
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<PublicSensor>& publicSensors);
......
......@@ -32,7 +32,7 @@ protected:
public:
SCError publishSensor(std::string publicName, std::string sensorPattern);
SCError publishVirtualSensor(std::string publicName, std::string vSensorExpression, TimeStamp tZero, uint64_t frequency);
SCError publishVirtualSensor(std::string publicName, std::string vSensorExpression, std::string vSensorId, TimeStamp tZero, uint64_t frequency);
SCError unPublishSensor(std::string publicName);
SCError getPublicSensorNames(std::list<std::string>& publicSensors);
SCError getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors);
......
......@@ -357,6 +357,7 @@ bool ConnectionImpl::initSchema() {
"unit varchar, " /* Unit of the sensor (e.g. W for Watts) */
"integrable boolean, " /* Indicates whether the sensor is integrable over time */
"expression varchar, " /* For virtual sensors: arithmetic expression to derive the virtual sensor's value */
"vsensorid varchar, " /* For virtual sensors: Unique sensorId for the sensor in the virtualsensors table */
"tzero bigint, " /* For virtual sensors: time of the first reading */
"frequency bigint", /* For virtual sensors: frequency at which this virtual sensor provides readings */
......
......@@ -29,6 +29,7 @@ PublicSensor::PublicSensor()
unit = "";
integrable = false;
expression = "";
v_sensorid = "";
t_zero = 0;
frequency = 0;
}
......@@ -42,6 +43,7 @@ PublicSensor::PublicSensor (const PublicSensor &copy)
unit = copy.unit;
integrable = copy.integrable;
expression = copy.expression;
v_sensorid = copy.v_sensorid;
t_zero = copy.t_zero;
frequency = copy.frequency;
}
......@@ -55,9 +57,9 @@ SCError SensorConfig::publishSensor(const char* publicName, const char* sensorPa
return impl->publishSensor(publicName, sensorPattern);
}
SCError SensorConfig::publishVirtualSensor(const char* publicName, const char* vSensorExpression, TimeStamp tZero, uint64_t frequency)
SCError SensorConfig::publishVirtualSensor(const char* publicName, const char* vSensorExpression, const char* vSensorId, TimeStamp tZero, uint64_t frequency)
{
return impl->publishVirtualSensor(publicName, vSensorExpression, tZero, frequency);
return impl->publishVirtualSensor(publicName, vSensorExpression, vSensorId, tZero, frequency);
}
SCError SensorConfig::unPublishSensor(const char* publicName)
......@@ -297,7 +299,7 @@ SCError SensorConfigImpl::publishSensor(std::string publicName, std::string sens
return SC_OK;
}
SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::string vSensorExpression, TimeStamp tZero, uint64_t frequency)
SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::string vSensorExpression, std::string vSensorId, TimeStamp tZero, uint64_t frequency)
{
/* Check if the publicName is valid */
if (!validateSensorPublicName(publicName.c_str())) {
......@@ -309,14 +311,20 @@ SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::stri
return SC_INVALIDSESSION;
}
/* TODO: Validate vSesnroExpression */
/* TODO: Validate vSensorExpression */
/* Check if the vSensorId is valid */
SensorId vSensor;
if (!vSensor.mqttTopicConvert(vSensorId)) {
return SC_INVALIDVSENSORID;
}
/* Insert the entry */
CassError rc = CASS_OK;
CassStatement* statement = nullptr;
CassFuture* future = nullptr;
const CassPrepared* prepared = nullptr;
const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, expression, tzero, frequency, virtual) VALUES (?,?,?,?,TRUE);";
const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, expression, vsensorid, tzero, frequency, virtual) VALUES (?,?,?,?,?,TRUE);";
future = cass_session_prepare(session, query);
cass_future_wait(future);
......@@ -336,6 +344,7 @@ SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::stri
cass_statement_bind_string_by_name(statement, "name", publicName.c_str());
cass_statement_bind_string_by_name(statement, "expression", vSensorExpression.c_str());
cass_statement_bind_string_by_name(statement, "vsensorid", vSensorId.c_str());
cass_statement_bind_int64_by_name(statement, "tzero", tZero.getRaw());
cass_statement_bind_int64_by_name(statement, "frequency", frequency);
......@@ -509,6 +518,8 @@ SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publi
cass_bool_t integrable;
const char* expression;
size_t expression_len;
const char* vsensorid;
size_t vsensorid_len;
int64_t tzero;
int64_t frequency;
PublicSensor sensor;
......@@ -536,6 +547,9 @@ SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publi
if (cass_value_get_string(cass_row_get_column_by_name(row, "expression"), &expression, &expression_len) != CASS_OK) {
expression = ""; expression_len = 0;
}
if (cass_value_get_string(cass_row_get_column_by_name(row, "vsensorid"), &vsensorid, &vsensorid_len) != CASS_OK) {
vsensorid = ""; vsensorid_len = 0;
}
if (cass_value_get_int64(cass_row_get_column_by_name(row, "tzero"), &tzero) != CASS_OK) {
tzero = 0;
}
......@@ -550,6 +564,7 @@ SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publi
sensor.unit = std::string(unit, unit_len);
sensor.integrable = integrable == cass_true ? true: false;
sensor.expression = std::string(expression, expression_len);
sensor.v_sensorid = std::string(vsensorid, vsensorid_len);
sensor.t_zero = tzero;
sensor.frequency = frequency;
......@@ -619,6 +634,8 @@ SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char
cass_bool_t integrable;
const char* expression;
size_t expression_len;
const char* vsensorid;
size_t vsensorid_len;
int64_t tzero;
int64_t frequency;
......@@ -645,6 +662,9 @@ SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char
if (cass_value_get_string(cass_row_get_column_by_name(row, "expression"), &expression, &expression_len) != CASS_OK) {
expression = ""; expression_len = 0;
}
if (cass_value_get_string(cass_row_get_column_by_name(row, "vsensorid"), &vsensorid, &vsensorid_len) != CASS_OK) {
vsensorid = ""; vsensorid_len = 0;
}
if (cass_value_get_int64(cass_row_get_column_by_name(row, "tzero"), &tzero) != CASS_OK) {
tzero = 0;
}
......@@ -659,6 +679,7 @@ SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char
sensor.unit = std::string(unit, unit_len);
sensor.integrable = integrable == cass_true ? true: false;
sensor.expression = std::string(expression, expression_len);
sensor.v_sensorid = std::string(vsensorid, vsensorid_len);
sensor.t_zero = tzero;
sensor.frequency = frequency;
......
......@@ -25,24 +25,25 @@ void SensorAction::printHelp(int argc, char* argv[])
/* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
std::cout << "SENSOR command help" << std::endl << std::endl;
std::cout << "The SENSOR command has the following options:" << std::endl;
std::cout << " PUBLISH <public name> <pattern> - Make a sensor publicly available" << std::endl;
std::cout << " under <public name> comprising of" << std::endl;
std::cout << " all internal sensors matching the" << std::endl;
std::cout << " given <pattern>." << std::endl;
std::cout << " VCREATE <public name> <expr> <t0> <freq> - Create a virtual public sensor" << std::endl;
std::cout << " that is visible as <public name>" << std::endl;
std::cout << " and evaluates <expr> starting at" << std::endl;
std::cout << " time t0 every <freq> nanoseconds." << std::endl;
std::cout << " LIST - List all public sensors." << std::endl;
std::cout << " LISTPUBLIC - Same as LIST, includes patterns." << std::endl;
std::cout << " SHOW <public name> - Show details for a given sensor." << std::endl;
std::cout << " SCALINGFACTOR <public name> <fac> - Set scaling factor to <fac>." << std::endl;
std::cout << " UNIT <public name> <unit> - Set unit to <unit>." << std::endl;
std::cout << " INTEGRABLE <public name> [ON|OFF] - Mark sensor integrable." << std::endl;
std::cout << " EXPRESSION <public name> <expr> - Change expression of virt sensor." << std::endl;
std::cout << " TZERO <public name> <t0> - Change t0 of virt sensor." << std::endl;
std::cout << " FREQUENCY <public name> <freq> - Change frequency of virt sensor." << std::endl;
std::cout << " UNPUBLISH <public name> - Unpublish a sensor." << std::endl;
std::cout << " PUBLISH <public name> <pattern> - Make a sensor publicly available under" << std::endl;
std::cout << " <public name> comprising of all internal" << std::endl;
std::cout << " sensors matching the given <pattern>." << std::endl;
std::cout << " VCREATE <public name> <expr> <vsensorid> <t0> <freq>" << std::endl;
std::cout << " - Create a virtual public sensor that is" << std::endl;
std::cout << " visible as <public name> and evaluates" << std::endl;
std::cout << " <expr> starting at time t0 every <freq>" << std::endl;
std::cout << " nanoseconds. Cached values are stored" << std::endl;
std::cout << " under the unique <vsensorid>." << std::endl;
std::cout << " LIST - List all public sensors." << std::endl;
std::cout << " LISTPUBLIC - Same as LIST, includes patterns." << std::endl;
std::cout << " SHOW <public name> - Show details for a given sensor." << std::endl;
std::cout << " SCALINGFACTOR <public name> <fac> - Set scaling factor to <fac>." << std::endl;
std::cout << " UNIT <public name> <unit> - Set unit to <unit>." << std::endl;
std::cout << " INTEGRABLE <public name> [ON|OFF] - Mark sensor integrable." << std::endl;
std::cout << " EXPRESSION <public name> <expr> - Change expression of virt sensor." << std::endl;
std::cout << " TZERO <public name> <t0> - Change t0 of virt sensor." << std::endl;
std::cout << " FREQUENCY <public name> <freq> - Change frequency of virt sensor." << std::endl;
std::cout << " UNPUBLISH <public name> - Unpublish a sensor." << std::endl;
}
/*
......@@ -76,11 +77,11 @@ int SensorAction::executeCommand(int argc, char* argv[], int argvidx, const char
}
else if (strcasecmp(argv[argvidx], "VCREATE") == 0) {
/* VCREATE needs four more parameters */
if (argvidx+4 >= argc) {
std::cout << "PUBLISH needs two more parameters!" << std::endl;
if (argvidx+5 >= argc) {
std::cout << "VCREATE needs five more parameters!" << std::endl;
goto executeCommandError;
}
doVCreateSensor(argv[argvidx+1], argv[argvidx+2], argv[argvidx+3], argv[argvidx+4]);
doVCreateSensor(argv[argvidx+1], argv[argvidx+2], argv[argvidx+3], argv[argvidx+4], argv[argvidx+5]);
}
else if (strcasecmp(argv[argvidx], "LIST") == 0) {
doList();
......@@ -196,7 +197,7 @@ void SensorAction::doPublishSensor(const char* publicName, const char* sensorPat
/*
* Create a virtual sensor
*/
void SensorAction::doVCreateSensor(const char* publicName, const char* expression, const char* tZero, const char* frequency)
void SensorAction::doVCreateSensor(const char* publicName, const char* expression, const char* vSensorId, const char* tZero, const char* frequency)
{
/* Convert tZero to TimeStamp */
DCDB::TimeStamp tz(tZero);
......@@ -211,12 +212,18 @@ void SensorAction::doVCreateSensor(const char* publicName, const char* expressio
DCDB::SCError err;
DCDB::SensorConfig sensorConfig(connection);
err = sensorConfig.publishVirtualSensor(publicName, expression, tz, freq);
err = sensorConfig.publishVirtualSensor(publicName, expression, vSensorId, tz, freq);
switch(err) {
case DCDB::SC_INVALIDPATTERN:
case DCDB::SC_INVALIDEXPRESSION:
std::cout << "Invalid expression: " << expression << std::endl;
break;
case DCDB::SC_INVALIDVSENSORID:
std::cout << "Invalid vsensorid: " << vSensorId << std::endl;
std::cout << "Valid vsensorids are provided as 128 bits hex values." << std::endl;
std::cout << "You may separate hex characters with slash characters for readability." << std::endl;
std::cout << "Example: /00000000/deadbeef/cafeaffe/0000/0001" << std::endl;
break;
case DCDB::SC_INVALIDPUBLICNAME:
std::cout << "Invalid sensor public name: " << publicName << std::endl;
break;
......@@ -279,6 +286,7 @@ void SensorAction::doShow(const char* publicName)
else {
std::cout << "Details for virtual sensor " << publicSensor.name << ":" << std::endl;
std::cout << "Expression: " << publicSensor.expression << std::endl;
std::cout << "vSensorId: " << publicSensor.v_sensorid << std::endl;
DCDB::TimeStamp tz(publicSensor.t_zero);
std::cout << "T-Zero: " << tz.getString() << " (" << tz.getRaw() << ")" << std::endl;
std::cout << "Frequency: " << publicSensor.frequency << std::endl;
......
......@@ -25,7 +25,7 @@ public:
protected:
DCDB::Connection* connection;
void doPublishSensor(const char* publicName, const char* sensorPattern);
void doVCreateSensor(const char* publicName, const char* expression, const char* tZero, const char* frequency);
void doVCreateSensor(const char* publicName, const char* expression, const char* vSensorId, const char* tZero, const char* frequency);
void doList();
void doListPublicSensors();
void doShow(const char* publicName);
......
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