From f4e5ef384c3431bbd3401d737e0cbedecb6006f2 Mon Sep 17 00:00:00 2001 From: gc87 Date: Thu, 2 Dec 2021 09:21:44 +0800 Subject: [PATCH] Fix opcua bool type parsing error - And add other type support --- neuron.yaml | 2 +- plugins/mqtt/command/read_write.c | 14 ++++++ plugins/opcua/opcua_handle.c | 11 +++-- plugins/opcua/open62541_client.c | 14 +++++- simulator/opcua_simulator.c | 32 ++++++++----- simulator/opcua_tags_add.json | 80 +++++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 17 deletions(-) create mode 100644 simulator/opcua_tags_add.json diff --git a/neuron.yaml b/neuron.yaml index ad3e14c9..90b57f51 100644 --- a/neuron.yaml +++ b/neuron.yaml @@ -27,7 +27,7 @@ opcua: default_key_file: ./opcua-default-key.der host: localhost port: 4840 - username: gaoc01 + username: user password: 123456 cert_file: "" key_file: "" \ No newline at end of file diff --git a/plugins/mqtt/command/read_write.c b/plugins/mqtt/command/read_write.c index 450923b4..de07f169 100644 --- a/plugins/mqtt/command/read_write.c +++ b/plugins/mqtt/command/read_write.c @@ -145,6 +145,20 @@ static int wrap_read_response_json_object(neu_fixed_array_t * array, json->tags[i - 1].value.val_double = value; break; } + case NEU_DTYPE_BOOL: { + bool value; + neu_dvalue_get_bool(val, &value); + json->tags[i - 1].t = NEU_JSON_BOOL; + json->tags[i - 1].value.val_bool = value; + break; + } + case NEU_DTYPE_BIT: { + bool value; + neu_dvalue_get_uint8(val, &value); + json->tags[i - 1].t = NEU_JSON_BIT; + json->tags[i - 1].value.val_bit = value; + break; + } case NEU_DTYPE_CSTR: { char *value; neu_dvalue_get_ref_cstr(val, &value); diff --git a/plugins/opcua/opcua_handle.c b/plugins/opcua/opcua_handle.c index be95ad7d..166f6f21 100644 --- a/plugins/opcua/opcua_handle.c +++ b/plugins/opcua/opcua_handle.c @@ -125,8 +125,13 @@ static int read_response_array_generate(vector_t * datas, int type = data->type; switch (type) { - case NEU_DTYPE_BYTE: { - val = neu_dvalue_new(NEU_DTYPE_BYTE); + case NEU_DTYPE_INT8: { + val = neu_dvalue_new(NEU_DTYPE_INT8); + neu_dvalue_set_int8(val, data->value.value_int8); + break; + } + case NEU_DTYPE_UINT8: { + val = neu_dvalue_new(NEU_DTYPE_UINT8); neu_dvalue_set_uint8(val, data->value.value_uint8); break; } @@ -172,7 +177,7 @@ static int read_response_array_generate(vector_t * datas, } case NEU_DTYPE_DOUBLE: { val = neu_dvalue_new(NEU_DTYPE_DOUBLE); - neu_dvalue_set_float(val, data->value.value_double); + neu_dvalue_set_double(val, data->value.value_double); break; } case NEU_DTYPE_CSTR: { diff --git a/plugins/opcua/open62541_client.c b/plugins/opcua/open62541_client.c index 2dfc19de..d6b1235f 100644 --- a/plugins/opcua/open62541_client.c +++ b/plugins/opcua/open62541_client.c @@ -215,7 +215,7 @@ static int client_set_read_value(opcua_data_t *data, UA_Variant *value) // byte(OPCUA) -> uint8(Neuron) if (UA_Variant_hasScalarType(value, &UA_TYPES[UA_TYPES_BYTE])) { data->value.value_uint8 = *(UA_Byte *) value->data; - data->type = NEU_DTYPE_BYTE; + data->type = NEU_DTYPE_UINT8; return 0; } @@ -256,6 +256,18 @@ static int client_set_read_value(opcua_data_t *data, UA_Variant *value) return 0; } + if (UA_Variant_hasScalarType(value, &UA_TYPES[UA_TYPES_INT64])) { + data->value.value_int64 = *(UA_Int64 *) value->data; + data->type = NEU_DTYPE_INT64; + return 0; + } + + if (UA_Variant_hasScalarType(value, &UA_TYPES[UA_TYPES_UINT64])) { + data->value.value_uint64 = *(UA_UInt64 *) value->data; + data->type = NEU_DTYPE_UINT64; + return 0; + } + if (UA_Variant_hasScalarType(value, &UA_TYPES[UA_TYPES_FLOAT])) { data->value.value_float = *(UA_Float *) value->data; data->type = NEU_DTYPE_FLOAT; diff --git a/simulator/opcua_simulator.c b/simulator/opcua_simulator.c index df83e883..2fee84d0 100644 --- a/simulator/opcua_simulator.c +++ b/simulator/opcua_simulator.c @@ -111,16 +111,21 @@ static void data_uint32_add(UA_Server *server, char *name, char *node_id, node_attributes_set(server, name, node_id, UA_TYPES_UINT32, &u32, readonly); } -// UA_DateTime -// static void data_datetime_add(UA_Server *server, char *name, char *node_id, -// uint32_t data, const int readonly) -// { -// UNUSED(server); -// UNUSED(name); -// UNUSED(node_id); -// UNUSED(data); -// UNUSED(readonly); -// } +// UA_Int64 +static void data_int64_add(UA_Server *server, char *name, char *node_id, + int64_t data, const int readonly) +{ + UA_Int64 i64 = data; + node_attributes_set(server, name, node_id, UA_TYPES_INT64, &i64, readonly); +} + +// UA_UInt64 +static void data_uint64_add(UA_Server *server, char *name, char *node_id, + uint64_t data, const int readonly) +{ + UA_UInt64 u64 = data; + node_attributes_set(server, name, node_id, UA_TYPES_UINT64, &u64, readonly); +} // UA_Float static void data_float_add(UA_Server *server, char *name, char *node_id, @@ -229,13 +234,16 @@ int main(int argc, char **argv) &config->securityPolicies[config->securityPoliciesSize - 1].policyUri, 2, logins); - data_int8_add(server, "type_int8", "neu.type.int8", -10, 0); - data_uint8_add(server, "type_uint8", "neu.type.uint8", 10, 0); + data_int8_add(server, "type_int8", "neu.type_int8", -10, 0); + data_uint8_add(server, "type_uint8", "neu.type_uint8", 10, 0); data_bool_add(server, "type_bool", "neu.type_bool", true, 0); data_int16_add(server, "type_int16", "neu.type_int16", -1001, 0); data_uint16_add(server, "type_uint16", "neu.type_uint16", 1001, 0); data_int32_add(server, "type_int32", "neu.type_int32", -100, 0); data_uint32_add(server, "type_uint32", "neu.type_uint32", 111111, 0); + data_int64_add(server, "type_int64", "neu.type_int64", -10011111111111, 0); + data_uint64_add(server, "type_uint64", "neu.type_uint64", 5678710019198784, + 0); data_float_add(server, "type_float", "neu.type_float", 38.9, 0); data_double_add(server, "type_double", "neu.type_double", 100038.9999, 0); data_string_add(server, "type_cstr", "nue.type_cstr", "hello world!", 0); diff --git a/simulator/opcua_tags_add.json b/simulator/opcua_tags_add.json new file mode 100644 index 00000000..09654a59 --- /dev/null +++ b/simulator/opcua_tags_add.json @@ -0,0 +1,80 @@ +{ + "function": 13, + "uuid": "554f5fd8-f437-11eb-975c-7704b9e17821", + "node_id": 6, + "group_config_name": "config_opcua_sample", + "tags": [ + { + "name": "type_int8", + "address": "1!neu.type_int8", + "attribute": 1, + "type": 3 + }, + { + "name": "type_uint8", + "address": "1!neu.type_uint8", + "attribute": 1, + "type": 7 + }, + { + "name": "type_bool", + "address": "1!neu.type_bool", + "attribute": 1, + "type": 13 + }, + { + "name": "type_int16", + "address": "1!neu.type_int16", + "attribute": 1, + "type": 4 + }, + { + "name": "type_uint16", + "address": "1!neu.type_uint16", + "attribute": 1, + "type": 8 + }, + { + "name": "type_int32", + "address": "1!neu.type_int32", + "attribute": 1, + "type": 5 + }, + { + "name": "type_uint32", + "address": "1!neu.type_uint32", + "attribute": 1, + "type": 8 + }, + { + "name": "type_datetime", + "address": "1!neu.type_datetime", + "attribute": 1, + "type": 8 + }, + { + "name": "type_int64", + "address": "1!neu.type_int64", + "attribute": 1, + "type": 6 + }, + { + "name": "type_uint64", + "address": "1!neu.type_uint64", + "attribute": 1, + "type": 10 + }, + { + "name": "type_float", + "address": "1!neu.type_float", + "attribute": 1, + "type": 11 + }, + { + "name": "type_double", + "address": "1!neu.type_double", + "attribute": 1, + "type": 12 + } + ] +} \ No newline at end of file