Merge pull request #470 from gc87/mqtt-user-defined-topic

feat(mqtt): user defined upload topic
This commit is contained in:
fengzeroz
2022-05-09 16:59:53 +08:00
committed by GitHub
7 changed files with 36 additions and 10 deletions
+1 -1
View File
@@ -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":""}
+1 -1
View File
@@ -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
+2 -1
View File
@@ -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;
+11 -3
View File
@@ -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;
}
+11 -1
View File
@@ -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",
+9 -2
View File
@@ -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;
+1 -1
View File
@@ -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\":\"\"}}";