mirror of
https://github.com/emqx/neuron.git
synced 2026-10-24 10:46:39 +08:00
Merge pull request #1307 from eeff/eeff/dev
feat(restful): add get ndriver tags api
This commit is contained in:
@@ -373,6 +373,11 @@ static int adapter_command(neu_adapter_t *adapter, neu_reqresp_head_t header,
|
||||
strcpy(header.receiver, cmd->node);
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_GET_NDRIVER_TAGS: {
|
||||
neu_req_get_ndriver_tags_t *cmd = (neu_req_get_ndriver_tags_t *) data;
|
||||
strcpy(header.receiver, cmd->ndriver);
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_UPDATE_LOG_LEVEL: {
|
||||
neu_req_update_log_level_t *cmd = (neu_req_update_log_level_t *) data;
|
||||
strcpy(header.receiver, cmd->node);
|
||||
@@ -453,6 +458,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_RESP_GET_TEMPLATE:
|
||||
case NEU_RESP_GET_TEMPLATES:
|
||||
case NEU_RESP_GET_NDRIVER_MAPS:
|
||||
case NEU_RESP_GET_NDRIVER_TAGS:
|
||||
case NEU_RESP_GET_GROUP:
|
||||
case NEU_RESP_ERROR:
|
||||
case NEU_REQRESP_TRANS_DATA:
|
||||
@@ -834,6 +840,28 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_GET_NDRIVER_TAGS: {
|
||||
neu_req_get_ndriver_tags_t *cmd =
|
||||
(neu_req_get_ndriver_tags_t *) &header[1];
|
||||
neu_resp_error_t error = { .error = 0 };
|
||||
UT_array * tags = NULL;
|
||||
|
||||
// TODO
|
||||
(void) cmd;
|
||||
utarray_new(tags, neu_ndriver_tag_get_icd());
|
||||
|
||||
neu_msg_exchange(header);
|
||||
if (error.error != NEU_ERR_SUCCESS) {
|
||||
header->type = NEU_RESP_ERROR;
|
||||
reply(adapter, header, &error);
|
||||
} else {
|
||||
neu_resp_get_tag_t resp = { .tags = tags };
|
||||
header->type = NEU_RESP_GET_NDRIVER_TAGS;
|
||||
reply(adapter, header, &resp);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_UPDATE_LOG_LEVEL: {
|
||||
neu_resp_error_t error = { 0 };
|
||||
|
||||
@@ -1398,6 +1426,12 @@ void *neu_msg_gen(neu_reqresp_head_t *header, void *data)
|
||||
case NEU_RESP_GET_NDRIVER_MAPS:
|
||||
data_size = sizeof(neu_resp_get_ndriver_maps_t);
|
||||
break;
|
||||
case NEU_REQ_GET_NDRIVER_TAGS:
|
||||
data_size = sizeof(neu_req_get_ndriver_tags_t);
|
||||
break;
|
||||
case NEU_RESP_GET_NDRIVER_TAGS:
|
||||
data_size = sizeof(neu_resp_get_ndriver_tags_t);
|
||||
break;
|
||||
case NEU_REQ_UPDATE_LOG_LEVEL:
|
||||
data_size = sizeof(neu_req_update_log_level_t);
|
||||
break;
|
||||
|
||||
@@ -721,3 +721,58 @@ int neu_tag_load_static_value(neu_datatag_t *tag, const char *s)
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static inline void ndriver_tag_cpy(void *_dst, const void *_src)
|
||||
{
|
||||
neu_ndriver_tag_t *dst = (neu_ndriver_tag_t *) _dst;
|
||||
neu_ndriver_tag_t *src = (neu_ndriver_tag_t *) _src;
|
||||
|
||||
dst->name = strdup(src->name);
|
||||
dst->address = strdup(src->address);
|
||||
dst->attribute = src->attribute;
|
||||
dst->type = src->type;
|
||||
dst->params = src->params ? strdup(src->params) : NULL;
|
||||
}
|
||||
|
||||
static inline void ndriver_tag_dtor(void *_elt)
|
||||
{
|
||||
neu_ndriver_tag_t *elt = (neu_ndriver_tag_t *) _elt;
|
||||
free(elt->name);
|
||||
free(elt->address);
|
||||
free(elt->params);
|
||||
}
|
||||
|
||||
UT_icd ndriver_tag_icd = { sizeof(neu_datatag_t), NULL, ndriver_tag_cpy,
|
||||
ndriver_tag_dtor };
|
||||
|
||||
UT_icd *neu_ndriver_tag_get_icd()
|
||||
{
|
||||
return &ndriver_tag_icd;
|
||||
}
|
||||
|
||||
neu_datatag_t *neu_ndriver_tag_dup(const neu_ndriver_tag_t *tag)
|
||||
{
|
||||
neu_datatag_t *new = calloc(1, sizeof(*new));
|
||||
ndriver_tag_cpy(new, tag);
|
||||
return new;
|
||||
}
|
||||
|
||||
void neu_ndriver_tag_copy(neu_ndriver_tag_t * tag,
|
||||
const neu_ndriver_tag_t *other)
|
||||
{
|
||||
ndriver_tag_dtor(tag);
|
||||
ndriver_tag_cpy(tag, other);
|
||||
}
|
||||
|
||||
void neu_ndriver_tag_fini(neu_ndriver_tag_t *tag)
|
||||
{
|
||||
ndriver_tag_dtor(tag);
|
||||
}
|
||||
|
||||
void neu_ndriver_tag_free(neu_ndriver_tag_t *tag)
|
||||
{
|
||||
if (tag) {
|
||||
ndriver_tag_dtor(tag);
|
||||
free(tag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -789,6 +789,7 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_WRITE_TAG:
|
||||
case NEU_REQ_GET_NODE_STATE:
|
||||
case NEU_REQ_GET_TAG:
|
||||
case NEU_REQ_GET_NDRIVER_TAGS:
|
||||
case NEU_REQ_NODE_CTL:
|
||||
case NEU_REQ_UPDATE_GROUP:
|
||||
case NEU_REQ_ADD_GROUP: {
|
||||
@@ -912,6 +913,7 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_RESP_UPDATE_TEMPLATE_TAG:
|
||||
case NEU_RESP_GET_TAG:
|
||||
case NEU_RESP_GET_TEMPLATE_TAG:
|
||||
case NEU_RESP_GET_NDRIVER_TAGS:
|
||||
case NEU_RESP_GET_GROUP:
|
||||
case NEU_RESP_GET_NODE_SETTING:
|
||||
case NEU_RESP_GET_NODE_STATE:
|
||||
|
||||
@@ -164,3 +164,95 @@ int neu_json_encode_get_ndriver_maps_resp(void *json_obj, void *param)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int neu_json_encode_ndriver_tag(void *json_obj, void *param)
|
||||
{
|
||||
int ret = 0;
|
||||
neu_json_ndriver_tag_t *tag = param;
|
||||
|
||||
neu_json_elem_t elems[] = {
|
||||
{
|
||||
.name = "name",
|
||||
.t = NEU_JSON_STR,
|
||||
.v.val_str = tag->name,
|
||||
},
|
||||
{
|
||||
.name = "address",
|
||||
.t = NEU_JSON_STR,
|
||||
.v.val_str = tag->address,
|
||||
},
|
||||
{
|
||||
.name = "attribute",
|
||||
.t = NEU_JSON_INT,
|
||||
.v.val_int = tag->attribute,
|
||||
},
|
||||
{
|
||||
.name = "type",
|
||||
.t = NEU_JSON_INT,
|
||||
.v.val_int = tag->type,
|
||||
},
|
||||
};
|
||||
|
||||
ret = neu_json_encode_field(json_obj, elems, NEU_JSON_ELEM_SIZE(elems));
|
||||
if (0 != ret) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_elem_t params_elem[] = {
|
||||
{
|
||||
.name = "params",
|
||||
.t = NEU_JSON_STR,
|
||||
.v.val_str = tag->params,
|
||||
},
|
||||
};
|
||||
ret = neu_json_encode_field(json_obj, params_elem,
|
||||
NEU_JSON_ELEM_SIZE(params_elem));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int neu_json_encode_ndriver_tag_array(void *json_obj, void *param)
|
||||
{
|
||||
neu_json_ndriver_tag_array_t *array = param;
|
||||
|
||||
if (!json_is_array((json_t *) json_obj)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < array->len; i++) {
|
||||
json_t *tag_obj = json_object();
|
||||
if (NULL == tag_obj || 0 != json_array_append_new(json_obj, tag_obj)) {
|
||||
return -1;
|
||||
}
|
||||
if (0 != neu_json_encode_ndriver_tag(tag_obj, &array->data[i])) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int neu_json_encode_get_ndriver_tags_resp(void *json_obj, void *param)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
void *tags_array = neu_json_array();
|
||||
if (NULL == tags_array) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0 != neu_json_encode_ndriver_tag_array(tags_array, param)) {
|
||||
neu_json_encode_free(tags_array);
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_elem_t resp_elems[] = { {
|
||||
.name = "tags",
|
||||
.t = NEU_JSON_OBJECT,
|
||||
.v.val_object = tags_array,
|
||||
} };
|
||||
ret = neu_json_encode_field(json_obj, resp_elems,
|
||||
NEU_JSON_ELEM_SIZE(resp_elems));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,25 @@ int neu_json_encode_ndriver_group_array(void *json_obj, void *param);
|
||||
|
||||
int neu_json_encode_get_ndriver_maps_resp(void *json_object, void *param);
|
||||
|
||||
typedef struct {
|
||||
char * name;
|
||||
char * address;
|
||||
int64_t attribute;
|
||||
int64_t type;
|
||||
char * params;
|
||||
} neu_json_ndriver_tag_t;
|
||||
|
||||
int neu_json_encode_ndriver_tag(void *json_obj, void *param);
|
||||
|
||||
typedef struct {
|
||||
int len;
|
||||
neu_json_ndriver_tag_t *data;
|
||||
} neu_json_ndriver_tag_array_t;
|
||||
|
||||
int neu_json_encode_ndriver_tag_array(void *json_obj, void *param);
|
||||
|
||||
int neu_json_encode_get_ndriver_tags_resp(void *json_obj, void *param);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
+8
-3
@@ -125,10 +125,15 @@ static char *get_param(const char *url, const char *name, size_t *len_p)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int len = strlen(name);
|
||||
char *p = strstr(query, name);
|
||||
int len = strlen(name);
|
||||
if (0 == len || NULL != strpbrk(name, "?&=#")) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *p = strstr(query, name);
|
||||
while (NULL != p) {
|
||||
if (0 == p[len] || '&' == p[len] || '=' == p[len] || '#' == p[len]) {
|
||||
if (('?' == p[-1] || '&' == p[-1]) &&
|
||||
(0 == p[len] || '&' == p[len] || '=' == p[len] || '#' == p[len])) {
|
||||
break;
|
||||
}
|
||||
p = strstr(p + len, name);
|
||||
|
||||
Reference in New Issue
Block a user