locka99 / opcua

A client and server implementation of the OPC UA specification written in Rust
Mozilla Public License 2.0
476 stars 129 forks source link

[HELP] Write custom datatype value to variable #230

Closed xujiesh0510 closed 1 year ago

xujiesh0510 commented 1 year ago

hi, i'm a newbie in rust. i have a variable ,its value type is custom datatype

// OPCUA for Rust // SPDX-License-Identifier: MPL-2.0 // Copyright (C) 2017-2022 Adam Lock // // This file was autogenerated from BatchPlant.Types.bsd by tools/schema/gen_types.js // // DO NOT EDIT THIS FILE

[derive(Debug, Clone, PartialEq)]

pub struct DataTypeChildOne { pub field_1: UAString, pub field_2: UAString }

impl MessageInfo for DataTypeParent { fn object_id(&self) -> ObjectId { ObjectId::DataTypeChildOne_Encoding_DefaultBinary ?????? //ObjectId do not have value DataTypeChildOne_Encoding_DefaultBinary } }

[derive(Debug, Clone, PartialEq)]

pub struct DataTypeParent { pub field_1: DataTypeChildOne, pub field_2: DataTypeChildOne, }

Qustion1 : how do i write/read custom datatype value to variable ?


fn add_variable_17(address_space: &mut AddressSpace) {
    // Variable
    let browse_name = "1:str";
    let display_name = "str";
    let value = Variant::Empty;
    let node_id = NodeId::new(2, 19);
    let mut node = Variable::new_data_value(&node_id, browse_name, display_name, NodeId::new(0, 12), None, None, value);

            let getter = AttrFnGetter::new(
                move |_, _, _, _, _, _| -> Result<Option<DataValue>, StatusCode> {

        Ok(Some(DataValue::new_now(  ???  ))) //  ??? i dont know how to convert DataTypeParent  to  Variant
                },
            );
            node.set_value_getter(Arc::new(Mutex::new(getter)));
 .....
}

In c# opcua server ,it's quite easy: variable.Value = new DataTypeParent { } // beacuse Value property is object type

Qustion2 : in order to let client know custom dataType, how to register custom data type to opcua ? (using StructureDefinition? how?)

  <UADataType NodeId="ns=1;i=14" BrowseName="1:DataTypeChildOne">
    <DisplayName>DataTypeChildOne</DisplayName>
    <References>
      <Reference ReferenceType="HasSubtype" IsForward="false">i=22</Reference>
    </References>
    <Definition Name="1:DataTypeChildOne">
      <Field Name="Field1" DataType="i=12">
        <Description>Field1</Description>
      </Field>
      <Field Name="Field2" DataType="i=12">
        <Description>Field2</Description>
      </Field>
    </Definition>
  </UADataType>

[ // Process definitions] (https://github.com/locka99/opcua/blob/b2c6e70b84511e975f29a144b472f1d67090c4fe/tools/schema/nodeset.js#L373) Definition node seems not processed . how do I register manually?

Qustion3 : how to convert a json string to my custom types(eg. DataTypeParent ) ?

Many thanks.

xujiesh0510 commented 1 year ago

After a few days' digging :

when client request DataTypeDefinition attribute value of a DataTypeNode, server shoud return the DataType information

request:

image

response:

image

Instead ,when client request DataTypeDefinition attribute value of a DataTypeNode, server just return None

image