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

Changes to auto-publish

- Auto-publish is now case insensitive with respect to the <sensor> and
<group> patterns
- If an invalid pattern was specified (missing <sensor> clause) the
configuration is aborted
- Checks for the uniqueness of sensor and group names have been
temporarily dropped as they interfere with templates and entities
(until we find a good solution for MQTT topics)
parent a9fadfc3
...@@ -52,8 +52,8 @@ protected: ...@@ -52,8 +52,8 @@ protected:
const string ALL_CLAUSE = "all"; const string ALL_CLAUSE = "all";
const string ALL_REC_CLAUSE = "all-recursive"; const string ALL_REC_CLAUSE = "all-recursive";
const std::string SENSOR_PATTERN = "<sensor>"; const std::string SENSOR_PATTERN = "(?i)<sensor>";
const std::string GROUP_PATTERN = "<group>"; const std::string GROUP_PATTERN = "(?i)<group>";
public: public:
...@@ -396,7 +396,11 @@ protected: ...@@ -396,7 +396,11 @@ protected:
} }
for (auto &u: *units) { for (auto &u: *units) {
constructSensorNames(*u, an); if(!constructSensorNames(*u, an)) {
an.clearUnits();
delete units;
return false;
}
if (an.getStreaming()) { if (an.getStreaming()) {
if (!unit(*u)) { if (!unit(*u)) {
LOG(error) << " Unit " << u->getName() << " did not pass the final check!"; LOG(error) << " Unit " << u->getName() << " did not pass the final check!";
...@@ -493,17 +497,19 @@ protected: ...@@ -493,17 +497,19 @@ protected:
* *
* Names are modified according to the sensorPattern specified in the global * Names are modified according to the sensorPattern specified in the global
* settings. Operates in tandem with the auto-publish feature. * settings. Operates in tandem with the auto-publish feature.
*
* @return true if successful, false otherwise
*/ */
//TODO: switch to textual MQTT topics and use only MQTTPrefix //TODO: switch to textual MQTT topics and use only MQTTPrefix
void constructSensorNames(UnitTemplate<SBase>& u, Analyzer& an) { bool constructSensorNames(UnitTemplate<SBase>& u, Analyzer& an) {
boost::regex sensorReg(SENSOR_PATTERN), groupReg(GROUP_PATTERN); boost::regex sensorReg(SENSOR_PATTERN), groupReg(GROUP_PATTERN);
boost::cmatch match; boost::cmatch match;
//TODO: move sensorpattern checks somewhere else //TODO: move sensorpattern checks somewhere else
if(_sensorPattern == "") if(_sensorPattern == "")
return; return true;
else if (!boost::regex_search(_sensorPattern.c_str(), match, sensorReg)) { else if (!boost::regex_search(_sensorPattern.c_str(), match, sensorReg)) {
LOG(error) << "Invalid sensor naming pattern " << _sensorPattern << ". You must at least include " << SENSOR_PATTERN << "!"; LOG(error) << "Invalid sensor naming pattern " << _sensorPattern << ". You must at least include <sensor>!";
return; return false;
} }
std::string name; std::string name;
...@@ -520,7 +526,7 @@ protected: ...@@ -520,7 +526,7 @@ protected:
} }
s->setName(name); s->setName(name);
} }
return; return true;
} }
// Instance of a QueryEngine object // Instance of a QueryEngine object
......
...@@ -101,10 +101,10 @@ public: ...@@ -101,10 +101,10 @@ public:
*/ */
bool checkName(const std::string& name) { bool checkName(const std::string& name) {
auto returnIt = _names.insert(name); auto returnIt = _names.insert(name);
if (!returnIt.second) { //if (!returnIt.second) {
LOG(error) << "Name \"" << name << "\" used twice!"; // LOG(error) << "Name \"" << name << "\" used twice!";
return false; // return false;
} //}
return true; return true;
} }
...@@ -130,10 +130,10 @@ public: ...@@ -130,10 +130,10 @@ public:
*/ */
bool checkGroup(const std::string& name) { bool checkGroup(const std::string& name) {
auto returnIt = _groups.insert(name); auto returnIt = _groups.insert(name);
if (!returnIt.second) { //if (!returnIt.second) {
LOG(error) << "Group name \"" << name << "\" used twice!"; // LOG(error) << "Group name \"" << name << "\" used twice!";
return false; // return false;
} //}
return true; return true;
} }
......
...@@ -235,7 +235,7 @@ bool MQTTPusher::sendMappings() { ...@@ -235,7 +235,7 @@ bool MQTTPusher::sendMappings() {
else else
publishCtr++; publishCtr++;
} }
LOGM(info) << "Sensor name auto-publish performed for all sensors!"; LOGM(info) << "Sensor name auto-publish performed for all " << publishCtr << " sensors!";
return true; return true;
} }
......
...@@ -56,8 +56,8 @@ protected: ...@@ -56,8 +56,8 @@ protected:
const char CLOSE_SQBRKET = ']'; const char CLOSE_SQBRKET = ']';
const char DASH = '-'; const char DASH = '-';
const std::string SENSOR_PATTERN = "<sensor>"; const std::string SENSOR_PATTERN = "(?i)<sensor>";
const std::string GROUP_PATTERN = "<group>"; const std::string GROUP_PATTERN = "(?i)<group>";
public: public:
ConfiguratorTemplate() : ConfiguratorTemplate() :
...@@ -254,7 +254,8 @@ public: ...@@ -254,7 +254,8 @@ public:
} }
} }
//read of config finished. Now we build the mqtt-topic for every sensor //read of config finished. Now we build the mqtt-topic for every sensor
constructSensorNames(); if(!constructSensorNames())
return false;
for(const auto& g : _sensorGroups) { for(const auto& g : _sensorGroups) {
for(const auto& s : g->getSensors()) { for(const auto& s : g->getSensors()) {
s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt()); s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt());
...@@ -838,15 +839,17 @@ protected: ...@@ -838,15 +839,17 @@ protected:
/** /**
* Adjusts the names of the sensors in generated groups according to the sensorPattern specified in the global * Adjusts the names of the sensors in generated groups according to the sensorPattern specified in the global
* settings. Operates in tandem with the auto-publish feature. * settings. Operates in tandem with the auto-publish feature.
*
* @return true if successful, false otherwise
*/ */
void constructSensorNames() { bool constructSensorNames() {
boost::regex sensorReg(SENSOR_PATTERN), groupReg(GROUP_PATTERN); boost::regex sensorReg(SENSOR_PATTERN), groupReg(GROUP_PATTERN);
boost::cmatch match; boost::cmatch match;
if(_sensorPattern == "") if(_sensorPattern == "")
return; return true;
else if (!boost::regex_search(_sensorPattern.c_str(), match, sensorReg)) { else if (!boost::regex_search(_sensorPattern.c_str(), match, sensorReg)) {
LOG(error) << "Invalid sensor naming pattern " << _sensorPattern << ". You must at least include " << SENSOR_PATTERN << "!"; LOG(error) << "Invalid sensor naming pattern " << _sensorPattern << ". You must at least include <sensor>!";
return; return false;
} }
std::string name; std::string name;
...@@ -859,7 +862,7 @@ protected: ...@@ -859,7 +862,7 @@ protected:
// Setting the auto-publish name back to the sensor // Setting the auto-publish name back to the sensor
s->setName(name); s->setName(name);
} }
return; return true;
} }
std::string _entityName; std::string _entityName;
......
...@@ -158,7 +158,8 @@ bool MSRConfigurator::readConfig(std::string cfgPath) { ...@@ -158,7 +158,8 @@ bool MSRConfigurator::readConfig(std::string cfgPath) {
} }
} }
//read of config finished. Now we build the mqtt-topic for every sensor //read of config finished. Now we build the mqtt-topic for every sensor
constructSensorNames(); if(!constructSensorNames())
return false;
for(const auto& g : _sensorGroups) { for(const auto& g : _sensorGroups) {
for(const auto& s : g->getSensors()) { for(const auto& s : g->getSensors()) {
s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt()); s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt());
......
...@@ -229,7 +229,8 @@ bool PerfeventConfigurator::readConfig(std::string cfgPath) { ...@@ -229,7 +229,8 @@ bool PerfeventConfigurator::readConfig(std::string cfgPath) {
} }
} }
//read of config finished. Now we build the mqtt-topic for every sensor //read of config finished. Now we build the mqtt-topic for every sensor
constructSensorNames(); if(!constructSensorNames())
return false;
for(const auto& g : _sensorGroups) { for(const auto& g : _sensorGroups) {
for(const auto& s : g->getSensors()) { for(const auto& s : g->getSensors()) {
s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt()); s->setMqtt(_mqttPrefix + g->getMqttPart() + s->getMqtt());
......
...@@ -74,7 +74,8 @@ bool ProcfsConfigurator::readConfig(std::string cfgPath) { ...@@ -74,7 +74,8 @@ bool ProcfsConfigurator::readConfig(std::string cfgPath) {
return false; return false;
} }
} }
constructSensorNames(); if(!constructSensorNames())
return false;
return true; return true;
} }
......
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