How to write a known Extension Object #691
Replies: 2 comments 7 replies
-
Hi @magiconair, Do you have any thought or tip on this? Thanks. |
Beta Was this translation helpful? Give feedback.
-
It's seems that we are not on the same page, but thanks again for your quick reply and the sample code! Here are more contexts. Below is some minimal sample code, hope we can be in sync. func WriteValue(id *ua.NodeID, input interface{}) error {
// get the DataType of the node
variableNode := c.Client.Node(id)
dataTypeNodeId, err := getDataType(variableNode, ctx)
if err != nil {
return fmt.Errorf("failed to get the DataType of node id \"%s\": %w", nodeId, err)
}
// just assume that we identified that the data type node id is i=14533, which is a extension object
// get the converted value to create new variant
validValue := toExtensionObject(input)
v, err := ua.NewVariant(validValue)
if err != nil {
log.Fatalf("invalid value: %v", err)
}
req := &ua.WriteRequest{
NodesToWrite: []*ua.WriteValue{
{
NodeID: id,
AttributeID: ua.AttributeIDValue,
Value: &ua.DataValue{
EncodingMask: ua.DataValueValue,
Value: v,
},
},
},
}
resp, err := c.Client.Write(ctx, req)
if err != nil {
return fmt.Errorf("write value to node id=%s failed, error: %v", nodeId, err)
}
}
// toExtensionObject converts the given input value to ExtensionObject
func toExtensionObject(input interface{}) *ua.ExtensionObject {
fmt.Printf("input value: %v, data type: %T", input, input)
// How to convert the input value to KeyValuePair struct
// so that I can get a valid ExtensionObject with correct TypeID?
return ua.NewExtensionObject(input)
}
// standard output => input value: map[Key:test Value:123], data type: map[string]interface {}
// error => write value to node id=ns=3;i=1020 failed, error: opcua: unsupported type: map[string]interface {} The If I have made a mistake or written something incorrectly, please let me know. |
Beta Was this translation helpful? Give feedback.
-
Hope I'm not asking a dumb question.
There are some known structs generated in extobjs_gen.go, but how to create an
ExtensionObject
with NewExtensionObject for one of these types?The ExtensionObjectTypeID function only accepts a value whose type has already been registered in the
TypeRegistry
, but I am not supposed to create a struct with matched type at this point.There are two approaches that I can only come up with:
Take the KeyValuePair (i=14533) for example
TypeRegistry
to get an instance of the matched type, but its types prop is private at the moment, there is no method for us to get the instance of the matched type.I have searched for that and found this, which is specific to the custom extension object I guess?
I have also got this discussion, but still need some example to know how to implement it.
I would like to know if there is any example or suggestion about how to do that.
Any help would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions