allow using multiple --mqttvar= args

This commit is contained in:
John
2022-04-30 15:47:42 +02:00
parent b7eabbd267
commit 9bb907506e
+13 -11
View File
@@ -104,7 +104,7 @@ static const char* g_password = nullptr; //!< optional password for MQTT broker
static const char* g_topic = nullptr; //!< optional topic template static const char* g_topic = nullptr; //!< optional topic template
static const char* g_globalTopic = nullptr; //!< optional global topic static const char* g_globalTopic = nullptr; //!< optional global topic
static const char* g_integrationFile = nullptr; //!< the integration settings file static const char* g_integrationFile = nullptr; //!< the integration settings file
static const char* g_integrationVars = nullptr; //!< the integration settings variables static vector<string>* g_integrationVars = nullptr; //!< the integration settings variables
static bool g_retain = false; //!< whether to retail all topics static bool g_retain = false; //!< whether to retail all topics
static int g_qos = 0; //!< the qos value for all topics static int g_qos = 0; //!< the qos value for all topics
static OutputFormat g_publishFormat = OF_NONE; //!< the OutputFormat for publishing messages static OutputFormat g_publishFormat = OF_NONE; //!< the OutputFormat for publishing messages
@@ -140,6 +140,8 @@ static char* replaceSecret(char *arg) {
return ret; return ret;
} }
void splitFields(const string& str, vector<string>* row);
/** /**
* The MQTT argument parsing function. * The MQTT argument parsing function.
* @param key the key from @a g_mqtt_argp_options. * @param key the key from @a g_mqtt_argp_options.
@@ -247,7 +249,10 @@ static error_t mqtt_parse_opt(int key, char *arg, struct argp_state *state) {
argp_error(state, "invalid mqttvar"); argp_error(state, "invalid mqttvar");
return EINVAL; return EINVAL;
} }
g_integrationVars = arg; if (!g_integrationVars) {
g_integrationVars = new vector<string>();
}
splitFields(arg, g_integrationVars);
break; break;
case O_JSON: // --mqttjson[=short] case O_JSON: // --mqttjson[=short]
@@ -475,8 +480,6 @@ string removeTrailingNonTopicPart(const string& str) {
return str.substr(0, pos + 1); return str.substr(0, pos + 1);
} }
void splitFields(const string& str, vector<string>* row);
MqttHandler::MqttHandler(UserInfo* userInfo, BusHandler* busHandler, MessageMap* messages) MqttHandler::MqttHandler(UserInfo* userInfo, BusHandler* busHandler, MessageMap* messages)
: DataSink(userInfo, "mqtt"), DataSource(busHandler), WaitThread(), m_messages(messages), m_connected(false), : DataSink(userInfo, "mqtt"), DataSource(busHandler), WaitThread(), m_messages(messages), m_connected(false),
m_initialConnectFailed(false), m_lastUpdateCheckResult("."), m_lastScanStatus(SCAN_STATUS_NONE), m_lastErrorLogTime(0) { m_initialConnectFailed(false), m_lastUpdateCheckResult("."), m_lastScanStatus(SCAN_STATUS_NONE), m_lastErrorLogTime(0) {
@@ -489,14 +492,16 @@ MqttHandler::MqttHandler(UserInfo* userInfo, BusHandler* busHandler, MessageMap*
} else { } else {
hasIntegration = true; hasIntegration = true;
if (g_integrationVars) { if (g_integrationVars) {
vector<string> strs; for (auto& str : *g_integrationVars) {
splitFields(g_integrationVars, &strs);
for (auto& str : strs) {
m_replacers.parseLine(str); m_replacers.parseLine(str);
} }
} }
} }
} }
if (g_integrationVars) {
delete g_integrationVars;
g_integrationVars = nullptr;
}
// determine topic and prefix // determine topic and prefix
StringReplacer& topic = m_replacers.get("topic"); StringReplacer& topic = m_replacers.get("topic");
if (g_topic) { if (g_topic) {
@@ -860,10 +865,7 @@ void splitFields(const string& str, vector<string>* row) {
std::istringstream istr; std::istringstream istr;
istr.str(str); istr.str(str);
unsigned int lineNo = 0; unsigned int lineNo = 0;
FileReader::splitFields(&istr, row, &lineNo); FileReader::splitFields(&istr, row, &lineNo, nullptr, nullptr, false);
if (row->size() == 1 && (*row)[0].empty()) {
row->clear();
}
} }
void MqttHandler::run() { void MqttHandler::run() {