diff --git a/README-CN.md b/README-CN.md index b0d6ed7c..4c7ff4ab 100644 --- a/README-CN.md +++ b/README-CN.md @@ -97,7 +97,6 @@ cd - mosquitto -v & pip3 install -r ft/requirements.txt python3 -m robot -P ft/ --variable neuron_api:http -d ft/http_reports ft -python3 -m robot -P ft/ --variable neuron_api:mqtt -d ft/mqtt_reports ft ``` diff --git a/README.md b/README.md index 595a5213..ffcdae89 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,6 @@ To run all functional testers mosquitto -v & pip3 install -r ft/requirements.txt python3 -m robot -P ft/ --variable neuron_api:http -d ft/http_reports ft -python3 -m robot -P ft/ --variable neuron_api:mqtt -d ft/mqtt_reports ft ``` diff --git a/ft/api_mqtt.resource b/ft/api_mqtt.resource index 11e2cecc..28b909a7 100644 --- a/ft/api_mqtt.resource +++ b/ft/api_mqtt.resource @@ -50,7 +50,7 @@ Recv Message ${msg} = Listen ${topic_res} timeout=10 Should Not Be True $msg == None ${res} = evaluate json.loads('''${msg}''') json - Should Be Equal As Strings ${res}[uuid] ${MQTT_UUID} + [RETURN] ${res} Pub And Recv @@ -63,7 +63,7 @@ Pub And Recv # try to receive a message with a matching UUID ${res} = Wait Until Keyword Succeeds 10s 0s Recv Message ${topic_res} - # Should Be Equal As Strings ${res}[command] ${cmd} + Should Be Equal As Strings ${res}[uuid] ${MQTT_UUID} # strip the `function` and `uuid` field Remove From Dictionary ${res} uuid command @@ -78,184 +78,160 @@ Check Response Status [Arguments] ${result} ${status} Log To Console MQTT API does not need to check status +_Http Check Response Status + [Arguments] ${result} ${status} + Integer response status ${status} + LOGIN ${token} = _Http LOGIN Add MQTT Node ${mqtt_node} = _Http Get Node ID ${NODE_MQTT} mqtt-adapter _Http Node Setting ${mqtt_node} ${MQTT_CONFIG} _Http Node Ctl ${mqtt_node} ${NODE_CTL_START} - _Http LOGOUT Sleep 1s Connect ${MQTT_CONFIG_HOST} ${MQTT_CONFIG_PORT} client_id=neuron-ft clean_session=${true} Subscribe ${TOPIC_STATUS_RES} qos=0 timeout=5 - Subscribe ${TOPIC_NODE_RES} qos=0 timeout=5 - Subscribe ${TOPIC_GCONFIG_RES} qos=0 timeout=5 - Subscribe ${TOPIC_TAGS_RES} qos=0 timeout=5 - Subscribe ${TOPIC_PLUGIN_RES} qos=0 timeout=5 - Subscribe ${TOPIC_SUBSCRIBE_RES} qos=0 timeout=5 Subscribe ${TOPIC_READ_RES} qos=0 timeout=5 Subscribe ${TOPIC_WRITE_RES} qos=0 timeout=5 - Subscribe ${TOPIC_TTYS_RES} qos=0 timeout=5 - Subscribe ${TOPIC_SCHEMA_RES} qos=0 timeout=5 - Subscribe ${TOPIC_SETTING_RES} qos=0 timeout=5 - Subscribe ${TOPIC_CTR_RES} qos=0 timeout=5 - Subscribe ${TOPIC_STATE_RES} qos=0 timeout=5 LOGOUT + _Http LOGOUT Unsubscribe All And Clear Messages Disconnect Add MQTT Node _Http Add Node type=${${NODE_MQTT}} name=mqtt-adapter plugin_name=mqtt +Add Plugin + [Arguments] ${node_type} ${name} ${kind} ${lib_name} + POST /api/v2/plugin {"node_type": ${node_type}, "name": ${name}, "kind": ${kind}, "lib_name": ${lib_name}} + + ${res} = Object response body + [RETURN] ${res}[0] + Add Node - [Arguments] &{args} - ${payload} = evaluate json.dumps(${args}) json - ${res} = Pub And Recv ${MQTT_CMD_ADD} ${payload} ${TOPIC_NODE_REQ} ${TOPIC_NODE_RES} + [Arguments] &{args} + POST /api/v2/node ${args} - [RETURN] ${res} - -Add Node And Return ID - [Arguments] ${node_type} ${node_name} ${plugin_name} - ${res} = Add Node type=${${node_type}} name=${node_name} plugin_name=${plugin_name} - Should Be Equal As Integers ${res}[error] ${ERR_SUCCESS} - - ${node_id} = Get Node ID ${node_type} ${node_name} - Should Not Be Equal As Integers ${node_id} 0 - - [RETURN] ${node_id} - -Get Nodes - [Arguments] ${node_type} - ${res} = Pub And Recv ${MQTT_CMD_GET} {"node_type": ${node_type} } ${TOPIC_NODE_REQ} ${TOPIC_NODE_RES} - - [RETURN] ${res} - -Del Node - [Arguments] ${node_id} - ${res} = Pub And Recv ${MQTT_CMD_DELETE} {"id": ${node_id}} ${TOPIC_NODE_REQ} ${TOPIC_NODE_RES} - - [RETURN] ${res} + ${res} = Object response body + [RETURN] ${res}[0] Get Node ID [Arguments] ${node_type} ${node_name} - ${res} = Get Nodes ${node_type} - ${node_id} Get Node By Name ${res}[nodes] ${node_name} - + GET /api/v2/node?type=${node_type} + Integer response status 200 + ${tmp} Array $.nodes + ${node_id} Get Node By Name ${tmp}[0] ${node_name} + Should Not Be Equal As Integers ${node_id} 0 [RETURN] ${node_id} Node Setting [Arguments] ${node_id} ${config} - ${res} = Pub And Recv ${MQTT_CMD_SET} {"node_id": ${node_id}, "params": ${config}} ${TOPIC_SETTING_REQ} ${TOPIC_SETTING_RES} - - [RETURN] ${res} - -Get Node Setting - [Arguments] ${node_id} - ${res} = Pub And Recv ${MQTT_CMD_GET} {"node_id": ${node_id}} ${TOPIC_SETTING_REQ} ${TOPIC_SETTING_RES} - - [RETURN] ${res} - -Get Node State - [Arguments] ${node_id} - ${res} = Pub And Recv ${MQTT_CMD_GET} {"node_id": ${node_id}} ${TOPIC_STATE_REQ} ${TOPIC_STATE_RES} - - [RETURN] ${res}[running] ${res}[link] + POST /api/v2/node/setting {"node_id": ${node_id}, "params": ${config}} + Integer response status 200 Node Ctl - [Arguments] ${node_id} ${cmd} - ${res} = Pub And Recv ${MQTT_CMD_NONE} {"id": ${node_id}, "cmd": ${cmd}} ${TOPIC_CTR_REQ} ${TOPIC_CTR_RES} + [Arguments] ${node_id} ${cmd} + POST /api/v2/node/ctl {"id": ${node_id}, "cmd": ${cmd}} + ${res} = Object response body - [RETURN] ${res} + [RETURN] ${res}[0] + +Add Group Config + [Arguments] ${node_id} ${grp_config_name} ${interval} + POST /api/v2/gconfig {"name": "${grp_config_name}", "node_id": ${node_id}, "interval": ${interval}} + ${res} = Object response body + + [RETURN] ${res}[0] + +Update Group Config + [Arguments] ${node_id} ${grp_config_name} ${interval} + PUT /api/v2/gconfig {"node_id": ${node_id}, "name": "${grp_config_name}", "interval": ${interval}} + ${res} = Object response body + + [RETURN] ${res}[0] + +Subscribe Group + [Arguments] ${driver_node_id} ${app_node_id} ${group_config_name} + POST /api/v2/subscribe {"src_node_id": ${driver_node_id}, "dst_node_id": ${app_node_id}, "name": "${group_config_name}"} + ${res} = Object response body + + [RETURN] ${res}[0] + +Unsubscribe Group + [Arguments] ${driver_node_id} ${app_node_id} ${group_config_name} + DELETE /api/v2/subscribe {"src_node_id": ${driver_node_id}, "dst_node_id": ${app_node_id}, "name": "${group_config_name}"} + ${res} = Object response body + + [RETURN] ${res}[0] + +Add Tags + [Arguments] ${node_id} ${group} ${tags} + POST /api/v2/tags {"node_id": ${node_id}, "group_config_name": "${group}", "tags": [${tags}]} + ${res} = Object response body + + [RETURN] ${res}[0] + +Add Tag And Return ID + [Arguments] ${node_id} ${group} ${tag} + ${res} = Add Tags ${node_id} ${group} ${tag} + Check Response Status ${res} 200 + Check Error Code ${res} ${ERR_SUCCESS} + ${tmp} = evaluate json.loads('''${tag}''') json + + Run Keyword And Return Get Tag ID ${node_id} ${group} ${tmp}[name] + +Get Tags + [Arguments] ${node_id} ${group} + GET /api/v2/tags?node_id=${node_id}&group_config_name=${group} + ${res} = Object response body + + [RETURN] ${res}[0] + +Update Tags + [Arguments] ${node_id} ${group} ${tags} + Put /api/v2/tags {"node_id": ${node_id}, "group_config_name": "${group}", "tags": [${tags}]} + ${res} = Object response body + + [RETURN] ${res}[0] + +Del Tags + [Arguments] ${node_id} ${group} ${ids} + DELETE /api/v2/tags {"node_id": ${node_id}, "group_config_name": "${group}", "ids": [${ids}]} + ${res} = Object response body + + [RETURN] ${res}[0] + +Get Tag ID + [Arguments] ${node_id} ${group} ${tag_name} + ${res} = Get Tags ${node_id} ${group} + Check Response Status ${res} 200 + + ${id} = Tag Find By Name ${res}[tags] ${tag_name} + Should Not Be Equal As Integers ${id} 0 + + [Return] ${id} + +Add Node And Return ID + [Arguments] ${node_type} ${node_name} ${plugin_name} + ${res} = Add Node type=${${node_type}} name=${node_name} plugin_name=${plugin_name} + + Log ${res} + _Http Check Response Status ${res} 200 + Check Error Code ${res} ${ERR_SUCCESS} + + ${node_id} = Get Node ID ${node_type} ${node_name} + Should Not Be Equal As Integers ${node_id} 0 + + [RETURN] ${node_id} Ping ${res} = Pub And Recv ${MQTT_CMD_NONE} {} ${TOPIC_PING_REQ} ${TOPIC_STATUS_RES} -Add Group Config - [Arguments] ${node_id} ${grp_config_name} ${interval} - ${res} = Pub And Recv ${MQTT_CMD_ADD} {"name": "${grp_config_name}", "node_id": ${node_id}, "interval": ${interval}} ${TOPIC_GCONFIG_REQ} ${TOPIC_GCONFIG_RES} - - [RETURN] ${res} - -Get Group Config - [Arguments] ${node_id} - ${res} = Pub And Recv ${MQTT_CMD_GET} {"node_id": ${node_id}} ${TOPIC_GCONFIG_REQ} ${TOPIC_GCONFIG_RES} - - [RETURN] ${res} - -Update Group Config - [Arguments] ${node_id} ${grp_config_name} ${interval} - ${res} = Pub And Recv ${MQTT_CMD_UPDATE} {"name": "${grp_config_name}", "node_id": ${node_id}, "interval": ${interval}} ${TOPIC_GCONFIG_REQ} ${TOPIC_GCONFIG_RES} - - [RETURN] ${res} - -Del Group Config - [Arguments] ${node_id} ${grp_config_name} - ${res} = Pub And Recv ${MQTT_CMD_DELETE} {"name": "${grp_config_name}", "node_id": ${node_id}} ${TOPIC_GCONFIG_REQ} ${TOPIC_GCONFIG_RES} - - [RETURN] ${res} - -Subscribe Group - [Arguments] ${driver_node_id} ${app_node_id} ${group_config_name} - ${res} = Pub And Recv ${MQTT_CMD_ADD} {"src_node_id": ${driver_node_id}, "dst_node_id": ${app_node_id}, "name": "${group_config_name}"} ${TOPIC_SUBSCRIBE_REQ} ${TOPIC_SUBSCRIBE_RES} - - [RETURN] ${res} - -Get Subscribe Group - [Arguments] ${node_id} - ${res} = Pub And Recv ${MQTT_CMD_GET} {"node_id": ${node_id}} ${TOPIC_SUBSCRIBE_REQ} ${TOPIC_SUBSCRIBE_RES} - - [RETURN] ${res} - -Unsubscribe Group - [Arguments] ${driver_node_id} ${app_node_id} ${group_config_name} - ${res} = Pub And Recv ${MQTT_CMD_DELETE} {"src_node_id": ${driver_node_id}, "dst_node_id": ${app_node_id}, "name": "${group_config_name}"} ${TOPIC_SUBSCRIBE_REQ} ${TOPIC_SUBSCRIBE_RES} - - [RETURN] ${res} - -Add Tags - [Arguments] ${node_id} ${group} ${tags} - ${res} = Pub And Recv ${MQTT_CMD_ADD} {"node_id": ${node_id}, "group_config_name": "${group}", "tags": [${tags}]} ${TOPIC_TAGS_REQ} ${TOPIC_TAGS_RES} - - [RETURN] ${res} - -Add Tag And Return ID - [Arguments] ${node_id} ${group} ${tag} - ${res} = Pub And Recv ${MQTT_CMD_ADD} {"node_id": ${node_id}, "group_config_name": "${group}", "tags": [${tag}]} ${TOPIC_TAGS_REQ} ${TOPIC_TAGS_RES} - - Check Error Code ${res} ${ERR_SUCCESS} - ${tmp} = evaluate json.loads('''${tag}''') json - - Run Keyword And Return Get Tag ID ${node_id} ${group} ${tmp}[name] - -Get Tags - [Arguments] ${node_id} ${group} - ${res} = Pub And Recv ${MQTT_CMD_GET} {"node_id": ${node_id}, "group_config_name": "${group}"} ${TOPIC_TAGS_REQ} ${TOPIC_TAGS_RES} - [Return] ${res} -Update Tags - [Arguments] ${node_id} ${group} ${tags} - ${res} = Pub And Recv ${MQTT_CMD_UPDATE} {"node_id": ${node_id}, "group_config_name": "${group}", "tags": [${tags}]} ${TOPIC_TAGS_REQ} ${TOPIC_TAGS_RES} - - [Return] ${res} - -Del Tags - [Arguments] ${node_id} ${group} ${ids} - ${res} = Pub And Recv ${MQTT_CMD_DELETE} {"node_id": ${node_id}, "group_config_name": "${group}", "ids": [${ids}]} ${TOPIC_TAGS_REQ} ${TOPIC_TAGS_RES} - - [Return] ${res} - -Get Tag ID - [Arguments] ${node_id} ${group} ${tag_name} - ${res} = Get Tags ${node_id} ${group} - - ${id} = Tag Find By Name ${res}[tags] ${tag_name} - - [Return] ${id} - Read Tags [Arguments] ${node_name} ${group} ${res} = Pub And Recv ${MQTT_CMD_NONE} {"node_name": "${node_name}", "group_name": "${group}"} ${TOPIC_READ_REQ} ${TOPIC_READ_RES} diff --git a/ft/common.resource b/ft/common.resource index 59623355..a5da48d8 100644 --- a/ft/common.resource +++ b/ft/common.resource @@ -58,46 +58,22 @@ ${TAG_DATA_TYPE_STRING} 15 ${PLUGIN_MODBUS_TCP} modbus-tcp ${PLUGIN_MQTT} mqtt -${MQTT_CMD_GET} get -${MQTT_CMD_SET} set -${MQTT_CMD_ADD} add -${MQTT_CMD_UPDATE} update -${MQTT_CMD_DELETE} delete ${MQTT_CMD_NONE} ${MQTT_CLIENTID} 123456 ${TOPIC_PING_REQ} neuron/${MQTT_CLIENTID}/ping -${TOPIC_NODE_REQ} neuron/${MQTT_CLIENTID}/node/req -${TOPIC_GCONFIG_REQ} neuron/${MQTT_CLIENTID}/gconfig/req -${TOPIC_TAGS_REQ} neuron/${MQTT_CLIENTID}/tags/req -${TOPIC_PLUGIN_REQ} neuron/${MQTT_CLIENTID}/plugin/req -${TOPIC_SUBSCRIBE_REQ} neuron/${MQTT_CLIENTID}/subscribe/req ${TOPIC_READ_REQ} neuron/${MQTT_CLIENTID}/read/req ${TOPIC_WRITE_REQ} neuron/${MQTT_CLIENTID}/write/req -${TOPIC_TTYS_REQ} neuron/${MQTT_CLIENTID}/ttys/req -${TOPIC_SCHEMA_REQ} neuron/${MQTT_CLIENTID}/schema/plugin/req -${TOPIC_SETTING_REQ} neuron/${MQTT_CLIENTID}/node/setting/req -${TOPIC_CTR_REQ} neuron/${MQTT_CLIENTID}/node/ctl/req -${TOPIC_STATE_REQ} neuron/${MQTT_CLIENTID}/node/state/req ${TOPIC_STATUS_RES} neuron/${MQTT_CLIENTID}/status -${TOPIC_NODE_RES} neuron/${MQTT_CLIENTID}/node/resp -${TOPIC_GCONFIG_RES} neuron/${MQTT_CLIENTID}/gconfig/resp -${TOPIC_TAGS_RES} neuron/${MQTT_CLIENTID}/tags/resp -${TOPIC_PLUGIN_RES} neuron/${MQTT_CLIENTID}/plugin/resp -${TOPIC_SUBSCRIBE_RES} neuron/${MQTT_CLIENTID}/subscribe/resp ${TOPIC_READ_RES} neuron/${MQTT_CLIENTID}/read/resp ${TOPIC_WRITE_RES} neuron/${MQTT_CLIENTID}/write/resp -${TOPIC_TTYS_RES} neuron/${MQTT_CLIENTID}/ttys/resp -${TOPIC_SCHEMA_RES} neuron/${MQTT_CLIENTID}/schema/plugin/resp -${TOPIC_SETTING_RES} neuron/${MQTT_CLIENTID}/node/setting/resp -${TOPIC_CTR_RES} neuron/${MQTT_CLIENTID}/node/ctl/resp -${TOPIC_STATE_RES} neuron/${MQTT_CLIENTID}/node/state/resp +${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": 0, +${MQTT_CONFIG} {"client-id": "${MQTT_CLIENTID}", "format": 1, ... "ssl": false, "host": "${MQTT_CONFIG_HOST}", "port": ${MQTT_CONFIG_PORT}, ... "username": "", "password": "", "ca":"", "cert":"", "key":""} diff --git a/ft/modbus_tcp_test.robot b/ft/modbus_tcp_test.robot index a16c403c..5e97e666 100644 --- a/ft/modbus_tcp_test.robot +++ b/ft/modbus_tcp_test.robot @@ -352,6 +352,50 @@ Read multiple points belonging to different areas(coil/input/input reg/hold reg) [Teardown] Del Tags ${test_node_id} ${group} ${tag1_id},${tag2_id},${tag3_id},${tag4_id},${tag5_id},${tag6_id},${tag7_id},${tag8_id},${tag9_id},${tag10_id} + +# Recv multiple points belonging to different areas(coil/input/input reg/hold reg) and different sites, the point address includes continuous and non-continuous, and the data include int16/uint16/int32/uint32/float type, it should return success +# Skip If Not MQTT API + +# ${tag1_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag1", "address": "1!000018", "attribute": ${TAG_ATTRIBUTE_RW}, "type": ${TAG_DATA_TYPE_BIT}} +# ${tag2_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag2", "address": "2!000018", "attribute": ${TAG_ATTRIBUTE_RW}, "type": ${TAG_DATA_TYPE_BIT}} +# ${tag3_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag3", "address": "3!000021", "attribute": ${TAG_ATTRIBUTE_RW}, "type": ${TAG_DATA_TYPE_BIT}} +# ${tag4_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag4", "address": "4!100018", "attribute": ${TAG_ATTRIBUTE_READ}, "type": ${TAG_DATA_TYPE_BIT}} +# ${tag5_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag5", "address": "5!300018", "attribute": ${TAG_ATTRIBUTE_READ}, "type": ${TAG_DATA_TYPE_INT16}} +# ${tag6_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag6", "address": "6!300018", "attribute": ${TAG_ATTRIBUTE_READ}, "type": ${TAG_DATA_TYPE_UINT32}} +# ${tag7_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag7", "address": "7!300022", "attribute": ${TAG_ATTRIBUTE_READ}, "type": ${TAG_DATA_TYPE_FLOAT}} +# ${tag8_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag8", "address": "8!400018", "attribute": ${TAG_ATTRIBUTE_RW}, "type": ${TAG_DATA_TYPE_UINT16}} +# ${tag9_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag9", "address": "9!400040", "attribute": ${TAG_ATTRIBUTE_RW}, "type": ${TAG_DATA_TYPE_INT32}} +# ${tag10_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag10", "address": "10!400022", "attribute": ${TAG_ATTRIBUTE_RW}, "type": ${TAG_DATA_TYPE_FLOAT}} + +# Should Not Be Equal As Integers ${tag1_id} -1 +# Should Not Be Equal As Integers ${tag2_id} -1 +# Should Not Be Equal As Integers ${tag3_id} -1 +# Should Not Be Equal As Integers ${tag4_id} -1 +# Should Not Be Equal As Integers ${tag5_id} -1 +# Should Not Be Equal As Integers ${tag6_id} -1 +# Should Not Be Equal As Integers ${tag7_id} -1 +# Should Not Be Equal As Integers ${tag8_id} -1 +# Should Not Be Equal As Integers ${tag9_id} -1 +# Should Not Be Equal As Integers ${tag10_id} -1 + +# Subscribe ${TOPIC_UPLOAD} qos=0 timeout=5 +# Sleep 1s 500ms +# ${res} = Recv Message ${TOPIC_UPLOAD} +# Log ${res}[tags] + +# Compare Tag Value As Int ${res}[tags] tag1 1 +# Compare Tag Value As Int ${res}[tags] tag2 1 +# Compare Tag Value As Int ${res}[tags] tag3 1 +# Compare Tag Value As Int ${res}[tags] tag4 1 +# Compare Tag Value As Int ${res}[tags] tag5 17 +# Compare Tag Value As Int ${res}[tags] tag6 1114130 +# Compare Tag Value As Float ${res}[tags] tag7 1.928576247884031e-39 +# Compare Tag Value As Int ${res}[tags] tag8 62226 +# Compare Tag Value As Int ${res}[tags] tag9 66651136 +# Compare Tag Value As Float ${res}[tags] tag10 11.789 + +# [Teardown] Del Tags ${test_node_id} ${group} ${tag1_id},${tag2_id},${tag3_id},${tag4_id},${tag5_id},${tag6_id},${tag7_id},${tag8_id},${tag9_id},${tag10_id} + *** Keywords *** Neuron Context Ready Import Neuron API Resource diff --git a/include/neuron/json/neu_json_mqtt.h b/include/neuron/json/neu_json_mqtt.h index bd21a497..f799a3c2 100644 --- a/include/neuron/json/neu_json_mqtt.h +++ b/include/neuron/json/neu_json_mqtt.h @@ -31,13 +31,6 @@ extern "C" { #include -#define NEU_MQTT_CMD_GET "get" -#define NEU_MQTT_CMD_SET "set" -#define NEU_MQTT_CMD_ADD "add" -#define NEU_MQTT_CMD_UPDATE "update" -#define NEU_MQTT_CMD_DELETE "delete" -#define NEU_MQTT_CMD_NONE "" - typedef struct { char *uuid; char *command; diff --git a/plugins/mqtt/CMakeLists.txt b/plugins/mqtt/CMakeLists.txt index a4bffe4a..16c75790 100644 --- a/plugins/mqtt/CMakeLists.txt +++ b/plugins/mqtt/CMakeLists.txt @@ -4,14 +4,8 @@ set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/plugins") set(src mqtt.c util.c - command/read_write.c - command/group_config.c - command/datatag.c - command/node.c command/ping.c - command/plugin.c - command/tty.c - command/schema.c + command/read_write.c command/command.c) add_library(plugin-mqtt SHARED ${src}) diff --git a/plugins/mqtt/command/command.c b/plugins/mqtt/command/command.c index 22e7c55d..634b9f22 100644 --- a/plugins/mqtt/command/command.c +++ b/plugins/mqtt/command/command.c @@ -1,14 +1,8 @@ #include #include -#include "datatag.h" -#include "group_config.h" -#include "node.h" #include "ping.h" -#include "plugin.h" #include "read_write.h" -#include "schema.h" -#include "tty.h" #include "command.h" @@ -20,238 +14,6 @@ static int ping_response(char **output_str, neu_plugin_t *plugin, return 0; } -// Maniplate node -static int node_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_nodes_req_t *req = NULL; - int rc = neu_json_decode_get_nodes_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_get(plugin, mqtt, req); - neu_json_decode_get_nodes_req_free(req); - } - - return rc; -} - -static int node_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_add_node_req_t *req = NULL; - int rc = neu_json_decode_add_node_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_add(plugin, mqtt, req); - neu_json_decode_add_node_req_free(req); - } - - return rc; -} - -static int node_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_del_node_req_t *req = NULL; - int rc = neu_json_decode_del_node_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_delete(plugin, mqtt, req); - neu_json_decode_del_node_req_free(req); - } - - return rc; -} - -// Manipulate group config -static int group_config_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_group_config_req_t *req = NULL; - int rc = neu_json_decode_get_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_get(plugin, mqtt, req); - neu_json_decode_get_group_config_req_free(req); - } - - return rc; -} - -static int group_config_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_add_group_config_req_t *req = NULL; - int rc = neu_json_decode_add_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_add(plugin, mqtt, req); - neu_json_decode_add_group_config_req_free(req); - } - - return rc; -} - -static int group_config_response_update(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_update_group_config_req_t *req = NULL; - int rc = neu_json_decode_update_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_update(plugin, mqtt, req); - neu_json_decode_update_group_config_req_free(req); - } - - return rc; -} - -static int group_config_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_del_group_config_req_t *req = NULL; - int rc = neu_json_decode_del_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_delete(plugin, mqtt, req); - neu_json_decode_del_group_config_req_free(req); - } - - return rc; -} - -// Manipulate data tags -static int datatag_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_tags_req_t *req = NULL; - int rc = neu_json_decode_get_tags_req(json_str, &req); - *output_str = command_get_tags(plugin, mqtt, req); - if (0 == rc) { - neu_json_decode_get_tags_req_free(req); - } - - return rc; -} - -static int datatag_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_add_tags_req_t *req = NULL; - int rc = neu_json_decode_add_tags_req(json_str, &req); - *output_str = command_add_tags(plugin, mqtt, req); - if (0 == rc) { - neu_json_decode_add_tags_req_free(req); - } - - return rc; -} - -static int datatag_response_update(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_update_tags_req_t *req = NULL; - int rc = neu_json_decode_update_tags_req(json_str, &req); - *output_str = command_update_tags(plugin, mqtt, req); - if (0 == rc) { - neu_json_decode_update_tags_req_free(req); - } - - return rc; -} - -static int datatag_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_del_tags_req_t *req = NULL; - int rc = neu_json_decode_del_tags_req(json_str, &req); - *output_str = command_delete_tags(plugin, mqtt, req); - if (0 == rc) { - neu_json_decode_del_tags_req_free(req); - } - - return rc; -} - -// Manipulate plugin libs -static int plugin_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_plugin_req_t *req = NULL; - int rc = neu_json_decode_get_plugin_req(json_str, &req); - if (0 == rc) { - *output_str = command_plugin_get(plugin, mqtt, req); - neu_json_decode_get_plugin_req_free(req); - } - - return rc; -} - -static int plugin_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_add_plugin_req_t *req = NULL; - int rc = neu_json_decode_add_plugin_req(json_str, &req); - if (0 == rc) { - *output_str = command_plugin_add(plugin, mqtt, req); - neu_json_decode_add_plugin_req_free(req); - } - - return rc; -} - -// static int plugin_response_update(char **output_str, neu_plugin_t *plugin, -// neu_json_mqtt_t *mqtt, char *json_str) -// { -// return 0; -// } - -static int plugin_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_del_plugin_req_t *req = NULL; - int rc = neu_json_decode_del_plugin_req(json_str, &req); - if (0 == rc) { - *output_str = command_plugin_delete(plugin, mqtt, req); - neu_json_decode_del_plugin_req_free(req); - } - - return rc; -} - -// Subscribe group config -static int subscribe_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_get_group_config_req_t *req = NULL; - int rc = neu_json_decode_get_group_config_req(json_str, &req); - if (0 == rc) { - *output_str = command_get_group_config_subscribes(plugin, mqtt, req); - neu_json_decode_get_group_config_req_free(req); - } - - return rc; -} - -static int subscribe_response_add(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_subscribe_req_t *req = NULL; - int rc = neu_json_decode_subscribe_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_subscribe(plugin, mqtt, req); - neu_json_decode_subscribe_req_free(req); - } - - return rc; -} - -static int subscribe_response_delete(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_unsubscribe_req_t *req = NULL; - int rc = neu_json_decode_unsubscribe_req(json_str, &req); - if (0 == rc) { - *output_str = command_group_config_unsubscribe(plugin, mqtt, req); - neu_json_decode_unsubscribe_req_free(req); - } - - return rc; -} - // Read tags with group config static int read_response(mqtt_response_t *response, neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, char *json_str) @@ -293,79 +55,6 @@ static int write_response(mqtt_response_t *response, neu_plugin_t *plugin, return rc; } -// Manipulate TTY -static int tty_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - UNUSED(json_str); - *output_str = command_tty_get(plugin, mqtt); - return 0; -} - -// Manipulate plugin schema -static int schema_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - UNUSED(json_str); - *output_str = command_schema_get(plugin, mqtt); - return 0; -} - -// Manipulate node setting -static int setting_response_set(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_node_setting_req_t *req = NULL; - int rc = neu_json_decode_node_setting_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_setting_set(plugin, mqtt, req, json_str); - neu_json_decode_node_setting_req_free(req); - } - - return rc; -} - -static int setting_response_get(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_node_setting_req_t *req = NULL; - int rc = neu_json_decode_node_setting_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_setting_get(plugin, mqtt, req); - neu_json_decode_node_setting_req_free(req); - } - - return rc; -} - -// Control the node -static int control_response(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_node_ctl_req_t *req = NULL; - int rc = neu_json_decode_node_ctl_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_control(plugin, mqtt, req); - neu_json_decode_node_ctl_req_free(req); - } - - return rc; -} - -// Manipulate node state -static int state_response(char **output_str, neu_plugin_t *plugin, - neu_json_mqtt_t *mqtt, char *json_str) -{ - neu_json_node_setting_req_t *req = NULL; - int rc = neu_json_decode_node_setting_req(json_str, &req); - if (0 == rc) { - *output_str = command_node_state_get(plugin, mqtt, req); - neu_json_decode_node_setting_req_free(req); - } - - return rc; -} - void command_response_handle(mqtt_response_t *response) { neu_plugin_t *plugin = (neu_plugin_t *) response->plugin; @@ -391,16 +80,6 @@ void command_response_handle(mqtt_response_t *response) return; } - if (0 != strcmp(mqtt->command, NEU_MQTT_CMD_GET) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_SET) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_ADD) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_UPDATE) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_DELETE) && - 0 != strcmp(mqtt->command, NEU_MQTT_CMD_NONE)) { - log_error("Unsupported command"); - return; - } - switch (response->type) { case TOPIC_TYPE_PING: { // Ping @@ -408,75 +87,6 @@ void command_response_handle(mqtt_response_t *response) break; } - case TOPIC_TYPE_NODE: { - // Node GET/ADD/UPDATE/DELETE - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = node_response_get(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = node_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = node_response_delete(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_GCONFIG: { - // Group Config GET/ADD/UPDATE/DELETE - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = group_config_response_get(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = group_config_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_UPDATE, mqtt->command)) { - rc = group_config_response_update(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = group_config_response_delete(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_TAGS: { - // Tag GET/ADD/UPDATE/DELETE - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = datatag_response_get(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = datatag_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_UPDATE, mqtt->command)) { - rc = datatag_response_update(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = datatag_response_delete(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_PLUGIN: { - // Plugin GET/ADD/UPDATE/DELETE - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = plugin_response_get(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = plugin_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = plugin_response_delete(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_SUBSCRIBE: { - // Subscribe GET/ADD/DELETE - if (0 == strcmp(NEU_MQTT_CMD_ADD, mqtt->command)) { - rc = subscribe_response_add(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_DELETE, mqtt->command)) { - rc = subscribe_response_delete(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = subscribe_response_get(&ret_str, plugin, mqtt, json_str); - } - - break; - } - case TOPIC_TYPE_READ: { rc = read_response(response, plugin, mqtt, json_str); break; @@ -486,43 +96,6 @@ void command_response_handle(mqtt_response_t *response) rc = write_response(response, plugin, mqtt, json_str); break; } - - case TOPIC_TYPE_TTYS: { - // TTY GET - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = tty_response_get(&ret_str, plugin, mqtt, json_str); - } - break; - } - - case TOPIC_TYPE_SCHEMA: { - // Schema GET - if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = schema_response_get(&ret_str, plugin, mqtt, json_str); - } - break; - } - - case TOPIC_TYPE_SETTING: { - // Setting SET/GET - if (0 == strcmp(NEU_MQTT_CMD_SET, mqtt->command)) { - rc = setting_response_set(&ret_str, plugin, mqtt, json_str); - } else if (0 == strcmp(NEU_MQTT_CMD_GET, mqtt->command)) { - rc = setting_response_get(&ret_str, plugin, mqtt, json_str); - } - - break; - } - - case TOPIC_TYPE_CTR: { - rc = control_response(&ret_str, plugin, mqtt, json_str); - break; - } - - case TOPIC_TYPE_STATE: { - rc = state_response(&ret_str, plugin, mqtt, json_str); - break; - } default: assert(1 == 0); break; diff --git a/plugins/mqtt/command/datatag.c b/plugins/mqtt/command/datatag.c deleted file mode 100644 index ade16bbe..00000000 --- a/plugins/mqtt/command/datatag.c +++ /dev/null @@ -1,330 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#include - -#include "datatag.h" -#include "json/neu_json_error.h" - -char *command_get_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_tags_req_t *req) -{ - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - if (NULL == req) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - log_info("Get tag list uuid:%s, node id:%d", mqtt->uuid, req->node_id); - - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - if (NULL == table) { - error.error = NEU_ERR_NODE_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - vector_t grp_configs = neu_system_get_group_configs(plugin, req->node_id); - neu_json_get_tags_resp_t res = { 0 }; - int index = 0; - - VECTOR_FOR_EACH(&grp_configs, iter) - { - neu_taggrp_config_t *config = - *(neu_taggrp_config_t **) iterator_get(&iter); - vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); - if (NULL != req->group_config_name && - strncmp(neu_taggrp_cfg_get_name(config), req->group_config_name, - strlen(req->group_config_name)) != 0) { - continue; - } - - res.n_tag += ids->size; - } - - res.tags = calloc(res.n_tag, sizeof(neu_json_get_tags_resp_tag_t)); - - VECTOR_FOR_EACH(&grp_configs, iter) - { - neu_taggrp_config_t *config = - *(neu_taggrp_config_t **) iterator_get(&iter); - const char *group_name = neu_taggrp_cfg_get_name(config); - - if (NULL != req->group_config_name && - strncmp(group_name, req->group_config_name, - strlen(req->group_config_name)) != 0) { - continue; - } - - vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); - - VECTOR_FOR_EACH(ids, iter_id) - { - neu_datatag_id_t *id = (neu_datatag_id_t *) iterator_get(&iter_id); - neu_datatag_t * tag = neu_datatag_tbl_get(table, *id); - - res.tags[index].id = *id; - res.tags[index].name = tag->name; - res.tags[index].group_config_name = (char *) group_name; - res.tags[index].type = tag->type; - res.tags[index].attribute = tag->attribute; - res.tags[index].address = tag->addr_str; - - index += 1; - } - } - - neu_json_encode_with_mqtt(&res, neu_json_encode_get_tags_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - vector_uninit(&grp_configs); - free(res.tags); - return result; -} - -char *command_add_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_tags_req_t *req) -{ - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - if (NULL == req) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - log_info("Add tags uuid:%s, node id:%d, group config name:%s", mqtt->uuid, - req->node_id, req->group_config_name); - - neu_taggrp_config_t *new_config = NULL; - neu_taggrp_config_t *config = neu_system_find_group_config( - plugin, req->node_id, req->group_config_name); - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - - if (NULL == table) { - error.error = NEU_ERR_NODE_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - if (NULL == config) { - new_config = neu_taggrp_cfg_new(req->group_config_name); - neu_taggrp_cfg_set_interval(new_config, 10000); - } else { - new_config = neu_taggrp_cfg_clone(config); - } - - vector_t * ids = neu_taggrp_cfg_get_datatag_ids(new_config); - neu_err_code_e code = { 0 }; - bool added = false; - - for (int i = 0; i < req->n_tag; i++) { - if (!neu_tag_check_attribute(req->tags[i].attribute)) { - code = NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT; - continue; - } - - neu_datatag_t *tag = - neu_datatag_alloc(req->tags[i].attribute, req->tags[i].type, - req->tags[i].address, req->tags[i].name); - - if (NULL == tag) { - code = NEU_ERR_ENOMEM; - continue; - } - - if (neu_datatag_tbl_add(table, tag)) { - vector_push_back(ids, &tag->id); - added = true; - } else { - log_warn_node(plugin, - "Add failed, tag name exists, uuid:%s, " - "node_id:%d, req_tag_name:%s", - mqtt->uuid, req->node_id, req->tags[i].name); - code = NEU_ERR_TAG_NAME_CONFLICT; - free(tag); - } - } - - int rv = 0; - if (config != NULL) { - rv = neu_system_update_group_config(plugin, req->node_id, new_config); - } else { - rv = neu_system_add_group_config(plugin, req->node_id, new_config); - } - - if (0 != rv) { - code = rv; - added = false; - } - - if (added) { - uint32_t event_id = neu_plugin_get_event_id(plugin); - neu_plugin_notify_event_add_tags(plugin, event_id, req->node_id, - req->group_config_name); - } - - error.error = code; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_update_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_tags_req_t *req) -{ - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - if (NULL == req) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - log_info("Update tags uuid:%s, node id:%d", mqtt->uuid, req->node_id); - - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - if (NULL == table) { - error.error = NEU_ERR_NODE_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - bool updated = false; - neu_err_code_e code = { 0 }; - for (int i = 0; i < req->n_tag; i++) { - neu_datatag_t *tag = neu_datatag_tbl_get(table, req->tags[i].id); - - if (tag == NULL) { - code = NEU_ERR_TAG_NOT_EXIST; - continue; - } - - if (!neu_tag_check_attribute(req->tags[i].attribute)) { - code = NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT; - continue; - } - - tag = neu_datatag_alloc(req->tags[i].attribute, req->tags[i].type, - req->tags[i].address, req->tags[i].name); - - if (NULL == tag) { - code = NEU_ERR_ENOMEM; - continue; - } - - if (0 != neu_datatag_tbl_update(table, req->tags[i].id, tag)) { - log_warn_node(plugin, - "Update failed, tag name exists, uuid:%s, " - "node_id:%d, tag_id:%d, req_tag_name:%s", - mqtt->uuid, req->node_id, req->tags[i].id, - req->tags[i].name); - code = NEU_ERR_TAG_NAME_CONFLICT; - free(tag); - } else { - updated = true; - } - } - - if (updated) { - uint32_t event_id = neu_plugin_get_event_id(plugin); - neu_plugin_notify_event_update_tags(plugin, event_id, req->node_id, - req->group_config_name); - } - - error.error = code; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_delete_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_tags_req_t *req) -{ - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - if (NULL == req) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - log_info("Delete tags uuid:%s, node id:%d, group config:%s", mqtt->uuid, - req->node_id, req->group_config_name); - - neu_taggrp_config_t *new_config = NULL; - neu_taggrp_config_t *config = neu_system_find_group_config( - plugin, req->node_id, req->group_config_name); - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - - if (NULL == config || NULL == table) { - error.error = NEU_ERR_NODE_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - new_config = neu_taggrp_cfg_clone(config); - bool deleted = false; - - for (int i = 0; i < req->n_id; i++) { - if (neu_datatag_tbl_remove(table, req->ids[i]) == 0) { - vector_t *ids = neu_taggrp_cfg_get_datatag_ids(new_config); - - VECTOR_FOR_EACH(ids, iter) - { - neu_datatag_id_t *id = (neu_datatag_id_t *) iterator_get(&iter); - if (*id == req->ids[i]) { - iterator_erase(ids, &iter); - break; - } - } - - deleted = true; - } - } - - int rv = neu_system_update_group_config(plugin, req->node_id, new_config); - if (0 != rv) { - error.error = rv; - deleted = false; - } - - if (deleted) { - uint32_t event_id = neu_plugin_get_event_id(plugin); - neu_plugin_notify_event_del_tags(plugin, event_id, req->node_id, - req->group_config_name); - } - - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} diff --git a/plugins/mqtt/command/datatag.h b/plugins/mqtt/command/datatag.h deleted file mode 100644 index baff3b82..00000000 --- a/plugins/mqtt/command/datatag.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#ifndef NEURON_PLUGIN_MQTT_COMMAND_DATATAG -#define NEURON_PLUGIN_MQTT_COMMAND_DATATAG - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_get_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_tags_req_t *req); -char *command_add_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_tags_req_t *req); -char *command_update_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_tags_req_t *req); -char *command_delete_tags(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_tags_req_t *req); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/plugins/mqtt/command/group_config.c b/plugins/mqtt/command/group_config.c deleted file mode 100644 index 7071baab..00000000 --- a/plugins/mqtt/command/group_config.c +++ /dev/null @@ -1,227 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#include - -#include "group_config.h" -#include "subscribe.h" -#include "json/neu_json_error.h" - -char *command_group_config_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_group_config_req_t *req) -{ - log_debug("Get group config uuid:%s, node id:%d", mqtt->uuid, req->node_id); - neu_json_get_group_config_resp_t res = { 0 }; - int index = 0; - vector_t configs = neu_system_get_group_configs(plugin, req->node_id); - - res.n_group_config = configs.size; - res.group_configs = - calloc(res.n_group_config, - sizeof(neu_json_get_group_config_resp_group_config_t)); - - VECTOR_FOR_EACH(&configs, iter) - { - neu_taggrp_config_t *config = - *(neu_taggrp_config_t **) iterator_get(&iter); - - res.group_configs[index].name = - (char *) neu_taggrp_cfg_get_name(config); - res.group_configs[index].interval = neu_taggrp_cfg_get_interval(config); - res.group_configs[index].tag_count = - neu_taggrp_cfg_get_datatag_ids(config)->size; - res.group_configs[index].pipe_count = - neu_taggrp_cfg_get_subpipes(config)->size; - - index += 1; - } - - char *result = NULL; - neu_json_encode_with_mqtt(&res, neu_json_encode_get_group_config_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - - vector_uninit(&configs); - free(res.group_configs); - return result; -} - -char *command_group_config_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_group_config_req_t *req) -{ - log_debug("Add group config uuid:%s, config:%s, node id:%d, " - "read interval:%d", - mqtt->uuid, req->name, req->node_id, req->interval); - neu_taggrp_config_t *config = neu_taggrp_cfg_new(req->name); - neu_taggrp_cfg_set_interval(config, req->interval); - neu_json_error_resp_t error = { 0 }; - error.error = neu_system_add_group_config(plugin, req->node_id, config); - - if (NEU_ERR_SUCCESS != error.error) { - neu_taggrp_cfg_free(config); - } - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_group_config_update(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_group_config_req_t *req) -{ - log_debug("Update group config uuid:%s, config:%s, src node id:%d," - "read interval:%d", - mqtt->uuid, req->name, req->node_id, req->interval); - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - neu_taggrp_config_t * config = - neu_system_find_group_config(plugin, req->node_id, req->name); - if (NULL == config) { - error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - neu_taggrp_cfg_set_interval(config, req->interval); - error.error = neu_system_update_group_config(plugin, req->node_id, config); - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_group_config_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_group_config_req_t *req) -{ - log_debug("Delete group config uuid:%s, config:%s, src node id:%d", - mqtt->uuid, req->name, req->node_id); - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - neu_taggrp_config_t * config = - neu_system_find_group_config(plugin, req->node_id, req->name); - neu_datatag_table_t *table = - neu_system_get_datatags_table(plugin, req->node_id); - - if (NULL == config || NULL == table) { - error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - if (neu_taggrp_cfg_get_subpipes(config)->size != 0) { - error.error = NEU_ERR_GRP_CONFIG_IN_USE; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); - - VECTOR_FOR_EACH(ids, iter) - { - neu_datatag_id_t *id = (neu_datatag_id_t *) iterator_get(&iter); - neu_datatag_tbl_remove(table, *id); - } - - error.error = neu_system_del_group_config(plugin, req->node_id, req->name); - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_group_config_subscribe(neu_plugin_t * plugin, - neu_json_mqtt_t * mqtt, - neu_json_subscribe_req_t *req) -{ - log_info("Subscribe uuid:%s, config name:%s", mqtt->uuid, req->name); - neu_json_error_resp_t error = { 0 }; - neu_taggrp_config_t * config = - neu_system_find_group_config(plugin, req->src_node_id, req->name); - if (NULL != config) { - neu_plugin_send_subscribe_cmd(plugin, req->src_node_id, - req->dst_node_id, config); - error.error = NEU_ERR_SUCCESS; - } else { - error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST; - } - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_group_config_unsubscribe(neu_plugin_t * plugin, - neu_json_mqtt_t * mqtt, - neu_json_unsubscribe_req_t *req) -{ - log_info("Unsubscribe uuid:%s, config name:%s", mqtt->uuid, req->name); - neu_json_error_resp_t error = { 0 }; - neu_taggrp_config_t * config = - neu_system_find_group_config(plugin, req->src_node_id, req->name); - if (NULL != config) { - neu_plugin_send_unsubscribe_cmd(plugin, req->src_node_id, - req->dst_node_id, config); - error.error = NEU_ERR_SUCCESS; - } else { - error.error = NEU_ERR_GRP_CONFIG_NOT_EXIST; - } - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_get_group_config_subscribes(neu_plugin_t * plugin, - neu_json_mqtt_t *mqtt, - neu_json_get_group_config_req_t *req) -{ - neu_json_get_subscribe_resp_t sub_grp_configs = { 0 }; - int index = 0; - vector_t *configs = neu_system_get_sub_group_configs(plugin, req->node_id); - - sub_grp_configs.n_group = configs->size; - sub_grp_configs.groups = - calloc(configs->size, sizeof(neu_json_get_subscribe_resp_group_t)); - - VECTOR_FOR_EACH(configs, iter) - { - neu_sub_grp_config_t *sgc = - (neu_sub_grp_config_t *) iterator_get(&iter); - - sub_grp_configs.groups[index].node_id = sgc->node_id; - sub_grp_configs.groups[index].group_config_name = - sgc->group_config_name; - - index += 1; - } - - vector_free(configs); - - char *result = NULL; - neu_json_encode_with_mqtt(&sub_grp_configs, - neu_json_encode_get_subscribe_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - - free(sub_grp_configs.groups); - - return result; -} diff --git a/plugins/mqtt/command/group_config.h b/plugins/mqtt/command/group_config.h deleted file mode 100644 index e1126a63..00000000 --- a/plugins/mqtt/command/group_config.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#ifndef NEURON_PLUGIN_MQTT_COMMAND_GROUP_CONFIG -#define NEURON_PLUGIN_MQTT_COMMAND_GROUP_CONFIG - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_group_config_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_group_config_req_t *req); -char *command_group_config_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_group_config_req_t *req); -char *command_group_config_update(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_update_group_config_req_t *req); -char *command_group_config_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_group_config_req_t *req); -char *command_group_config_subscribe(neu_plugin_t * plugin, - neu_json_mqtt_t * mqtt, - neu_json_subscribe_req_t *req); -char *command_group_config_unsubscribe(neu_plugin_t * plugin, - neu_json_mqtt_t * mqtt, - neu_json_unsubscribe_req_t *req); -char *command_get_group_config_subscribes(neu_plugin_t * plugin, - neu_json_mqtt_t *mqtt, - neu_json_get_group_config_req_t *req); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/command/node.c b/plugins/mqtt/command/node.c deleted file mode 100644 index 617b10d1..00000000 --- a/plugins/mqtt/command/node.c +++ /dev/null @@ -1,161 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#include - -#include "node.h" - -char *command_node_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_nodes_req_t *req) -{ - log_info("Get node list uuid:%s, node type:%d", mqtt->uuid, req->node_type); - neu_node_type_e node_type = req->node_type; - char * result = NULL; - neu_json_error_resp_t error = { 0 }; - if (NEU_NODE_TYPE_MAX <= req->node_type) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - neu_json_get_nodes_resp_t res = { 0 }; - int index = 0; - vector_t nodes = neu_system_get_nodes(plugin, node_type); - - res.n_node = nodes.size; - res.nodes = malloc(res.n_node * sizeof(neu_json_get_nodes_resp_node_t)); - memset(res.nodes, 0, res.n_node * sizeof(neu_json_get_nodes_resp_node_t)); - - VECTOR_FOR_EACH(&nodes, iter) - { - neu_node_info_t *info = (neu_node_info_t *) iterator_get(&iter); - - res.nodes[index].id = info->node_id; - res.nodes[index].name = info->node_name; - index += 1; - } - - vector_uninit(&nodes); - - neu_json_encode_with_mqtt(&res, neu_json_encode_get_nodes_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_node_req_t *req) -{ - log_info("Add node uuid:%s, node type:%d", mqtt->uuid, req->type); - neu_json_error_resp_t error = { 0 }; - if (NEU_NODE_TYPE_MAX <= req->type || NEU_NODE_TYPE_UNKNOW >= req->type) { - error.error = NEU_ERR_NODE_TYPE_INVALID; - } else { - error.error = - neu_system_add_node(plugin, req->type, req->name, req->plugin_name); - } - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_node_req_t *req) -{ - log_info("Delete node uuid:%s, node id:%d", mqtt->uuid, req->id); - neu_json_error_resp_t error = { 0 }; - error.error = neu_system_del_node(plugin, req->id); - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_setting_set(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req, - const char * json_str) -{ - log_info("Set node setting uuid:%s, node id:%d", mqtt->uuid, req->node_id); - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - error.error = neu_plugin_set_node_setting(plugin, req->node_id, json_str); - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_setting_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req) -{ - log_info("Get node setting uuid:%s, node id:%d", mqtt->uuid, req->node_id); - neu_json_error_resp_t error = { 0 }; - char * settings = NULL; - char * result = NULL; - error.error = neu_plugin_get_node_setting(plugin, req->node_id, &settings); - if (0 != error.error) { - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - if (0 != - neu_json_encode_setting_with_mqtt(req->node_id, settings, mqtt, - &result)) { - error.error = NEU_ERR_PARAM_IS_WRONG; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - } - free(settings); - return result; -} - -char *command_node_state_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req) -{ - log_info("Get node state uuid:%s, node id:%d", mqtt->uuid, req->node_id); - neu_plugin_state_t state = { 0 }; - neu_json_get_node_state_resp_t res = { 0 }; - neu_json_error_resp_t error = { 0 }; - char * result = NULL; - error.error = neu_plugin_get_node_state(plugin, req->node_id, &state); - if (0 != error.error) { - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - res.running = state.running; - res.link = state.link; - neu_json_encode_with_mqtt(&res, neu_json_encode_get_node_state_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_node_control(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_ctl_req_t *req) -{ - log_info("Node control uuid:%s, node id:%d", mqtt->uuid, req->id); - char * result = NULL; - neu_json_error_resp_t error = { 0 }; - error.error = neu_plugin_node_ctl(plugin, req->id, req->cmd); - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} diff --git a/plugins/mqtt/command/node.h b/plugins/mqtt/command/node.h deleted file mode 100644 index c19f529e..00000000 --- a/plugins/mqtt/command/node.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#ifndef NEURON_PLUGIN_MQTT_COMMAND_NODE -#define NEURON_PLUGIN_MQTT_COMMAND_NODE - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_node_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_nodes_req_t *req); -char *command_node_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_node_req_t *req); -char *command_node_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_node_req_t *req); -char *command_node_setting_set(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req, - const char * json_str); -char *command_node_setting_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req); -char *command_node_state_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_setting_req_t *req); -char *command_node_control(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_node_ctl_req_t *req); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/command/plugin.c b/plugins/mqtt/command/plugin.c deleted file mode 100644 index f83bb556..00000000 --- a/plugins/mqtt/command/plugin.c +++ /dev/null @@ -1,91 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#include - -#include "plugin.h" - -char *command_plugin_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_plugin_req_t *req) -{ - log_info("Get plugin list uuid:%s, plugin id:%d", mqtt->uuid, req->id); - neu_json_get_plugin_resp_t res = { 0 }; - int index = 0; - vector_t plugin_libs = neu_system_get_plugin(plugin); - - res.n_plugin_lib = plugin_libs.size; - if (0 < req->id) { - res.n_plugin_lib = 1; - } - - res.plugin_libs = - calloc(res.n_plugin_lib, sizeof(neu_json_get_plugin_resp_plugin_lib_t)); - - VECTOR_FOR_EACH(&plugin_libs, iter) - { - plugin_lib_info_t *info = (plugin_lib_info_t *) iterator_get(&iter); - - if (0 < req->id && (req->id != info->plugin_id.id_val)) { - continue; - } - - res.plugin_libs[index].node_type = info->node_type; - res.plugin_libs[index].id = info->plugin_id.id_val; - res.plugin_libs[index].kind = info->plugin_kind; - res.plugin_libs[index].name = (char *) info->plugin_name; - res.plugin_libs[index].lib_name = (char *) info->plugin_lib_name; - - index += 1; - } - - vector_uninit(&plugin_libs); - - char *result = NULL; - neu_json_encode_with_mqtt(&res, neu_json_encode_get_plugin_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_plugin_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_plugin_req_t *req) -{ - log_info("Add plugin uuid:%s, node name:%s", mqtt->uuid, req->lib_name); - neu_json_error_resp_t error = { 0 }; - - error.error = neu_system_add_plugin(plugin, req->lib_name); - - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} - -char *command_plugin_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_plugin_req_t *req) -{ - log_info("Delete plugin uuid:%s, plugin id:%d", mqtt->uuid, req->id); - neu_json_error_resp_t error = { 0 }; - plugin_id_t id = { 0 }; - id.id_val = req->id; - error.error = neu_system_del_plugin(plugin, id); - char *result = NULL; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; -} \ No newline at end of file diff --git a/plugins/mqtt/command/plugin.h b/plugins/mqtt/command/plugin.h deleted file mode 100644 index a1a15b88..00000000 --- a/plugins/mqtt/command/plugin.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#ifndef NEURON_PLUGIN_MQTT_COMMAND_PLUGIN -#define NEURON_PLUGIN_MQTT_COMMAND_PLUGIN - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_plugin_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_get_plugin_req_t *req); -char *command_plugin_add(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_add_plugin_req_t *req); -char *command_plugin_delete(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, - neu_json_del_plugin_req_t *req); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/command/schema.c b/plugins/mqtt/command/schema.c deleted file mode 100644 index bc8af7b3..00000000 --- a/plugins/mqtt/command/schema.c +++ /dev/null @@ -1,69 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#include -#include -#include - -#include "schema.h" - -#define SCHEMA_PATH "./plugin_param_schema.json" - -static char *schema_inject(char *buf, neu_json_mqtt_t *mqtt) -{ - json_error_t error; - json_t * schema = json_loads(buf, 0, &error); - - if (NULL == schema) { - log_debug("json error, column:%d, line:%d, pos:%d, %s, %s", - error.column, error.line, error.position, error.source, - error.text); - return NULL; - } - - json_t *root = json_object(); - json_object_set(root, "schema", schema); - json_object_set(root, "uuid", json_string(mqtt->uuid)); - json_object_set(root, "command", json_string(mqtt->command)); - char *ret_str = json_dumps(root, 4096); - return ret_str; -} - -char *command_schema_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt) -{ - UNUSED(plugin); - - size_t len = 0; - char * buf = NULL; - buf = file_string_read(&len, SCHEMA_PATH); - if (buf == NULL) { - log_info("open %s error: %d", SCHEMA_PATH, errno); - - char * result = NULL; - neu_json_error_resp_t error = { 0 }; - error.error = NEU_ERR_FAILURE; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - return result; - } - - char *ret_str = schema_inject(buf, mqtt); - free(buf); - return ret_str; -} diff --git a/plugins/mqtt/command/schema.h b/plugins/mqtt/command/schema.h deleted file mode 100644 index 223f68ba..00000000 --- a/plugins/mqtt/command/schema.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#ifndef NEURON_PLUGIN_MQTT_COMMAND_SCHEMA -#define NEURON_PLUGIN_MQTT_COMMAND_SCHEMA - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_schema_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/command/tty.c b/plugins/mqtt/command/tty.c deleted file mode 100644 index 31d6e074..00000000 --- a/plugins/mqtt/command/tty.c +++ /dev/null @@ -1,51 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#include -#include -#include - -#include "tty.h" - -char *command_tty_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt) -{ - log_info("Get tty uuid:%s", mqtt->uuid); - - UNUSED(plugin); - neu_json_get_tty_resp_t ttys_res = { 0 }; - char * result = NULL; - ttys_res.n_tty = tty_file_list_get(&ttys_res.ttys); - if (ttys_res.n_tty == -1) { - neu_json_error_resp_t error = { 0 }; - error.error = NEU_ERR_FAILURE; - neu_json_encode_with_mqtt(&error, neu_json_encode_error_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - } else { - neu_json_encode_with_mqtt(&ttys_res, neu_json_encode_get_tty_resp, mqtt, - neu_json_encode_mqtt_resp, &result); - - for (int i = 0; i < ttys_res.n_tty; i++) { - free(ttys_res.ttys[i]); - } - - free(ttys_res.ttys); - } - - return result; -} diff --git a/plugins/mqtt/command/tty.h b/plugins/mqtt/command/tty.h deleted file mode 100644 index bca62495..00000000 --- a/plugins/mqtt/command/tty.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * NEURON IIoT System for Industry 4.0 - * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - **/ - -#ifndef NEURON_PLUGIN_MQTT_COMMAND_TTY -#define NEURON_PLUGIN_MQTT_COMMAND_TTY - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -#include "common.h" - -char *command_tty_get(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/plugins/mqtt/mqtt.c b/plugins/mqtt/mqtt.c index dddfa89d..860cd2e2 100644 --- a/plugins/mqtt/mqtt.c +++ b/plugins/mqtt/mqtt.c @@ -131,46 +131,21 @@ static void topics_cleanup(vector_t *topics) static void topics_generate(vector_t *topics, char *name) { - topics_add(topics, real_topic_generate(TOPIC_PING_REQ, name), QOS0, - real_topic_generate(TOPIC_STATUS_RES, name), QOS0, - TOPIC_TYPE_PING); - topics_add(topics, real_topic_generate(TOPIC_NODE_REQ, name), QOS0, - real_topic_generate(TOPIC_NODE_RES, name), QOS0, - TOPIC_TYPE_NODE); - topics_add(topics, real_topic_generate(TOPIC_GCONFIG_REQ, name), QOS0, - real_topic_generate(TOPIC_GCONFIG_RES, name), QOS0, - TOPIC_TYPE_GCONFIG); - topics_add(topics, real_topic_generate(TOPIC_TAGS_REQ, name), QOS0, - real_topic_generate(TOPIC_TAGS_RES, name), QOS0, - TOPIC_TYPE_TAGS); - topics_add(topics, real_topic_generate(TOPIC_PLUGIN_REQ, name), QOS0, - real_topic_generate(TOPIC_PLUGIN_RES, name), QOS0, - TOPIC_TYPE_PLUGIN); - topics_add(topics, real_topic_generate(TOPIC_SUBSCRIBE_REQ, name), QOS0, - real_topic_generate(TOPIC_SUBSCRIBE_RES, name), QOS0, - TOPIC_TYPE_SUBSCRIBE); - topics_add(topics, real_topic_generate(TOPIC_READ_REQ, name), QOS0, - real_topic_generate(TOPIC_READ_RES, name), QOS0, - TOPIC_TYPE_READ); - topics_add(topics, real_topic_generate(TOPIC_WRITE_REQ, name), QOS0, - real_topic_generate(TOPIC_WRITE_RES, name), QOS0, - TOPIC_TYPE_WRITE); - topics_add(topics, real_topic_generate(TOPIC_TTYS_REQ, name), QOS0, - real_topic_generate(TOPIC_TTYS_RES, name), QOS0, - TOPIC_TYPE_TTYS); - topics_add(topics, real_topic_generate(TOPIC_SCHEMA_REQ, name), QOS0, - real_topic_generate(TOPIC_SCHEMA_RES, name), QOS0, - TOPIC_TYPE_SCHEMA); - topics_add(topics, real_topic_generate(TOPIC_SETTING_REQ, name), QOS0, - real_topic_generate(TOPIC_SETTING_RES, name), QOS0, - TOPIC_TYPE_SETTING); - topics_add(topics, real_topic_generate(TOPIC_CTR_REQ, name), QOS0, - real_topic_generate(TOPIC_CTR_RES, name), QOS0, TOPIC_TYPE_CTR); - topics_add(topics, real_topic_generate(TOPIC_STATE_REQ, name), QOS0, - real_topic_generate(TOPIC_STATE_RES, name), QOS0, - TOPIC_TYPE_STATE); - topics_add(topics, NULL, QOS0, real_topic_generate(TOPIC_UPLOAD_RES, name), - QOS0, TOPIC_TYPE_UPLOAD); + char *ping_req = real_topic_generate(TOPIC_PING_REQ, name); + char *ping_res = real_topic_generate(TOPIC_STATUS_RES, name); + topics_add(topics, ping_req, QOS0, ping_res, QOS0, TOPIC_TYPE_PING); + + char *read_req = real_topic_generate(TOPIC_READ_REQ, name); + char *read_res = real_topic_generate(TOPIC_READ_RES, name); + topics_add(topics, read_req, QOS0, read_res, QOS0, TOPIC_TYPE_READ); + + char *write_req = real_topic_generate(TOPIC_WRITE_REQ, name); + char *write_res = real_topic_generate(TOPIC_WRITE_RES, name); + topics_add(topics, write_req, QOS0, write_res, QOS0, TOPIC_TYPE_WRITE); + + char *upload_req = NULL; + char *upload_res = real_topic_generate(TOPIC_UPLOAD_RES, name); + topics_add(topics, upload_req, QOS0, upload_res, QOS0, TOPIC_TYPE_UPLOAD); } static bool topic_match(const void *key, const void *item) diff --git a/plugins/mqtt/mqtt.h b/plugins/mqtt/mqtt.h index cfec5c98..d875d3e3 100644 --- a/plugins/mqtt/mqtt.h +++ b/plugins/mqtt/mqtt.h @@ -31,32 +31,12 @@ extern "C" { #define TIMEOUT 3000U #define TOPIC_PING_REQ "neuron/%s/ping" -#define TOPIC_NODE_REQ "neuron/%s/node/req" -#define TOPIC_GCONFIG_REQ "neuron/%s/gconfig/req" -#define TOPIC_TAGS_REQ "neuron/%s/tags/req" -#define TOPIC_PLUGIN_REQ "neuron/%s/plugin/req" -#define TOPIC_SUBSCRIBE_REQ "neuron/%s/subscribe/req" #define TOPIC_READ_REQ "neuron/%s/read/req" #define TOPIC_WRITE_REQ "neuron/%s/write/req" -#define TOPIC_TTYS_REQ "neuron/%s/ttys/req" -#define TOPIC_SCHEMA_REQ "neuron/%s/schema/plugin/req" -#define TOPIC_SETTING_REQ "neuron/%s/node/setting/req" -#define TOPIC_CTR_REQ "neuron/%s/node/ctl/req" -#define TOPIC_STATE_REQ "neuron/%s/node/state/req" #define TOPIC_STATUS_RES "neuron/%s/status" -#define TOPIC_NODE_RES "neuron/%s/node/resp" -#define TOPIC_GCONFIG_RES "neuron/%s/gconfig/resp" -#define TOPIC_TAGS_RES "neuron/%s/tags/resp" -#define TOPIC_PLUGIN_RES "neuron/%s/plugin/resp" -#define TOPIC_SUBSCRIBE_RES "neuron/%s/subscribe/resp" #define TOPIC_READ_RES "neuron/%s/read/resp" #define TOPIC_WRITE_RES "neuron/%s/write/resp" -#define TOPIC_TTYS_RES "neuron/%s/ttys/resp" -#define TOPIC_SCHEMA_RES "neuron/%s/schema/plugin/resp" -#define TOPIC_SETTING_RES "neuron/%s/node/setting/resp" -#define TOPIC_CTR_RES "neuron/%s/node/ctl/resp" -#define TOPIC_STATE_RES "neuron/%s/node/state/resp" #define TOPIC_UPLOAD_RES "neuron/%s/upload" #define QOS0 0 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c64a8bdb..0124ba76 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -115,8 +115,7 @@ target_link_libraries(http_test neuron-base gtest_main gtest yaml jansson nng) #file(COPY ${CMAKE_SOURCE_DIR}/plugin_param_schema.json DESTINATION ${CMAKE_BINARY_DIR}/tests) add_executable(mqtt_test mqtt_test.cc ${CMAKE_SOURCE_DIR}/src/config.c - ${CMAKE_SOURCE_DIR}/plugins/mqtt/util.c - ${CMAKE_SOURCE_DIR}/plugins/mqtt/command/node.c) + ${CMAKE_SOURCE_DIR}/plugins/mqtt/util.c) target_include_directories(mqtt_test PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include diff --git a/tests/mqtt_test.cc b/tests/mqtt_test.cc index e816cc81..02a214d9 100644 --- a/tests/mqtt_test.cc +++ b/tests/mqtt_test.cc @@ -1,6 +1,5 @@ #include -#include "command/node.h" #include "util.h" #include