mirror of
https://github.com/emqx/neuron.git
synced 2026-10-17 04:40:42 +08:00
custom type
This commit is contained in:
@@ -851,6 +851,8 @@ static inline void neu_trans_data_free(neu_reqresp_trans_data_t *data)
|
||||
{
|
||||
if (tag_value->value.type == NEU_TYPE_PTR) {
|
||||
free(tag_value->value.value.ptr.ptr);
|
||||
} else if (tag_value->value.type == NEU_TYPE_CUSTOM) {
|
||||
json_decref(tag_value->value.value.json);
|
||||
}
|
||||
}
|
||||
utarray_free(data->tags);
|
||||
@@ -971,6 +973,10 @@ static inline void neu_tag_value_to_json(neu_resp_tag_value_meta_t *tag_value,
|
||||
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;
|
||||
case NEU_TYPE_CUSTOM:
|
||||
tag_json->t = NEU_JSON_OBJECT;
|
||||
tag_json->value.val_object = tag_value->value.value.json;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
typedef enum {
|
||||
NEU_TYPE_INT8 = 1,
|
||||
NEU_TYPE_UINT8 = 2,
|
||||
@@ -135,6 +137,7 @@ typedef union {
|
||||
char str[NEU_VALUE_SIZE];
|
||||
neu_value_bytes_t bytes;
|
||||
neu_value_ptr_t ptr;
|
||||
json_t * json;
|
||||
} neu_value_u;
|
||||
|
||||
static inline char *neu_value_str(neu_type_e type, neu_value_u value)
|
||||
@@ -205,6 +208,12 @@ static inline char *neu_value_str(neu_type_e type, neu_value_u value)
|
||||
value.str[2]);
|
||||
break;
|
||||
}
|
||||
case NEU_TYPE_CUSTOM: {
|
||||
char *result = json_dumps(value.json, JSON_REAL_PRECISION(16));
|
||||
snprintf(str, sizeof(str), "type: %s, value: %s", neu_type_string(type),
|
||||
result);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 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,
|
||||
bias REAL NOT NULL check (bias BETWEEN -1000 AND 1000),
|
||||
type INTEGER NOT NULL check (type BETWEEN 0 AND 22),
|
||||
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;
|
||||
@@ -22,6 +22,8 @@
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <jansson.h>
|
||||
|
||||
#include "utils/uthash.h"
|
||||
|
||||
#include "define.h"
|
||||
@@ -91,7 +93,13 @@ void neu_driver_cache_destroy(neu_driver_cache_t *cache)
|
||||
free(elem->value.value.ptr.ptr);
|
||||
elem->value.value.ptr.ptr = NULL;
|
||||
}
|
||||
} else if (elem->value.type == NEU_TYPE_CUSTOM) {
|
||||
if (elem->value.value.json != NULL) {
|
||||
json_decref(elem->value.value.json);
|
||||
elem->value.value.json = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
free(elem);
|
||||
}
|
||||
pthread_mutex_unlock(&cache->mtx);
|
||||
@@ -190,6 +198,13 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
|
||||
}
|
||||
}
|
||||
break;
|
||||
case NEU_TYPE_CUSTOM: {
|
||||
if (json_equal(elem->value_old.value.json, value.value.json) !=
|
||||
0) {
|
||||
elem->changed = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NEU_TYPE_PTR: {
|
||||
if (elem->value_old.value.ptr.length !=
|
||||
value.value.ptr.length) {
|
||||
@@ -277,6 +292,12 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
|
||||
|
||||
break;
|
||||
}
|
||||
case NEU_TYPE_CUSTOM: {
|
||||
if (json_equal(elem->value.value.json, value.value.json) != 0) {
|
||||
elem->changed = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NEU_TYPE_FLOAT:
|
||||
if (elem->value.precision == 0) {
|
||||
elem->changed = elem->value.value.f32 != value.value.f32;
|
||||
@@ -316,8 +337,7 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
|
||||
elem->changed = true;
|
||||
}
|
||||
|
||||
elem->value.type = value.type;
|
||||
if (elem->value.type == NEU_TYPE_PTR) {
|
||||
if (value.type == NEU_TYPE_PTR) {
|
||||
elem->value.value.ptr.length = value.value.ptr.length;
|
||||
elem->value.value.ptr.type = value.value.ptr.type;
|
||||
if (elem->value.value.ptr.ptr != NULL) {
|
||||
@@ -326,9 +346,17 @@ void neu_driver_cache_update_change(neu_driver_cache_t *cache,
|
||||
elem->value.value.ptr.ptr = calloc(1, value.value.ptr.length);
|
||||
memcpy(elem->value.value.ptr.ptr, value.value.ptr.ptr,
|
||||
value.value.ptr.length);
|
||||
} else if (value.type == NEU_TYPE_CUSTOM) {
|
||||
if (elem->value.type == NEU_TYPE_CUSTOM &&
|
||||
elem->value.value.json != NULL) {
|
||||
json_decref(elem->value.value.json);
|
||||
elem->value.value.json = NULL;
|
||||
}
|
||||
elem->value.value.json = value.value.json;
|
||||
} else {
|
||||
elem->value.value = value.value;
|
||||
}
|
||||
elem->value.type = value.type;
|
||||
|
||||
memset(elem->metas, 0, sizeof(neu_tag_meta_t) * NEU_TAG_META_SIZE);
|
||||
for (int i = 0; i < n_meta; i++) {
|
||||
@@ -414,6 +442,9 @@ int neu_driver_cache_meta_get(neu_driver_cache_t *cache, const char *group,
|
||||
memcpy(value->value.value.ptr.ptr, elem->value.value.ptr.ptr,
|
||||
elem->value.value.ptr.length);
|
||||
break;
|
||||
case NEU_TYPE_CUSTOM:
|
||||
value->value.value.json = json_deep_copy(elem->value.value.json);
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NEU_TAG_META_SIZE; i++) {
|
||||
@@ -498,6 +529,9 @@ int neu_driver_cache_meta_get_changed(neu_driver_cache_t *cache,
|
||||
memcpy(value->value.value.ptr.ptr, elem->value.value.ptr.ptr,
|
||||
elem->value.value.ptr.length);
|
||||
break;
|
||||
case NEU_TYPE_CUSTOM:
|
||||
value->value.value.json = json_deep_copy(elem->value.value.json);
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NEU_TAG_META_SIZE; i++) {
|
||||
@@ -534,6 +568,11 @@ void neu_driver_cache_del(neu_driver_cache_t *cache, const char *group,
|
||||
free(elem->value.value.ptr.ptr);
|
||||
elem->value.value.ptr.ptr = NULL;
|
||||
}
|
||||
} else if (elem->value.type == NEU_TYPE_CUSTOM) {
|
||||
if (elem->value.value.json != NULL) {
|
||||
json_decref(elem->value.value.json);
|
||||
elem->value.value.json = NULL;
|
||||
}
|
||||
}
|
||||
free(elem);
|
||||
}
|
||||
|
||||
@@ -1987,6 +1987,8 @@ static int report_callback(void *usr_data)
|
||||
{
|
||||
if (tag_value->value.type == NEU_TYPE_PTR) {
|
||||
free(tag_value->value.value.ptr.ptr);
|
||||
} else if (tag_value->value.type == NEU_TYPE_CUSTOM) {
|
||||
json_decref(tag_value->value.value.json);
|
||||
}
|
||||
}
|
||||
utarray_free(data->tags);
|
||||
@@ -2299,6 +2301,8 @@ static void read_report_group(int64_t timestamp, int64_t timeout,
|
||||
(timestamp - value.timestamp) > timeout && timeout > 0) {
|
||||
if (value.value.type == NEU_TYPE_PTR) {
|
||||
free(value.value.value.ptr.ptr);
|
||||
} else if (value.value.type == NEU_TYPE_CUSTOM) {
|
||||
json_decref(value.value.value.json);
|
||||
}
|
||||
tag_value.value.type = NEU_TYPE_ERROR;
|
||||
tag_value.value.value.i32 = NEU_ERR_PLUGIN_TAG_VALUE_EXPIRED;
|
||||
|
||||
Reference in New Issue
Block a user