From 21ddcf8bce264ef5f43e8f867572d5feb6fc2de0 Mon Sep 17 00:00:00 2001 From: fengzero Date: Thu, 21 Oct 2021 08:53:36 +0000 Subject: [PATCH] api modify & plugin param schema get node: add plugin_id in response get plugin: add query param plugin_id --- include/neu_adapter.h | 1 + plugin_param.json | 110 +++++++++++++++++++++++++++++++ plugins/restful/adapter_handle.c | 5 +- plugins/restful/plugin_handle.c | 20 ++++-- src/adapter/adapter.c | 10 +++ src/adapter/adapter_internal.h | 1 + src/core/neu_manager.c | 4 +- src/parser/neu_json_node.c | 29 ++++---- src/parser/neu_json_node.h | 1 + tests/json_nodes_test.cc | 9 +-- 10 files changed, 167 insertions(+), 23 deletions(-) create mode 100644 plugin_param.json diff --git a/include/neu_adapter.h b/include/neu_adapter.h index 407280b2..7f9dcb06 100644 --- a/include/neu_adapter.h +++ b/include/neu_adapter.h @@ -132,6 +132,7 @@ typedef enum neu_node_type { typedef struct neu_node_info { neu_node_id_t node_id; char * node_name; + plugin_id_t plugin_id; // TODO: add node attribute } neu_node_info_t; diff --git a/plugin_param.json b/plugin_param.json new file mode 100644 index 00000000..e4c43696 --- /dev/null +++ b/plugin_param.json @@ -0,0 +1,110 @@ +{ + "plugins": [ + "mqtt-plugin", + "modbus-tcp-plugin", + "opcua-plugin" + ], + "mqtt-plugin": { + "params": [ + "host", + "port" + ], + "host": { + "description": "broker host", + "type": "string", + "default": "127.0.0.1", + "valid": { + "length": 30 + } + }, + "port": { + "description": "broker port", + "type": "int", + "default": 1883, + "valid": { + "min": 1, + "max": 65535 + } + } + }, + "opcua-plugin": { + "tag_type": [ + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15 + ], + "params": [ + "host", + "port" + ], + "host": { + "description": "opcua host", + "type": "string", + "default": "127.0.0.1", + "valid": { + "length": 30 + } + }, + "port": { + "description": "opcua port", + "type": "int", + "default": 4840, + "valid": { + "min": 1, + "max": 65535 + } + } + }, + "modbus-tcp-plugin": { + "tag_type": [ + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 14 + ], + "params": [ + "host", + "port", + "server_mode" + ], + "host": { + "description": "modbus host", + "type": "string", + "default": "127.0.0.1", + "valid": { + "length": 30 + } + }, + "port": { + "description": "modbus port", + "type": "int", + "default": 502, + "valid": { + "min": 1, + "max": 65535 + } + }, + "server_mode": { + "description": "server mode", + "type": "bool", + "default": false, + "valid": {} + } + } +} \ No newline at end of file diff --git a/plugins/restful/adapter_handle.c b/plugins/restful/adapter_handle.c index 2bbad97a..b7f08952 100644 --- a/plugins/restful/adapter_handle.c +++ b/plugins/restful/adapter_handle.c @@ -101,8 +101,9 @@ void handle_get_adapter(nng_aio *aio) { neu_node_info_t *info = (neu_node_info_t *) iterator_get(&iter); - nodes_res.nodes[index].id = info->node_id; - nodes_res.nodes[index].name = info->node_name; + nodes_res.nodes[index].id = info->node_id; + nodes_res.nodes[index].name = info->node_name; + nodes_res.nodes[index].plugin_id = info->plugin_id.id_val; index += 1; } diff --git a/plugins/restful/plugin_handle.c b/plugins/restful/plugin_handle.c index df9bb362..9a8391b5 100644 --- a/plugins/restful/plugin_handle.c +++ b/plugins/restful/plugin_handle.c @@ -83,14 +83,20 @@ void handle_update_plugin(nng_aio *aio) void handle_get_plugin(nng_aio *aio) { - neu_plugin_t * plugin = neu_rest_get_plugin(); - char * result = NULL; - neu_parse_get_plugins_res_t plugin_res = { 0 }; - int index = 0; + neu_plugin_t * plugin = neu_rest_get_plugin(); + neu_parse_get_plugins_res_t plugin_res = { 0 }; + char * s_plugin_id = http_get_param(aio, "plugin_id"); + char * result = NULL; + int index = 0; + uint32_t plugin_id = 0; + vector_t plugin_libs = neu_system_get_plugin(plugin); - vector_t plugin_libs = neu_system_get_plugin(plugin); + plugin_id = atoi(s_plugin_id); plugin_res.n_plugin = plugin_libs.size; + if (plugin_id > 0) { + plugin_res.n_plugin = 1; + } plugin_res.plugin_libs = calloc(plugin_res.n_plugin, sizeof(neu_parse_get_plugins_res_lib_t)); @@ -98,6 +104,10 @@ void handle_get_plugin(nng_aio *aio) { plugin_lib_info_t *info = (plugin_lib_info_t *) iterator_get(&iter); + if (plugin_id > 0 && plugin_id != info->plugin_id.id_val) { + continue; + } + plugin_res.plugin_libs[index].node_type = info->node_type; plugin_res.plugin_libs[index].plugin_id = info->plugin_id.id_val; plugin_res.plugin_libs[index].kind = info->plugin_kind; diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index 60d6def9..aba90328 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -1052,3 +1052,13 @@ adapter_type_e neu_adapter_get_type(neu_adapter_t *adapter) return adapter->type; } + +plugin_id_t neu_adapter_get_plugin_id(neu_adapter_t *adapter) +{ + if (adapter == NULL) { + plugin_id_t id = { 0 }; + return id; + } + + return adapter->plugin_id; +} \ No newline at end of file diff --git a/src/adapter/adapter_internal.h b/src/adapter/adapter_internal.h index b54c7f56..67d5e068 100644 --- a/src/adapter/adapter_internal.h +++ b/src/adapter/adapter_internal.h @@ -37,5 +37,6 @@ neu_manager_t *neu_adapter_get_manager(neu_adapter_t *adapter); nng_socket neu_adapter_get_sock(neu_adapter_t *adapter); adapter_id_t neu_adapter_get_id(neu_adapter_t *adapter); adapter_type_e neu_adapter_get_type(neu_adapter_t *adapter); +plugin_id_t neu_adapter_get_plugin_id(neu_adapter_t *adapter); #endif diff --git a/src/core/neu_manager.c b/src/core/neu_manager.c index 923a4c3f..b757b970 100644 --- a/src/core/neu_manager.c +++ b/src/core/neu_manager.c @@ -108,7 +108,7 @@ static const char *const manager_url = "inproc://neu_manager"; #endif #define WEBSERVER_PLUGIN_NAME "webserver-plugin-proxy" #define MQTT_PLUGIN_NAME "mqtt-plugin" -#define MODBUS_TCP_PLUGIN_NAME "modbus-plugin-tcp" +#define MODBUS_TCP_PLUGIN_NAME "modbus-tcp-plugin" #define OPCUA_PLUGIN_NAME "opcua-plugin" // definition for adapter names @@ -1538,6 +1538,8 @@ int neu_manager_get_nodes(neu_manager_t *manager, neu_node_type_e node_type, node_info.node_id = neu_manager_adapter_id_to_node_id(manager, adapter_id); node_info.node_name = strdup(adapter_name); + node_info.plugin_id = + neu_adapter_get_plugin_id(reg_entity->adapter); vector_push_back(result_nodes, &node_info); } } diff --git a/src/parser/neu_json_node.c b/src/parser/neu_json_node.c index 4b8cfe94..ecc30d31 100644 --- a/src/parser/neu_json_node.c +++ b/src/parser/neu_json_node.c @@ -157,17 +157,24 @@ int neu_parse_encode_get_nodes(void *json_object, void *param) void * array = NULL; for (int i = 0; i < res->n_node; i++) { - neu_json_elem_t nodes[] = { { - .name = "name", - .t = NEU_JSON_STR, - .v.val_str = res->nodes[i].name, - }, - { - .name = "id", - .t = NEU_JSON_INT, - .v.val_int = res->nodes[i].id, - } }; - array = neu_json_encode_array(array, nodes, 2); + neu_json_elem_t nodes[] = { + { + .name = "name", + .t = NEU_JSON_STR, + .v.val_str = res->nodes[i].name, + }, + { + .name = "id", + .t = NEU_JSON_INT, + .v.val_int = res->nodes[i].id, + }, + { + .name = "plugin_id", + .t = NEU_JSON_INT, + .v.val_int = res->nodes[i].plugin_id, + }, + }; + array = neu_json_encode_array(array, nodes, NEU_JSON_ELEM_SIZE(nodes)); } neu_json_elem_t elems[] = { { .name = "nodes", diff --git a/src/parser/neu_json_node.h b/src/parser/neu_json_node.h index 287320ff..e30fe63a 100644 --- a/src/parser/neu_json_node.h +++ b/src/parser/neu_json_node.h @@ -44,6 +44,7 @@ typedef struct { typedef struct { uint32_t id; char * name; + uint32_t plugin_id; } neu_parse_get_nodes_res_node_t; typedef struct { diff --git a/tests/json_nodes_test.cc b/tests/json_nodes_test.cc index ff12bd6a..4f50305e 100644 --- a/tests/json_nodes_test.cc +++ b/tests/json_nodes_test.cc @@ -35,10 +35,11 @@ TEST(JsonNodesTest, GetNodesDecode) TEST(JsonNodesTest, GetNodesEncode) { - char *buf = (char *) "{\"nodes\": " - "[{\"name\": \"node1\", \"id\": 123}, " - "{\"name\": \"node2\", \"id\": 456}, " - "{\"name\": \"node3\", \"id\": 789}]}"; + char *buf = (char *) "{\"nodes\": [{\"name\": \"node1\", \"id\": 123, " + "\"plugin_id\": 0}, " + "{\"name\": \"node2\", \"id\": 456, \"plugin_id\": " + "0}, {\"name\": " + "\"node3\", \"id\": 789, \"plugin_id\": 0}]}"; char * result = NULL; neu_parse_get_nodes_res_t res = { .n_node = 3,