diff --git a/ft/common.resource b/ft/common.resource index a5da48d8..9a2ce962 100644 --- a/ft/common.resource +++ b/ft/common.resource @@ -73,7 +73,7 @@ ${TOPIC_UPLOAD} neuron/${MQTT_CLIENTID}/upload ${MQTT_UUID} 123456-789abc-123456-aabbcc ${MQTT_CONFIG_HOST} 127.0.0.1 ${MQTT_CONFIG_PORT} ${1883} -${MQTT_CONFIG} {"client-id": "${MQTT_CLIENTID}", "format": 1, +${MQTT_CONFIG} {"client-id": "${MQTT_CLIENTID}", "upload-topic": "", "format": 1, ... "ssl": false, "host": "${MQTT_CONFIG_HOST}", "port": ${MQTT_CONFIG_PORT}, ... "username": "", "password": "", "ca":"", "cert":"", "key":""} diff --git a/ft/persistence_test.robot b/ft/persistence_test.robot index 90530b76..c339ec15 100644 --- a/ft/persistence_test.robot +++ b/ft/persistence_test.robot @@ -93,7 +93,7 @@ Get the setted adapters information after stopping and restarting Neuron, it sho Node Setting ${modbus_tcp2_id} {"host": "127.0.0.1", "port": 502, "connection_mode": 0} ${mqtt_id} = Get Node ID ${NODE_MQTT} mqtt-1 - Node Setting ${mqtt_id} {"client-id":"upload123", "format": 0, "ssl":false,"host":"127.0.0.1","port":1883,"username":"admin","password":"0000","ca":"", "cert":"", "key":""} + Node Setting ${mqtt_id} {"client-id":"upload123", "upload-topic":"", "format": 0, "ssl":false,"host":"127.0.0.1","port":1883,"username":"admin","password":"0000","ca":"", "cert":"", "key":""} Neuron Stop Running ${process} ${process} = Neuron Start Running diff --git a/include/neuron/connection/mqtt_client_intf.h b/include/neuron/connection/mqtt_client_intf.h index ea565f9b..c8d93755 100644 --- a/include/neuron/connection/mqtt_client_intf.h +++ b/include/neuron/connection/mqtt_client_intf.h @@ -59,7 +59,8 @@ typedef struct { char *keypass; /*application*/ - int format; + int format; + char *upload_topic; } neu_mqtt_option_t; typedef void *neu_mqtt_client_t; diff --git a/plugins/mqtt/mqtt.c b/plugins/mqtt/mqtt.c index 860cd2e2..21bdc5d2 100644 --- a/plugins/mqtt/mqtt.c +++ b/plugins/mqtt/mqtt.c @@ -129,7 +129,7 @@ static void topics_cleanup(vector_t *topics) vector_clear(topics); } -static void topics_generate(vector_t *topics, char *name) +static void topics_generate(vector_t *topics, char *name, char *upload_topic) { char *ping_req = real_topic_generate(TOPIC_PING_REQ, name); char *ping_res = real_topic_generate(TOPIC_STATUS_RES, name); @@ -143,8 +143,15 @@ static void topics_generate(vector_t *topics, char *name) char *write_res = real_topic_generate(TOPIC_WRITE_RES, name); topics_add(topics, write_req, QOS0, write_res, QOS0, TOPIC_TYPE_WRITE); + /// UPLOAD TOPIC SETTING char *upload_req = NULL; - char *upload_res = real_topic_generate(TOPIC_UPLOAD_RES, name); + char *upload_res = NULL; + if (NULL != upload_topic && 0 < strlen(upload_topic)) { + upload_res = upload_topic; + } else { + upload_res = real_topic_generate(TOPIC_UPLOAD_RES, name); + } + topics_add(topics, upload_req, QOS0, upload_res, QOS0, TOPIC_TYPE_UPLOAD); } @@ -460,7 +467,8 @@ static mqtt_routine_t *mqtt_routine_start(neu_plugin_t *plugin, } vector_init(&routine->topics, 1, sizeof(struct topic_pair)); - topics_generate(&routine->topics, routine->option.clientid); + topics_generate(&routine->topics, routine->option.clientid, + routine->option.upload_topic); topics_subscribe(&routine->topics, routine->client); return routine; } diff --git a/plugins/mqtt/mqtt.json b/plugins/mqtt/mqtt.json index af26f6ac..29036b6a 100644 --- a/plugins/mqtt/mqtt.json +++ b/plugins/mqtt/mqtt.json @@ -1,6 +1,6 @@ { "client-id": { - "name": "client-id", + "name": "client id", "description": "Neuron MQTT client ID", "type": "string", "attribute": "required", @@ -9,6 +9,16 @@ "length": 255 } }, + "upload-topic": { + "name": "upload topic", + "description": "User-defined upload topic", + "type": "string", + "attribute": "optional", + "default": "", + "valid": { + "length": 255 + } + }, "format": { "name": "upload format", "description": "Upload format on mqtt", diff --git a/plugins/mqtt/util.c b/plugins/mqtt/util.c index 90de792e..1f4a7f48 100644 --- a/plugins/mqtt/util.c +++ b/plugins/mqtt/util.c @@ -41,6 +41,11 @@ void mqtt_option_uninit(neu_mqtt_option_t *option) option->clientid = NULL; } + if (NULL != option->upload_topic) { + free(option->upload_topic); + option->upload_topic = NULL; + } + if (NULL != option->topic) { free(option->topic); option->topic = NULL; @@ -106,6 +111,7 @@ int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option) int ret = 0; char * error = NULL; neu_json_elem_t id = { .name = "client-id", .t = NEU_JSON_STR }; + neu_json_elem_t upload = { .name = "upload-topic", .t = NEU_JSON_STR }; neu_json_elem_t format = { .name = "format", .t = NEU_JSON_INT }; neu_json_elem_t ssl = { .name = "ssl", .t = NEU_JSON_BOOL }; neu_json_elem_t host = { .name = "host", .t = NEU_JSON_STR }; @@ -116,8 +122,8 @@ int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option) neu_json_elem_t cert = { .name = "cert", .t = NEU_JSON_STR }; neu_json_elem_t key = { .name = "key", .t = NEU_JSON_STR }; - ret = neu_parse_param(config->buf, &error, 10, &id, &format, &ssl, &host, - &port, &username, &password, &ca, &cert, &key); + ret = neu_parse_param(config->buf, &error, 11, &id, &upload, &format, &ssl, + &host, &port, &username, &password, &ca, &cert, &key); if (0 != ret) { return ret; } @@ -142,6 +148,7 @@ int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option) option->port = calloc(10, sizeof(char)); snprintf(option->port, 10, "%d", p); option->clientid = id.v.val_str; + option->upload_topic = upload.v.val_str; option->format = format.v.val_int; option->username = username.v.val_str; option->password = password.v.val_str; diff --git a/tests/mqtt_test.cc b/tests/mqtt_test.cc index 02a214d9..0435ddbb 100644 --- a/tests/mqtt_test.cc +++ b/tests/mqtt_test.cc @@ -10,7 +10,7 @@ TEST(MQTTTest, mqtt_option_init_by_config) neu_mqtt_option_t option; memset(&option, 0, sizeof(neu_mqtt_option_t)); config.buf = (char *) "{\"node_id\":5,\"params\":{\"client-id\":" - "\"upload123\", \"format\": " + "\"upload123\", \"upload-topic\":\"\", \"format\": " "0,\"ssl\":false,\"host\":\"192.168.10." "116\",\"port\":1883,\"username\":\"\",\"password\":" "\"\",\"ca\":\"\", \"cert\":\"\", \"key\":\"\"}}";