Merge pull request #2483 from fengzeroz/v2.11

mqtt: config enable_topic
This commit is contained in:
fengzeroz
2025-06-12 11:08:48 +08:00
committed by GitHub
4 changed files with 49 additions and 18 deletions
+10
View File
@@ -105,6 +105,16 @@
"default": true,
"valid": {}
},
"enable_topic": {
"name": "Enable driver topic",
"name_zh": "启动驱动相关主题",
"description": "Subscription driver related topics, including read/req, write/req and action/req.",
"description_zh": "是否订阅驱动相关主题,包括 read/req、write/req 和 action/req。",
"attribute": "optional",
"type": "bool",
"default": true,
"valid": {}
},
"write-req-topic": {
"name": "Write Request Topic",
"name_zh": "写请求主题",
+19 -7
View File
@@ -297,13 +297,19 @@ int mqtt_config_parse(neu_plugin_t *plugin, const char *setting,
.t = NEU_JSON_INT };
neu_json_elem_t host = { .name = "host", .t = NEU_JSON_STR };
neu_json_elem_t port = { .name = "port", .t = NEU_JSON_INT };
neu_json_elem_t username = { .name = "username", .t = NEU_JSON_STR };
neu_json_elem_t password = { .name = "password", .t = NEU_JSON_STR };
neu_json_elem_t ssl = { .name = "ssl", .t = NEU_JSON_BOOL };
neu_json_elem_t ca = { .name = "ca", .t = NEU_JSON_STR };
neu_json_elem_t cert = { .name = "cert", .t = NEU_JSON_STR };
neu_json_elem_t key = { .name = "key", .t = NEU_JSON_STR };
neu_json_elem_t keypass = { .name = "keypass", .t = NEU_JSON_STR };
neu_json_elem_t username = { .name = "username", .t = NEU_JSON_STR };
neu_json_elem_t password = { .name = "password", .t = NEU_JSON_STR };
neu_json_elem_t ssl = { .name = "ssl", .t = NEU_JSON_BOOL };
neu_json_elem_t ca = { .name = "ca", .t = NEU_JSON_STR };
neu_json_elem_t cert = { .name = "cert", .t = NEU_JSON_STR };
neu_json_elem_t key = { .name = "key", .t = NEU_JSON_STR };
neu_json_elem_t keypass = { .name = "keypass", .t = NEU_JSON_STR };
neu_json_elem_t enable_topic = {
.name = "enable_topic",
.t = NEU_JSON_BOOL,
.v.val_bool = true,
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
};
neu_json_elem_t upload_drv_state = {
.name = "upload_drv_state",
.t = NEU_JSON_BOOL,
@@ -379,6 +385,11 @@ int mqtt_config_parse(neu_plugin_t *plugin, const char *setting,
free(schema.v.val_str);
}
ret = neu_parse_param(setting, &err_param, 1, &enable_topic);
if (0 != ret) {
enable_topic.v.val_bool = true; // default to true
}
ret = neu_parse_param(setting, &err_param, 2, &driver_action_req_topic,
&driver_action_resp_topic);
if (0 != ret) {
@@ -469,6 +480,7 @@ int mqtt_config_parse(neu_plugin_t *plugin, const char *setting,
config->client_id = client_id.v.val_str;
config->qos = qos.v.val_int;
config->format = format.v.val_int;
config->enable_topic = enable_topic.v.val_bool;
config->write_req_topic = write_req_topic.v.val_str;
config->write_resp_topic = write_resp_topic.v.val_str;
config->driver_action_req_topic = driver_action_req_topic.v.val_str;
+10 -7
View File
@@ -56,13 +56,16 @@ static inline const char *mqtt_upload_format_str(mqtt_upload_format_e f)
}
typedef struct {
neu_mqtt_version_e version; // mqtt version
char * client_id; // client id
neu_mqtt_qos_e qos; // message QoS
mqtt_upload_format_e format; // upload format
char * write_req_topic; // write request topic
char * write_resp_topic; // write response topic
char * driver_action_req_topic; // driver action request topic
neu_mqtt_version_e version; // mqtt version
char * client_id; // client id
neu_mqtt_qos_e qos; // message QoS
mqtt_upload_format_e format; // upload format
bool enable_topic; // default true
char *write_req_topic; // write request topic
char *write_resp_topic; // write response topic
char *driver_action_req_topic; // driver action request topic
char * driver_action_resp_topic; // driver action response topic
bool upload_err; // Upload tag error code flag
bool upload_drv_state; // upload driver state flag
+10 -4
View File
@@ -350,7 +350,9 @@ int mqtt_plugin_config(neu_plugin_t *plugin, const char *setting)
}
} else if (neu_mqtt_client_is_open(plugin->client)) {
started = true;
plugin->unsubscribe(plugin, &plugin->config);
if (plugin->config.enable_topic) {
plugin->unsubscribe(plugin, &plugin->config);
}
rv = neu_mqtt_client_close(plugin->client);
if (0 != rv) {
plog_error(plugin, "neu_mqtt_client_close fail");
@@ -386,8 +388,10 @@ int mqtt_plugin_config(neu_plugin_t *plugin, const char *setting)
rv = NEU_ERR_EINTERNAL;
goto error;
}
if (0 != (rv = plugin->subscribe(plugin, &config))) {
goto error;
if (plugin->config.enable_topic) {
if (0 != (rv = plugin->subscribe(plugin, &config))) {
goto error;
}
}
}
@@ -445,7 +449,9 @@ end:
int mqtt_plugin_stop(neu_plugin_t *plugin)
{
if (plugin->client) {
plugin->unsubscribe(plugin, &plugin->config);
if (plugin->config.enable_topic) {
plugin->unsubscribe(plugin, &plugin->config);
}
neu_mqtt_client_close(plugin->client);
plog_notice(plugin, "mqtt client closed");
}