From bfdfad5b7770be10abcfab48ce1f4bc54d28a22f Mon Sep 17 00:00:00 2001 From: fengzero Date: Thu, 12 Jun 2025 02:51:42 +0000 Subject: [PATCH] mqtt: config enable_topic --- plugins/mqtt/mqtt.json | 10 ++++++++++ plugins/mqtt/mqtt_config.c | 26 +++++++++++++++++++------- plugins/mqtt/mqtt_config.h | 17 ++++++++++------- plugins/mqtt/mqtt_plugin_intf.c | 14 ++++++++++---- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/plugins/mqtt/mqtt.json b/plugins/mqtt/mqtt.json index 269da2e52..fe92ce36f 100644 --- a/plugins/mqtt/mqtt.json +++ b/plugins/mqtt/mqtt.json @@ -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": "写请求主题", diff --git a/plugins/mqtt/mqtt_config.c b/plugins/mqtt/mqtt_config.c index dfad485d3..c1cae3c27 100644 --- a/plugins/mqtt/mqtt_config.c +++ b/plugins/mqtt/mqtt_config.c @@ -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; diff --git a/plugins/mqtt/mqtt_config.h b/plugins/mqtt/mqtt_config.h index b4c1342af..f355f4c84 100644 --- a/plugins/mqtt/mqtt_config.h +++ b/plugins/mqtt/mqtt_config.h @@ -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 diff --git a/plugins/mqtt/mqtt_plugin_intf.c b/plugins/mqtt/mqtt_plugin_intf.c index f480301fd..7439befd6 100644 --- a/plugins/mqtt/mqtt_plugin_intf.c +++ b/plugins/mqtt/mqtt_plugin_intf.c @@ -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 @@ int mqtt_plugin_start(neu_plugin_t *plugin) 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"); }