Commit 1c9b609d authored by Michael Ott's avatar Michael Ott
Browse files

Avoid conflicts for MQTT topics when creating new sensors

parent 91e1e5a5
......@@ -73,7 +73,7 @@ void usage(int argc, char* argv[])
std::cout << " MQTTPrefix - MQTT prefix to use for sensors" << std::endl;
}
sensor_t* createSensor(DCDB::SensorConfig& sensorConfig, const std::string& name, const std::string& prefix, const int suffix) {
sensor_t* createSensor(DCDB::SensorConfig& sensorConfig, const std::string& name, const std::string& prefix, int& suffix) {
sensor_t* sensor = new sensor_t;
sensor->name = name;
sensor->count = 0;
......@@ -88,20 +88,19 @@ sensor_t* createSensor(DCDB::SensorConfig& sensorConfig, const std::string& name
} else {
int prefixLen = prefix.size() - std::count(prefix.begin(), prefix.end(), '/');
int suffixLen = 28-prefixLen;
std::stringstream ss;
ss << std::setfill ('0') << std::setw(suffixLen) << std::hex << suffix;
std::string suffixStr = ss.str();
if (suffixLen < suffixStr.size()) {
std::cerr << "The specified MQTT prefix (" << prefixLen << ") length is too long, it should not exceed " << 28 - suffixStr.size() << " hex characters" << std::endl;
exit(EXIT_FAILURE);
}
if (suffixStr.size() < suffixLen) {
suffixStr.insert(0, std::string(suffixLen-suffixStr.size(), '0'));
}
sensor->topic = prefix + suffixStr;
do {
std::stringstream ss;
ss << std::setfill ('0') << std::setw(suffixLen) << std::hex << suffix;
std::string suffixStr = ss.str();
if (suffixLen < suffixStr.size()) {
std::cerr << "The specified MQTT prefix (" << prefixLen << ") length is too long, it should not exceed " << 28 - suffixStr.size() << " hex characters" << std::endl;
exit(EXIT_FAILURE);
}
sensor->topic = prefix + suffixStr;
suffix++;
} while (sensorConfig.getPublicSensorByPattern(psensor, sensor->topic.c_str()) != DCDB::SC_UNKNOWNSENSOR);
sensor->publicName = name;
std::replace(sensor->publicName.begin(), sensor->publicName.end(), ' ', '_');
......@@ -242,7 +241,6 @@ int main(int argc, char** argv)
if (col != tsColumn) {
sensor = createSensor(sensorConfig, *i, prefix, topics);
sensorsByCol.insert(std::pair<int,sensor_t*>(col, sensor));
topics++;
}
col++;
}
......@@ -275,7 +273,6 @@ int main(int argc, char** argv)
} else {
sensor = createSensor(sensorConfig, *i, prefix, topics);
sensorsByName.insert(std::pair<std::string,sensor_t*>(sensor->name, sensor));
topics++;
}
}
col++;
......
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