mirror of
https://github.com/emqx/neuron.git
synced 2026-10-29 23:19:18 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8189c4ff8 | ||
|
|
b8f06d14a0 | ||
|
|
088d5ccdbb | ||
|
|
df2c33d71c | ||
|
|
b2abc42675 | ||
|
|
6779877965 | ||
|
|
2e05ab4bba | ||
|
|
6793c0a1f0 | ||
|
|
8bd802c492 | ||
|
|
d2502d0482 | ||
|
|
a058f8deb8 | ||
|
|
54c7a73878 | ||
|
|
250d19af8d | ||
|
|
515d31c4e8 | ||
|
|
264796888c | ||
|
|
ad3e7b929d | ||
|
|
1ed8d3775c | ||
|
|
6e7883c1bc | ||
|
|
f8f4da41a1 | ||
|
|
6fd41502fe | ||
|
|
d5df0b6dc6 | ||
|
|
58eb2e6864 | ||
|
|
2915e9790c | ||
|
|
b4482f19be | ||
|
|
356ba04f12 | ||
|
|
0f75d43b5c | ||
|
|
0dd8fc3262 | ||
|
|
2180913545 | ||
|
|
96ec0c5a81 | ||
|
|
5c6140ec35 | ||
|
|
a867dd6266 | ||
|
|
7593060a3e | ||
|
|
a7946c4f17 | ||
|
|
224f748ddf | ||
|
|
734bf74367 | ||
|
|
315f44baf1 | ||
|
|
53999c225f | ||
|
|
3381f531d9 | ||
|
|
b6921c6d1d | ||
|
|
d3094dd316 | ||
|
|
14a3dbd03b | ||
|
|
22b5802c08 | ||
|
|
13599083d0 | ||
|
|
27529b5a91 | ||
|
|
58775e1761 | ||
|
|
fa21137553 | ||
|
|
764bd59b0d | ||
|
|
0baedd7efd | ||
|
|
1357d11b70 | ||
|
|
0b9c4db032 | ||
|
|
768eed6426 | ||
|
|
6ef055c5e2 | ||
|
|
253cdb9e91 | ||
|
|
4d6a3a2c69 | ||
|
|
6cef73d435 | ||
|
|
27766d7ec2 | ||
|
|
e8d5ee7866 | ||
|
|
3d38558a0b | ||
|
|
c711378067 | ||
|
|
65129936bc | ||
|
|
a28a0e0df5 | ||
|
|
0094e29415 | ||
|
|
2668fbfa94 | ||
|
|
bafd1b9d0d | ||
|
|
9db6a96f02 | ||
|
|
df1aa78d4f | ||
|
|
5208bb08c3 | ||
|
|
80aca7abf3 | ||
|
|
c0824c4bf1 | ||
|
|
87718d57f1 | ||
|
|
f0d16c369c | ||
|
|
9c2d9215ed | ||
|
|
f8e6d30442 | ||
|
|
566a794e3e | ||
|
|
66bad84a57 | ||
|
|
b69499ba65 | ||
|
|
462b7e08a5 | ||
|
|
59f1252ebb | ||
|
|
d01eeb954d | ||
|
|
ce4b0afcd0 | ||
|
|
315d40b7ad | ||
|
|
82986bdfd6 | ||
|
|
3c0f4ac355 | ||
|
|
667435ca10 | ||
|
|
c76a420ddb |
@@ -8,7 +8,7 @@ on:
|
||||
- published
|
||||
|
||||
env:
|
||||
VERSION: 2.8.0
|
||||
VERSION: 2.8.3
|
||||
UI_VERSION: 2.6.1
|
||||
BRANCH: v2.8
|
||||
|
||||
|
||||
+6
-1
@@ -102,6 +102,11 @@ if (CLIB)
|
||||
add_definitions(-DNEU_CLIB=${CLIB})
|
||||
endif()
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
message(STATUS "build release")
|
||||
add_definitions(-DNEU_RELEASE)
|
||||
endif()
|
||||
|
||||
|
||||
add_library(neuron-base SHARED)
|
||||
target_sources(neuron-base PRIVATE ${NEURON_BASE_SOURCES} ${NEURON_SRC_PARSE})
|
||||
@@ -109,7 +114,7 @@ target_include_directories(neuron-base
|
||||
PRIVATE include/neuron src)
|
||||
target_link_libraries(neuron-base libssl.a libcrypto.a)
|
||||
target_link_libraries(neuron-base nng libzlog.so jansson jwt
|
||||
${CMAKE_THREAD_LIBS_INIT})
|
||||
${CMAKE_THREAD_LIBS_INIT} -lm)
|
||||
add_dependencies(neuron-base neuron-version)
|
||||
|
||||
# dependency imposed by nng
|
||||
|
||||
@@ -53,6 +53,20 @@ typedef enum {
|
||||
|
||||
typedef struct neu_mqtt_client_s neu_mqtt_client_t;
|
||||
|
||||
/**
|
||||
* Check that `topic_filter` is a valid MQTT topic filter.
|
||||
*/
|
||||
bool neu_mqtt_topic_filter_is_valid(const char *topic_filter);
|
||||
|
||||
/**
|
||||
* Check the given `topic_filter` matches the given `topic_name`.
|
||||
*
|
||||
* @Precondition `topic_filter` must be a valid MQTT topic filter.
|
||||
* @Precondition `topic_name` must be a valid MQTT topic name.
|
||||
*/
|
||||
bool neu_mqtt_topic_filter_is_match(const char *topic_filter,
|
||||
const char *topic_name);
|
||||
|
||||
typedef void (*neu_mqtt_client_connection_cb_t)(void *data);
|
||||
typedef void (*neu_mqtt_client_publish_cb_t)(int errcode, neu_mqtt_qos_e qos,
|
||||
char *topic, uint8_t *payload,
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
#define NEU_VERSION_MAJOR 2
|
||||
#define NEU_VERSION_MINOR 8
|
||||
#define NEU_VERSION_FIX 0
|
||||
#define NEU_VERSION_FIX 3
|
||||
|
||||
#define NEU_GET_VERSION_MAJOR(V) ((0xffff0000 & V) >> 16)
|
||||
#define NEU_GET_VERSION_MINOR(V) ((0x0000ff00 & V) >> 8)
|
||||
|
||||
@@ -82,7 +82,6 @@ typedef enum {
|
||||
NEU_ERR_TAG_DESCRIPTION_TOO_LONG = 2208,
|
||||
NEU_ERR_TAG_PRECISION_INVALID = 2209,
|
||||
NEU_ERR_TAG_EXIST = 2210,
|
||||
NEU_ERR_TAG_VALUE_INVALID = 2211,
|
||||
|
||||
NEU_ERR_LIBRARY_NOT_FOUND = 2301,
|
||||
NEU_ERR_LIBRARY_INFO_INVALID = 2302,
|
||||
|
||||
@@ -62,6 +62,30 @@ typedef struct {
|
||||
|
||||
int neu_json_encode_read_resp(void *json_object, void *param);
|
||||
int neu_json_encode_read_resp1(void *json_object, void *param);
|
||||
int neu_json_encode_read_paginate_resp(void *json_object, void *param);
|
||||
|
||||
typedef struct {
|
||||
int64_t error;
|
||||
char * name;
|
||||
enum neu_json_type t;
|
||||
union neu_json_value value;
|
||||
uint8_t precision;
|
||||
int n_meta;
|
||||
neu_json_tag_meta_t *metas;
|
||||
neu_datatag_t datatag;
|
||||
} neu_json_read_paginate_resp_tag_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned int current_page;
|
||||
unsigned int page_size;
|
||||
unsigned int total;
|
||||
} neu_json_read_meta_resp_t;
|
||||
|
||||
typedef struct {
|
||||
neu_json_read_meta_resp_t meta;
|
||||
int n_tag;
|
||||
neu_json_read_paginate_resp_tag_t *tags;
|
||||
} neu_json_read_paginate_resp_t;
|
||||
|
||||
typedef struct {
|
||||
char * group;
|
||||
@@ -134,10 +158,28 @@ typedef struct {
|
||||
int neu_json_decode_read_req(char *buf, neu_json_read_req_t **result);
|
||||
void neu_json_decode_read_req_free(neu_json_read_req_t *req);
|
||||
|
||||
typedef struct {
|
||||
char *group;
|
||||
char *node;
|
||||
char *name;
|
||||
char *desc;
|
||||
bool sync;
|
||||
int current_page;
|
||||
int page_size;
|
||||
bool is_error;
|
||||
} neu_json_read_paginate_req_t;
|
||||
|
||||
int neu_json_decode_read_paginate_req(char * buf,
|
||||
neu_json_read_paginate_req_t **result);
|
||||
void neu_json_decode_read_paginate_req_free(neu_json_read_paginate_req_t *req);
|
||||
|
||||
int neu_json_encode_read_periodic_resp(void *json_object, void *param);
|
||||
|
||||
void neu_json_metas_to_json(neu_tag_meta_t *metas, int n_meta,
|
||||
neu_json_read_resp_tag_t *json_tag);
|
||||
void neu_json_metas_to_json_paginate(
|
||||
neu_tag_meta_t *metas, int n_meta,
|
||||
neu_json_read_paginate_resp_tag_t *json_tag);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
+41
-14
@@ -41,16 +41,22 @@ typedef enum {
|
||||
NEU_METRIC_TYPE_GAUAGE,
|
||||
NEU_METRIC_TYPE_COUNTER_SET,
|
||||
NEU_METRIC_TYPE_ROLLING_COUNTER,
|
||||
|
||||
NEU_METRIC_TYPE_FLAG_NO_RESET = 0x80,
|
||||
} neu_metric_type_e;
|
||||
|
||||
#define NEU_METRIC_TYPE_MASK 0x0F
|
||||
|
||||
// node running state
|
||||
#define NEU_METRIC_RUNNING_STATE "running_state"
|
||||
#define NEU_METRIC_RUNNING_STATE_TYPE NEU_METRIC_TYPE_GAUAGE
|
||||
#define NEU_METRIC_RUNNING_STATE_TYPE \
|
||||
(NEU_METRIC_TYPE_GAUAGE | NEU_METRIC_TYPE_FLAG_NO_RESET)
|
||||
#define NEU_METRIC_RUNNING_STATE_HELP "Node running state"
|
||||
|
||||
// node link state
|
||||
#define NEU_METRIC_LINK_STATE "link_state"
|
||||
#define NEU_METRIC_LINK_STATE_TYPE NEU_METRIC_TYPE_GAUAGE
|
||||
#define NEU_METRIC_LINK_STATE_TYPE \
|
||||
(NEU_METRIC_TYPE_GAUAGE | NEU_METRIC_TYPE_FLAG_NO_RESET)
|
||||
#define NEU_METRIC_LINK_STATE_HELP "Node link state"
|
||||
|
||||
// last round trip time in millisends
|
||||
@@ -86,13 +92,15 @@ typedef enum {
|
||||
// maintained by neuron core
|
||||
// number of tags in group
|
||||
#define NEU_METRIC_TAGS_TOTAL "tags_total"
|
||||
#define NEU_METRIC_TAGS_TOTAL_TYPE NEU_METRIC_TYPE_GAUAGE
|
||||
#define NEU_METRIC_TAGS_TOTAL_TYPE \
|
||||
(NEU_METRIC_TYPE_GAUAGE | NEU_METRIC_TYPE_FLAG_NO_RESET)
|
||||
#define NEU_METRIC_TAGS_TOTAL_HELP "Total number of tags in the node"
|
||||
|
||||
// maintained by neuron core
|
||||
// number of tags in group
|
||||
#define NEU_METRIC_GROUP_TAGS_TOTAL "group_tags_total"
|
||||
#define NEU_METRIC_GROUP_TAGS_TOTAL_TYPE NEU_METRIC_TYPE_GAUAGE
|
||||
#define NEU_METRIC_GROUP_TAGS_TOTAL_TYPE \
|
||||
(NEU_METRIC_TYPE_GAUAGE | NEU_METRIC_TYPE_FLAG_NO_RESET)
|
||||
#define NEU_METRIC_GROUP_TAGS_TOTAL_HELP "Total number of tags in the group"
|
||||
|
||||
// number of messages sent in last group timer
|
||||
@@ -301,9 +309,24 @@ void neu_metrics_unregister_entry(const char *name);
|
||||
typedef void (*neu_metrics_cb_t)(const neu_metrics_t *metrics, void *data);
|
||||
void neu_metrics_visist(neu_metrics_cb_t cb, void *data);
|
||||
|
||||
static inline bool neu_metric_type_is_counter(neu_metric_type_e type)
|
||||
{
|
||||
return NEU_METRIC_TYPE_COUNTER == (type & NEU_METRIC_TYPE_MASK);
|
||||
}
|
||||
|
||||
static inline bool neu_metric_type_is_rolling_counter(neu_metric_type_e type)
|
||||
{
|
||||
return NEU_METRIC_TYPE_ROLLING_COUNTER == (type & NEU_METRIC_TYPE_MASK);
|
||||
}
|
||||
|
||||
static inline bool neu_metric_type_no_reset(neu_metric_type_e type)
|
||||
{
|
||||
return NEU_METRIC_TYPE_FLAG_NO_RESET & type;
|
||||
}
|
||||
|
||||
static inline const char *neu_metric_type_str(neu_metric_type_e type)
|
||||
{
|
||||
if (NEU_METRIC_TYPE_COUNTER == type) {
|
||||
if (neu_metric_type_is_counter(type)) {
|
||||
return "counter";
|
||||
} else {
|
||||
return "gauge";
|
||||
@@ -316,7 +339,7 @@ int neu_metric_entries_add(neu_metric_entry_t **entries, const char *name,
|
||||
|
||||
static inline void neu_metric_entry_free(neu_metric_entry_t *entry)
|
||||
{
|
||||
if (NEU_METRIC_TYPE_ROLLING_COUNTER == entry->type) {
|
||||
if (neu_metric_type_is_rolling_counter(entry->type)) {
|
||||
neu_rolling_counter_free(entry->rcnt);
|
||||
}
|
||||
free(entry);
|
||||
@@ -445,9 +468,9 @@ static inline int neu_node_metrics_update(neu_node_metrics_t *node_metrics,
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (NEU_METRIC_TYPE_COUNTER == entry->type) {
|
||||
if (neu_metric_type_is_counter(entry->type)) {
|
||||
entry->value += n;
|
||||
} else if (NEU_METRIC_TYPE_ROLLING_COUNTER == entry->type) {
|
||||
} else if (neu_metric_type_is_rolling_counter(entry->type)) {
|
||||
entry->value =
|
||||
neu_rolling_counter_inc(entry->rcnt, global_timestamp, n);
|
||||
} else {
|
||||
@@ -465,9 +488,11 @@ static inline void neu_node_metrics_reset(neu_node_metrics_t *node_metrics)
|
||||
pthread_mutex_lock(&node_metrics->lock);
|
||||
HASH_LOOP(hh, node_metrics->entries, entry)
|
||||
{
|
||||
entry->value = 0;
|
||||
if (NEU_METRIC_TYPE_ROLLING_COUNTER == entry->type) {
|
||||
neu_rolling_counter_reset(entry->rcnt);
|
||||
if (!neu_metric_type_no_reset(entry->type)) {
|
||||
entry->value = 0;
|
||||
if (neu_metric_type_is_rolling_counter(entry->type)) {
|
||||
neu_rolling_counter_reset(entry->rcnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -476,9 +501,11 @@ static inline void neu_node_metrics_reset(neu_node_metrics_t *node_metrics)
|
||||
{
|
||||
HASH_LOOP(hh, g->entries, entry)
|
||||
{
|
||||
entry->value = 0;
|
||||
if (NEU_METRIC_TYPE_ROLLING_COUNTER == entry->type) {
|
||||
neu_rolling_counter_reset(entry->rcnt);
|
||||
if (!neu_metric_type_no_reset(entry->type)) {
|
||||
entry->value = 0;
|
||||
if (neu_metric_type_is_rolling_counter(entry->type)) {
|
||||
neu_rolling_counter_reset(entry->rcnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+215
-13
@@ -30,8 +30,10 @@ extern "C" {
|
||||
#include "json/neu_json_rw.h"
|
||||
|
||||
#include "define.h"
|
||||
#include "errcodes.h"
|
||||
#include "tag.h"
|
||||
#include "type.h"
|
||||
#include <math.h>
|
||||
|
||||
typedef struct {
|
||||
neu_node_running_state_e running;
|
||||
@@ -44,6 +46,8 @@ typedef enum neu_reqresp_type {
|
||||
|
||||
NEU_REQ_READ_GROUP,
|
||||
NEU_RESP_READ_GROUP,
|
||||
NEU_REQ_READ_GROUP_PAGINATE,
|
||||
NEU_RESP_READ_GROUP_PAGINATE,
|
||||
NEU_REQ_WRITE_TAG,
|
||||
NEU_REQ_WRITE_TAGS,
|
||||
NEU_REQ_WRITE_GTAGS,
|
||||
@@ -119,11 +123,13 @@ typedef enum neu_reqresp_type {
|
||||
static const char *neu_reqresp_type_string_t[] = {
|
||||
[NEU_RESP_ERROR] = "NEU_RESP_ERROR",
|
||||
|
||||
[NEU_REQ_READ_GROUP] = "NEU_REQ_READ_GROUP",
|
||||
[NEU_RESP_READ_GROUP] = "NEU_RESP_READ_GROUP",
|
||||
[NEU_REQ_WRITE_TAG] = "NEU_REQ_WRITE_TAG",
|
||||
[NEU_REQ_WRITE_TAGS] = "NEU_REQ_WRITE_TAGS",
|
||||
[NEU_REQ_WRITE_GTAGS] = "NEU_REQ_WRITE_GTAGS",
|
||||
[NEU_REQ_READ_GROUP] = "NEU_REQ_READ_GROUP",
|
||||
[NEU_RESP_READ_GROUP] = "NEU_RESP_READ_GROUP",
|
||||
[NEU_REQ_READ_GROUP_PAGINATE] = "NEU_REQ_READ_GROUP_PAGINATE",
|
||||
[NEU_RESP_READ_GROUP_PAGINATE] = "NEU_RESP_READ_GROUP_PAGINATE",
|
||||
[NEU_REQ_WRITE_TAG] = "NEU_REQ_WRITE_TAG",
|
||||
[NEU_REQ_WRITE_TAGS] = "NEU_REQ_WRITE_TAGS",
|
||||
[NEU_REQ_WRITE_GTAGS] = "NEU_REQ_WRITE_GTAGS",
|
||||
|
||||
[NEU_REQ_SUBSCRIBE_GROUP] = "NEU_REQ_SUBSCRIBE_GROUP",
|
||||
[NEU_REQ_UNSUBSCRIBE_GROUP] = "NEU_REQ_UNSUBSCRIBE_GROUP",
|
||||
@@ -612,6 +618,26 @@ static inline void neu_req_read_group_fini(neu_req_read_group_t *req)
|
||||
free(req->desc);
|
||||
}
|
||||
|
||||
typedef struct neu_req_read_group_paginate {
|
||||
char *driver;
|
||||
char *group;
|
||||
char *name;
|
||||
char *desc;
|
||||
bool sync;
|
||||
int current_page;
|
||||
int page_size;
|
||||
bool is_error;
|
||||
} neu_req_read_group_paginate_t;
|
||||
|
||||
static inline void
|
||||
neu_req_read_group_paginate_fini(neu_req_read_group_paginate_t *req)
|
||||
{
|
||||
free(req->driver);
|
||||
free(req->group);
|
||||
free(req->name);
|
||||
free(req->desc);
|
||||
}
|
||||
|
||||
typedef struct neu_req_write_tag {
|
||||
char * driver;
|
||||
char * group;
|
||||
@@ -694,6 +720,30 @@ typedef struct {
|
||||
UT_array *tags; // neu_resp_tag_value_meta_t
|
||||
} neu_resp_read_group_t;
|
||||
|
||||
typedef struct neu_resp_tag_value_meta_paginate {
|
||||
char tag[NEU_TAG_NAME_LEN];
|
||||
neu_dvalue_t value;
|
||||
neu_tag_meta_t metas[NEU_TAG_META_SIZE];
|
||||
neu_datatag_t datatag;
|
||||
} neu_resp_tag_value_meta_paginate_t;
|
||||
|
||||
static inline UT_icd *neu_resp_tag_value_meta_paginate_icd()
|
||||
{
|
||||
static UT_icd icd = { sizeof(neu_resp_tag_value_meta_paginate_t), NULL,
|
||||
NULL, NULL };
|
||||
return &icd;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
char *driver;
|
||||
char *group;
|
||||
int current_page;
|
||||
int page_size;
|
||||
bool is_error;
|
||||
|
||||
UT_array *tags; // neu_resp_tag_value_meta_paginate_t
|
||||
} neu_resp_read_group_paginate_t;
|
||||
|
||||
static inline void neu_resp_read_free(neu_resp_read_group_t *resp)
|
||||
{
|
||||
utarray_foreach(resp->tags, neu_resp_tag_value_meta_t *, tag_value)
|
||||
@@ -707,6 +757,20 @@ static inline void neu_resp_read_free(neu_resp_read_group_t *resp)
|
||||
utarray_free(resp->tags);
|
||||
}
|
||||
|
||||
static inline void
|
||||
neu_resp_read_paginate_free(neu_resp_read_group_paginate_t *resp)
|
||||
{
|
||||
utarray_foreach(resp->tags, neu_resp_tag_value_meta_t *, tag_value)
|
||||
{
|
||||
if (tag_value->value.type == NEU_TYPE_PTR) {
|
||||
free(tag_value->value.value.ptr.ptr);
|
||||
}
|
||||
}
|
||||
free(resp->driver);
|
||||
free(resp->group);
|
||||
utarray_free(resp->tags);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
uint16_t index;
|
||||
pthread_mutex_t mtx;
|
||||
@@ -841,14 +905,26 @@ static inline void neu_tag_value_to_json(neu_resp_tag_value_meta_t *tag_value,
|
||||
tag_json->value.val_int = tag_value->value.value.u64;
|
||||
break;
|
||||
case NEU_TYPE_FLOAT:
|
||||
tag_json->t = NEU_JSON_FLOAT;
|
||||
tag_json->value.val_float = tag_value->value.value.f32;
|
||||
tag_json->precision = tag_value->value.precision;
|
||||
if (isnan(tag_value->value.value.f32)) {
|
||||
tag_json->t = NEU_JSON_FLOAT;
|
||||
tag_json->value.val_float = tag_value->value.value.f32;
|
||||
tag_json->error = NEU_ERR_PLUGIN_TAG_VALUE_EXPIRED;
|
||||
} else {
|
||||
tag_json->t = NEU_JSON_FLOAT;
|
||||
tag_json->value.val_float = tag_value->value.value.f32;
|
||||
tag_json->precision = tag_value->value.precision;
|
||||
}
|
||||
break;
|
||||
case NEU_TYPE_DOUBLE:
|
||||
tag_json->t = NEU_JSON_DOUBLE;
|
||||
tag_json->value.val_double = tag_value->value.value.d64;
|
||||
tag_json->precision = tag_value->value.precision;
|
||||
if (isnan(tag_value->value.value.d64)) {
|
||||
tag_json->t = NEU_JSON_DOUBLE;
|
||||
tag_json->value.val_double = tag_value->value.value.d64;
|
||||
tag_json->error = NEU_ERR_PLUGIN_TAG_VALUE_EXPIRED;
|
||||
} else {
|
||||
tag_json->t = NEU_JSON_DOUBLE;
|
||||
tag_json->value.val_double = tag_value->value.value.d64;
|
||||
tag_json->precision = tag_value->value.precision;
|
||||
}
|
||||
break;
|
||||
case NEU_TYPE_BOOL:
|
||||
tag_json->t = NEU_JSON_BOOL;
|
||||
@@ -859,6 +935,131 @@ static inline void neu_tag_value_to_json(neu_resp_tag_value_meta_t *tag_value,
|
||||
tag_json->value.val_bit = tag_value->value.value.u8;
|
||||
break;
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
tag_json->t = NEU_JSON_STR;
|
||||
tag_json->value.val_str = tag_value->value.value.str;
|
||||
break;
|
||||
case NEU_TYPE_PTR:
|
||||
tag_json->t = NEU_JSON_STR;
|
||||
tag_json->value.val_str = (char *) tag_value->value.value.ptr.ptr;
|
||||
break;
|
||||
case NEU_TYPE_BYTES:
|
||||
tag_json->t = NEU_JSON_BYTES;
|
||||
tag_json->value.val_bytes.length = tag_value->value.value.bytes.length;
|
||||
tag_json->value.val_bytes.bytes = tag_value->value.value.bytes.bytes;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void
|
||||
neu_tag_value_to_json_paginate(neu_resp_tag_value_meta_paginate_t *tag_value,
|
||||
neu_json_read_paginate_resp_tag_t * tag_json)
|
||||
{
|
||||
tag_json->name = tag_value->tag;
|
||||
tag_json->error = 0;
|
||||
|
||||
tag_json->datatag.name = strdup(tag_value->datatag.name);
|
||||
tag_json->datatag.address = strdup(tag_value->datatag.address);
|
||||
tag_json->datatag.attribute = tag_value->datatag.attribute;
|
||||
tag_json->datatag.type = tag_value->datatag.type;
|
||||
tag_json->datatag.precision = tag_value->datatag.precision;
|
||||
tag_json->datatag.decimal = tag_value->datatag.decimal;
|
||||
tag_json->datatag.description = strdup(tag_value->datatag.description);
|
||||
tag_json->datatag.option = tag_value->datatag.option;
|
||||
memcpy(tag_json->datatag.meta, tag_value->datatag.meta,
|
||||
NEU_TAG_META_LENGTH);
|
||||
|
||||
for (int k = 0; k < NEU_TAG_META_SIZE; k++) {
|
||||
if (strlen(tag_value->metas[k].name) > 0) {
|
||||
tag_json->n_meta++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (tag_json->n_meta > 0) {
|
||||
tag_json->metas = (neu_json_tag_meta_t *) calloc(
|
||||
tag_json->n_meta, sizeof(neu_json_tag_meta_t));
|
||||
}
|
||||
neu_json_metas_to_json_paginate(tag_value->metas, NEU_TAG_META_SIZE,
|
||||
tag_json);
|
||||
|
||||
switch (tag_value->value.type) {
|
||||
case NEU_TYPE_ERROR:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.i32;
|
||||
tag_json->error = tag_value->value.value.i32;
|
||||
break;
|
||||
case NEU_TYPE_UINT8:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.u8;
|
||||
break;
|
||||
case NEU_TYPE_INT8:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.i8;
|
||||
break;
|
||||
case NEU_TYPE_INT16:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.i16;
|
||||
break;
|
||||
case NEU_TYPE_INT32:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.i32;
|
||||
break;
|
||||
case NEU_TYPE_INT64:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.i64;
|
||||
break;
|
||||
case NEU_TYPE_WORD:
|
||||
case NEU_TYPE_UINT16:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.u16;
|
||||
break;
|
||||
case NEU_TYPE_DWORD:
|
||||
case NEU_TYPE_UINT32:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.u32;
|
||||
break;
|
||||
case NEU_TYPE_LWORD:
|
||||
case NEU_TYPE_UINT64:
|
||||
tag_json->t = NEU_JSON_INT;
|
||||
tag_json->value.val_int = tag_value->value.value.u64;
|
||||
break;
|
||||
case NEU_TYPE_FLOAT:
|
||||
if (isnan(tag_value->value.value.f32)) {
|
||||
tag_json->t = NEU_JSON_FLOAT;
|
||||
tag_json->value.val_float = tag_value->value.value.f32;
|
||||
tag_json->error = NEU_ERR_PLUGIN_TAG_VALUE_EXPIRED;
|
||||
} else {
|
||||
tag_json->t = NEU_JSON_FLOAT;
|
||||
tag_json->value.val_float = tag_value->value.value.f32;
|
||||
tag_json->precision = tag_value->value.precision;
|
||||
}
|
||||
break;
|
||||
case NEU_TYPE_DOUBLE:
|
||||
if (isnan(tag_value->value.value.d64)) {
|
||||
tag_json->t = NEU_JSON_DOUBLE;
|
||||
tag_json->value.val_double = tag_value->value.value.d64;
|
||||
tag_json->error = NEU_ERR_PLUGIN_TAG_VALUE_EXPIRED;
|
||||
} else {
|
||||
tag_json->t = NEU_JSON_DOUBLE;
|
||||
tag_json->value.val_double = tag_value->value.value.d64;
|
||||
tag_json->precision = tag_value->value.precision;
|
||||
}
|
||||
break;
|
||||
case NEU_TYPE_BOOL:
|
||||
tag_json->t = NEU_JSON_BOOL;
|
||||
tag_json->value.val_bool = tag_value->value.value.boolean;
|
||||
break;
|
||||
case NEU_TYPE_BIT:
|
||||
tag_json->t = NEU_JSON_BIT;
|
||||
tag_json->value.val_bit = tag_value->value.value.u8;
|
||||
break;
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
tag_json->t = NEU_JSON_STR;
|
||||
tag_json->value.val_str = tag_value->value.value.str;
|
||||
break;
|
||||
@@ -904,10 +1105,11 @@ inline static void neu_msg_exchange(neu_reqresp_head_t *header)
|
||||
typedef struct {
|
||||
char driver[NEU_NODE_NAME_LEN];
|
||||
char path[128];
|
||||
int prg_int_param1;
|
||||
int prg_int_param2;
|
||||
bool del_flag;
|
||||
char prg_str_param1[128];
|
||||
char prg_str_param2[128];
|
||||
char prg_str_param3[128];
|
||||
char prg_str_param4[128];
|
||||
} neu_req_prgfile_upload_t;
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -69,6 +69,7 @@ struct neu_plugin_group {
|
||||
|
||||
void * user_data;
|
||||
neu_plugin_group_free group_free;
|
||||
uint32_t interval;
|
||||
};
|
||||
|
||||
typedef int (*neu_plugin_tag_validator_t)(const neu_datatag_t *tag);
|
||||
|
||||
@@ -59,6 +59,7 @@ typedef union {
|
||||
} value16;
|
||||
struct {
|
||||
neu_datatag_endian_e endian;
|
||||
bool is_default;
|
||||
} value32;
|
||||
struct {
|
||||
neu_datatag_endian_e endian;
|
||||
@@ -66,6 +67,7 @@ typedef union {
|
||||
struct {
|
||||
uint16_t length;
|
||||
neu_datatag_string_type_e type;
|
||||
bool is_default;
|
||||
} string;
|
||||
|
||||
struct {
|
||||
|
||||
+29
-21
@@ -31,25 +31,27 @@ extern "C" {
|
||||
#include <stdio.h>
|
||||
|
||||
typedef enum {
|
||||
NEU_TYPE_INT8 = 1,
|
||||
NEU_TYPE_UINT8 = 2,
|
||||
NEU_TYPE_INT16 = 3,
|
||||
NEU_TYPE_UINT16 = 4,
|
||||
NEU_TYPE_INT32 = 5,
|
||||
NEU_TYPE_UINT32 = 6,
|
||||
NEU_TYPE_INT64 = 7,
|
||||
NEU_TYPE_UINT64 = 8,
|
||||
NEU_TYPE_FLOAT = 9,
|
||||
NEU_TYPE_DOUBLE = 10,
|
||||
NEU_TYPE_BIT = 11,
|
||||
NEU_TYPE_BOOL = 12,
|
||||
NEU_TYPE_STRING = 13,
|
||||
NEU_TYPE_BYTES = 14,
|
||||
NEU_TYPE_ERROR = 15,
|
||||
NEU_TYPE_WORD = 16,
|
||||
NEU_TYPE_DWORD = 17,
|
||||
NEU_TYPE_LWORD = 18,
|
||||
NEU_TYPE_PTR = 19,
|
||||
NEU_TYPE_INT8 = 1,
|
||||
NEU_TYPE_UINT8 = 2,
|
||||
NEU_TYPE_INT16 = 3,
|
||||
NEU_TYPE_UINT16 = 4,
|
||||
NEU_TYPE_INT32 = 5,
|
||||
NEU_TYPE_UINT32 = 6,
|
||||
NEU_TYPE_INT64 = 7,
|
||||
NEU_TYPE_UINT64 = 8,
|
||||
NEU_TYPE_FLOAT = 9,
|
||||
NEU_TYPE_DOUBLE = 10,
|
||||
NEU_TYPE_BIT = 11,
|
||||
NEU_TYPE_BOOL = 12,
|
||||
NEU_TYPE_STRING = 13,
|
||||
NEU_TYPE_BYTES = 14,
|
||||
NEU_TYPE_ERROR = 15,
|
||||
NEU_TYPE_WORD = 16,
|
||||
NEU_TYPE_DWORD = 17,
|
||||
NEU_TYPE_LWORD = 18,
|
||||
NEU_TYPE_PTR = 19,
|
||||
NEU_TYPE_TIME = 20,
|
||||
NEU_TYPE_DATA_AND_TIME = 21,
|
||||
} neu_type_e;
|
||||
|
||||
inline static const char *neu_type_string(neu_type_e type)
|
||||
@@ -93,6 +95,10 @@ inline static const char *neu_type_string(neu_type_e type)
|
||||
return "NEU_TYPE_LWORD";
|
||||
case NEU_TYPE_PTR:
|
||||
return "NEU_TYPE_PTR";
|
||||
case NEU_TYPE_TIME:
|
||||
return "NEU_TYPE_TIME";
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
return "NEU_TYPE_DATA_AND_TIME";
|
||||
}
|
||||
|
||||
return "";
|
||||
@@ -130,7 +136,7 @@ typedef union {
|
||||
|
||||
static inline char *neu_value_str(neu_type_e type, neu_value_u value)
|
||||
{
|
||||
static __thread char str[32] = { 0 };
|
||||
static __thread char str[128] = { 0 };
|
||||
|
||||
switch (type) {
|
||||
case NEU_TYPE_INT8:
|
||||
@@ -188,7 +194,9 @@ static inline char *neu_value_str(neu_type_e type, neu_value_u value)
|
||||
snprintf(str, sizeof(str), "type: %s, value: %u", neu_type_string(type),
|
||||
value.boolean);
|
||||
break;
|
||||
case NEU_TYPE_STRING: {
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME: {
|
||||
snprintf(str, sizeof(str), "type: %s, value: %c%c%c",
|
||||
neu_type_string(type), value.str[0], value.str[1],
|
||||
value.str[2]);
|
||||
|
||||
@@ -87,4 +87,6 @@ void neu_async_queue_remove(neu_async_queue_t *q, neu_async_queue_filter filter,
|
||||
*/
|
||||
void neu_async_queue_clean(neu_async_queue_t *q);
|
||||
|
||||
int neu_async_queue_len(neu_async_queue_t *q);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
import jwt # pip install pyjwt
|
||||
import time
|
||||
|
||||
with open("neuron.key", "r") as f:
|
||||
private_key = f.read()
|
||||
|
||||
current_time = int(time.time())
|
||||
two_years = 60 * 60 * 24 * 365 * 2
|
||||
header = {"alg": "RS256", "typ": "JWT"}
|
||||
payload = {
|
||||
"aud": "neuron",
|
||||
"bodyEncode": 0,
|
||||
"exp": current_time + two_years,
|
||||
"iat": current_time,
|
||||
"iss": "neuron",
|
||||
}
|
||||
token = jwt.encode(payload, private_key, algorithm="RS256", headers=header).decode(
|
||||
"utf-8"
|
||||
)
|
||||
print(f"Bearer {token}")
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2024 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.
|
||||
**/
|
||||
BEGIN TRANSACTION;
|
||||
|
||||
ALTER TABLE tags RENAME TO temp_tags;
|
||||
|
||||
CREATE TABLE
|
||||
IF NOT EXISTS tags (
|
||||
driver_name TEXT NOT NULL,
|
||||
group_name TEXT NOT NULL,
|
||||
name TEXT NULL check (length (name) <= 128),
|
||||
address TEXT NULL check (length (address) <= 128),
|
||||
attribute INTEGER NOT NULL check (attribute BETWEEN 0 AND 15),
|
||||
precision INTEGER NOT NULL check (precision BETWEEN 0 AND 17),
|
||||
decimal REAL NOT NULL,
|
||||
type INTEGER NOT NULL check (type BETWEEN 0 AND 21),
|
||||
description TEXT NULL check (length (description) <= 512),
|
||||
value TEXT,
|
||||
UNIQUE (driver_name, group_name, name),
|
||||
FOREIGN KEY (driver_name, group_name) REFERENCES groups (driver_name, name) ON UPDATE CASCADE ON DELETE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO tags SELECT * FROM temp_tags;
|
||||
|
||||
DROP TABLE temp_tags;
|
||||
|
||||
COMMIT;
|
||||
@@ -162,7 +162,16 @@ static inline int start(neu_plugin_t *plugin, const char *url)
|
||||
return rv;
|
||||
}
|
||||
|
||||
nng_mtx_lock(plugin->mtx);
|
||||
while (plugin->receiving) {
|
||||
nng_mtx_unlock(plugin->mtx);
|
||||
nng_msleep(10);
|
||||
nng_mtx_lock(plugin->mtx);
|
||||
}
|
||||
plugin->receiving = true;
|
||||
nng_recv_aio(plugin->sock, plugin->recv_aio);
|
||||
nng_mtx_unlock(plugin->mtx);
|
||||
|
||||
return NEU_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -313,9 +322,13 @@ static int ekuiper_plugin_request(neu_plugin_t * plugin,
|
||||
case NEU_REQRESP_TRANS_DATA: {
|
||||
neu_reqresp_trans_data_t *trans_data = data;
|
||||
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_5S, 1, NULL);
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_30S, 1, NULL);
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_60S, 1, NULL);
|
||||
if (plugin->started) {
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_5S, 1, NULL);
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_30S, 1,
|
||||
NULL);
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_60S, 1,
|
||||
NULL);
|
||||
}
|
||||
|
||||
if (disconnected) {
|
||||
plog_debug(plugin, "not connected");
|
||||
|
||||
@@ -37,6 +37,7 @@ struct neu_plugin {
|
||||
nng_socket sock;
|
||||
nng_mtx * mtx;
|
||||
bool started;
|
||||
bool receiving;
|
||||
nng_aio * recv_aio;
|
||||
char * host;
|
||||
uint16_t port;
|
||||
|
||||
@@ -83,6 +83,9 @@ void recv_data_callback(void *arg)
|
||||
rv = nng_aio_result(plugin->recv_aio);
|
||||
if (0 != rv) {
|
||||
plog_error(plugin, "nng_recv error: %s", nng_strerror(rv));
|
||||
nng_mtx_lock(plugin->mtx);
|
||||
plugin->receiving = false;
|
||||
nng_mtx_unlock(plugin->mtx);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
"description_zh": "MQTT 客户端 ID",
|
||||
"type": "string",
|
||||
"attribute": "required",
|
||||
"default": "neuron_${random_str}",
|
||||
"valid": {
|
||||
"length": 255
|
||||
}
|
||||
@@ -64,7 +63,6 @@
|
||||
"description_zh": "接收点位写入请求的 MQTT 主题。",
|
||||
"attribute": "required",
|
||||
"type": "string",
|
||||
"default": "/neuron/${node-name}/write/req",
|
||||
"valid": {
|
||||
"length": 255
|
||||
}
|
||||
@@ -76,7 +74,6 @@
|
||||
"description_zh": "发送点位写入响应的 MQTT 主题。",
|
||||
"attribute": "required",
|
||||
"type": "string",
|
||||
"default": "/neuron/${node-name}/write/resp",
|
||||
"valid": {
|
||||
"length": 255
|
||||
}
|
||||
|
||||
@@ -366,9 +366,13 @@ static int mqtt_plugin_request(neu_plugin_t *plugin, neu_reqresp_head_t *head,
|
||||
error = handle_read_response(plugin, head->ctx, data);
|
||||
break;
|
||||
case NEU_REQRESP_TRANS_DATA: {
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_5S, 1, NULL);
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_30S, 1, NULL);
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_60S, 1, NULL);
|
||||
if (plugin->client && neu_mqtt_client_is_open(plugin->client)) {
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_5S, 1, NULL);
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_30S, 1,
|
||||
NULL);
|
||||
NEU_PLUGIN_UPDATE_METRIC(plugin, NEU_METRIC_TRANS_DATA_60S, 1,
|
||||
NULL);
|
||||
}
|
||||
error = handle_trans_data(plugin, data);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -72,6 +72,9 @@ static struct neu_http_handler cors_handler[] = {
|
||||
{
|
||||
.url = "/api/v2/read",
|
||||
},
|
||||
{
|
||||
.url = "/api/v2/read/paginate",
|
||||
},
|
||||
{
|
||||
.url = "/api/v2/write",
|
||||
},
|
||||
@@ -259,6 +262,12 @@ static struct neu_http_handler rest_handlers[] = {
|
||||
.url = "/api/v2/read",
|
||||
.value.handler = handle_read,
|
||||
},
|
||||
{
|
||||
.method = NEU_HTTP_METHOD_POST,
|
||||
.type = NEU_HTTP_HANDLER_FUNCTION,
|
||||
.url = "/api/v2/read/paginate",
|
||||
.value.handler = handle_read_paginate,
|
||||
},
|
||||
{
|
||||
.method = NEU_HTTP_METHOD_POST,
|
||||
.type = NEU_HTTP_HANDLER_FUNCTION,
|
||||
|
||||
@@ -181,7 +181,7 @@ static inline void gen_single_node_metrics(neu_node_metrics_t *node_metrics,
|
||||
pthread_mutex_lock(&node_metrics->lock);
|
||||
HASH_LOOP(hh, node_metrics->entries, e)
|
||||
{
|
||||
if (NEU_METRIC_TYPE_ROLLING_COUNTER == e->type) {
|
||||
if (neu_metric_type_is_rolling_counter(e->type)) {
|
||||
// force clean stale value
|
||||
e->value = neu_rolling_counter_inc(e->rcnt, global_timestamp, 0);
|
||||
}
|
||||
@@ -196,7 +196,7 @@ static inline void gen_single_node_metrics(neu_node_metrics_t *node_metrics,
|
||||
{
|
||||
HASH_LOOP(hh, g->entries, e)
|
||||
{
|
||||
if (NEU_METRIC_TYPE_ROLLING_COUNTER == e->type) {
|
||||
if (neu_metric_type_is_rolling_counter(e->type)) {
|
||||
// force clean stale value
|
||||
e->value =
|
||||
neu_rolling_counter_inc(e->rcnt, global_timestamp, 0);
|
||||
@@ -278,7 +278,7 @@ static void gen_all_node_metrics(neu_metrics_t *metrics, int type_filter,
|
||||
pthread_mutex_lock(&n->lock);
|
||||
HASH_FIND_STR(n->entries, r->name, e);
|
||||
if (e) {
|
||||
if (NEU_METRIC_TYPE_ROLLING_COUNTER == e->type) {
|
||||
if (neu_metric_type_is_rolling_counter(e->type)) {
|
||||
// force clean stale value
|
||||
e->value =
|
||||
neu_rolling_counter_inc(e->rcnt, global_timestamp, 0);
|
||||
@@ -294,7 +294,7 @@ static void gen_all_node_metrics(neu_metrics_t *metrics, int type_filter,
|
||||
{
|
||||
HASH_FIND_STR(g->entries, r->name, e);
|
||||
if (e) {
|
||||
if (NEU_METRIC_TYPE_ROLLING_COUNTER == e->type) {
|
||||
if (neu_metric_type_is_rolling_counter(e->type)) {
|
||||
// force clean stale value
|
||||
e->value = neu_rolling_counter_inc(e->rcnt,
|
||||
global_timestamp, 0);
|
||||
|
||||
@@ -223,6 +223,10 @@ static int dashb_plugin_request(neu_plugin_t * plugin,
|
||||
case NEU_RESP_READ_GROUP:
|
||||
handle_read_resp(header->ctx, (neu_resp_read_group_t *) data);
|
||||
break;
|
||||
case NEU_RESP_READ_GROUP_PAGINATE:
|
||||
handle_read_paginate_resp(header->ctx,
|
||||
(neu_resp_read_group_paginate_t *) data);
|
||||
break;
|
||||
default:
|
||||
nlog_fatal("recv unhandle msg: %s",
|
||||
neu_reqresp_type_string(header->type));
|
||||
|
||||
@@ -80,6 +80,61 @@ void handle_read(nng_aio *aio)
|
||||
})
|
||||
}
|
||||
|
||||
void handle_read_paginate(nng_aio *aio)
|
||||
{
|
||||
neu_plugin_t *plugin = neu_rest_get_plugin();
|
||||
|
||||
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
|
||||
aio, neu_json_read_paginate_req_t, neu_json_decode_read_paginate_req, {
|
||||
int ret = 0;
|
||||
neu_reqresp_head_t header = { 0 };
|
||||
neu_req_read_group_paginate_t cmd = { 0 };
|
||||
int err_type;
|
||||
header.ctx = aio;
|
||||
header.type = NEU_REQ_READ_GROUP_PAGINATE;
|
||||
|
||||
if (strlen(req->node) >= NEU_NODE_NAME_LEN) {
|
||||
err_type = NEU_ERR_NODE_NAME_TOO_LONG;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (strlen(req->node) >= NEU_GROUP_NAME_LEN) {
|
||||
err_type = NEU_ERR_GROUP_NAME_TOO_LONG;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (strlen(req->node) >= NEU_TAG_NAME_LEN) {
|
||||
err_type = NEU_ERR_TAG_NAME_TOO_LONG;
|
||||
goto error;
|
||||
}
|
||||
|
||||
cmd.driver = req->node;
|
||||
cmd.group = req->group;
|
||||
cmd.name = req->name;
|
||||
cmd.desc = req->desc;
|
||||
cmd.sync = req->sync;
|
||||
cmd.current_page = req->current_page;
|
||||
cmd.page_size = req->page_size;
|
||||
cmd.is_error = req->is_error;
|
||||
req->node = NULL;
|
||||
req->group = NULL;
|
||||
ret = neu_plugin_op(plugin, header, &cmd);
|
||||
if (ret != 0) {
|
||||
neu_req_read_group_paginate_fini(&cmd);
|
||||
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
|
||||
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
|
||||
});
|
||||
}
|
||||
goto success;
|
||||
|
||||
error:
|
||||
NEU_JSON_RESPONSE_ERROR(
|
||||
err_type, { neu_http_response(aio, err_type, result_error); });
|
||||
|
||||
success:;
|
||||
})
|
||||
}
|
||||
|
||||
void handle_write(nng_aio *aio)
|
||||
{
|
||||
neu_plugin_t *plugin = neu_rest_get_plugin();
|
||||
@@ -384,3 +439,114 @@ void handle_read_resp(nng_aio *aio, neu_resp_read_group_t *resp)
|
||||
free(api_res.tags);
|
||||
free(result);
|
||||
}
|
||||
|
||||
void handle_read_paginate_resp(nng_aio * aio,
|
||||
neu_resp_read_group_paginate_t *resp)
|
||||
{
|
||||
neu_json_read_paginate_resp_t api_res = { 0 };
|
||||
char * result = NULL;
|
||||
|
||||
if (resp->is_error) {
|
||||
UT_array *filtered_tags;
|
||||
utarray_new(filtered_tags, neu_resp_tag_value_meta_paginate_icd());
|
||||
|
||||
utarray_foreach(resp->tags, neu_resp_tag_value_meta_paginate_t *,
|
||||
tag_value)
|
||||
{
|
||||
if (tag_value->value.type == NEU_TYPE_ERROR) {
|
||||
neu_resp_tag_value_meta_paginate_t new_tag_value;
|
||||
memcpy(&new_tag_value, tag_value,
|
||||
sizeof(neu_resp_tag_value_meta_paginate_t));
|
||||
|
||||
new_tag_value.datatag.name = strdup(tag_value->datatag.name);
|
||||
new_tag_value.datatag.address =
|
||||
strdup(tag_value->datatag.address);
|
||||
new_tag_value.datatag.description =
|
||||
strdup(tag_value->datatag.description);
|
||||
|
||||
utarray_push_back(filtered_tags, &new_tag_value);
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < utarray_len(resp->tags); ++i) {
|
||||
neu_resp_tag_value_meta_paginate_t *orig_tag_value;
|
||||
orig_tag_value =
|
||||
(neu_resp_tag_value_meta_paginate_t *) utarray_eltptr(
|
||||
resp->tags, i);
|
||||
if (orig_tag_value) {
|
||||
free(orig_tag_value->datatag.name);
|
||||
free(orig_tag_value->datatag.address);
|
||||
free(orig_tag_value->datatag.description);
|
||||
}
|
||||
}
|
||||
|
||||
utarray_free(resp->tags);
|
||||
resp->tags = filtered_tags;
|
||||
}
|
||||
|
||||
unsigned int total_tags = utarray_len(resp->tags);
|
||||
|
||||
api_res.meta.current_page = resp->current_page;
|
||||
api_res.meta.page_size = resp->page_size;
|
||||
api_res.meta.total = total_tags;
|
||||
|
||||
if (resp->current_page > 0 && resp->page_size > 0) {
|
||||
unsigned int start_index = (resp->current_page - 1) * resp->page_size;
|
||||
unsigned int end_index = start_index + resp->page_size;
|
||||
|
||||
if (start_index >= total_tags) {
|
||||
api_res.n_tag = 0;
|
||||
api_res.tags = calloc(1, sizeof(neu_json_read_paginate_resp_tag_t));
|
||||
} else {
|
||||
if (end_index > total_tags) {
|
||||
end_index = total_tags;
|
||||
}
|
||||
|
||||
api_res.n_tag = end_index - start_index;
|
||||
api_res.tags = calloc(api_res.n_tag,
|
||||
sizeof(neu_json_read_paginate_resp_tag_t));
|
||||
|
||||
for (unsigned int i = start_index; i < end_index; i++) {
|
||||
neu_resp_tag_value_meta_paginate_t *tag_value =
|
||||
(neu_resp_tag_value_meta_paginate_t *) utarray_eltptr(
|
||||
resp->tags, i);
|
||||
neu_tag_value_to_json_paginate(tag_value,
|
||||
&api_res.tags[i - start_index]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
api_res.n_tag = total_tags;
|
||||
api_res.tags =
|
||||
calloc(api_res.n_tag, sizeof(neu_json_read_paginate_resp_tag_t));
|
||||
int index = 0;
|
||||
|
||||
utarray_foreach(resp->tags, neu_resp_tag_value_meta_paginate_t *,
|
||||
tag_value)
|
||||
{
|
||||
neu_tag_value_to_json_paginate(tag_value, &api_res.tags[index++]);
|
||||
}
|
||||
}
|
||||
|
||||
neu_json_encode_by_fn(&api_res, neu_json_encode_read_paginate_resp,
|
||||
&result);
|
||||
|
||||
for (unsigned int i = 0; i < utarray_len(resp->tags); ++i) {
|
||||
neu_resp_tag_value_meta_paginate_t *final_tag_value;
|
||||
final_tag_value = (neu_resp_tag_value_meta_paginate_t *) utarray_eltptr(
|
||||
resp->tags, i);
|
||||
if (final_tag_value) {
|
||||
free(final_tag_value->datatag.name);
|
||||
free(final_tag_value->datatag.address);
|
||||
free(final_tag_value->datatag.description);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < api_res.n_tag; i++) {
|
||||
if (api_res.tags[i].n_meta > 0) {
|
||||
free(api_res.tags[i].metas);
|
||||
}
|
||||
}
|
||||
neu_http_ok(aio, result);
|
||||
free(api_res.tags);
|
||||
free(result);
|
||||
}
|
||||
@@ -27,9 +27,12 @@
|
||||
void handle_rw_init();
|
||||
void handle_rw_uninit();
|
||||
void handle_read(nng_aio *aio);
|
||||
void handle_read_paginate(nng_aio *aio);
|
||||
void handle_write(nng_aio *aio);
|
||||
void handle_write_tags(nng_aio *aio);
|
||||
void handle_write_gtags(nng_aio *aio);
|
||||
void handle_read_resp(nng_aio *aio, neu_resp_read_group_t *resp);
|
||||
void handle_read_paginate_resp(nng_aio * aio,
|
||||
neu_resp_read_group_paginate_t *resp);
|
||||
|
||||
#endif
|
||||
+68
-26
@@ -426,13 +426,6 @@ static int adapter_update_metric(neu_adapter_t *adapter,
|
||||
return neu_node_metrics_update(adapter->metrics, group, metric_name, n);
|
||||
}
|
||||
|
||||
static inline void adapter_reset_metrics(neu_adapter_t *adapter)
|
||||
{
|
||||
if (NULL != adapter->metrics) {
|
||||
neu_node_metrics_reset(adapter->metrics);
|
||||
}
|
||||
}
|
||||
|
||||
static int adapter_command(neu_adapter_t *adapter, neu_reqresp_head_t header,
|
||||
void *data)
|
||||
{
|
||||
@@ -451,6 +444,12 @@ static int adapter_command(neu_adapter_t *adapter, neu_reqresp_head_t header,
|
||||
strcpy(pheader->receiver, cmd->driver);
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_READ_GROUP_PAGINATE: {
|
||||
neu_req_read_group_paginate_t *cmd =
|
||||
(neu_req_read_group_paginate_t *) data;
|
||||
strcpy(pheader->receiver, cmd->driver);
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_WRITE_TAG: {
|
||||
neu_req_write_tag_t *cmd = (neu_req_write_tag_t *) data;
|
||||
strcpy(pheader->receiver, cmd->driver);
|
||||
@@ -734,6 +733,30 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
|
||||
break;
|
||||
}
|
||||
case NEU_RESP_READ_GROUP_PAGINATE:
|
||||
adapter->module->intf_funs->request(
|
||||
adapter->plugin, (neu_reqresp_head_t *) header, &header[1]);
|
||||
neu_resp_read_paginate_free(
|
||||
(neu_resp_read_group_paginate_t *) &header[1]);
|
||||
neu_msg_free(msg);
|
||||
break;
|
||||
case NEU_REQ_READ_GROUP_PAGINATE: {
|
||||
neu_resp_error_t error = { 0 };
|
||||
|
||||
if (adapter->module->type == NEU_NA_TYPE_DRIVER) {
|
||||
neu_adapter_driver_read_group_paginate(
|
||||
(neu_adapter_driver_t *) adapter, header);
|
||||
} else {
|
||||
neu_req_read_group_paginate_fini(
|
||||
(neu_req_read_group_paginate_t *) &header[1]);
|
||||
error.error = NEU_ERR_GROUP_NOT_ALLOW;
|
||||
header->type = NEU_RESP_ERROR;
|
||||
neu_msg_exchange(header);
|
||||
reply(adapter, header, &error);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case NEU_REQ_WRITE_TAG: {
|
||||
neu_resp_error_t error = { 0 };
|
||||
|
||||
@@ -783,7 +806,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_NODE_SETTING: {
|
||||
neu_req_node_setting_t *cmd = (neu_req_node_setting_t *) &header[1];
|
||||
neu_resp_error_t error = { 0 };
|
||||
|
||||
nlog_notice("setting node:%s params:%s", cmd->node, cmd->setting);
|
||||
error.error = neu_adapter_set_setting(adapter, cmd->setting);
|
||||
if (error.error == NEU_ERR_SUCCESS) {
|
||||
adapter_storage_setting(adapter->name, cmd->setting);
|
||||
@@ -876,7 +899,8 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_ADD_GROUP: {
|
||||
neu_req_add_group_t *cmd = (neu_req_add_group_t *) &header[1];
|
||||
neu_resp_error_t error = { 0 };
|
||||
|
||||
nlog_notice("add group node:%s group:%s interval:%d", cmd->driver,
|
||||
cmd->group, cmd->interval);
|
||||
if (cmd->interval < NEU_GROUP_INTERVAL_LIMIT) {
|
||||
error.error = NEU_ERR_GROUP_PARAMETER_INVALID;
|
||||
} else {
|
||||
@@ -901,7 +925,8 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_UPDATE_DRIVER_GROUP: {
|
||||
neu_req_update_group_t *cmd = (neu_req_update_group_t *) &header[1];
|
||||
neu_resp_update_group_t resp = { 0 };
|
||||
|
||||
nlog_notice("update group node:%s old_name:%s new_name:%s interval:%d",
|
||||
header->receiver, cmd->group, cmd->new_name, cmd->interval);
|
||||
if (adapter->module->type == NEU_NA_TYPE_DRIVER) {
|
||||
resp.error = neu_adapter_driver_update_group(
|
||||
(neu_adapter_driver_t *) adapter, cmd->group, cmd->new_name,
|
||||
@@ -926,7 +951,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_DEL_GROUP: {
|
||||
neu_req_del_group_t *cmd = (neu_req_del_group_t *) &header[1];
|
||||
neu_resp_error_t error = { 0 };
|
||||
|
||||
nlog_notice("del group node:%s group:%s", cmd->driver, cmd->group);
|
||||
if (adapter->module->type == NEU_NA_TYPE_DRIVER) {
|
||||
error.error = neu_adapter_driver_del_group(
|
||||
(neu_adapter_driver_t *) adapter, cmd->group);
|
||||
@@ -982,6 +1007,8 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
|
||||
if (adapter->module->type == NEU_NA_TYPE_DRIVER) {
|
||||
for (int i = 0; i < cmd->n_tag; i++) {
|
||||
nlog_notice("del tag node:%s group:%s tag:%s", cmd->driver,
|
||||
cmd->group, cmd->tags[i]);
|
||||
int ret = neu_adapter_driver_del_tag(
|
||||
(neu_adapter_driver_t *) adapter, cmd->group, cmd->tags[i]);
|
||||
if (0 == ret) {
|
||||
@@ -1071,12 +1098,6 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
|
||||
if (adapter->module->type != NEU_NA_TYPE_DRIVER) {
|
||||
resp.error = NEU_ERR_GROUP_NOT_ALLOW;
|
||||
} else if (neu_adapter_driver_new_group_count(
|
||||
(neu_adapter_driver_t *) adapter, cmd) +
|
||||
neu_adapter_driver_group_count(
|
||||
(neu_adapter_driver_t *) adapter) >
|
||||
NEU_GROUP_MAX_PER_NODE) {
|
||||
resp.error = NEU_ERR_GROUP_MAX_GROUPS;
|
||||
} else {
|
||||
if (neu_adapter_validate_gtags(adapter, cmd, &resp) == 0 &&
|
||||
neu_adapter_try_add_gtags(adapter, cmd, &resp) == 0 &&
|
||||
@@ -1207,22 +1228,30 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
int neu_adapter_validate_gtags(neu_adapter_t *adapter, neu_req_add_gtag_t *cmd,
|
||||
neu_resp_add_tag_t *resp)
|
||||
{
|
||||
neu_adapter_driver_t *driver_adapter = (neu_adapter_driver_t *) adapter;
|
||||
if (neu_adapter_driver_new_group_count(driver_adapter, cmd) +
|
||||
neu_adapter_driver_group_count(driver_adapter) >
|
||||
NEU_GROUP_MAX_PER_NODE) {
|
||||
resp->error = NEU_ERR_GROUP_MAX_GROUPS;
|
||||
resp->index = 0;
|
||||
return NEU_ERR_GROUP_MAX_GROUPS;
|
||||
}
|
||||
|
||||
for (int group_index = 0; group_index < cmd->n_group; group_index++) {
|
||||
for (int tag_index = 0; tag_index < cmd->groups[group_index].n_tag;
|
||||
tag_index++) {
|
||||
neu_gdatatag_t *current_group = &cmd->groups[group_index];
|
||||
for (int tag_index = 0; tag_index < current_group->n_tag; tag_index++) {
|
||||
int validation_result = neu_adapter_driver_validate_tag(
|
||||
(neu_adapter_driver_t *) adapter,
|
||||
cmd->groups[group_index].group,
|
||||
&cmd->groups[group_index].tags[tag_index]);
|
||||
if (validation_result == 0) {
|
||||
resp->index += 1;
|
||||
} else {
|
||||
driver_adapter, current_group->group,
|
||||
¤t_group->tags[tag_index]);
|
||||
if (validation_result != 0) {
|
||||
resp->error = validation_result;
|
||||
resp->index = 0;
|
||||
return validation_result;
|
||||
}
|
||||
resp->index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1342,6 +1371,11 @@ void neu_adapter_destroy(neu_adapter_t *adapter)
|
||||
}
|
||||
|
||||
neu_event_close(adapter->events);
|
||||
#ifdef NEU_RELEASE
|
||||
if (adapter->handle != NULL) {
|
||||
dlclose(adapter->handle);
|
||||
}
|
||||
#endif
|
||||
free(adapter);
|
||||
}
|
||||
|
||||
@@ -1387,6 +1421,10 @@ int neu_adapter_start(neu_adapter_t *adapter)
|
||||
if (error == NEU_ERR_SUCCESS) {
|
||||
adapter->state = NEU_NODE_RUNNING_STATE_RUNNING;
|
||||
adapter_storage_state(adapter->name, adapter->state);
|
||||
if (NEU_NA_TYPE_DRIVER == neu_adapter_get_type(adapter)) {
|
||||
neu_adapter_driver_start_group_timer(
|
||||
(neu_adapter_driver_t *) adapter);
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
@@ -1425,7 +1463,11 @@ int neu_adapter_stop(neu_adapter_t *adapter)
|
||||
if (error == NEU_ERR_SUCCESS) {
|
||||
adapter->state = NEU_NODE_RUNNING_STATE_STOPPED;
|
||||
adapter_storage_state(adapter->name, adapter->state);
|
||||
adapter_reset_metrics(adapter);
|
||||
if (NEU_NA_TYPE_DRIVER == neu_adapter_get_type(adapter)) {
|
||||
neu_adapter_driver_stop_group_timer(
|
||||
(neu_adapter_driver_t *) adapter);
|
||||
}
|
||||
neu_adapter_reset_metrics(adapter);
|
||||
}
|
||||
|
||||
return error;
|
||||
|
||||
@@ -96,6 +96,13 @@ int neu_adapter_set_setting(neu_adapter_t *adapter, const char *config);
|
||||
int neu_adapter_get_setting(neu_adapter_t *adapter, char **config);
|
||||
neu_node_state_t neu_adapter_get_state(neu_adapter_t *adapter);
|
||||
|
||||
static inline void neu_adapter_reset_metrics(neu_adapter_t *adapter)
|
||||
{
|
||||
if (NULL != adapter->metrics) {
|
||||
neu_node_metrics_reset(adapter->metrics);
|
||||
}
|
||||
}
|
||||
|
||||
int neu_adapter_register_group_metric(neu_adapter_t *adapter,
|
||||
const char *group_name, const char *name,
|
||||
const char *help, neu_metric_type_e type,
|
||||
|
||||
@@ -157,6 +157,8 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
|
||||
case NEU_TYPE_BIT:
|
||||
case NEU_TYPE_BOOL:
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
case NEU_TYPE_WORD:
|
||||
case NEU_TYPE_DWORD:
|
||||
case NEU_TYPE_LWORD:
|
||||
@@ -299,6 +301,8 @@ int neu_driver_cache_meta_get(neu_driver_cache_t *cache, const char *group,
|
||||
value->value.value.boolean = elem->value.value.boolean;
|
||||
break;
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
memcpy(value->value.value.str, elem->value.value.str,
|
||||
sizeof(elem->value.value.str));
|
||||
break;
|
||||
@@ -381,6 +385,8 @@ int neu_driver_cache_meta_get_changed(neu_driver_cache_t *cache,
|
||||
value->value.value.boolean = elem->value.value.boolean;
|
||||
break;
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
memcpy(value->value.value.str, elem->value.value.str,
|
||||
sizeof(elem->value.value.str));
|
||||
break;
|
||||
|
||||
+427
-137
@@ -89,6 +89,10 @@ static void read_group(int64_t timestamp, int64_t timeout,
|
||||
neu_tag_cache_type_e cache_type,
|
||||
neu_driver_cache_t *cache, const char *group,
|
||||
UT_array *tags, UT_array *tag_values);
|
||||
static void read_group_paginate(int64_t timestamp, int64_t timeout,
|
||||
neu_tag_cache_type_e cache_type,
|
||||
neu_driver_cache_t *cache, const char *group,
|
||||
UT_array *tags, UT_array *tag_values);
|
||||
static void read_report_group(int64_t timestamp, int64_t timeout,
|
||||
neu_tag_cache_type_e cache_type,
|
||||
neu_driver_cache_t *cache, const char *group,
|
||||
@@ -102,8 +106,48 @@ static void update_with_meta(neu_adapter_t *adapter, const char *group,
|
||||
const char *tag, neu_dvalue_t value,
|
||||
neu_tag_meta_t *metas, int n_meta);
|
||||
static void write_response(neu_adapter_t *adapter, void *r, neu_error error);
|
||||
static group_t *find_group(neu_adapter_driver_t *driver, const char *name);
|
||||
static void store_write_tag(group_t *group, to_be_write_tag_t *tag);
|
||||
static group_t * find_group(neu_adapter_driver_t *driver, const char *name);
|
||||
static void store_write_tag(group_t *group, to_be_write_tag_t *tag);
|
||||
static inline void start_group_timer(neu_adapter_driver_t *driver,
|
||||
group_t * grp);
|
||||
static inline void stop_group_timer(neu_adapter_driver_t *driver, group_t *grp);
|
||||
|
||||
static void format_tag_value(neu_dvalue_t *value)
|
||||
{
|
||||
double scale = pow(10, 5);
|
||||
|
||||
int negative = 1;
|
||||
|
||||
if (value->value.d64 < 0) {
|
||||
value->value.d64 *= -1;
|
||||
negative = -1;
|
||||
}
|
||||
|
||||
int64_t integer_part = (int64_t) value->value.d64;
|
||||
double decimal_part = value->value.d64 - integer_part;
|
||||
decimal_part *= scale;
|
||||
decimal_part = round(decimal_part);
|
||||
char str[6] = { 0 };
|
||||
snprintf(str, sizeof(str), "%05" PRId64 "", (int64_t) decimal_part);
|
||||
int i = 0, flag = 0;
|
||||
for (; i < 4; i++) {
|
||||
if (str[i] == '0' && str[i + 1] == '0') {
|
||||
flag = 1;
|
||||
break;
|
||||
} else if (str[i] == '9' && str[i + 1] == '9') {
|
||||
flag = 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag != 0 && i != 0) {
|
||||
decimal_part = round(decimal_part / pow(10, 5 - i));
|
||||
value->value.d64 = (double) integer_part + decimal_part / pow(10, i);
|
||||
} else {
|
||||
value->value.d64 = (double) integer_part + decimal_part / scale;
|
||||
}
|
||||
|
||||
value->value.d64 *= negative;
|
||||
}
|
||||
|
||||
static void write_response(neu_adapter_t *adapter, void *r, neu_error error)
|
||||
{
|
||||
@@ -168,7 +212,7 @@ static void update_with_meta(neu_adapter_t *adapter, const char *group,
|
||||
update_metric(&driver->adapter, NEU_METRIC_TAG_READ_ERRORS_TOTAL,
|
||||
NEU_TYPE_ERROR == value.type, NULL);
|
||||
}
|
||||
nlog_info(
|
||||
nlog_debug(
|
||||
"update driver: %s, group: %s, tag: %s, type: %s, timestamp: %" PRId64
|
||||
" n_meta: %d",
|
||||
driver->adapter.name, group, tag, neu_type_string(value.type),
|
||||
@@ -181,14 +225,8 @@ static void update_im(neu_adapter_t *adapter, const char *group,
|
||||
{
|
||||
neu_adapter_driver_t *driver = (neu_adapter_driver_t *) adapter;
|
||||
|
||||
if (tag == NULL || value.type == NEU_TYPE_ERROR) {
|
||||
nlog_warn("update_im tag is null or value is error %d",
|
||||
value.value.i32);
|
||||
return;
|
||||
}
|
||||
|
||||
UT_array *tags = neu_adapter_driver_get_ptag(driver, group, tag);
|
||||
if (tags == NULL) {
|
||||
if (tag == NULL) {
|
||||
nlog_warn("update_im tag is null");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -196,24 +234,32 @@ static void update_im(neu_adapter_t *adapter, const char *group,
|
||||
value, metas, n_meta, true);
|
||||
driver->adapter.cb_funs.update_metric(&driver->adapter,
|
||||
NEU_METRIC_TAG_READS_TOTAL, 1, NULL);
|
||||
if (value.type == NEU_TYPE_ERROR) {
|
||||
return;
|
||||
}
|
||||
|
||||
UT_array *tags = neu_adapter_driver_get_ptag(driver, group, tag);
|
||||
if (tags == NULL) {
|
||||
return;
|
||||
}
|
||||
neu_datatag_t *first = utarray_front(tags);
|
||||
|
||||
if (first == NULL ||
|
||||
!neu_tag_attribute_test(first, NEU_ATTRIBUTE_SUBSCRIBE)) {
|
||||
utarray_free(tags);
|
||||
nlog_info("update immediately, driver: %s, "
|
||||
"group: %s, tag: %s, type: %s, "
|
||||
"timestamp: %" PRId64,
|
||||
driver->adapter.name, group, tag, neu_type_string(value.type),
|
||||
global_timestamp);
|
||||
nlog_debug("update immediately, driver: %s, "
|
||||
"group: %s, tag: %s, type: %s, "
|
||||
"timestamp: %" PRId64,
|
||||
driver->adapter.name, group, tag,
|
||||
neu_type_string(value.type), global_timestamp);
|
||||
return;
|
||||
}
|
||||
|
||||
nlog_info("update and report immediately, driver: %s, "
|
||||
"group: %s, tag: %s, type: %s, "
|
||||
"timestamp: %" PRId64,
|
||||
driver->adapter.name, group, tag, neu_type_string(value.type),
|
||||
global_timestamp);
|
||||
nlog_debug("update and report immediately, driver: %s, "
|
||||
"group: %s, tag: %s, type: %s, "
|
||||
"timestamp: %" PRId64,
|
||||
driver->adapter.name, group, tag, neu_type_string(value.type),
|
||||
global_timestamp);
|
||||
|
||||
neu_reqresp_head_t header = {
|
||||
.type = NEU_REQRESP_TRANS_DATA,
|
||||
@@ -324,9 +370,7 @@ int neu_adapter_driver_uninit(neu_adapter_driver_t *driver)
|
||||
HASH_DEL(driver->groups, el);
|
||||
|
||||
neu_adapter_driver_try_del_tag(driver, neu_group_tag_size(el->group));
|
||||
neu_adapter_del_timer((neu_adapter_t *) driver, el->report);
|
||||
neu_event_del_timer(driver->driver_events, el->read);
|
||||
neu_event_del_timer(driver->driver_events, el->write);
|
||||
stop_group_timer(driver, el);
|
||||
if (el->grp.group_free != NULL) {
|
||||
el->grp.group_free(&el->grp);
|
||||
}
|
||||
@@ -349,34 +393,67 @@ int neu_adapter_driver_uninit(neu_adapter_driver_t *driver)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void start_group_timer(neu_adapter_driver_t *driver, group_t *grp)
|
||||
{
|
||||
uint32_t interval = neu_group_get_interval(grp->group);
|
||||
|
||||
neu_event_timer_param_t param = {
|
||||
.second = interval / 1000,
|
||||
.millisecond = interval % 1000,
|
||||
.usr_data = (void *) grp,
|
||||
.type = NEU_EVENT_TIMER_NOBLOCK,
|
||||
};
|
||||
|
||||
param.type = driver->adapter.module->timer_type;
|
||||
param.cb = read_callback;
|
||||
grp->read = neu_event_add_timer(driver->driver_events, param);
|
||||
|
||||
struct timespec t1 = {
|
||||
.tv_sec = 0,
|
||||
.tv_nsec = 1000 * 1000 * 20,
|
||||
};
|
||||
struct timespec t2 = { 0 };
|
||||
nanosleep(&t1, &t2);
|
||||
|
||||
param.type = NEU_EVENT_TIMER_NOBLOCK;
|
||||
param.cb = report_callback;
|
||||
grp->report = neu_adapter_add_timer((neu_adapter_t *) driver, param);
|
||||
|
||||
param.second = 0;
|
||||
param.millisecond = 3;
|
||||
param.cb = write_callback;
|
||||
grp->write = neu_event_add_timer(driver->driver_events, param);
|
||||
}
|
||||
|
||||
void neu_adapter_driver_start_group_timer(neu_adapter_driver_t *driver)
|
||||
{
|
||||
group_t *el = NULL, *tmp = NULL;
|
||||
|
||||
HASH_ITER(hh, driver->groups, el, tmp)
|
||||
{
|
||||
uint32_t interval = neu_group_get_interval(el->group);
|
||||
neu_event_timer_param_t param = {
|
||||
.second = interval / 1000,
|
||||
.millisecond = interval % 1000,
|
||||
.usr_data = el,
|
||||
.type = NEU_EVENT_TIMER_NOBLOCK,
|
||||
};
|
||||
start_group_timer(driver, el);
|
||||
neu_adapter_update_group_metric(
|
||||
&driver->adapter, neu_group_get_name(el->group),
|
||||
NEU_METRIC_GROUP_TAGS_TOTAL, neu_group_tag_size(el->group));
|
||||
}
|
||||
|
||||
param.type = driver->adapter.module->timer_type;
|
||||
param.cb = read_callback;
|
||||
el->read = neu_event_add_timer(driver->driver_events, param);
|
||||
driver->adapter.cb_funs.update_metric(
|
||||
&driver->adapter, NEU_METRIC_TAGS_TOTAL, driver->tag_cnt, NULL);
|
||||
}
|
||||
|
||||
struct timespec t1 = {
|
||||
.tv_sec = 0,
|
||||
.tv_nsec = 1000 * 1000 * 20,
|
||||
};
|
||||
struct timespec t2 = { 0 };
|
||||
nanosleep(&t1, &t2);
|
||||
|
||||
param.type = NEU_EVENT_TIMER_NOBLOCK;
|
||||
param.cb = report_callback;
|
||||
el->report = neu_adapter_add_timer((neu_adapter_t *) driver, param);
|
||||
static inline void stop_group_timer(neu_adapter_driver_t *driver, group_t *grp)
|
||||
{
|
||||
if (grp->report) {
|
||||
neu_adapter_del_timer((neu_adapter_t *) driver, grp->report);
|
||||
grp->report = NULL;
|
||||
}
|
||||
if (grp->read) {
|
||||
neu_event_del_timer(driver->driver_events, grp->read);
|
||||
grp->read = NULL;
|
||||
}
|
||||
if (grp->write) {
|
||||
neu_event_del_timer(driver->driver_events, grp->write);
|
||||
grp->write = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -384,13 +461,7 @@ void neu_adapter_driver_stop_group_timer(neu_adapter_driver_t *driver)
|
||||
{
|
||||
group_t *el = NULL, *tmp = NULL;
|
||||
|
||||
HASH_ITER(hh, driver->groups, el, tmp)
|
||||
{
|
||||
neu_adapter_del_timer((neu_adapter_t *) driver, el->report);
|
||||
el->report = NULL;
|
||||
neu_event_del_timer(driver->driver_events, el->read);
|
||||
el->read = NULL;
|
||||
}
|
||||
HASH_ITER(hh, driver->groups, el, tmp) { stop_group_timer(driver, el); }
|
||||
}
|
||||
|
||||
void neu_adapter_driver_read_group(neu_adapter_driver_t *driver,
|
||||
@@ -466,12 +537,112 @@ void neu_adapter_driver_read_group(neu_adapter_driver_t *driver,
|
||||
driver->adapter.cb_funs.response(&driver->adapter, req, &resp);
|
||||
}
|
||||
|
||||
void neu_adapter_driver_read_group_paginate(neu_adapter_driver_t *driver,
|
||||
neu_reqresp_head_t * req)
|
||||
{
|
||||
neu_req_read_group_paginate_t *cmd =
|
||||
(neu_req_read_group_paginate_t *) &req[1];
|
||||
group_t *g = find_group(driver, cmd->group);
|
||||
if (g == NULL) {
|
||||
neu_resp_error_t error = { .error = NEU_ERR_GROUP_NOT_EXIST };
|
||||
req->type = NEU_RESP_ERROR;
|
||||
neu_req_read_group_paginate_fini(cmd);
|
||||
driver->adapter.cb_funs.response(&driver->adapter, req, &error);
|
||||
return;
|
||||
}
|
||||
|
||||
neu_resp_read_group_paginate_t resp = { 0 };
|
||||
neu_group_t * group = g->group;
|
||||
UT_array *tags = neu_group_query_read_tag(group, cmd->name, cmd->desc);
|
||||
|
||||
utarray_new(resp.tags, neu_resp_tag_value_meta_paginate_icd());
|
||||
|
||||
if (driver->adapter.state != NEU_NODE_RUNNING_STATE_RUNNING) {
|
||||
utarray_foreach(tags, neu_datatag_t *, tag)
|
||||
{
|
||||
neu_resp_tag_value_meta_paginate_t tag_value = { 0 };
|
||||
strcpy(tag_value.tag, tag->name);
|
||||
tag_value.value.type = NEU_TYPE_ERROR;
|
||||
tag_value.value.value.i32 = NEU_ERR_PLUGIN_NOT_RUNNING;
|
||||
|
||||
tag_value.datatag.name = strdup(tag->name);
|
||||
tag_value.datatag.address = strdup(tag->address);
|
||||
tag_value.datatag.attribute = tag->attribute;
|
||||
tag_value.datatag.type = tag->type;
|
||||
tag_value.datatag.precision = tag->precision;
|
||||
tag_value.datatag.decimal = tag->decimal;
|
||||
tag_value.datatag.description = strdup(tag->description);
|
||||
tag_value.datatag.option = tag->option;
|
||||
memcpy(tag_value.datatag.meta, tag->meta, NEU_TAG_META_LENGTH);
|
||||
|
||||
utarray_push_back(resp.tags, &tag_value);
|
||||
}
|
||||
} else if (cmd->sync) {
|
||||
if (NULL == driver->adapter.module->intf_funs->driver.group_sync) {
|
||||
// plugin does not support sync read
|
||||
utarray_foreach(tags, neu_datatag_t *, tag)
|
||||
{
|
||||
neu_resp_tag_value_meta_paginate_t tag_value = { 0 };
|
||||
strcpy(tag_value.tag, tag->name);
|
||||
tag_value.value.type = NEU_TYPE_ERROR;
|
||||
tag_value.value.value.i32 =
|
||||
NEU_ERR_PLUGIN_NOT_SUPPORT_READ_SYNC;
|
||||
|
||||
tag_value.datatag.name = strdup(tag->name);
|
||||
tag_value.datatag.address = strdup(tag->address);
|
||||
tag_value.datatag.attribute = tag->attribute;
|
||||
tag_value.datatag.type = tag->type;
|
||||
tag_value.datatag.precision = tag->precision;
|
||||
tag_value.datatag.decimal = tag->decimal;
|
||||
tag_value.datatag.description = strdup(tag->description);
|
||||
tag_value.datatag.option = tag->option;
|
||||
memcpy(tag_value.datatag.meta, tag->meta, NEU_TAG_META_LENGTH);
|
||||
|
||||
utarray_push_back(resp.tags, &tag_value);
|
||||
}
|
||||
} else {
|
||||
// sync read to update cache
|
||||
driver->adapter.module->intf_funs->driver.group_sync(
|
||||
driver->adapter.plugin, &g->grp);
|
||||
// fetch updated data from cache
|
||||
read_group_paginate(
|
||||
global_timestamp,
|
||||
neu_group_get_interval(group) *
|
||||
NEU_DRIVER_TAG_CACHE_EXPIRE_TIME,
|
||||
neu_adapter_get_tag_cache_type(&driver->adapter), driver->cache,
|
||||
cmd->group, tags, resp.tags);
|
||||
}
|
||||
} else {
|
||||
read_group_paginate(global_timestamp,
|
||||
neu_group_get_interval(group) *
|
||||
NEU_DRIVER_TAG_CACHE_EXPIRE_TIME,
|
||||
neu_adapter_get_tag_cache_type(&driver->adapter),
|
||||
driver->cache, cmd->group, tags, resp.tags);
|
||||
}
|
||||
|
||||
resp.driver = cmd->driver;
|
||||
resp.group = cmd->group;
|
||||
resp.current_page = cmd->current_page;
|
||||
resp.page_size = cmd->page_size;
|
||||
resp.is_error = cmd->is_error;
|
||||
cmd->driver = NULL; // ownership moved
|
||||
cmd->group = NULL; // ownership moved
|
||||
|
||||
utarray_free(tags);
|
||||
neu_req_read_group_paginate_fini(cmd);
|
||||
|
||||
req->type = NEU_RESP_READ_GROUP_PAGINATE;
|
||||
driver->adapter.cb_funs.response(&driver->adapter, req, &resp);
|
||||
}
|
||||
|
||||
static void fix_value(neu_datatag_t *tag, neu_type_e value_type,
|
||||
neu_dvalue_t *value)
|
||||
{
|
||||
switch (tag->type) {
|
||||
case NEU_TYPE_BOOL:
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
case NEU_TYPE_BYTES:
|
||||
break;
|
||||
case NEU_TYPE_BIT:
|
||||
@@ -637,26 +808,6 @@ static void cal_decimal(neu_type_e tag_type, neu_type_e value_type,
|
||||
}
|
||||
}
|
||||
|
||||
bool is_value_in_range(neu_type_e type, int64_t value)
|
||||
{
|
||||
switch (type) {
|
||||
case NEU_TYPE_INT8:
|
||||
return value >= INT8_MIN && value <= INT8_MAX;
|
||||
case NEU_TYPE_UINT8:
|
||||
return value >= 0 && value <= UINT8_MAX;
|
||||
case NEU_TYPE_INT16:
|
||||
return value >= INT16_MIN && value <= INT16_MAX;
|
||||
case NEU_TYPE_UINT16:
|
||||
return value >= 0 && value <= UINT16_MAX;
|
||||
case NEU_TYPE_INT32:
|
||||
return value >= INT32_MIN && value <= INT32_MAX;
|
||||
case NEU_TYPE_UINT32:
|
||||
return value >= 0 && value <= UINT32_MAX;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void neu_adapter_driver_write_tags(neu_adapter_driver_t *driver,
|
||||
neu_reqresp_head_t * req)
|
||||
{
|
||||
@@ -688,8 +839,7 @@ void neu_adapter_driver_write_tags(neu_adapter_driver_t *driver,
|
||||
for (int i = 0; i < cmd->n_tag; i++) {
|
||||
neu_datatag_t *tag = neu_group_find_tag(g->group, cmd->tags[i].tag);
|
||||
if (tag != NULL && neu_tag_attribute_test(tag, NEU_ATTRIBUTE_WRITE) &&
|
||||
neu_tag_attribute_test(tag, NEU_ATTRIBUTE_STATIC) == false &&
|
||||
is_value_in_range(tag->type, cmd->tags[i].value.value.i64)) {
|
||||
neu_tag_attribute_test(tag, NEU_ATTRIBUTE_STATIC) == false) {
|
||||
if (tag->type == NEU_TYPE_FLOAT || tag->type == NEU_TYPE_DOUBLE) {
|
||||
if (cmd->tags[i].value.type == NEU_TYPE_INT64) {
|
||||
cmd->tags[i].value.value.d64 =
|
||||
@@ -776,9 +926,7 @@ void neu_adapter_driver_write_gtags(neu_adapter_driver_t *driver,
|
||||
neu_group_find_tag(g->group, cmd->groups[i].tags[k].tag);
|
||||
if (tag != NULL &&
|
||||
neu_tag_attribute_test(tag, NEU_ATTRIBUTE_WRITE) &&
|
||||
neu_tag_attribute_test(tag, NEU_ATTRIBUTE_STATIC) == false &&
|
||||
is_value_in_range(tag->type,
|
||||
cmd->groups[i].tags[k].value.value.i64)) {
|
||||
neu_tag_attribute_test(tag, NEU_ATTRIBUTE_STATIC) == false) {
|
||||
|
||||
if (tag->type == NEU_TYPE_FLOAT ||
|
||||
tag->type == NEU_TYPE_DOUBLE) {
|
||||
@@ -860,12 +1008,6 @@ void neu_adapter_driver_write_tag(neu_adapter_driver_t *driver,
|
||||
neu_tag_free(tag);
|
||||
return;
|
||||
}
|
||||
if (!is_value_in_range(tag->type, cmd->value.value.i64)) {
|
||||
driver->adapter.cb_funs.driver.write_response(
|
||||
&driver->adapter, req, NEU_ERR_TAG_VALUE_INVALID);
|
||||
neu_tag_free(tag);
|
||||
return;
|
||||
}
|
||||
if (tag->type == NEU_TYPE_FLOAT || tag->type == NEU_TYPE_DOUBLE) {
|
||||
if (cmd->value.type == NEU_TYPE_INT64) {
|
||||
cmd->value.value.d64 = (double) cmd->value.value.i64;
|
||||
@@ -917,13 +1059,6 @@ int neu_adapter_driver_add_group(neu_adapter_driver_t *driver, const char *name,
|
||||
if (find == NULL) {
|
||||
find = calloc(1, sizeof(group_t));
|
||||
|
||||
neu_event_timer_param_t param = {
|
||||
.second = interval / 1000,
|
||||
.millisecond = interval % 1000,
|
||||
.usr_data = (void *) find,
|
||||
.type = NEU_EVENT_TIMER_NOBLOCK,
|
||||
};
|
||||
|
||||
pthread_mutex_init(&find->wt_mtx, NULL);
|
||||
pthread_mutex_init(&find->apps_mtx, NULL);
|
||||
|
||||
@@ -934,28 +1069,13 @@ int neu_adapter_driver_add_group(neu_adapter_driver_t *driver, const char *name,
|
||||
find->name = strdup(name);
|
||||
find->group = neu_group_new(name, interval);
|
||||
find->grp.group_name = strdup(name);
|
||||
find->grp.interval = interval;
|
||||
neu_group_split_static_tags(find->group, &find->static_tags,
|
||||
&find->grp.tags);
|
||||
|
||||
param.type = driver->adapter.module->timer_type;
|
||||
param.cb = read_callback;
|
||||
find->read = neu_event_add_timer(driver->driver_events, param);
|
||||
|
||||
struct timespec t1 = {
|
||||
.tv_sec = 0,
|
||||
.tv_nsec = 1000 * 1000 * 20,
|
||||
};
|
||||
struct timespec t2 = { 0 };
|
||||
nanosleep(&t1, &t2);
|
||||
|
||||
param.type = NEU_EVENT_TIMER_NOBLOCK;
|
||||
param.cb = report_callback;
|
||||
find->report = neu_adapter_add_timer((neu_adapter_t *) driver, param);
|
||||
|
||||
param.second = 0;
|
||||
param.millisecond = 3;
|
||||
param.cb = write_callback;
|
||||
find->write = neu_event_add_timer(driver->driver_events, param);
|
||||
if (NEU_NODE_RUNNING_STATE_RUNNING == driver->adapter.state) {
|
||||
start_group_timer(driver, find);
|
||||
}
|
||||
|
||||
REGISTER_GROUP_METRIC(&driver->adapter, find->name,
|
||||
NEU_METRIC_GROUP_TAGS_TOTAL,
|
||||
@@ -999,11 +1119,14 @@ int neu_adapter_driver_update_group(neu_adapter_driver_t *driver,
|
||||
return NEU_ERR_GROUP_EXIST;
|
||||
}
|
||||
}
|
||||
} else if (neu_group_get_interval(find->group) == interval) {
|
||||
return 0; // no change
|
||||
}
|
||||
|
||||
// stop the timer first to avoid race condition
|
||||
neu_adapter_del_timer((neu_adapter_t *) driver, find->report);
|
||||
neu_event_del_timer(driver->driver_events, find->read);
|
||||
if (NEU_NODE_RUNNING_STATE_RUNNING == driver->adapter.state) {
|
||||
stop_group_timer(driver, find);
|
||||
}
|
||||
|
||||
// a diminutive value should keep the interval untouched
|
||||
if (interval < NEU_GROUP_INTERVAL_LIMIT) {
|
||||
@@ -1023,7 +1146,6 @@ int neu_adapter_driver_update_group(neu_adapter_driver_t *driver,
|
||||
find->grp.group_name = new_name_cp2;
|
||||
neu_adapter_metric_update_group_name((neu_adapter_t *) driver, name,
|
||||
new_name);
|
||||
find->timestamp = global_timestamp; // trigger group_change
|
||||
HASH_ADD_STR(driver->groups, name, find);
|
||||
} else {
|
||||
free(new_name_cp1);
|
||||
@@ -1032,31 +1154,14 @@ int neu_adapter_driver_update_group(neu_adapter_driver_t *driver,
|
||||
}
|
||||
}
|
||||
|
||||
neu_event_timer_param_t param = {
|
||||
.second = interval / 1000,
|
||||
.millisecond = interval % 1000,
|
||||
.usr_data = (void *) find,
|
||||
.type = NEU_EVENT_TIMER_NOBLOCK,
|
||||
};
|
||||
|
||||
find->timestamp = global_timestamp; // trigger group_change
|
||||
find->grp.interval = interval;
|
||||
neu_group_set_interval(find->group, interval);
|
||||
|
||||
// restore the timers
|
||||
|
||||
param.type = driver->adapter.module->timer_type;
|
||||
param.cb = read_callback;
|
||||
find->read = neu_event_add_timer(driver->driver_events, param);
|
||||
|
||||
struct timespec t1 = {
|
||||
.tv_sec = 0,
|
||||
.tv_nsec = 1000 * 1000 * 20,
|
||||
};
|
||||
struct timespec t2 = { 0 };
|
||||
nanosleep(&t1, &t2);
|
||||
|
||||
param.type = NEU_EVENT_TIMER_NOBLOCK;
|
||||
param.cb = report_callback;
|
||||
find->report = neu_adapter_add_timer((neu_adapter_t *) driver, param);
|
||||
if (NEU_NODE_RUNNING_STATE_RUNNING == driver->adapter.state) {
|
||||
start_group_timer(driver, find);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1072,9 +1177,10 @@ int neu_adapter_driver_del_group(neu_adapter_driver_t *driver, const char *name)
|
||||
|
||||
neu_adapter_driver_try_del_tag(driver, neu_group_tag_size(find->group));
|
||||
|
||||
neu_adapter_del_timer((neu_adapter_t *) driver, find->report);
|
||||
neu_event_del_timer(driver->driver_events, find->read);
|
||||
neu_event_del_timer(driver->driver_events, find->write);
|
||||
if (NEU_NODE_RUNNING_STATE_RUNNING == driver->adapter.state) {
|
||||
stop_group_timer(driver, find);
|
||||
}
|
||||
|
||||
if (find->grp.group_free != NULL) {
|
||||
find->grp.group_free(&find->grp);
|
||||
}
|
||||
@@ -1620,6 +1726,7 @@ static void group_change(void *arg, int64_t timestamp, UT_array *static_tags,
|
||||
|
||||
neu_plugin_group_t grp = {
|
||||
.group_name = strdup(group->name),
|
||||
.interval = neu_group_get_interval(group->group),
|
||||
.tags = NULL,
|
||||
.group_free = NULL,
|
||||
.user_data = NULL,
|
||||
@@ -1716,7 +1823,7 @@ static void read_report_group(int64_t timestamp, int64_t timeout,
|
||||
if (neu_driver_cache_meta_get_changed(cache, group, tag->name,
|
||||
&value, tag_value.metas,
|
||||
NEU_TAG_META_SIZE) != 0) {
|
||||
nlog_info("tag: %s not changed", tag->name);
|
||||
nlog_debug("tag: %s not changed", tag->name);
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
@@ -1740,6 +1847,14 @@ static void read_report_group(int64_t timestamp, int64_t timeout,
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((tag->type == NEU_TYPE_FLOAT && isnan(value.value.value.f32)) ||
|
||||
(tag->type == NEU_TYPE_DOUBLE && isnan(value.value.value.d64))) {
|
||||
tag_value.value.type = NEU_TYPE_ERROR;
|
||||
tag_value.value.value.i32 = NEU_ERR_PLUGIN_TAG_VALUE_EXPIRED;
|
||||
utarray_push_back(tag_values, &tag_value);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (tag->type) {
|
||||
case NEU_TYPE_UINT16:
|
||||
case NEU_TYPE_INT16:
|
||||
@@ -1861,6 +1976,9 @@ static void read_report_group(int64_t timestamp, int64_t timeout,
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (tag->precision == 0 && tag->type == NEU_TYPE_DOUBLE) {
|
||||
format_tag_value(&tag_value.value);
|
||||
}
|
||||
}
|
||||
|
||||
utarray_push_back(tag_values, &tag_value);
|
||||
@@ -2016,6 +2134,178 @@ static void read_group(int64_t timestamp, int64_t timeout,
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (tag->precision == 0 && tag->type == NEU_TYPE_DOUBLE) {
|
||||
format_tag_value(&tag_value.value);
|
||||
}
|
||||
}
|
||||
utarray_push_back(tag_values, &tag_value);
|
||||
}
|
||||
}
|
||||
|
||||
static void read_group_paginate(int64_t timestamp, int64_t timeout,
|
||||
neu_tag_cache_type_e cache_type,
|
||||
neu_driver_cache_t *cache, const char *group,
|
||||
UT_array *tags, UT_array *tag_values)
|
||||
{
|
||||
utarray_foreach(tags, neu_datatag_t *, tag)
|
||||
{
|
||||
neu_resp_tag_value_meta_paginate_t tag_value = { 0 };
|
||||
neu_driver_cache_value_t value = { 0 };
|
||||
|
||||
strcpy(tag_value.tag, tag->name);
|
||||
|
||||
tag_value.datatag.name = strdup(tag->name);
|
||||
tag_value.datatag.address = strdup(tag->address);
|
||||
tag_value.datatag.attribute = tag->attribute;
|
||||
tag_value.datatag.type = tag->type;
|
||||
tag_value.datatag.precision = tag->precision;
|
||||
tag_value.datatag.decimal = tag->decimal;
|
||||
tag_value.datatag.description = strdup(tag->description);
|
||||
tag_value.datatag.option = tag->option;
|
||||
memcpy(tag_value.datatag.meta, tag->meta, NEU_TAG_META_LENGTH);
|
||||
|
||||
if (neu_driver_cache_meta_get(cache, group, tag->name, &value,
|
||||
tag_value.metas,
|
||||
NEU_TAG_META_SIZE) != 0) {
|
||||
tag_value.value.type = NEU_TYPE_ERROR;
|
||||
tag_value.value.value.i32 = NEU_ERR_PLUGIN_TAG_NOT_READY;
|
||||
|
||||
utarray_push_back(tag_values, &tag_value);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (value.value.type == NEU_TYPE_ERROR) {
|
||||
tag_value.value = value.value;
|
||||
utarray_push_back(tag_values, &tag_value);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (tag->type) {
|
||||
case NEU_TYPE_UINT16:
|
||||
case NEU_TYPE_INT16:
|
||||
switch (tag->option.value16.endian) {
|
||||
case NEU_DATATAG_ENDIAN_B16:
|
||||
value.value.value.u16 = htons(value.value.value.u16);
|
||||
break;
|
||||
case NEU_DATATAG_ENDIAN_L16:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case NEU_TYPE_FLOAT:
|
||||
case NEU_TYPE_UINT32:
|
||||
case NEU_TYPE_INT32:
|
||||
switch (tag->option.value32.endian) {
|
||||
case NEU_DATATAG_ENDIAN_LB32: {
|
||||
uint16_t *v1 = (uint16_t *) value.value.value.bytes.bytes;
|
||||
uint16_t *v2 = (uint16_t *) (value.value.value.bytes.bytes + 2);
|
||||
|
||||
neu_htons_p(v1);
|
||||
neu_htons_p(v2);
|
||||
break;
|
||||
}
|
||||
case NEU_DATATAG_ENDIAN_BB32:
|
||||
value.value.value.u32 = htonl(value.value.value.u32);
|
||||
break;
|
||||
case NEU_DATATAG_ENDIAN_BL32:
|
||||
value.value.value.u32 = htonl(value.value.value.u32);
|
||||
uint16_t *v1 = (uint16_t *) value.value.value.bytes.bytes;
|
||||
uint16_t *v2 = (uint16_t *) (value.value.value.bytes.bytes + 2);
|
||||
|
||||
neu_htons_p(v1);
|
||||
neu_htons_p(v2);
|
||||
break;
|
||||
case NEU_DATATAG_ENDIAN_LL32:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case NEU_TYPE_DOUBLE:
|
||||
case NEU_TYPE_INT64:
|
||||
case NEU_TYPE_UINT64:
|
||||
switch (tag->option.value64.endian) {
|
||||
case NEU_DATATAG_ENDIAN_B64:
|
||||
value.value.value.u64 = neu_htonll(value.value.value.u64);
|
||||
break;
|
||||
case NEU_DATATAG_ENDIAN_L64:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (cache_type != NEU_TAG_CACHE_TYPE_NEVER &&
|
||||
!neu_tag_attribute_test(tag, NEU_ATTRIBUTE_STATIC) &&
|
||||
(timestamp - value.timestamp) > timeout) {
|
||||
if (value.value.type == NEU_TYPE_PTR) {
|
||||
free(value.value.value.ptr.ptr);
|
||||
}
|
||||
tag_value.value.type = NEU_TYPE_ERROR;
|
||||
tag_value.value.value.i32 = NEU_ERR_PLUGIN_TAG_VALUE_EXPIRED;
|
||||
} else {
|
||||
if (value.value.type == NEU_TYPE_PTR) {
|
||||
tag_value.value.type = NEU_TYPE_PTR;
|
||||
tag_value.value.value.ptr.length = value.value.value.ptr.length;
|
||||
tag_value.value.value.ptr.type = value.value.value.ptr.type;
|
||||
tag_value.value.value.ptr.ptr = value.value.value.ptr.ptr;
|
||||
} else {
|
||||
tag_value.value = value.value;
|
||||
}
|
||||
if (tag->decimal != 0) {
|
||||
tag_value.value.type = NEU_TYPE_DOUBLE;
|
||||
switch (tag->type) {
|
||||
case NEU_TYPE_INT8:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.i8 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_UINT8:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.u8 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_INT16:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.i16 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_UINT16:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.u16 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_INT32:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.i32 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_UINT32:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.u32 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_INT64:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.i64 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_UINT64:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.u64 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_FLOAT:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.f32 * tag->decimal;
|
||||
break;
|
||||
case NEU_TYPE_DOUBLE:
|
||||
tag_value.value.value.d64 =
|
||||
(double) tag_value.value.value.d64 * tag->decimal;
|
||||
break;
|
||||
default:
|
||||
tag_value.value.type = tag->type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (tag->precision == 0 && tag->type == NEU_TYPE_DOUBLE) {
|
||||
format_tag_value(&tag_value.value);
|
||||
}
|
||||
}
|
||||
utarray_push_back(tag_values, &tag_value);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ void neu_adapter_driver_stop_group_timer(neu_adapter_driver_t *driver);
|
||||
|
||||
void neu_adapter_driver_read_group(neu_adapter_driver_t *driver,
|
||||
neu_reqresp_head_t * req);
|
||||
void neu_adapter_driver_read_group_paginate(neu_adapter_driver_t *driver,
|
||||
neu_reqresp_head_t * req);
|
||||
|
||||
void neu_adapter_driver_write_tag(neu_adapter_driver_t *driver,
|
||||
neu_reqresp_head_t * req);
|
||||
|
||||
+47
-6
@@ -212,16 +212,17 @@ static unsigned cpu_usage()
|
||||
return (double) work / total * 100.0 * sysconf(_SC_NPROCESSORS_CONF);
|
||||
}
|
||||
|
||||
static bool has_core_dumps()
|
||||
static bool has_core_dump_in_dir(const char *dir, const char *prefix)
|
||||
{
|
||||
DIR *dp = opendir("core");
|
||||
if (NULL == dp) {
|
||||
DIR *dp = opendir(dir);
|
||||
if (dp == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
for (struct dirent *de = NULL; NULL != (de = readdir(dp));) {
|
||||
if (0 == strncmp("core", de->d_name, 4)) {
|
||||
struct dirent *de;
|
||||
bool found = false;
|
||||
while ((de = readdir(dp)) != NULL) {
|
||||
if (strncmp(prefix, de->d_name, strlen(prefix)) == 0) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
@@ -231,6 +232,46 @@ static bool has_core_dumps()
|
||||
return found;
|
||||
}
|
||||
|
||||
static char *get_core_dir(const char *core_pattern)
|
||||
{
|
||||
char *last_slash = strrchr(core_pattern, '/');
|
||||
if (last_slash != NULL) {
|
||||
static char core_dir[256];
|
||||
ptrdiff_t path_length = last_slash - core_pattern + 1;
|
||||
strncpy(core_dir, core_pattern, path_length);
|
||||
core_dir[path_length] = '\0';
|
||||
return core_dir;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool has_core_dumps()
|
||||
{
|
||||
if (has_core_dump_in_dir("core", "core-neuron")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
FILE *fp = fopen("/proc/sys/kernel/core_pattern", "r");
|
||||
if (fp == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char core_pattern[256];
|
||||
if (fgets(core_pattern, sizeof(core_pattern), fp) == NULL) {
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
char *core_dir =
|
||||
(core_pattern[0] == '|') ? "/var/crash/" : get_core_dir(core_pattern);
|
||||
if (core_dir == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return has_core_dump_in_dir(core_dir, "core-neuron");
|
||||
}
|
||||
|
||||
static inline void metrics_unregister_entry(const char *name)
|
||||
{
|
||||
neu_metric_entry_t *e = NULL;
|
||||
|
||||
@@ -34,6 +34,8 @@ extern "C" {
|
||||
XX(NEU_RESP_ERROR, neu_resp_error_t) \
|
||||
XX(NEU_REQ_READ_GROUP, neu_req_read_group_t) \
|
||||
XX(NEU_RESP_READ_GROUP, neu_resp_read_group_t) \
|
||||
XX(NEU_REQ_READ_GROUP_PAGINATE, neu_req_read_group_paginate_t) \
|
||||
XX(NEU_RESP_READ_GROUP_PAGINATE, neu_resp_read_group_paginate_t) \
|
||||
XX(NEU_REQ_WRITE_TAG, neu_req_write_tag_t) \
|
||||
XX(NEU_REQ_WRITE_TAGS, neu_req_write_tags_t) \
|
||||
XX(NEU_REQ_WRITE_GTAGS, neu_req_write_gtags_t) \
|
||||
|
||||
+29
-12
@@ -161,19 +161,24 @@ int neu_datatag_parse_addr_option(const neu_datatag_t * datatag,
|
||||
|
||||
switch (t) {
|
||||
case 'H':
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_H;
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_H;
|
||||
option->string.is_default = false;
|
||||
break;
|
||||
case 'L':
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_L;
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_L;
|
||||
option->string.is_default = false;
|
||||
break;
|
||||
case 'D':
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_D;
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_D;
|
||||
option->string.is_default = false;
|
||||
break;
|
||||
case 'E':
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_D;
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_E;
|
||||
option->string.is_default = false;
|
||||
break;
|
||||
default:
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_H;
|
||||
option->string.type = NEU_DATATAG_STRING_TYPE_H;
|
||||
option->string.is_default = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -210,10 +215,13 @@ int neu_datatag_parse_addr_option(const neu_datatag_t * datatag,
|
||||
}
|
||||
case NEU_TYPE_FLOAT:
|
||||
case NEU_TYPE_INT32:
|
||||
case NEU_TYPE_UINT32: {
|
||||
case NEU_TYPE_UINT32:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
case NEU_TYPE_TIME: {
|
||||
char *op = find_last_character(datatag->address, '#');
|
||||
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_LL32;
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_LL32;
|
||||
option->value32.is_default = true;
|
||||
if (op != NULL) {
|
||||
char e1 = 0;
|
||||
char e2 = 0;
|
||||
@@ -221,16 +229,20 @@ int neu_datatag_parse_addr_option(const neu_datatag_t * datatag,
|
||||
|
||||
if (n == 2) {
|
||||
if (e1 == 'B' && e2 == 'B') {
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_BB32;
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_BB32;
|
||||
option->value32.is_default = false;
|
||||
}
|
||||
if (e1 == 'B' && e2 == 'L') {
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_BL32;
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_BL32;
|
||||
option->value32.is_default = false;
|
||||
}
|
||||
if (e1 == 'L' && e2 == 'L') {
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_LL32;
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_LL32;
|
||||
option->value32.is_default = false;
|
||||
}
|
||||
if (e1 == 'L' && e2 == 'B') {
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_LB32;
|
||||
option->value32.endian = NEU_DATATAG_ENDIAN_LB32;
|
||||
option->value32.is_default = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -523,6 +535,8 @@ int neu_tag_get_static_value_json(neu_datatag_t *tag, neu_json_type_e *t,
|
||||
v->val_double = value->d64;
|
||||
break;
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
*t = NEU_JSON_STR;
|
||||
v->val_str = value->str;
|
||||
break;
|
||||
@@ -625,7 +639,8 @@ int neu_tag_set_static_value_json(neu_datatag_t *tag, neu_json_type_e t,
|
||||
break;
|
||||
}
|
||||
case NEU_JSON_STR: {
|
||||
if (NEU_TYPE_STRING == tag->type) {
|
||||
if (NEU_TYPE_STRING == tag->type || NEU_TYPE_TIME == tag->type ||
|
||||
NEU_TYPE_DATA_AND_TIME == tag->type) {
|
||||
snprintf(value.str, sizeof(value.str), "%s", v->val_str);
|
||||
} else {
|
||||
rv = -1;
|
||||
@@ -695,6 +710,8 @@ char *neu_tag_dump_static_value(const neu_datatag_t *tag)
|
||||
jval = json_real(value.d64);
|
||||
break;
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
jval = json_string(value.str);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -125,7 +125,9 @@ void neu_conn_stop(neu_conn_t *conn)
|
||||
void neu_conn_start(neu_conn_t *conn)
|
||||
{
|
||||
pthread_mutex_lock(&conn->mtx);
|
||||
conn->stop = false;
|
||||
conn->state.recv_bytes = 0;
|
||||
conn->state.send_bytes = 0;
|
||||
conn->stop = false;
|
||||
pthread_mutex_unlock(&conn->mtx);
|
||||
}
|
||||
|
||||
@@ -1290,6 +1292,12 @@ int neu_conn_stream_consume(neu_conn_t *conn, void *context,
|
||||
break;
|
||||
} else {
|
||||
pthread_mutex_lock(&conn->mtx);
|
||||
if (conn->offset - used < 0) {
|
||||
pthread_mutex_unlock(&conn->mtx);
|
||||
zlog_warn(conn->param.log, "reset offset: %d, used: %d",
|
||||
conn->offset, used);
|
||||
return -1;
|
||||
}
|
||||
conn->offset -= used;
|
||||
memmove(conn->buf, conn->buf + used, conn->offset);
|
||||
neu_protocol_unpack_buf_init(&protocol_buf, conn->buf,
|
||||
@@ -1320,10 +1328,18 @@ int neu_conn_stream_tcp_server_consume(neu_conn_t *conn, int fd, void *context,
|
||||
neu_conn_tcp_server_close_client(conn, fd);
|
||||
break;
|
||||
} else {
|
||||
pthread_mutex_lock(&conn->mtx);
|
||||
if (conn->offset - used < 0) {
|
||||
pthread_mutex_unlock(&conn->mtx);
|
||||
zlog_warn(conn->param.log, "reset offset: %d, used: %d",
|
||||
conn->offset, used);
|
||||
return -1;
|
||||
}
|
||||
conn->offset -= used;
|
||||
memmove(conn->buf, conn->buf + used, conn->offset);
|
||||
neu_protocol_unpack_buf_init(&protocol_buf, conn->buf,
|
||||
conn->offset);
|
||||
pthread_mutex_unlock(&conn->mtx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include <nng/supplemental/util/platform.h>
|
||||
|
||||
#include "connection/mqtt_client.h"
|
||||
#include "errcodes.h"
|
||||
#include "event/event.h"
|
||||
#include "utils/asprintf.h"
|
||||
#include "utils/time.h"
|
||||
@@ -179,6 +180,93 @@ static inline uint8_t neu_mqtt_version_to_nng_mqtt_version(neu_mqtt_version_e v)
|
||||
}
|
||||
}
|
||||
|
||||
bool neu_mqtt_topic_filter_is_valid(const char *topic_filter)
|
||||
{
|
||||
if (NULL == topic_filter || '\0' == *topic_filter ||
|
||||
strlen(topic_filter) > 65535) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char c;
|
||||
while ((c = *topic_filter++)) {
|
||||
switch (c) {
|
||||
case '#':
|
||||
// '#' must be last character
|
||||
return '\0' == *topic_filter;
|
||||
case '+':
|
||||
// '+' must occupy an entire level
|
||||
if (*topic_filter && '/' != *topic_filter) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
for (; '/' != c; c = *topic_filter++) {
|
||||
if ('#' == c || '+' == c) {
|
||||
return false;
|
||||
} else if ('\0' == c) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool topic_filter_is_match(const char *topic_filter,
|
||||
const char *topic_name,
|
||||
uint32_t topic_name_len)
|
||||
{
|
||||
// The Server MUST NOT match Topic Filters starting with a wildcard
|
||||
// character (# or +) with Topic Names beginning with a $ character
|
||||
// [MQTT-4.7.2-1].
|
||||
if ('$' == topic_name[0]) {
|
||||
if ('$' != topic_filter[0]) {
|
||||
return false;
|
||||
}
|
||||
} else if (0 == strcmp("#", topic_filter)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t i = 0;
|
||||
while (true) {
|
||||
switch (*topic_filter) {
|
||||
case '#':
|
||||
return true;
|
||||
case '+':
|
||||
while (i < topic_name_len && '/' != topic_name[i]) {
|
||||
++i;
|
||||
};
|
||||
++topic_filter;
|
||||
break;
|
||||
case '/':
|
||||
if (i < topic_name_len && '/' == topic_name[i]) {
|
||||
++i;
|
||||
} else if ('#' != topic_filter[1]) { // '#' include parent level
|
||||
return false;
|
||||
}
|
||||
++topic_filter;
|
||||
break;
|
||||
case '\0':
|
||||
return i == topic_name_len;
|
||||
default:
|
||||
for (; *topic_filter && '/' != *topic_filter; ++topic_filter, ++i) {
|
||||
if (!(i < topic_name_len && *topic_filter == topic_name[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool neu_mqtt_topic_filter_is_match(const char *topic_filter,
|
||||
const char *topic_name)
|
||||
{
|
||||
return topic_filter_is_match(topic_filter, topic_name, strlen(topic_name));
|
||||
}
|
||||
|
||||
static inline task_t *task_new(neu_mqtt_client_t *client)
|
||||
{
|
||||
task_t *task = calloc(1, sizeof(*task));
|
||||
@@ -405,6 +493,26 @@ static inline void subscriptions_free(subscription_t *subscriptions)
|
||||
}
|
||||
}
|
||||
|
||||
static inline subscription_t *
|
||||
subscription_find_match(subscription_t *subscriptions, const char *topic_name,
|
||||
uint32_t topic_name_len)
|
||||
{
|
||||
subscription_t *sub = NULL, *tmp = NULL;
|
||||
HASH_FIND(hh, subscriptions, topic_name, topic_name_len, sub);
|
||||
if (NULL == sub) {
|
||||
// subscription wildcard matching
|
||||
// TODO: linear scanning is enough for current usages,
|
||||
// but may pose performance issues in the future
|
||||
HASH_ITER(hh, subscriptions, sub, tmp)
|
||||
{
|
||||
if (topic_filter_is_match(sub->topic, topic_name, topic_name_len)) {
|
||||
return sub;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sub;
|
||||
}
|
||||
|
||||
static int resub_cb(void *data)
|
||||
{
|
||||
neu_mqtt_client_t *client = data;
|
||||
@@ -517,10 +625,9 @@ static void recv_cb(void *arg)
|
||||
log(debug, "recv [%.*s, QoS%d] %" PRIu32 " bytes", (unsigned) topic_len,
|
||||
topic, (int) qos, payload_len);
|
||||
|
||||
// find matching subscription
|
||||
// NOTE: does not support wildcards
|
||||
nng_mtx_lock(client->mtx);
|
||||
HASH_FIND(hh, client->subscriptions, topic, topic_len, subscription);
|
||||
subscription =
|
||||
subscription_find_match(client->subscriptions, topic, topic_len);
|
||||
if (NULL != subscription) {
|
||||
task_t *task = client_alloc_task(client);
|
||||
if (NULL != task) {
|
||||
@@ -1375,6 +1482,10 @@ int neu_mqtt_client_subscribe(neu_mqtt_client_t *client, neu_mqtt_qos_e qos,
|
||||
int rv = 0;
|
||||
subscription_t *subscription = NULL;
|
||||
|
||||
if (!neu_mqtt_topic_filter_is_valid(topic)) {
|
||||
return NEU_ERR_MQTT_SUBSCRIBE_FAILURE;
|
||||
}
|
||||
|
||||
nng_mtx_lock(client->mtx);
|
||||
if (NULL == client->recv_aio) {
|
||||
if ((rv = nng_aio_alloc(&client->recv_aio, recv_cb, client)) != 0) {
|
||||
|
||||
+38
-13
@@ -162,7 +162,8 @@ neu_manager_t *neu_manager_create()
|
||||
|
||||
void neu_manager_destroy(neu_manager_t *manager)
|
||||
{
|
||||
neu_req_node_init_t uninit = { 0 };
|
||||
neu_req_node_init_t uninit = { 0 };
|
||||
int send_msg_success = 1;
|
||||
|
||||
UT_array *addrs = neu_node_manager_get_addrs_all(manager->node_manager);
|
||||
|
||||
@@ -176,15 +177,19 @@ void neu_manager_destroy(neu_manager_t *manager)
|
||||
if (0 != neu_send_msg_to(manager->server_fd, addr, msg)) {
|
||||
nlog_error("manager -> %s uninit msg send fail",
|
||||
&addr->sun_path[1]);
|
||||
send_msg_success = 0;
|
||||
neu_msg_free(msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
utarray_free(addrs);
|
||||
|
||||
while (1) {
|
||||
usleep(1000 * 100);
|
||||
if (neu_node_manager_size(manager->node_manager) == 0) {
|
||||
break;
|
||||
if (send_msg_success) {
|
||||
while (1) {
|
||||
usleep(1000 * 100);
|
||||
if (neu_node_manager_size(manager->node_manager) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +241,8 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
break;
|
||||
}
|
||||
|
||||
if (init->state == NEU_NODE_RUNNING_STATE_RUNNING ||
|
||||
if (init->state == NEU_NODE_RUNNING_STATE_READY ||
|
||||
init->state == NEU_NODE_RUNNING_STATE_RUNNING ||
|
||||
init->state == NEU_NODE_RUNNING_STATE_STOPPED) {
|
||||
neu_adapter_t *adapter =
|
||||
neu_node_manager_find(manager->node_manager, init->node);
|
||||
@@ -699,8 +705,11 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
}
|
||||
case NEU_REQ_ADD_NODE: {
|
||||
neu_req_add_node_t *cmd = (neu_req_add_node_t *) &header[1];
|
||||
int error = neu_manager_add_node(manager, cmd->node, cmd->plugin,
|
||||
cmd->setting, false, false);
|
||||
nlog_notice("add node name:%s plugin:%s", cmd->node, cmd->plugin);
|
||||
int error =
|
||||
neu_manager_add_node(manager, cmd->node, cmd->plugin, cmd->setting,
|
||||
NEU_NODE_RUNNING_STATE_INIT, false);
|
||||
|
||||
neu_resp_error_t e = { .error = error };
|
||||
|
||||
if (error == NEU_ERR_SUCCESS) {
|
||||
@@ -719,7 +728,8 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_UPDATE_NODE: {
|
||||
neu_req_update_node_t *cmd = (neu_req_update_node_t *) &header[1];
|
||||
neu_resp_error_t e = { 0 };
|
||||
|
||||
nlog_notice("update node name:%s new_name:%s", cmd->node,
|
||||
cmd->new_name);
|
||||
if (NULL == neu_node_manager_find(manager->node_manager, cmd->node)) {
|
||||
e.error = NEU_ERR_NODE_NOT_EXIST;
|
||||
} else if (NULL !=
|
||||
@@ -743,7 +753,8 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_DEL_NODE: {
|
||||
neu_req_del_node_t *cmd = (neu_req_del_node_t *) &header[1];
|
||||
neu_resp_error_t error = { 0 };
|
||||
neu_adapter_t * adapter =
|
||||
nlog_notice("del node name:%s", cmd->node);
|
||||
neu_adapter_t *adapter =
|
||||
neu_node_manager_find(manager->node_manager, cmd->node);
|
||||
bool single =
|
||||
neu_node_manager_is_single(manager->node_manager, cmd->node);
|
||||
@@ -841,6 +852,10 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
neu_resp_error_t error = { 0 };
|
||||
uint16_t app_port = 0;
|
||||
|
||||
nlog_notice("add sub app_name:%s driver_node:%s group:%s params:%s",
|
||||
cmd->app, cmd->driver, cmd->group,
|
||||
cmd->params != NULL ? cmd->params : "");
|
||||
|
||||
error.error = neu_manager_subscribe(manager, cmd->app, cmd->driver,
|
||||
cmd->group, cmd->params, &app_port);
|
||||
|
||||
@@ -866,7 +881,9 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
|
||||
for (uint16_t i = 0; i < cmd->n_group; ++i) {
|
||||
neu_req_subscribe_group_info_t *info = &cmd->groups[i];
|
||||
|
||||
nlog_notice("add sub app_name:%s driver_node:%s group:%s params:%s",
|
||||
cmd->app, info->driver, info->group,
|
||||
info->params != NULL ? info->params : "");
|
||||
error.error =
|
||||
neu_manager_subscribe(manager, cmd->app, info->driver,
|
||||
info->group, info->params, &info->port);
|
||||
@@ -894,7 +911,9 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_UPDATE_SUBSCRIBE_GROUP: {
|
||||
neu_req_subscribe_t *cmd = (neu_req_subscribe_t *) &header[1];
|
||||
neu_resp_error_t error = { 0 };
|
||||
|
||||
nlog_notice("update sub app_name:%s driver_node:%s group:%s params:%s",
|
||||
cmd->app, cmd->driver, cmd->group,
|
||||
cmd->params != NULL ? cmd->params : "");
|
||||
error.error = neu_manager_update_subscribe(
|
||||
manager, cmd->app, cmd->driver, cmd->group, cmd->params);
|
||||
|
||||
@@ -914,7 +933,8 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_UNSUBSCRIBE_GROUP: {
|
||||
neu_req_unsubscribe_t *cmd = (neu_req_unsubscribe_t *) &header[1];
|
||||
neu_resp_error_t error = { 0 };
|
||||
|
||||
nlog_notice("del sub app_name:%s driver_node:%s group:%s", cmd->app,
|
||||
cmd->driver, cmd->group);
|
||||
error.error =
|
||||
neu_manager_unsubscribe(manager, cmd->app, cmd->driver, cmd->group);
|
||||
|
||||
@@ -1007,6 +1027,7 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_REQ_GET_GROUP:
|
||||
case NEU_REQ_GET_NODE_SETTING:
|
||||
case NEU_REQ_READ_GROUP:
|
||||
case NEU_REQ_READ_GROUP_PAGINATE:
|
||||
case NEU_REQ_WRITE_TAG:
|
||||
case NEU_REQ_WRITE_TAGS:
|
||||
case NEU_REQ_WRITE_GTAGS:
|
||||
@@ -1017,6 +1038,9 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
NULL) {
|
||||
if (NEU_REQ_READ_GROUP == header->type) {
|
||||
neu_req_read_group_fini((neu_req_read_group_t *) &header[1]);
|
||||
} else if (NEU_REQ_READ_GROUP_PAGINATE == header->type) {
|
||||
neu_req_read_group_paginate_fini(
|
||||
(neu_req_read_group_paginate_t *) &header[1]);
|
||||
} else if (NEU_REQ_WRITE_TAG == header->type) {
|
||||
neu_req_write_tag_fini((neu_req_write_tag_t *) &header[1]);
|
||||
} else if (NEU_REQ_WRITE_TAGS == header->type) {
|
||||
@@ -1244,6 +1268,7 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
|
||||
case NEU_RESP_GET_NODE_SETTING:
|
||||
case NEU_RESP_ERROR:
|
||||
case NEU_RESP_READ_GROUP:
|
||||
case NEU_RESP_READ_GROUP_PAGINATE:
|
||||
case NEU_RESP_PRGFILE_PROCESS:
|
||||
forward_msg(manager, header, header->receiver);
|
||||
break;
|
||||
|
||||
@@ -125,7 +125,6 @@ static int neuron_run(const neu_cli_args_t *args)
|
||||
int rv = 0;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
signal(SIGABRT, sig_handler);
|
||||
signal(SIGTERM, sig_handler);
|
||||
signal(SIGKILL, sig_handler);
|
||||
|
||||
|
||||
+326
-19
@@ -43,6 +43,7 @@ int neu_json_encode_read_resp(void *json_object, void *param)
|
||||
neu_json_read_resp_tag_t *p_tag = resp->tags;
|
||||
for (int i = 0; i < resp->n_tag; i++) {
|
||||
neu_json_elem_t tag_elems[2 + NEU_TAG_META_SIZE] = { 0 };
|
||||
int if_precision = 0;
|
||||
|
||||
tag_elems[0].name = "name";
|
||||
tag_elems[0].t = NEU_JSON_STR;
|
||||
@@ -57,16 +58,24 @@ int neu_json_encode_read_resp(void *json_object, void *param)
|
||||
tag_elems[1].t = p_tag->t;
|
||||
tag_elems[1].v = p_tag->value;
|
||||
tag_elems[1].precision = p_tag->precision;
|
||||
|
||||
if (p_tag->t == NEU_JSON_FLOAT || p_tag->t == NEU_JSON_DOUBLE) {
|
||||
if_precision = 1;
|
||||
tag_elems[2].name = "transferPrecision";
|
||||
tag_elems[2].t = NEU_JSON_INT;
|
||||
tag_elems[2].v.val_int =
|
||||
p_tag->precision > 0 ? p_tag->precision : 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 0; k < p_tag->n_meta; k++) {
|
||||
tag_elems[2 + k].name = p_tag->metas[k].name;
|
||||
tag_elems[2 + k].t = p_tag->metas[k].t;
|
||||
tag_elems[2 + k].v = p_tag->metas[k].value;
|
||||
tag_elems[if_precision + 2 + k].name = p_tag->metas[k].name;
|
||||
tag_elems[if_precision + 2 + k].t = p_tag->metas[k].t;
|
||||
tag_elems[if_precision + 2 + k].v = p_tag->metas[k].value;
|
||||
}
|
||||
|
||||
tag_array =
|
||||
neu_json_encode_array(tag_array, tag_elems, 2 + p_tag->n_meta);
|
||||
tag_array = neu_json_encode_array(tag_array, tag_elems,
|
||||
2 + if_precision + p_tag->n_meta);
|
||||
p_tag++;
|
||||
}
|
||||
|
||||
@@ -81,6 +90,103 @@ int neu_json_encode_read_resp(void *json_object, void *param)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int neu_json_encode_read_paginate_resp(void *json_object, void *param)
|
||||
{
|
||||
int ret = 0;
|
||||
neu_json_read_paginate_resp_t *resp =
|
||||
(neu_json_read_paginate_resp_t *) param;
|
||||
|
||||
void * tag_array = neu_json_array();
|
||||
neu_json_read_paginate_resp_tag_t *p_tag = resp->tags;
|
||||
|
||||
for (int i = 0; i < resp->n_tag; i++) {
|
||||
neu_json_elem_t tag_elems[9 + NEU_TAG_META_SIZE] = { 0 };
|
||||
int if_precision = 0;
|
||||
|
||||
tag_elems[0].name = "name";
|
||||
tag_elems[0].t = NEU_JSON_STR;
|
||||
tag_elems[0].v.val_str = p_tag->name;
|
||||
|
||||
tag_elems[1].name = "type";
|
||||
tag_elems[1].t = NEU_JSON_INT;
|
||||
tag_elems[1].v.val_int = p_tag->datatag.type;
|
||||
|
||||
tag_elems[2].name = "address";
|
||||
tag_elems[2].t = NEU_JSON_STR;
|
||||
tag_elems[2].v.val_str = p_tag->datatag.address;
|
||||
|
||||
tag_elems[3].name = "attribute";
|
||||
tag_elems[3].t = NEU_JSON_INT;
|
||||
tag_elems[3].v.val_int = p_tag->datatag.attribute;
|
||||
|
||||
tag_elems[4].name = "description";
|
||||
tag_elems[4].t = NEU_JSON_STR;
|
||||
tag_elems[4].v.val_str = p_tag->datatag.description;
|
||||
|
||||
tag_elems[5].name = "precision";
|
||||
tag_elems[5].t = NEU_JSON_INT;
|
||||
tag_elems[5].v.val_int = p_tag->datatag.precision;
|
||||
|
||||
tag_elems[6].name = "decimal";
|
||||
tag_elems[6].t = NEU_JSON_DOUBLE;
|
||||
tag_elems[6].v.val_double = p_tag->datatag.decimal;
|
||||
|
||||
if (p_tag->error != 0) {
|
||||
tag_elems[7].name = "error";
|
||||
tag_elems[7].t = NEU_JSON_INT;
|
||||
tag_elems[7].v.val_int = p_tag->error;
|
||||
} else {
|
||||
tag_elems[7].name = "value";
|
||||
tag_elems[7].t = p_tag->t;
|
||||
tag_elems[7].v = p_tag->value;
|
||||
tag_elems[7].precision = p_tag->precision;
|
||||
|
||||
if (p_tag->t == NEU_JSON_FLOAT || p_tag->t == NEU_JSON_DOUBLE) {
|
||||
if_precision = 1;
|
||||
tag_elems[8].name = "transferPrecision";
|
||||
tag_elems[8].t = NEU_JSON_INT;
|
||||
tag_elems[8].v.val_int =
|
||||
p_tag->precision > 0 ? p_tag->precision : 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 0; k < p_tag->n_meta; k++) {
|
||||
tag_elems[if_precision + 8 + k].name = p_tag->metas[k].name;
|
||||
tag_elems[if_precision + 8 + k].t = p_tag->metas[k].t;
|
||||
tag_elems[if_precision + 8 + k].v = p_tag->metas[k].value;
|
||||
}
|
||||
|
||||
tag_array = neu_json_encode_array(tag_array, tag_elems,
|
||||
8 + if_precision + p_tag->n_meta);
|
||||
|
||||
free(p_tag->datatag.name);
|
||||
free(p_tag->datatag.address);
|
||||
free(p_tag->datatag.description);
|
||||
|
||||
p_tag++;
|
||||
}
|
||||
|
||||
void * meta_object = neu_json_encode_new();
|
||||
neu_json_elem_t meta_elems[] = {
|
||||
{ .name = "currentPage",
|
||||
.t = NEU_JSON_INT,
|
||||
.v.val_int = resp->meta.current_page },
|
||||
{ .name = "pageSize",
|
||||
.t = NEU_JSON_INT,
|
||||
.v.val_int = resp->meta.page_size },
|
||||
{ .name = "total", .t = NEU_JSON_INT, .v.val_int = resp->meta.total }
|
||||
};
|
||||
neu_json_encode_field(meta_object, meta_elems, 3);
|
||||
|
||||
neu_json_elem_t resp_elems[] = {
|
||||
{ .name = "meta", .t = NEU_JSON_OBJECT, .v.val_object = meta_object },
|
||||
{ .name = "items", .t = NEU_JSON_OBJECT, .v.val_object = tag_array },
|
||||
};
|
||||
ret = neu_json_encode_field(json_object, resp_elems, 2);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int neu_json_encode_read_resp1(void *json_object, void *param)
|
||||
{
|
||||
int ret = 0;
|
||||
@@ -407,14 +513,19 @@ void neu_json_decode_write_free(neu_json_write_t *req)
|
||||
|
||||
int neu_json_decode_read_req(char *buf, neu_json_read_req_t **result)
|
||||
{
|
||||
int ret = 0;
|
||||
void * json_obj = NULL;
|
||||
neu_json_read_req_t *req = calloc(1, sizeof(neu_json_read_req_t));
|
||||
if (req == NULL) {
|
||||
int ret = 0;
|
||||
void *json_obj = NULL;
|
||||
|
||||
json_obj = neu_json_decode_new(buf);
|
||||
if (NULL == json_obj) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
json_obj = neu_json_decode_new(buf);
|
||||
neu_json_read_req_t *req = calloc(1, sizeof(neu_json_read_req_t));
|
||||
if (req == NULL) {
|
||||
neu_json_decode_free(json_obj);
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_elem_t req_elems[] = {
|
||||
{
|
||||
@@ -436,15 +547,6 @@ int neu_json_decode_read_req(char *buf, neu_json_read_req_t **result)
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
};
|
||||
ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems),
|
||||
req_elems);
|
||||
if (ret != 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
req->node = req_elems[0].v.val_str;
|
||||
req->group = req_elems[1].v.val_str;
|
||||
req->sync = req_elems[2].v.val_bool;
|
||||
|
||||
neu_json_elem_t query_elems[] = {
|
||||
{
|
||||
@@ -458,6 +560,17 @@ int neu_json_decode_read_req(char *buf, neu_json_read_req_t **result)
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
};
|
||||
|
||||
ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems),
|
||||
req_elems);
|
||||
if (ret != 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
req->node = req_elems[0].v.val_str;
|
||||
req->group = req_elems[1].v.val_str;
|
||||
req->sync = req_elems[2].v.val_bool;
|
||||
|
||||
if (req_elems[3].v.val_object) {
|
||||
ret = neu_json_decode_by_json(req_elems[3].v.val_object,
|
||||
NEU_JSON_ELEM_SIZE(query_elems),
|
||||
@@ -493,6 +606,123 @@ void neu_json_decode_read_req_free(neu_json_read_req_t *req)
|
||||
free(req);
|
||||
}
|
||||
|
||||
int neu_json_decode_read_paginate_req(char * buf,
|
||||
neu_json_read_paginate_req_t **result)
|
||||
{
|
||||
int ret = 0;
|
||||
void *json_obj = NULL;
|
||||
|
||||
json_obj = neu_json_decode_new(buf);
|
||||
if (NULL == json_obj) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_read_paginate_req_t *req =
|
||||
calloc(1, sizeof(neu_json_read_paginate_req_t));
|
||||
if (req == NULL) {
|
||||
neu_json_decode_free(json_obj);
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_elem_t req_elems[] = {
|
||||
{
|
||||
.name = "node",
|
||||
.t = NEU_JSON_STR,
|
||||
},
|
||||
{
|
||||
.name = "group",
|
||||
.t = NEU_JSON_STR,
|
||||
},
|
||||
{
|
||||
.name = "sync",
|
||||
.t = NEU_JSON_BOOL,
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
{
|
||||
.name = "query",
|
||||
.t = NEU_JSON_OBJECT,
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
};
|
||||
|
||||
neu_json_elem_t query_elems[] = {
|
||||
{
|
||||
.name = "name",
|
||||
.t = NEU_JSON_STR,
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
{
|
||||
.name = "description",
|
||||
.t = NEU_JSON_STR,
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
{
|
||||
.name = "currentPage",
|
||||
.t = NEU_JSON_INT,
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
{
|
||||
.name = "pageSize",
|
||||
.t = NEU_JSON_INT,
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
{
|
||||
.name = "isError",
|
||||
.t = NEU_JSON_BOOL,
|
||||
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
|
||||
},
|
||||
};
|
||||
|
||||
ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems),
|
||||
req_elems);
|
||||
|
||||
if (ret != 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
req->node = req_elems[0].v.val_str;
|
||||
req->group = req_elems[1].v.val_str;
|
||||
req->sync = req_elems[2].v.val_bool;
|
||||
|
||||
if (req_elems[3].v.val_object) {
|
||||
ret = neu_json_decode_by_json(req_elems[3].v.val_object,
|
||||
NEU_JSON_ELEM_SIZE(query_elems),
|
||||
query_elems);
|
||||
|
||||
if (ret != 0) {
|
||||
goto error;
|
||||
}
|
||||
req->name = query_elems[0].v.val_str;
|
||||
req->desc = query_elems[1].v.val_str;
|
||||
req->current_page = query_elems[2].v.val_int;
|
||||
req->page_size = query_elems[3].v.val_int;
|
||||
req->is_error = query_elems[4].v.val_bool;
|
||||
}
|
||||
|
||||
*result = req;
|
||||
neu_json_decode_free(json_obj);
|
||||
return ret;
|
||||
|
||||
error:
|
||||
free(query_elems[0].v.val_str);
|
||||
free(query_elems[1].v.val_str);
|
||||
free(req_elems[0].v.val_str);
|
||||
free(req_elems[1].v.val_str);
|
||||
free(req);
|
||||
if (json_obj != NULL) {
|
||||
neu_json_decode_free(json_obj);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void neu_json_decode_read_paginate_req_free(neu_json_read_paginate_req_t *req)
|
||||
{
|
||||
free(req->group);
|
||||
free(req->node);
|
||||
|
||||
free(req);
|
||||
}
|
||||
|
||||
int neu_json_encode_read_periodic_resp(void *json_object, void *param)
|
||||
{
|
||||
int ret = 0;
|
||||
@@ -579,6 +809,83 @@ void neu_json_metas_to_json(neu_tag_meta_t *metas, int n_meta,
|
||||
json_tag->metas[k].value.val_bit = metas[k].value.value.u8;
|
||||
break;
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
json_tag->metas[k].t = NEU_JSON_STR;
|
||||
json_tag->metas[k].value.val_str = metas[k].value.value.str;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void neu_json_metas_to_json_paginate(
|
||||
neu_tag_meta_t *metas, int n_meta,
|
||||
neu_json_read_paginate_resp_tag_t *json_tag)
|
||||
{
|
||||
for (int k = 0; k < n_meta; k++) {
|
||||
if (strlen(metas[k].name) > 0) {
|
||||
json_tag->metas[k].name = metas[k].name;
|
||||
switch (metas[k].value.type) {
|
||||
case NEU_TYPE_UINT8:
|
||||
json_tag->metas[k].t = NEU_JSON_INT;
|
||||
json_tag->metas[k].value.val_int = metas[k].value.value.u8;
|
||||
break;
|
||||
case NEU_TYPE_INT8:
|
||||
json_tag->metas[k].t = NEU_JSON_INT;
|
||||
json_tag->metas[k].value.val_int = metas[k].value.value.i8;
|
||||
break;
|
||||
case NEU_TYPE_INT16:
|
||||
json_tag->metas[k].t = NEU_JSON_INT;
|
||||
json_tag->metas[k].value.val_int = metas[k].value.value.i16;
|
||||
break;
|
||||
case NEU_TYPE_INT32:
|
||||
json_tag->metas[k].t = NEU_JSON_INT;
|
||||
json_tag->metas[k].value.val_int = metas[k].value.value.i32;
|
||||
break;
|
||||
case NEU_TYPE_INT64:
|
||||
json_tag->metas[k].t = NEU_JSON_INT;
|
||||
json_tag->metas[k].value.val_int = metas[k].value.value.i64;
|
||||
break;
|
||||
case NEU_TYPE_WORD:
|
||||
case NEU_TYPE_UINT16:
|
||||
json_tag->metas[k].t = NEU_JSON_INT;
|
||||
json_tag->metas[k].value.val_int = metas[k].value.value.u16;
|
||||
break;
|
||||
case NEU_TYPE_DWORD:
|
||||
case NEU_TYPE_UINT32:
|
||||
json_tag->metas[k].t = NEU_JSON_INT;
|
||||
json_tag->metas[k].value.val_int = metas[k].value.value.u32;
|
||||
break;
|
||||
case NEU_TYPE_LWORD:
|
||||
case NEU_TYPE_UINT64:
|
||||
json_tag->metas[k].t = NEU_JSON_INT;
|
||||
json_tag->metas[k].value.val_int = metas[k].value.value.u64;
|
||||
break;
|
||||
case NEU_TYPE_FLOAT:
|
||||
json_tag->metas[k].t = NEU_JSON_FLOAT;
|
||||
json_tag->metas[k].value.val_float = metas[k].value.value.f32;
|
||||
break;
|
||||
case NEU_TYPE_DOUBLE:
|
||||
json_tag->metas[k].t = NEU_JSON_DOUBLE;
|
||||
json_tag->metas[k].value.val_double = metas[k].value.value.d64;
|
||||
break;
|
||||
case NEU_TYPE_BOOL:
|
||||
json_tag->metas[k].t = NEU_JSON_BOOL;
|
||||
json_tag->metas[k].value.val_bool =
|
||||
metas[k].value.value.boolean;
|
||||
break;
|
||||
case NEU_TYPE_BIT:
|
||||
json_tag->metas[k].t = NEU_JSON_BIT;
|
||||
json_tag->metas[k].value.val_bit = metas[k].value.value.u8;
|
||||
break;
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
json_tag->metas[k].t = NEU_JSON_STR;
|
||||
json_tag->metas[k].value.val_str = metas[k].value.value.str;
|
||||
break;
|
||||
|
||||
@@ -199,6 +199,8 @@ int neu_json_tag_check_type(neu_json_tag_t *tag)
|
||||
return NEU_JSON_BOOL == tag->t;
|
||||
case NEU_TYPE_BYTES:
|
||||
case NEU_TYPE_STRING:
|
||||
case NEU_TYPE_TIME:
|
||||
case NEU_TYPE_DATA_AND_TIME:
|
||||
return NEU_JSON_STR == tag->t &&
|
||||
strlen(tag->value.val_str) <= NEU_VALUE_SIZE;
|
||||
}
|
||||
@@ -292,7 +294,8 @@ int neu_json_decode_add_tags_req(char *buf, neu_json_add_tags_req_t **result)
|
||||
|
||||
json_obj = neu_json_decode_new(buf);
|
||||
if (NULL == json_obj) {
|
||||
goto decode_fail;
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_elem_t req_elems[] = {
|
||||
@@ -563,7 +566,8 @@ int neu_json_decode_add_gtags_req(char *buf, neu_json_add_gtags_req_t **result)
|
||||
|
||||
json_obj = neu_json_decode_new(buf);
|
||||
if (NULL == json_obj) {
|
||||
goto decode_fail;
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_elem_t req_elems[] = {
|
||||
@@ -657,6 +661,10 @@ int neu_json_decode_del_tags_req(char *buf, neu_json_del_tags_req_t **result)
|
||||
}
|
||||
|
||||
json_obj = neu_json_decode_new(buf);
|
||||
if (NULL == json_obj) {
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_elem_t req_elems[] = { {
|
||||
.name = "node",
|
||||
@@ -773,7 +781,8 @@ int neu_json_decode_update_tags_req(char * buf,
|
||||
|
||||
json_obj = neu_json_decode_new(buf);
|
||||
if (NULL == json_obj) {
|
||||
goto decode_fail;
|
||||
free(req);
|
||||
return -1;
|
||||
}
|
||||
|
||||
neu_json_elem_t req_elems[] = {
|
||||
|
||||
@@ -159,4 +159,16 @@ void neu_async_queue_clean(neu_async_queue_t *q)
|
||||
free(el);
|
||||
}
|
||||
pthread_mutex_unlock(&q->mtx);
|
||||
}
|
||||
|
||||
int neu_async_queue_len(neu_async_queue_t *q)
|
||||
{
|
||||
int count = 0;
|
||||
element *elt = NULL;
|
||||
|
||||
pthread_mutex_lock(&q->mtx);
|
||||
DL_COUNT(q->list, elt, count);
|
||||
pthread_mutex_unlock(&q->mtx);
|
||||
|
||||
return count;
|
||||
}
|
||||
@@ -320,7 +320,6 @@ int neu_http_response(nng_aio *aio, neu_err_code_e code, char *content)
|
||||
case NEU_ERR_TAG_PRECISION_INVALID:
|
||||
case NEU_ERR_TAG_NAME_TOO_LONG:
|
||||
case NEU_ERR_TAG_ADDRESS_TOO_LONG:
|
||||
case NEU_ERR_TAG_VALUE_INVALID:
|
||||
case NEU_ERR_GROUP_MAX_GROUPS:
|
||||
case NEU_ERR_LICENSE_MAX_TAGS:
|
||||
case NEU_ERR_LICENSE_BAD_CLOCK:
|
||||
|
||||
+53
-5
@@ -17,6 +17,7 @@
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**/
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -25,6 +26,44 @@
|
||||
#include "utils/log.h"
|
||||
#include "json/json.h"
|
||||
|
||||
static double format_tag_value(float ele_value)
|
||||
{
|
||||
double scale = pow(10, 5);
|
||||
double value = ele_value;
|
||||
int negative = 1;
|
||||
|
||||
if (value < 0) {
|
||||
value *= -1;
|
||||
negative = -1;
|
||||
}
|
||||
|
||||
int64_t integer_part = (int64_t)(value);
|
||||
double decimal_part = value - integer_part;
|
||||
decimal_part *= scale;
|
||||
decimal_part = round(decimal_part);
|
||||
|
||||
char str[6] = { 0 };
|
||||
snprintf(str, sizeof(str), "%05" PRId64 "", (int64_t) decimal_part);
|
||||
int i = 0, flag = 0;
|
||||
for (; i < 4; i++) {
|
||||
if (str[i] == '0' && str[i + 1] == '0') {
|
||||
flag = 1;
|
||||
break;
|
||||
} else if (str[i] == '9' && str[i + 1] == '9') {
|
||||
flag = 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag != 0 && i != 0) {
|
||||
decimal_part = round(decimal_part / pow(10, 5 - i));
|
||||
value = (double) integer_part + decimal_part / pow(10, i);
|
||||
} else {
|
||||
value = (double) integer_part + decimal_part / scale;
|
||||
}
|
||||
|
||||
return value * negative;
|
||||
}
|
||||
|
||||
static json_t *encode_object_value(neu_json_elem_t *ele)
|
||||
{
|
||||
json_t *ob = NULL;
|
||||
@@ -42,9 +81,14 @@ static json_t *encode_object_value(neu_json_elem_t *ele)
|
||||
case NEU_JSON_DOUBLE:
|
||||
ob = json_realp(ele->v.val_double, ele->precision);
|
||||
break;
|
||||
case NEU_JSON_FLOAT:
|
||||
ob = json_realp(ele->v.val_float, ele->precision);
|
||||
case NEU_JSON_FLOAT: {
|
||||
double t = ele->v.val_float;
|
||||
if (ele->precision == 0) {
|
||||
t = format_tag_value(ele->v.val_float);
|
||||
}
|
||||
ob = json_realp(t, ele->precision);
|
||||
break;
|
||||
}
|
||||
case NEU_JSON_BOOL:
|
||||
ob = json_boolean(ele->v.val_bool);
|
||||
break;
|
||||
@@ -69,10 +113,14 @@ static json_t *encode_object(json_t *object, neu_json_elem_t ele)
|
||||
case NEU_JSON_STR:
|
||||
json_object_set_new(ob, ele.name, json_string(ele.v.val_str));
|
||||
break;
|
||||
case NEU_JSON_FLOAT:
|
||||
json_object_set_new(ob, ele.name,
|
||||
json_realp(ele.v.val_float, ele.precision));
|
||||
case NEU_JSON_FLOAT: {
|
||||
double t = ele.v.val_float;
|
||||
if (ele.precision == 0) {
|
||||
t = format_tag_value(ele.v.val_float);
|
||||
}
|
||||
json_object_set_new(ob, ele.name, json_realp(t, ele.precision));
|
||||
break;
|
||||
}
|
||||
case NEU_JSON_DOUBLE:
|
||||
json_object_set_new(ob, ele.name,
|
||||
json_realp(ele.v.val_double, ele.precision));
|
||||
|
||||
@@ -452,80 +452,6 @@ class TestModbus:
|
||||
assert error.NEU_ERR_TAG_ADDRESS_FORMAT_INVALID == response.json()[
|
||||
'error']
|
||||
|
||||
@description(given="created modbus node and tags", when="write tags with invalid value", then="write failed")
|
||||
def test_write_tags_invalid_value(self, param):
|
||||
response = api.write_tag(node=param[0], group='group', tag=hold_int16[0]['name'], value=32768)
|
||||
assert 400 == response.status_code
|
||||
assert error.NEU_ERR_TAG_VALUE_INVALID == response.json()['error']
|
||||
|
||||
response = api.write_tag(node=param[0], group='group', tag=hold_uint16[0]['name'], value=-1)
|
||||
assert 400 == response.status_code
|
||||
assert error.NEU_ERR_TAG_VALUE_INVALID == response.json()['error']
|
||||
|
||||
response = api.write_tag(node=param[0], group='group', tag=hold_int32[0]['name'], value=2147483648)
|
||||
assert 400 == response.status_code
|
||||
assert error.NEU_ERR_TAG_VALUE_INVALID == response.json()['error']
|
||||
|
||||
response = api.write_tag(node=param[0], group='group', tag=hold_uint32[0]['name'], value=-1)
|
||||
assert 400 == response.status_code
|
||||
assert error.NEU_ERR_TAG_VALUE_INVALID == response.json()['error']
|
||||
|
||||
response = api.write_tags(node=param[0], group='group', tag_values=[
|
||||
{"tag": hold_int16[0]['name'], "value": 32768},
|
||||
{"tag": hold_uint16[0]['name'], "value": 2},
|
||||
{"tag": hold_int32[0]['name'], "value": 3},
|
||||
{"tag": hold_uint32[0]['name'], "value": 4}])
|
||||
assert 404 == response.status_code
|
||||
assert error.NEU_ERR_TAG_NOT_EXIST == response.json()['error']
|
||||
|
||||
response = api.write_tags(node=param[0], group='group', tag_values=[
|
||||
{"tag": hold_int16[0]['name'], "value": 1},
|
||||
{"tag": hold_uint16[0]['name'], "value": -1},
|
||||
{"tag": hold_int32[0]['name'], "value": 3},
|
||||
{"tag": hold_uint32[0]['name'], "value": 4}])
|
||||
assert 404 == response.status_code
|
||||
assert error.NEU_ERR_TAG_NOT_EXIST == response.json()['error']
|
||||
|
||||
response = api.write_tags(node=param[0], group='group', tag_values=[
|
||||
{"tag": hold_int16[0]['name'], "value": 1},
|
||||
{"tag": hold_uint16[0]['name'], "value": 2},
|
||||
{"tag": hold_int32[0]['name'], "value": 2147483648},
|
||||
{"tag": hold_uint32[0]['name'], "value": 4}])
|
||||
assert 404 == response.status_code
|
||||
assert error.NEU_ERR_TAG_NOT_EXIST == response.json()['error']
|
||||
|
||||
response = api.write_tags(node=param[0], group='group', tag_values=[
|
||||
{"tag": hold_int16[0]['name'], "value": 1},
|
||||
{"tag": hold_uint16[0]['name'], "value": 2},
|
||||
{"tag": hold_int32[0]['name'], "value": 3},
|
||||
{"tag": hold_uint32[0]['name'], "value": -1}])
|
||||
assert 404 == response.status_code
|
||||
assert error.NEU_ERR_TAG_NOT_EXIST == response.json()['error']
|
||||
|
||||
modbus_write_gtags = {"node": param[0], "groups": [{"group": "group", "tags" : [{"tag": "hold_int16", "value": 32768},
|
||||
{"tag": "hold_uint16", "value": 2}, {"tag": "hold_int32", "value": 3}, {"tag": "hold_uint32", "value": 4}]}]}
|
||||
response = api.write_gtags(json=modbus_write_gtags)
|
||||
assert 404 == response.status_code
|
||||
assert error.NEU_ERR_TAG_NOT_EXIST == response.json()['error']
|
||||
|
||||
modbus_write_gtags = {"node": param[0], "groups": [{"group": "group", "tags" : [{"tag": "hold_int16", "value": 1},
|
||||
{"tag": "hold_uint16", "value": -1}, {"tag": "hold_int32", "value": 3}, {"tag": "hold_uint32", "value": 4}]}]}
|
||||
response = api.write_gtags(json=modbus_write_gtags)
|
||||
assert 404 == response.status_code
|
||||
assert error.NEU_ERR_TAG_NOT_EXIST == response.json()['error']
|
||||
|
||||
modbus_write_gtags = {"node": param[0], "groups": [{"group": "group", "tags" : [{"tag": "hold_int16", "value": 1},
|
||||
{"tag": "hold_uint16", "value": 2}, {"tag": "hold_int32", "value": 2147483648}, {"tag": "hold_uint32", "value": 4}]}]}
|
||||
response = api.write_gtags(json=modbus_write_gtags)
|
||||
assert 404 == response.status_code
|
||||
assert error.NEU_ERR_TAG_NOT_EXIST == response.json()['error']
|
||||
|
||||
modbus_write_gtags = {"node": param[0], "groups": [{"group": "group", "tags" : [{"tag": "hold_int16", "value": 1},
|
||||
{"tag": "hold_uint16", "value": 2}, {"tag": "hold_int32", "value": 3}, {"tag": "hold_uint32", "value": -1}]}]}
|
||||
response = api.write_gtags(json=modbus_write_gtags)
|
||||
assert 404 == response.status_code
|
||||
assert error.NEU_ERR_TAG_NOT_EXIST == response.json()['error']
|
||||
|
||||
@description(given="created modbus node and tags", when="write/read tags", then="write/read success")
|
||||
def test_write_read_tags(self, param):
|
||||
api.write_tag_check(
|
||||
|
||||
@@ -0,0 +1,293 @@
|
||||
import neuron.api as api
|
||||
import neuron.error as error
|
||||
import neuron.config as config
|
||||
from neuron.common import *
|
||||
|
||||
tcp_port = random_port()
|
||||
|
||||
|
||||
@pytest.fixture(params=[('modbus-tcp', config.PLUGIN_MODBUS_TCP)], scope='class')
|
||||
def param(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope='class')
|
||||
def simulator_setup_teardown(param):
|
||||
p = process.start_simulator(
|
||||
['./modbus_simulator', 'tcp', f'{tcp_port}', 'ip_v4'])
|
||||
|
||||
response = api.add_node(node=param[0], plugin=param[1])
|
||||
assert 200 == response.status_code
|
||||
|
||||
response = api.add_group(node=param[0], group='group')
|
||||
assert 200 == response.status_code
|
||||
|
||||
response = api.modbus_tcp_node_setting(
|
||||
node=param[0], interval=1, port=tcp_port)
|
||||
|
||||
yield
|
||||
process.stop_simulator(p)
|
||||
|
||||
|
||||
hold_bit = [{"name": "hold_bit", "address": "1!400001.15",
|
||||
"attribute": config.NEU_TAG_ATTRIBUTE_READ_SUBSCRIBE, "type": config.NEU_TYPE_BIT, "description": "123"}]
|
||||
hold_int16 = [{"name": "hold_int16", "address": "1!400001",
|
||||
"attribute": config.NEU_TAG_ATTRIBUTE_RW_SUBSCRIBE, "type": config.NEU_TYPE_INT16, "description": "123"}]
|
||||
hold_uint16 = [{"name": "hold_uint16", "address": "1!400002",
|
||||
"attribute": config.NEU_TAG_ATTRIBUTE_RW_SUBSCRIBE, "type": config.NEU_TYPE_UINT16, "description": "456"}]
|
||||
hold_int32 = [{"name": "hold_int32", "address": "1!400003",
|
||||
"attribute": config.NEU_TAG_ATTRIBUTE_RW_SUBSCRIBE, "type": config.NEU_TYPE_INT32, "description": "456"}]
|
||||
hold_uint32 = [{"name": "hold_uint32", "address": "1!400015",
|
||||
"attribute": config.NEU_TAG_ATTRIBUTE_RW_SUBSCRIBE, "type": config.NEU_TYPE_UINT32}]
|
||||
hold_float = [{"name": "hold_float", "address": "1!400017",
|
||||
"attribute": config.NEU_TAG_ATTRIBUTE_RW_SUBSCRIBE, "type": config.NEU_TYPE_FLOAT}]
|
||||
hold_string = [{"name": "hold_string", "address": "1!400020.10",
|
||||
"attribute": config.NEU_TAG_ATTRIBUTE_RW_SUBSCRIBE, "type": config.NEU_TYPE_STRING}]
|
||||
hold_bytes = [{"name": "hold_bytes", "address": "1!400040.10",
|
||||
"attribute": config.NEU_TAG_ATTRIBUTE_RW_SUBSCRIBE, "type": config.NEU_TYPE_BYTES}]
|
||||
|
||||
|
||||
class TestReadPaginate:
|
||||
|
||||
@description(given="created modbus node", when="add multiple tags", then="add success")
|
||||
def test_add_tags(self, param):
|
||||
api.add_tags_check(node=param[0], group='group', tags=hold_bit)
|
||||
api.add_tags_check(node=param[0], group='group', tags=hold_int16)
|
||||
api.add_tags_check(node=param[0], group='group', tags=hold_uint16)
|
||||
api.add_tags_check(node=param[0], group='group', tags=hold_int32)
|
||||
api.add_tags_check(node=param[0], group='group', tags=hold_uint32)
|
||||
api.add_tags_check(node=param[0], group='group', tags=hold_float)
|
||||
api.add_tags_check(node=param[0], group='group', tags=hold_string)
|
||||
api.add_tags_check(node=param[0], group='group', tags=hold_bytes)
|
||||
|
||||
|
||||
@description(given="created modbus node and tags", when="write/read tags without filter", then="write/read success")
|
||||
def test_write_read_tags(self, param):
|
||||
api.write_tag_check(
|
||||
node=param[0], group='group', tag=hold_int16[0]['name'], value=-123)
|
||||
api.write_tag_check(
|
||||
node=param[0], group='group', tag=hold_uint16[0]['name'], value=123)
|
||||
api.write_tag_check(
|
||||
node=param[0], group='group', tag=hold_int32[0]['name'], value=-1234)
|
||||
api.write_tag_check(
|
||||
node=param[0], group='group', tag=hold_uint32[0]['name'], value=1234)
|
||||
api.write_tag_check(
|
||||
node=param[0], group='group', tag=hold_float[0]['name'], value=13.4121)
|
||||
api.write_tag_check(
|
||||
node=param[0], group='group', tag=hold_string[0]['name'], value='hello')
|
||||
api.write_tag_check(
|
||||
node=param[0], group='group', tag=hold_bytes[0]['name'], value=[0x1, 0x2, 0x3, 0x4])
|
||||
|
||||
time.sleep(0.3)
|
||||
response = api.read_tags_paginate(node=param[0], group='group')
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
|
||||
assert 8 == len(response.json()["items"])
|
||||
|
||||
assert "hold_bit" == response.json()["items"][0]["name"]
|
||||
assert 1 == response.json()["items"][0]["value"]
|
||||
|
||||
assert "hold_int16" == response.json()["items"][1]["name"]
|
||||
assert -123 == response.json()["items"][1]["value"]
|
||||
|
||||
assert "hold_uint16" == response.json()["items"][2]["name"]
|
||||
assert 123 == response.json()["items"][2]["value"]
|
||||
|
||||
assert "hold_int32" == response.json()["items"][3]["name"]
|
||||
assert -1234 == response.json()["items"][3]["value"]
|
||||
|
||||
assert "hold_uint32" == response.json()["items"][4]["name"]
|
||||
assert 1234 == response.json()["items"][4]["value"]
|
||||
|
||||
assert "hold_float" == response.json()["items"][5]["name"]
|
||||
assert compare_float(13.4121, response.json()["items"][5]["value"])
|
||||
|
||||
assert "hold_string" == response.json()["items"][6]["name"]
|
||||
assert 'hello' == response.json()["items"][6]["value"]
|
||||
|
||||
assert "hold_bytes" == response.json()["items"][7]["name"]
|
||||
assert [0x1, 0x2, 0x3, 0x4, 0, 0, 0, 0, 0, 0] == response.json()["items"][7]["value"]
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with filter(name)", then="read success")
|
||||
def test_read_tags_filter_name(self, param):
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"name": "int16"})
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 2 == response.json()["meta"]["total"]
|
||||
|
||||
assert 2 == len(response.json()["items"])
|
||||
|
||||
assert "hold_int16" == response.json()["items"][0]["name"]
|
||||
assert -123 == response.json()["items"][0]["value"]
|
||||
assert "hold_uint16" == response.json()["items"][1]["name"]
|
||||
assert 123 == response.json()["items"][1]["value"]
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with filter(desc)", then="read success")
|
||||
def test_read_tags_filter_desc(self, param):
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"description": "12"})
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 2 == response.json()["meta"]["total"]
|
||||
|
||||
assert 2 == len(response.json()["items"])
|
||||
|
||||
assert "hold_bit" == response.json()["items"][0]["name"]
|
||||
assert 1 == response.json()["items"][0]["value"]
|
||||
assert "hold_int16" == response.json()["items"][1]["name"]
|
||||
assert -123 == response.json()["items"][1]["value"]
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with paginate", then="read success")
|
||||
def test_read_tags_paginate(self, param):
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"currentPage": 1, "pageSize": 5})
|
||||
assert 1 == response.json()["meta"]["currentPage"]
|
||||
assert 5 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
assert 5 == len(response.json()["items"])
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"currentPage": 2, "pageSize": 5})
|
||||
assert 2 == response.json()["meta"]["currentPage"]
|
||||
assert 5 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
assert 3 == len(response.json()["items"])
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"currentPage": 3, "pageSize": 5})
|
||||
assert 3 == response.json()["meta"]["currentPage"]
|
||||
assert 5 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
assert 0 == len(response.json()["items"])
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"currentPage": 6, "pageSize": 5})
|
||||
assert 6 == response.json()["meta"]["currentPage"]
|
||||
assert 5 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
assert 0 == len(response.json()["items"])
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"currentPage": 0, "pageSize": 5})
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 5 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
assert 8 == len(response.json()["items"])
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with filter(isError)", then="read success")
|
||||
def test_read_tags_filter_isError(self, param):
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"isError": True})
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 0 == response.json()["meta"]["total"]
|
||||
assert 0 == len(response.json()["items"])
|
||||
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_STOP)
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"isError": True})
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
assert 8 == len(response.json()["items"])
|
||||
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_START)
|
||||
time.sleep(0.3)
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with filter(name&desc)", then="read success")
|
||||
def test_read_tags_filter_name_desc(self, param):
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"name": "16", "description": "123"})
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 1 == response.json()["meta"]["total"]
|
||||
assert 1 == len(response.json()["items"])
|
||||
assert "hold_int16" == response.json()["items"][0]["name"]
|
||||
assert -123 == response.json()["items"][0]["value"]
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with filter(isError&name)", then="read success")
|
||||
def test_read_tags_filter_isError_name(self, param):
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_STOP)
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"isError": True, "name": "16"})
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 2 == response.json()["meta"]["total"]
|
||||
assert 2 == len(response.json()["items"])
|
||||
assert "hold_int16" == response.json()["items"][0]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][0]["error"]
|
||||
assert "hold_uint16" == response.json()["items"][1]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][1]["error"]
|
||||
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_START)
|
||||
time.sleep(0.3)
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with filter(isError&desc)", then="read success")
|
||||
def test_read_tags_filter_isError_desc(self, param):
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_STOP)
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"isError": True, "description": "12"})
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 2 == response.json()["meta"]["total"]
|
||||
assert 2 == len(response.json()["items"])
|
||||
assert "hold_bit" == response.json()["items"][0]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][0]["error"]
|
||||
assert "hold_int16" == response.json()["items"][1]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][1]["error"]
|
||||
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_START)
|
||||
time.sleep(0.3)
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with filter(isError&name&desc)", then="read success")
|
||||
def test_read_tags_filter_isError_name_desc(self, param):
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_STOP)
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"isError": True, "name":"16", "description": "12"})
|
||||
|
||||
assert 0 == response.json()["meta"]["currentPage"]
|
||||
assert 0 == response.json()["meta"]["pageSize"]
|
||||
assert 1 == response.json()["meta"]["total"]
|
||||
assert 1 == len(response.json()["items"])
|
||||
assert "hold_int16" == response.json()["items"][0]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][0]["error"]
|
||||
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_START)
|
||||
time.sleep(0.3)
|
||||
|
||||
@description(given="created modbus node and tags", when="read tags with paginate & filter(isError)", then="read success")
|
||||
def test_read_tags_paginate_filter_isError(self, param):
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_STOP)
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"currentPage": 1, "pageSize": 5, "isError": True})
|
||||
assert 1 == response.json()["meta"]["currentPage"]
|
||||
assert 5 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
assert 5 == len(response.json()["items"])
|
||||
assert "hold_bit" == response.json()["items"][0]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][0]["error"]
|
||||
assert "hold_int16" == response.json()["items"][1]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][1]["error"]
|
||||
assert "hold_uint16" == response.json()["items"][2]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][2]["error"]
|
||||
assert "hold_int32" == response.json()["items"][3]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][3]["error"]
|
||||
assert "hold_uint32" == response.json()["items"][4]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][4]["error"]
|
||||
|
||||
response = api.read_tags_paginate(node=param[0], group='group', query= {"currentPage": 2, "pageSize": 5, "isError": True})
|
||||
assert 2 == response.json()["meta"]["currentPage"]
|
||||
assert 5 == response.json()["meta"]["pageSize"]
|
||||
assert 8 == response.json()["meta"]["total"]
|
||||
assert 3 == len(response.json()["items"])
|
||||
assert "hold_float" == response.json()["items"][0]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][0]["error"]
|
||||
assert "hold_string" == response.json()["items"][1]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][1]["error"]
|
||||
assert "hold_bytes" == response.json()["items"][2]["name"]
|
||||
assert error.NEU_ERR_PLUGIN_NOT_RUNNING == response.json()["items"][2]["error"]
|
||||
|
||||
api.node_ctl_check(node='modbus-tcp', ctl=config.NEU_CTL_START)
|
||||
time.sleep(0.3)
|
||||
+19
-2
@@ -154,6 +154,11 @@ def read_tags(node, group, sync=False, query=None):
|
||||
body["query"] = query
|
||||
return requests.post(url=config.BASE_URL + "/api/v2/read", headers={"Authorization": config.default_jwt}, json=body)
|
||||
|
||||
def read_tags_paginate(node, group, sync=False, query=None):
|
||||
body = {"node": node, "group": group, "sync": sync}
|
||||
if query:
|
||||
body["query"] = query
|
||||
return requests.post(url=config.BASE_URL + "/api/v2/read/paginate", headers={"Authorization": config.default_jwt}, json=body)
|
||||
|
||||
def read_tag(node, group, tag, sync=False):
|
||||
response = read_tags(node, group, sync, query={"name": tag})
|
||||
@@ -295,8 +300,8 @@ def get_version():
|
||||
return requests.get(url=config.BASE_URL + "/api/v2/version", headers={"Authorization": config.default_jwt})
|
||||
|
||||
|
||||
def prgfile_upload(driver, path, params):
|
||||
return requests.post(url=config.BASE_URL + "/api/v2/prgfile", headers={"Authorization": config.default_jwt}, json={"driver": driver, "path": path, "parameters": params})
|
||||
def prgfile_upload(driver, path, del_flag, params):
|
||||
return requests.post(url=config.BASE_URL + "/api/v2/prgfile", headers={"Authorization": config.default_jwt}, json={"driver": driver, "path": path, "del_flag": del_flag, "parameters": params})
|
||||
|
||||
|
||||
def prgfile_process(driver):
|
||||
@@ -318,6 +323,18 @@ def modbus_rtu_node_setting(node, port=502, connection_mode=0, transport_mode=0,
|
||||
"parity": parity, "baud": baud, "data": data})
|
||||
|
||||
|
||||
def modbus_tcp_node_setting_endian(node, port, connection_mode=0, transport_mode=0, interval=0, host='127.0.0.1', timeout=3000, max_retries=2, endianness=1):
|
||||
return node_setting(node, json={"connection_mode": connection_mode, "transport_mode": transport_mode, "interval": interval,
|
||||
"host": host, "port": port, "timeout": timeout, "max_retries": max_retries, "endianess": endianness})
|
||||
|
||||
|
||||
def modbus_rtu_node_setting_endian(node, port=502, connection_mode=0, transport_mode=0, interval=0, host='127.0.0.1', timeout=3000, max_retries=2, link=1, device="", stop=0, parity=0, baud=4, data=3, endianness=1):
|
||||
return node_setting(node, json={"connection_mode": connection_mode, "transport_mode": transport_mode,
|
||||
"interval": interval, "host": host, "port": port, "timeout": timeout,
|
||||
"max_retries": max_retries, "link": link, "device": device, "stop": stop,
|
||||
"parity": parity, "baud": baud, "data": data, "endianess": endianness})
|
||||
|
||||
|
||||
def mqtt_node_setting(node):
|
||||
return node_setting(node, json={"client-id": "neuron_aBcDeF", "qos": 0, "format": 0,
|
||||
"write-req-topic": f"/neuron/{node}/write/req", "write-resp-topic": f"/neuron/{node}/write/resp",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
BASE_URL = "http://127.0.0.1:7000"
|
||||
default_jwt = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJuZXVyb24iLCJib2R5RW5jb2RlIjowLCJleHAiOjE3MTc4MzUyNjIsImlhdCI6MTY4NjI5OTI2MiwiaXNzIjoibmV1cm9uIn0.tJ8Z12S5lzzyqvJedFr11w0FmQ8l9inFHuzcuA_3uKoNBJSZlQAkcWyDAO2XELyuZFtHPrk8pJFgEqTzwphtBAybVXbeHBRWED5XuHvYqznzwk1J2UCOuw_ehmHnLszZHRAcMoeLWIRIx0KM7BXvZAyFA2J1axJntxfRrurNMn6MbfnGGtza6hJfuQ6pG-h4cHhrBOogf8nw3f8RLfd16vbWobhEp8KBUrwsfn2Iv4hMXvZMekv_PXboLO74f7oMkEL6pdM9g6G6rDBTjTU8FssfeyJcAP7Xb9mS2G7GNW5B8twtPVLtpOQH9VNbLa4OQxdMb0WN86i9vSDiCOoN6A"
|
||||
default_jwt = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJuZXVyb24iLCJib2R5RW5jb2RlIjowLCJleHAiOjE3ODExNTc4MzcsImlhdCI6MTcxODA4NTgzOCwiaXNzIjoibmV1cm9uIn0.OmMIxGiaJYcuetslmliVGAWtjIZxNgvOCsZGdljHUSnFBOpTV1BqFSBqR55tZx51z09RPEdm8tIeGZN-jUUQgXgS22K7LUqhAYXgGY8bhh37pN6GXPzaHCdjan3VzS_5ywuiGHgNIjr5RrHNTCjLGb_dYlUGL-Hv0z0cOC6ObbqS0snlEOJsxNcjOaRMqbpwtxuTKc4_QA0yWtYTyGXjhXVA2ZAg1HsF9M_9XJ2qk-_6qEadAR2MzjphJvsk-um1oHOGtJhGGxlRNONL6yxFav2BTHs2dSz2P1YanVsUtFF0HO44HYPU4vs511RuOvcHyTM1TV3BudKgDpCeWKpq1Q"
|
||||
|
||||
NEU_NODE_DRIVER = 1
|
||||
NEU_NODE_APP = 2
|
||||
@@ -68,6 +68,11 @@ NEU_DATA_6 = 1
|
||||
NEU_DATA_7 = 2
|
||||
NEU_DATA_8 = 3
|
||||
|
||||
ENDIAN_ABCD = 1,
|
||||
ENDIAN_BADC = 2,
|
||||
ENDIAN_DCBA = 3,
|
||||
ENDIAN_CDAB = 4,
|
||||
|
||||
PLUGIN_MODBUS_TCP = "Modbus TCP"
|
||||
PLUGIN_MODBUS_RTU = "Modbus RTU"
|
||||
PLUGIN_MQTT = "MQTT"
|
||||
|
||||
@@ -53,7 +53,6 @@ NEU_ERR_TAG_ADDRESS_TOO_LONG = 2207
|
||||
NEU_ERR_TAG_DESCRIPTION_TOO_LONG = 2208
|
||||
NEU_ERR_TAG_PRECISION_INVALID = 2209
|
||||
NEU_ERR_TAG_EXIST = 2210
|
||||
NEU_ERR_TAG_VALUE_INVALID = 2211
|
||||
|
||||
NEU_ERR_LIBRARY_NOT_FOUND = 2301
|
||||
NEU_ERR_LIBRARY_INFO_INVALID = 2302
|
||||
|
||||
@@ -64,6 +64,13 @@ target_include_directories(rolling_counter_test PRIVATE
|
||||
)
|
||||
target_link_libraries(rolling_counter_test neuron-base gtest_main gtest)
|
||||
|
||||
add_executable(mqtt_client_test mqtt_client_test.cc)
|
||||
target_include_directories(mqtt_client_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
target_link_libraries(mqtt_client_test neuron-base gtest_main gtest)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(json_test)
|
||||
gtest_discover_tests(http_test)
|
||||
@@ -73,3 +80,4 @@ gtest_discover_tests(tag_sort_test)
|
||||
gtest_discover_tests(modbus_test)
|
||||
gtest_discover_tests(async_queue_test)
|
||||
gtest_discover_tests(rolling_counter_test)
|
||||
gtest_discover_tests(mqtt_client_test)
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "connection/mqtt_client.h"
|
||||
#include "utils/log.h"
|
||||
|
||||
zlog_category_t *neuron = NULL;
|
||||
|
||||
TEST(MQTTClientTest, test_topic_filter_is_valid)
|
||||
{
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_valid(NULL));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_valid(""));
|
||||
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_valid("sport/tennis#"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_valid("sport/tennis/#/ranking"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("#"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("sport/#"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("sport/tennis/player/#"));
|
||||
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_valid("sport+"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_valid("sport/+player"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("+"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("+/+"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("sport/+/player"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("+/tennis/#"));
|
||||
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("$SYS"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("$SYS/#"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("sport"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("sport/tennis"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_valid("sport/tennis/player"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_valid("sport/tennis/player/ranking"));
|
||||
}
|
||||
|
||||
TEST(MQTTClientTest, test_topic_filter_is_match)
|
||||
{
|
||||
const char *filter = "#";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "$sport"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "sport"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "sport/tennis"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player"));
|
||||
|
||||
filter = "sport/tennis/player/#";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/tennis"));
|
||||
ASSERT_EQ(false,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/play"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player/"));
|
||||
ASSERT_EQ(
|
||||
true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player/ranking"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_match(
|
||||
filter, "sport/tennis/player/ranking/score/wimbledon"));
|
||||
|
||||
filter = "+";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "$finance"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "/finance"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "finance"));
|
||||
|
||||
filter = "/+";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "finance"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "/finance"));
|
||||
|
||||
filter = "+/+";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "$finance"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "finance"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "/finance"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "sport/tennis"));
|
||||
|
||||
filter = "sport/+";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "sport/"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "sport/tennis"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/tennis/"));
|
||||
ASSERT_EQ(false,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/ranking"));
|
||||
|
||||
filter = "sport/tennis/+";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/tennis"));
|
||||
ASSERT_EQ(
|
||||
false,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player/ranking"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player1"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player2"));
|
||||
|
||||
filter = "sport/+/player";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/tennis"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/tennis/"));
|
||||
ASSERT_EQ(false,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/ranking"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player"));
|
||||
ASSERT_EQ(false,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player/"));
|
||||
ASSERT_EQ(
|
||||
false,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player/ranking"));
|
||||
|
||||
filter = "+/tennis/#";
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/"));
|
||||
ASSERT_EQ(false, neu_mqtt_topic_filter_is_match(filter, "sport/baseball"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "sport/tennis"));
|
||||
ASSERT_EQ(true, neu_mqtt_topic_filter_is_match(filter, "sport/tennis/"));
|
||||
ASSERT_EQ(true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player"));
|
||||
ASSERT_EQ(
|
||||
true,
|
||||
neu_mqtt_topic_filter_is_match(filter, "sport/tennis/player/ranking"));
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
zlog_init("./config/dev.conf");
|
||||
neuron = zlog_get_category("neuron");
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Reference in New Issue
Block a user