Commit a68b62c8 authored by Alessio Netti's avatar Alessio Netti
Browse files

Overloaded publishSensor method in SensorConfig

- Now supports publishing of metadata information as well
parent 0904f1c4
......@@ -93,6 +93,11 @@ SCError SensorConfig::publishSensor(const char* publicName, const char* sensorPa
return impl->publishSensor(publicName, sensorPattern);
}
SCError SensorConfig::publishSensor(const PublicSensor& sensor)
{
return impl->publishSensor(sensor);
}
SCError SensorConfig::publishVirtualSensor(const char* publicName, const char* vSensorExpression, const char* vSensorId, TimeStamp tZero, uint64_t interval)
{
return impl->publishVirtualSensor(publicName, vSensorExpression, vSensorId, tZero, interval);
......@@ -336,6 +341,77 @@ SCError SensorConfigImpl::publishSensor(std::string publicName, std::string sens
return SC_OK;
}
SCError SensorConfigImpl::publishSensor(const PublicSensor& sensor)
{
/* Check if the pattern matches the requirements */
SensorId sid;
if (!validateSensorPattern(sensor.pattern.c_str()) || !sid.mqttTopicConvert(sensor.pattern)) {
return SC_INVALIDPATTERN;
}
/* Check if the publicName is valid */
if (!validateSensorPublicName(sensor.name.c_str())) {
return SC_INVALIDPUBLICNAME;
}
/* Check if the session is valid */
if (!session) {
return SC_INVALIDSESSION;
}
/* 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, pattern, virtual, scaling_factor, unit, sensor_mask, operations, interval, ttl) VALUES (?,?, FALSE, ?, ?, ?, ?, ?, ?);";
future = cass_session_prepare(session, query);
cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
connection->printError(future);
cass_future_free(future);
return SC_UNKNOWNERROR;
} else {
prepared = cass_future_get_prepared(future);
}
cass_future_free(future);
statement = cass_prepared_bind(prepared);
cass_statement_bind_string_by_name(statement, "name", sensor.name.c_str());
cass_statement_bind_string_by_name(statement, "pattern", sid.getId().c_str());
cass_statement_bind_double_by_name(statement, "scaling_factor", sensor.scaling_factor);
cass_statement_bind_string_by_name(statement, "unit", sensor.unit.c_str());
cass_statement_bind_int64_by_name(statement, "sensor_mask", sensor.sensor_mask);
cass_statement_bind_string_by_name(statement, "operations", sensor.operations.c_str());
cass_statement_bind_int64_by_name(statement, "interval", sensor.interval);
cass_statement_bind_int64_by_name(statement, "ttl", sensor.ttl);
future = cass_session_execute(session, statement);
cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
connection->printError(future);
cass_prepared_free(prepared);
cass_future_free(future);
cass_statement_free(statement);
return SC_UNKNOWNERROR;
}
cass_prepared_free(prepared);
cass_future_free(future);
cass_statement_free(statement);
return SC_OK;
}
SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::string vSensorExpression, std::string vSensorId, TimeStamp tZero, uint64_t interval)
{
/* Check if the publicName is valid */
......
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