mirror of
https://github.com/emqx/neuron.git
synced 2026-10-22 09:23:52 +08:00
Merge pull request #328 from lixiumei123/main
Adding tag_id persistence
This commit is contained in:
@@ -15,6 +15,11 @@ ${mqtt-1} mqtt-1
|
||||
${tag1} {"name": "tag1", "address": "1!400001", "attribute": 1, "type": 4}
|
||||
${tag2} {"name": "tag2", "address": "1!00001", "attribute": 3, "type": 14}
|
||||
${tag3} {"name": "tag3", "address": "2!400003", "attribute": 1, "type": 5}
|
||||
${tag4} {"name": "tag4", "address": "2!400005", "attribute": 1, "type": 4}
|
||||
${tag5} {"name": "tag5", "address": "2!400006", "attribute": 1, "type": 4}
|
||||
${tag6} {"name": "tag6", "address": "2!400007", "attribute": 1, "type": 4}
|
||||
${tag7} {"name": "tag7", "address": "2!400008", "attribute": 1, "type": 4}
|
||||
${tag8} {"name": "tag8", "address": "2!400009", "attribute": 1, "type": 4}
|
||||
|
||||
*** Test Cases ***
|
||||
Get the newly added adapters after stopping and restarting Neuron, it should return success
|
||||
@@ -215,6 +220,48 @@ Get the updated group_configs after stopping and restarting Neuron, it should re
|
||||
${ret} = Group Config Check ${res}[group_configs] ${test_gconfig2} 1500
|
||||
Should Be Equal As Integers ${ret} 0
|
||||
|
||||
[Teardown] Neuron Stop Running ${process}
|
||||
|
||||
Get the newly added tag_id duplicated with the tag_id in the loaded persistence file, it should return a failure
|
||||
Skip If Not Http API
|
||||
|
||||
${process} = Neuron Start Running
|
||||
|
||||
${node_id} = Get Node ID ${NODE_DRIVER} modbus-tcp-1
|
||||
Should Not Be Equal As Integers ${node_id} 0
|
||||
|
||||
${res} = Add Tags ${node_id} ${test_gconfig1} ${tag4},${tag5},${tag6}
|
||||
Check Response Status ${res} 200
|
||||
|
||||
${res} = Get Tags ${node_id} ${test_gconfig1}
|
||||
Check Response Status ${res} 200
|
||||
${tag4_id} = Get Tag ID ${node_id} ${test_gconfig1} tag4
|
||||
Should Be Equal As Integers ${tag4_id} 1
|
||||
${tag5_id} = Get Tag ID ${node_id} ${test_gconfig1} tag5
|
||||
Should Be Equal As Integers ${tag5_id} 2
|
||||
${tag6_id} = Get Tag ID ${node_id} ${test_gconfig1} tag6
|
||||
Should Be Equal As Integers ${tag6_id} 3
|
||||
|
||||
${res} = Del Tags ${node_id} ${test_gconfig1} ${tag4_id}
|
||||
|
||||
Neuron Stop Running ${process}
|
||||
${process} = Neuron Start Running
|
||||
|
||||
${res} = Add Tags ${node_id} ${test_gconfig1} ${tag7},${tag8}
|
||||
Check Response Status ${res} 200
|
||||
|
||||
${res} = Get Tags ${node_id} ${test_gconfig1}
|
||||
Check Response Status ${res} 200
|
||||
${tag5_id} = Get Tag ID ${node_id} ${test_gconfig1} tag5
|
||||
Should Be Equal As Integers ${tag5_id} 2
|
||||
${tag6_id} = Get Tag ID ${node_id} ${test_gconfig1} tag6
|
||||
Should Be Equal As Integers ${tag6_id} 3
|
||||
${tag7_id} = Get Tag ID ${node_id} ${test_gconfig1} tag7
|
||||
Should Be Equal As Integers ${tag7_id} 1
|
||||
${tag8_id} = Get Tag ID ${node_id} ${test_gconfig1} tag8
|
||||
Should Be Equal As Integers ${tag8_id} 4
|
||||
|
||||
|
||||
[Teardown] Neuron Stop Running ${process}
|
||||
|
||||
Get the newly add tags after stopping and restarting Neuron, it should return success
|
||||
|
||||
@@ -80,6 +80,10 @@ bool neu_tag_check_attribute(neu_attribute_e attribute);
|
||||
// The id is set to zero, and we make a copy of the name.
|
||||
neu_datatag_t *neu_datatag_alloc(neu_attribute_e attr, neu_dtype_e type,
|
||||
neu_addr_str_t addr, const char *name);
|
||||
// The id is set to the value passed in, and we make a copy of the name.
|
||||
neu_datatag_t *neu_datatag_alloc_with_id(neu_attribute_e attr, neu_dtype_e type,
|
||||
neu_addr_str_t addr, const char *name,
|
||||
neu_datatag_id_t id);
|
||||
void neu_datatag_free(neu_datatag_t *datatag);
|
||||
|
||||
int neu_datatag_parse_addr_option(neu_datatag_t * datatag,
|
||||
|
||||
@@ -289,9 +289,9 @@ static int persister_singleton_load_datatags(neu_adapter_t * adapter,
|
||||
vector_t *ids = neu_taggrp_cfg_get_datatag_ids(grp_config);
|
||||
VECTOR_FOR_EACH(datatag_infos, iter)
|
||||
{
|
||||
neu_persist_datatag_info_t *p = iterator_get(&iter);
|
||||
neu_datatag_t * tag =
|
||||
neu_datatag_alloc(p->attribute, p->type, p->address, p->name);
|
||||
neu_persist_datatag_info_t *p = iterator_get(&iter);
|
||||
neu_datatag_t * tag = neu_datatag_alloc_with_id(
|
||||
p->attribute, p->type, p->address, p->name, p->id);
|
||||
if (NULL == tag) {
|
||||
rv = NEU_ERR_ENOMEM;
|
||||
break;
|
||||
|
||||
@@ -121,8 +121,21 @@ datatag_id_t neu_datatag_tbl_add(neu_datatag_table_t *tag_tbl,
|
||||
datatag_id_t id = 0;
|
||||
|
||||
pthread_mutex_lock(&tag_tbl->mtx);
|
||||
if (NULL == neu_hash_table_get(&tag_tbl->tag_name_table, datatag->name) &&
|
||||
neu_id_alloc(&tag_tbl->datatag_table, &datatag->id, datatag) == 0) {
|
||||
|
||||
if (NULL == neu_hash_table_get(&tag_tbl->tag_name_table, datatag->name)) {
|
||||
if (datatag->id > 0) {
|
||||
if (neu_id_set(&tag_tbl->datatag_table, datatag->id, datatag) !=
|
||||
0) {
|
||||
log_error("Failed to set tag_id");
|
||||
pthread_mutex_unlock(&tag_tbl->mtx);
|
||||
return id;
|
||||
}
|
||||
} else if (neu_id_alloc(&tag_tbl->datatag_table, &datatag->id,
|
||||
datatag) != 0) {
|
||||
log_error("Failed to alloc tag_id");
|
||||
pthread_mutex_unlock(&tag_tbl->mtx);
|
||||
return id;
|
||||
}
|
||||
id = datatag->id;
|
||||
neu_hash_table_set(&tag_tbl->tag_name_table, datatag->name, datatag);
|
||||
}
|
||||
|
||||
@@ -62,6 +62,39 @@ error:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
neu_datatag_t *neu_datatag_alloc_with_id(neu_attribute_e attr, neu_dtype_e type,
|
||||
neu_addr_str_t addr, const char *name,
|
||||
neu_datatag_id_t id)
|
||||
{
|
||||
neu_datatag_t *tag = malloc(sizeof(neu_datatag_t));
|
||||
|
||||
if (NULL == tag) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tag->id = id;
|
||||
tag->attribute = attr;
|
||||
tag->type = type;
|
||||
|
||||
tag->addr_str = strdup(addr);
|
||||
if (NULL == tag->addr_str) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
tag->name = strdup(name);
|
||||
if (NULL == tag->name) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return tag;
|
||||
|
||||
error:
|
||||
free(tag->addr_str);
|
||||
free(tag->name);
|
||||
free(tag);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void neu_datatag_free(neu_datatag_t *datatag)
|
||||
{
|
||||
if (datatag) {
|
||||
|
||||
@@ -1839,6 +1839,7 @@ int neu_manager_get_persist_datatag_infos(neu_manager_t *manager,
|
||||
.attribute = tag->attribute,
|
||||
.name = strdup(tag->name),
|
||||
.address = strdup(tag->addr_str),
|
||||
.id = tag->id,
|
||||
|
||||
};
|
||||
if (0 != vector_push_back(datatag_infos, &datatag_info)) {
|
||||
|
||||
@@ -59,6 +59,10 @@ int neu_json_decode_datatag_req(char *buf, neu_json_datatag_req_t **result)
|
||||
{
|
||||
.name = "address",
|
||||
.t = NEU_JSON_STR,
|
||||
},
|
||||
{
|
||||
.name = "id",
|
||||
.t = NEU_JSON_INT,
|
||||
} };
|
||||
ret = neu_json_decode_array_by_json(
|
||||
json_obj, "tags", i, NEU_JSON_ELEM_SIZE(tag_elems), tag_elems);
|
||||
@@ -70,6 +74,7 @@ int neu_json_decode_datatag_req(char *buf, neu_json_datatag_req_t **result)
|
||||
p_tag->name = tag_elems[1].v.val_str;
|
||||
p_tag->attribute = tag_elems[2].v.val_int;
|
||||
p_tag->address = tag_elems[3].v.val_str;
|
||||
p_tag->id = tag_elems[4].v.val_int;
|
||||
p_tag++;
|
||||
}
|
||||
|
||||
@@ -134,6 +139,11 @@ int neu_json_encode_datatag_resp(void *json_object, void *param)
|
||||
.name = "address",
|
||||
.t = NEU_JSON_STR,
|
||||
.v.val_str = p_tag->address,
|
||||
},
|
||||
{
|
||||
.name = "id",
|
||||
.t = NEU_JSON_INT,
|
||||
.v.val_int = p_tag->id,
|
||||
} };
|
||||
tag_array = neu_json_encode_array(tag_array, tag_elems,
|
||||
NEU_JSON_ELEM_SIZE(tag_elems));
|
||||
|
||||
@@ -32,6 +32,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int64_t id;
|
||||
char * address;
|
||||
char * name;
|
||||
int64_t type;
|
||||
@@ -47,6 +48,7 @@ int neu_json_decode_datatag_req(char *buf, neu_json_datatag_req_t **result);
|
||||
void neu_json_decode_datatag_req_free(neu_json_datatag_req_t *req);
|
||||
|
||||
typedef struct {
|
||||
int64_t id;
|
||||
char * address;
|
||||
char * name;
|
||||
int64_t type;
|
||||
|
||||
@@ -65,10 +65,11 @@ TEST(JsonPlugin, PluginPersistenceDecode)
|
||||
TEST(JsonDatatags, DatatagPersistenceDecode)
|
||||
{
|
||||
char *buf =
|
||||
(char *) "{\"tags\": [{\"attribute\": 1, \"type\": 3, "
|
||||
(char *) "{\"tags\": [{\"id\": 1, \"attribute\": 1, \"type\": 3, "
|
||||
"\"address\": \"1!400001\", \"name\": \"modbus1\"}, "
|
||||
"{\"attribute\": 1, \"type\": 4, \"address\": \"1!400002\", "
|
||||
"\"name\": \"modbus2\"}, {\"attribute\": 1, "
|
||||
"{\"id\": 2, \"attribute\": 1, \"type\": 4, \"address\": "
|
||||
"\"1!400002\", "
|
||||
"\"name\": \"modbus2\"}, {\"id\": 3, \"attribute\": 1, "
|
||||
"\"type\": 4, \"address\": \"1!400003\", \"name\": "
|
||||
"\"modbus3\"}]}";
|
||||
|
||||
@@ -76,16 +77,19 @@ TEST(JsonDatatags, DatatagPersistenceDecode)
|
||||
|
||||
EXPECT_EQ(0, neu_json_decode_datatag_req(buf, &req));
|
||||
|
||||
EXPECT_EQ(1, req->tags[0].id);
|
||||
EXPECT_EQ(1, req->tags[0].attribute);
|
||||
EXPECT_EQ(3, req->tags[0].type);
|
||||
EXPECT_STREQ("1!400001", req->tags[0].address);
|
||||
EXPECT_STREQ("modbus1", req->tags[0].name);
|
||||
|
||||
EXPECT_EQ(2, req->tags[1].id);
|
||||
EXPECT_EQ(1, req->tags[1].attribute);
|
||||
EXPECT_EQ(4, req->tags[1].type);
|
||||
EXPECT_STREQ("1!400002", req->tags[1].address);
|
||||
EXPECT_STREQ("modbus2", req->tags[1].name);
|
||||
|
||||
EXPECT_EQ(3, req->tags[2].id);
|
||||
EXPECT_EQ(1, req->tags[2].attribute);
|
||||
EXPECT_EQ(4, req->tags[2].type);
|
||||
EXPECT_STREQ("1!400003", req->tags[2].address);
|
||||
@@ -321,10 +325,12 @@ TEST(JsonDatatags, DatatagPersistenceEncode)
|
||||
{
|
||||
char *buf =
|
||||
(char *) "{\"tags\": [{\"type\": 3, \"name\": \"modbus1\", "
|
||||
"\"attribute\": 1, \"address\": \"1!400001\"}, {\"type\": 4, "
|
||||
"\"attribute\": 1, \"address\": \"1!400001\", \"id\": 1}, {"
|
||||
"\"type\": 4, "
|
||||
"\"name\": \"modbus2\", \"attribute\": 1, \"address\": "
|
||||
"\"1!400002\"}, {\"type\": 4, \"name\": \"modbus3\", "
|
||||
"\"attribute\": 1, \"address\": \"1!400003\"}]}";
|
||||
"\"1!400002\", \"id\": 2}, {\"type\": 4, \"name\": "
|
||||
"\"modbus3\", "
|
||||
"\"attribute\": 1, \"address\": \"1!400003\", \"id\": 3}]}";
|
||||
char *buf2 = (char *) "{\"tags\": []}";
|
||||
|
||||
char *result = NULL;
|
||||
@@ -343,16 +349,19 @@ TEST(JsonDatatags, DatatagPersistenceEncode)
|
||||
resp.tags[0].type = 3;
|
||||
resp.tags[0].address = strdup("1!400001");
|
||||
resp.tags[0].name = strdup("modbus1");
|
||||
resp.tags[0].id = 1;
|
||||
|
||||
resp.tags[1].attribute = 1;
|
||||
resp.tags[1].type = 4;
|
||||
resp.tags[1].address = strdup("1!400002");
|
||||
resp.tags[1].name = strdup("modbus2");
|
||||
resp.tags[1].id = 2;
|
||||
|
||||
resp.tags[2].attribute = 1;
|
||||
resp.tags[2].type = 4;
|
||||
resp.tags[2].address = strdup("1!400003");
|
||||
resp.tags[2].name = strdup("modbus3");
|
||||
resp.tags[2].id = 3;
|
||||
|
||||
EXPECT_EQ(
|
||||
0, neu_json_encode_by_fn(&resp, neu_json_encode_datatag_resp, &result));
|
||||
|
||||
@@ -141,12 +141,12 @@ TEST(PersistenceDatatagTest, Datatag)
|
||||
const char *grp_config_name = "test-group";
|
||||
const char *plugins_fname = neu_persister_get_plugins_fname(persister);
|
||||
|
||||
neu_json_datatag_req_tag_t tag1 = { (char *) "1!400001", (char *) "modbus1",
|
||||
3, 1 };
|
||||
neu_json_datatag_req_tag_t tag2 = { (char *) "1!400002", (char *) "modbus2",
|
||||
4, 1 };
|
||||
neu_json_datatag_req_tag_t tag3 = { (char *) "1!400003", (char *) "modbus3",
|
||||
5, 1 };
|
||||
neu_json_datatag_req_tag_t tag1 = { 1, (char *) "1!400001",
|
||||
(char *) "modbus1", 3, 1 };
|
||||
neu_json_datatag_req_tag_t tag2 = { 2, (char *) "1!400002",
|
||||
(char *) "modbus2", 4, 1 };
|
||||
neu_json_datatag_req_tag_t tag3 = { 3, (char *) "1!400003",
|
||||
(char *) "modbus3", 5, 1 };
|
||||
|
||||
neu_json_datatag_req_t *req =
|
||||
(neu_json_datatag_req_t *) malloc(sizeof(neu_json_datatag_req_t));
|
||||
@@ -178,14 +178,17 @@ TEST(PersistenceDatatagTest, Datatag)
|
||||
EXPECT_STREQ("1!400001", info->address);
|
||||
EXPECT_EQ(3, info->type);
|
||||
EXPECT_EQ(1, info->attribute);
|
||||
EXPECT_EQ(1, info->id);
|
||||
} else if (strcmp(info->name, "modbus2") == 0) {
|
||||
EXPECT_STREQ("1!400002", info->address);
|
||||
EXPECT_EQ(4, info->type);
|
||||
EXPECT_EQ(1, info->attribute);
|
||||
EXPECT_EQ(2, info->id);
|
||||
} else if (strcmp(info->name, "modbus3") == 0) {
|
||||
EXPECT_STREQ("1!400003", info->address);
|
||||
EXPECT_EQ(5, info->type);
|
||||
EXPECT_EQ(1, info->attribute);
|
||||
EXPECT_EQ(3, info->id);
|
||||
} else {
|
||||
EXPECT_EQ(1, 2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user