diff --git a/ft/modbus_exception_test.robot b/ft/modbus_exception_test.robot index 1796694c..1d784d8f 100644 --- a/ft/modbus_exception_test.robot +++ b/ft/modbus_exception_test.robot @@ -20,12 +20,12 @@ The connection between the modbus driver and the modbus simulator is disconnecte Should Not Be Equal As Integers ${tag1_id} -1 Sleep 1s 500ms - Write Tags ${test_node_name} ${group} {"id": ${tag1_id}, "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag1", "value": 1} Integer response body error ${ERR_PLUGIN_WRITE_FAILURE} Node Setting ${test_node_id} {"host": "127.0.0.1", "port": 9000, "connection_mode": 0} - Write Tags ${test_node_name} ${group} {"id": ${tag1_id}, "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag1", "value": 1} Integer response body error ${ERR_SUCCESS} [Teardown] Terminate All Processes kill=false diff --git a/ft/modbus_tcp_test.robot b/ft/modbus_tcp_test.robot index 6e5238c1..aea34df7 100644 --- a/ft/modbus_tcp_test.robot +++ b/ft/modbus_tcp_test.robot @@ -44,21 +44,21 @@ Read unsubscribed point, it should return failure Read/Write a point in the hold reg or coil area, and the data as bit/int16/uint16/int32/uint32/float type, it should return success [Template] Read Write A Point In The hold/coil Reg Area Should Success - ${test_node_id} ${test_node_name} ${group} ${tag_bit} coil bit 1 - ${test_node_id} ${test_node_name} ${group} ${tag_int16} hold reg int16 123 - ${test_node_id} ${test_node_name} ${group} ${tag_uint16} hold reg uint16 456 - ${test_node_id} ${test_node_name} ${group} ${tag_int32} hold reg int32 66778899 - ${test_node_id} ${test_node_name} ${group} ${tag_uint32} hold reg int32 667788995 - ${test_node_id} ${test_node_name} ${group} ${tag_float} hold reg float 11.22 + ${test_node_id} ${test_node_name} "tag_bit" ${group} ${tag_bit} coil bit 1 + ${test_node_id} ${test_node_name} "tag_int16" ${group} ${tag_int16} hold reg int16 123 + ${test_node_id} ${test_node_name} "tag_uint16" ${group} ${tag_uint16} hold reg uint16 456 + ${test_node_id} ${test_node_name} "tag_int32" ${group} ${tag_int32} hold reg int32 66778899 + ${test_node_id} ${test_node_name} "tag_uint32" ${group} ${tag_uint32} hold reg int32 667788995 + ${test_node_id} ${test_node_name} "tag_float" ${group} ${tag_float} hold reg float 11.22 Read a point in the input or input reg area, and the data as bit/int16/uint16/int32/uint32/float type, it should return success [Template] Read A Point In The Input Or Input Reg Area Should Return Success - ${test_node_id} ${test_node_name} ${group} input bit ${tag1_bit} 0 ${tag2_bit} 1 - ${test_node_id} ${test_node_name} ${group} input reg int16 ${tag1_int16} 1020 ${tag2_int16} 1021 - ${test_node_id} ${test_node_name} ${group} input reg uint16 ${tag1_uint16} 1018 ${tag2_uint16} 1019 - ${test_node_id} ${test_node_name} ${group} input reg int32 ${tag1_int32} 66126834 ${tag2_int32} 66191360 - ${test_node_id} ${test_node_name} ${group} input reg uint32 ${tag1_uint32} 66585593 ${tag2_uint32} 66650112 - ${test_node_id} ${test_node_name} ${group} input reg float ${tag1_float} 7.346839692639297e-40 ${tag2_float} 6.428596834936531e-40 + ${test_node_id} ${test_node_name} ${group} input bit ${tag1_bit} 0 ${tag2_bit} 1 + ${test_node_id} ${test_node_name} ${group} input reg int16 ${tag1_int16} 1020 ${tag2_int16} 1021 + ${test_node_id} ${test_node_name} ${group} input reg uint16 ${tag1_uint16} 1018 ${tag2_uint16} 1019 + ${test_node_id} ${test_node_name} ${group} input reg int32 ${tag1_int32} 66126834 ${tag2_int32} 66191360 + ${test_node_id} ${test_node_name} ${group} input reg uint32 ${tag1_uint32} 66585593 ${tag2_uint32} 66650112 + ${test_node_id} ${test_node_name} ${group} input reg float ${tag1_float} 7.346839692639297e-40 ${tag2_float} 6.428596834936531e-40 Read multiple points in the coil area, the point address includes continuous and non-continuous, and the data as bit type, it should return success ${tag1_id} = Add Tag And Return ID ${test_node_id} ${group} {"name": "tag1", "address": "1!000001", "attribute": ${TAG_ATTRIBUTE_RW}, "type": ${TAG_DATA_TYPE_BIT}} @@ -91,11 +91,11 @@ Read multiple points in the coil area, the point address includes continuous and Compare Tag Value As Int ${res}[tags] tag7 0 Compare Tag Value As Int ${res}[tags] tag8 0 - Write Tags ${test_node_name} ${group} {"id": ${tag2_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag3_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag4_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag5_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag7_id}, "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag2", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag3", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag4", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag5", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag7", "value": 1} Sleep 1s 500ms ${res} = Read Tags ${test_node_name} ${group} @@ -209,14 +209,14 @@ Read multiple points in the hold reg area, the point address includes continuous Compare Tag Value As Int ${res}[tags] tag7 0 Compare Tag Value As Float ${res}[tags] tag8 0.0 - Write Tags ${test_node_name} ${group} {"id": ${tag1_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag2_id}, "value": 2} - Write Tags ${test_node_name} ${group} {"id": ${tag3_id}, "value": 10} - Write Tags ${test_node_name} ${group} {"id": ${tag4_id}, "value": 12} - Write Tags ${test_node_name} ${group} {"id": ${tag5_id}, "value": 6946924} - Write Tags ${test_node_name} ${group} {"id": ${tag6_id}, "value": 7143536} - Write Tags ${test_node_name} ${group} {"id": ${tag7_id}, "value": 66651131} - Write Tags ${test_node_name} ${group} {"id": ${tag8_id}, "value": 11.234} + Write Tags ${test_node_name} ${group} {"name": "tag1", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag2", "value": 2} + Write Tags ${test_node_name} ${group} {"name": "tag3", "value": 10} + Write Tags ${test_node_name} ${group} {"name": "tag4", "value": 12} + Write Tags ${test_node_name} ${group} {"name": "tag5", "value": 6946924} + Write Tags ${test_node_name} ${group} {"name": "tag6", "value": 7143536} + Write Tags ${test_node_name} ${group} {"name": "tag7", "value": 66651131} + Write Tags ${test_node_name} ${group} {"name": "tag8", "value": 11.234} Sleep 1s 500ms ${res} = Read Tags ${test_node_name} ${group} @@ -269,12 +269,12 @@ Read multiple points belonging to different areas(coil/input/input reg/hold reg) Compare Tag Value As Int ${res}[tags] tag9 0 Compare Tag Value As Float ${res}[tags] tag10 0.0 - Write Tags ${test_node_name} ${group} {"id": ${tag1_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag2_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag3_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag8_id}, "value": 62225} - Write Tags ${test_node_name} ${group} {"id": ${tag9_id}, "value": 66651134} - Write Tags ${test_node_name} ${group} {"id": ${tag10_id}, "value": 11.123} + Write Tags ${test_node_name} ${group} {"name": "tag1", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag2", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag3", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag8", "value": 62225} + Write Tags ${test_node_name} ${group} {"name": "tag9", "value": 66651134} + Write Tags ${test_node_name} ${group} {"name": "tag10", "value": 11.123} Sleep 1s 500ms ${res} = Read Tags ${test_node_name} ${group} @@ -329,12 +329,12 @@ Read multiple points belonging to different areas(coil/input/input reg/hold reg) Compare Tag Value As Int ${res}[tags] tag9 0 Compare Tag Value As Float ${res}[tags] tag10 0.0 - Write Tags ${test_node_name} ${group} {"id": ${tag1_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag2_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag3_id}, "value": 1} - Write Tags ${test_node_name} ${group} {"id": ${tag8_id}, "value": 62226} - Write Tags ${test_node_name} ${group} {"id": ${tag9_id}, "value": 66651136} - Write Tags ${test_node_name} ${group} {"id": ${tag10_id}, "value": 11.789} + Write Tags ${test_node_name} ${group} {"name": "tag1", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag2", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag3", "value": 1} + Write Tags ${test_node_name} ${group} {"name": "tag8", "value": 62226} + Write Tags ${test_node_name} ${group} {"name": "tag9", "value": 66651136} + Write Tags ${test_node_name} ${group} {"name": "tag10", "value": 11.789} Sleep 1s 500ms ${res} = Read Tags ${test_node_name} ${group} @@ -379,7 +379,7 @@ Stop All Processes Terminate All Processes kill=false Read Write A Point In The hold/coil Reg Area Should Success - [Arguments] ${node_id} ${node_name} ${group} ${tag} ${area} ${type} ${value} + [Arguments] ${node_id} ${node_name} ${tag_name} ${group} ${tag} ${area} ${type} ${value} Log Read a point in the ${area} reg area, and the data as ${type}, it should return success Log To Console Read a point in the ${area} reg area, and the data as ${type}, it should return success @@ -398,7 +398,7 @@ Read Write A Point In The hold/coil Reg Area Should Success #Check Error Code ${res} ${ERR_SUCCESS} Run Keyword ${cmp} ${res}[tags] ${res}[tags][0][name] 0 - ${res} = Write Tags ${node_name} ${group} {"id": ${tag_id}, "value": ${value}} + ${res} = Write Tags ${node_name} ${group} {"name": ${tag_name}, "value": ${value}} Sleep 1s 500ms ${res} = Read Tags ${node_name} ${group} diff --git a/include/json/neu_json_fn.h b/include/json/neu_json_fn.h index ce7e967b..5eccda3e 100644 --- a/include/json/neu_json_fn.h +++ b/include/json/neu_json_fn.h @@ -26,6 +26,8 @@ extern "C" { #include +#include "neu_datatag_table.h" + #include "neu_data_expr.h" #include "neu_json_error.h" #include "neu_json_rw.h" @@ -41,7 +43,8 @@ int neu_json_encode_with_mqtt(void *param, neu_json_encode_fn fn, int neu_json_encode_setting_with_mqtt(uint32_t node_id, char *setting, void *mqtt_param, char **result); -neu_data_val_t *neu_parse_write_req_to_val(neu_json_write_req_t *req); +neu_data_val_t *neu_parse_write_req_to_val(neu_json_write_req_t *req, + neu_datatag_table_t *); #define NEU_JSON_RESPONSE_ERROR(err, func) \ { \ diff --git a/include/json/neu_json_rw.h b/include/json/neu_json_rw.h index 6682ad59..4ed47f79 100644 --- a/include/json/neu_json_rw.h +++ b/include/json/neu_json_rw.h @@ -53,7 +53,7 @@ typedef struct { int neu_json_encode_read_resp(void *json_object, void *param); typedef struct { - int64_t id; + char * name; enum neu_json_type t; union neu_json_value value; } neu_json_write_req_tag_t; diff --git a/include/neu_datatag_table.h b/include/neu_datatag_table.h index 4585ec2b..39a85b3e 100644 --- a/include/neu_datatag_table.h +++ b/include/neu_datatag_table.h @@ -33,6 +33,8 @@ neu_datatag_table_t *neu_datatag_tbl_create(void); void neu_datatag_tbl_destroy(neu_datatag_table_t *); size_t neu_datatag_tbl_size(neu_datatag_table_t *); neu_datatag_t * neu_datatag_tbl_get(neu_datatag_table_t *, datatag_id_t); +neu_datatag_t * neu_datatag_tbl_get_by_name(neu_datatag_table_t *, + const char *name); // We take ownership of the datatag, this implies that the datatag and the // tag name should reside in heap. diff --git a/plugins/mqtt/command/read_write.c b/plugins/mqtt/command/read_write.c index 05b3e165..bf65c8a4 100644 --- a/plugins/mqtt/command/read_write.c +++ b/plugins/mqtt/command/read_write.c @@ -329,12 +329,21 @@ int command_rw_write_request(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, return -2; } + neu_datatag_table_t *table = neu_system_get_datatags_table(plugin, node_id); + neu_int_val_t int_val; neu_data_val_t *val; for (int i = 0; i < write_req->n_tag; i++) { enum neu_json_type type = write_req->tags[i].t; union neu_json_value value = write_req->tags[i].value; - neu_datatag_id_t id = write_req->tags[i].id; + neu_datatag_t * tag = + neu_datatag_tbl_get_by_name(table, write_req->tags[i].name); + + if (tag == NULL) { + continue; + } + + neu_datatag_id_t id = tag->id; switch (type) { case NEU_JSON_INT: { diff --git a/plugins/restful/rw_handle.c b/plugins/restful/rw_handle.c index 5cb7b2c2..b8bf2192 100644 --- a/plugins/restful/rw_handle.c +++ b/plugins/restful/rw_handle.c @@ -195,7 +195,10 @@ void handle_write(nng_aio *aio) }) return; } - neu_data_val_t *data = neu_parse_write_req_to_val(req); + neu_datatag_table_t *table = + neu_system_get_datatags_table(plugin, node_id); + + neu_data_val_t *data = neu_parse_write_req_to_val(req, table); uint32_t event_id = neu_plugin_get_event_id(plugin); write_add_ctx(event_id, aio); neu_plugin_send_write_cmd(plugin, event_id, node_id, config, data); diff --git a/src/base/neu_datatag_table.c b/src/base/neu_datatag_table.c index 582deadc..fe5655da 100644 --- a/src/base/neu_datatag_table.c +++ b/src/base/neu_datatag_table.c @@ -75,6 +75,18 @@ neu_datatag_t *neu_datatag_tbl_get(neu_datatag_table_t *tag_tbl, return datatag; } +neu_datatag_t *neu_datatag_tbl_get_by_name(neu_datatag_table_t *tag_tbl, + const char * name) +{ + neu_datatag_t *datatag; + + pthread_mutex_lock(&tag_tbl->mtx); + datatag = neu_hash_table_get(&tag_tbl->tag_name_table, name); + pthread_mutex_unlock(&tag_tbl->mtx); + + return datatag; +} + int neu_datatag_tbl_update(neu_datatag_table_t *tag_tbl, datatag_id_t tag_id, neu_datatag_t *datatag) { diff --git a/src/parser/neu_json_fn.c b/src/parser/neu_json_fn.c index a842f359..6878f7aa 100644 --- a/src/parser/neu_json_fn.c +++ b/src/parser/neu_json_fn.c @@ -23,6 +23,7 @@ #include #include +#include "neu_datatag_table.h" #include "json/json.h" #include "json/neu_json_fn.h" @@ -174,7 +175,8 @@ int neu_parse_param(char *buf, char **err_param, int n, neu_json_elem_t *ele, return ret; } -neu_data_val_t *neu_parse_write_req_to_val(neu_json_write_req_t *req) +neu_data_val_t *neu_parse_write_req_to_val(neu_json_write_req_t *req, + neu_datatag_table_t * tbl) { neu_data_val_t * val = neu_dvalue_unit_new(); neu_fixed_array_t *array = @@ -183,6 +185,12 @@ neu_data_val_t *neu_parse_write_req_to_val(neu_json_write_req_t *req) for (int i = 0; i < req->n_tag; i++) { neu_int_val_t iv; neu_data_val_t *v; + neu_datatag_t * tag = + neu_datatag_tbl_get_by_name(tbl, req->tags[i].name); + + if (tag == NULL) { + continue; + } switch (req->tags[i].t) { case NEU_JSON_INT: @@ -214,7 +222,7 @@ neu_data_val_t *neu_parse_write_req_to_val(neu_json_write_req_t *req) break; } - neu_int_val_init(&iv, req->tags[i].id, v); + neu_int_val_init(&iv, tag->id, v); neu_fixed_array_set(array, i, &iv); } diff --git a/src/parser/neu_json_rw.c b/src/parser/neu_json_rw.c index 260a4f93..3f636537 100644 --- a/src/parser/neu_json_rw.c +++ b/src/parser/neu_json_rw.c @@ -109,10 +109,11 @@ int neu_json_decode_write_req(char *buf, neu_json_write_req_t **result) .t = NEU_JSON_VALUE, }, { - .name = "id", - .t = NEU_JSON_INT, + .name = "name", + .t = NEU_JSON_STR, } }; - ret = neu_json_decode_array_by_json( + + ret = neu_json_decode_array_by_json( json_obj, "tags", i, NEU_JSON_ELEM_SIZE(tag_elems), tag_elems); if (ret != 0) { goto decode_fail; @@ -137,8 +138,8 @@ int neu_json_decode_write_req(char *buf, neu_json_write_req_t **result) default: break; } - p_tag->t = tag_elems[0].t; - p_tag->id = tag_elems[1].v.val_int; + p_tag->t = tag_elems[0].t; + p_tag->name = tag_elems[1].v.val_str; p_tag++; } @@ -169,6 +170,7 @@ void neu_json_decode_write_req_free(neu_json_write_req_t *req) if (p_tag->t == NEU_JSON_STR) { free(p_tag->value.val_str); } + free(p_tag->name); p_tag++; } diff --git a/tests/json_rw_test.cc b/tests/json_rw_test.cc index b32da280..6f15a6f8 100644 --- a/tests/json_rw_test.cc +++ b/tests/json_rw_test.cc @@ -59,11 +59,12 @@ TEST(JsonAPITest, ReadResEncode) TEST(JsonAPITest, WriteReqDecode) { - char *buf = (char *) "{\"node_name\": \"name1\", \"group_config_name\": " - "\"config_opcua_sample\", \"tags\": " - "[{\"id\":1, \"value\":8877},{\"id\":2, " - "\"value\":11.22},{\"id\":3, \"value\": \"hello " - "world\"}]}"; + char *buf = + (char *) "{\"node_name\": \"name1\", \"group_config_name\": " + "\"config_opcua_sample\", \"tags\": " + "[{\"name\":\"name1\", \"value\":8877},{\"name\":\"name2\", " + "\"value\":11.22},{\"name\":\"name3\", \"value\": \"hello " + "world\"}]}"; neu_json_write_req_t *req = NULL; EXPECT_EQ(0, neu_json_decode_write_req(buf, &req)); @@ -73,15 +74,15 @@ TEST(JsonAPITest, WriteReqDecode) EXPECT_STREQ("config_opcua_sample", req->group_config_name); - EXPECT_EQ(1, req->tags[0].id); + EXPECT_STREQ("name1", req->tags[0].name); EXPECT_EQ(NEU_JSON_INT, req->tags[0].t); EXPECT_EQ(8877, req->tags[0].value.val_int); - EXPECT_EQ(2, req->tags[1].id); + EXPECT_STREQ("name2", req->tags[1].name); EXPECT_EQ(NEU_JSON_DOUBLE, req->tags[1].t); EXPECT_EQ(11.22, req->tags[1].value.val_double); - EXPECT_EQ(3, req->tags[2].id); + EXPECT_STREQ("name3", req->tags[2].name); EXPECT_EQ(NEU_JSON_STR, req->tags[2].t); EXPECT_STREQ("hello world", req->tags[2].value.val_str);