feat(inputs.opcua): add use regular reads workaround (#11630)

This commit is contained in:
R290 2022-09-26 19:33:59 +02:00 committed by GitHub
parent 2b37d7e508
commit c78139c8b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 11 deletions

View File

@ -95,6 +95,9 @@ Plugin minimum tested version: 1.16
# [inputs.opcua.workarounds]
## Set additional valid status codes, StatusOK (0x0) is always considered valid
# additional_valid_status_codes = ["0xC0"]
## Use regular reads instead of registered reads
# use_regular_reads = false
```
## Node Configuration
@ -152,6 +155,10 @@ This example group configuration has two groups with two nodes each:
]
```
## Metrics
Metrics are produced according to the defined node ID and group configuration.
## Example Output
```text

View File

@ -26,6 +26,7 @@ var sampleConfig string
type OpcuaWorkarounds struct {
AdditionalValidStatusCodes []string `toml:"additional_valid_status_codes"`
UseRegularReads bool `toml:"use_regular_reads"`
}
// OpcUA type
@ -365,17 +366,31 @@ func Connect(o *OpcUA) error {
return fmt.Errorf("error in Client Connection: %s", err)
}
regResp, err := o.client.RegisterNodes(&ua.RegisterNodesRequest{
NodesToRegister: o.nodeIDs,
})
if err != nil {
return fmt.Errorf("registerNodes failed: %v", err)
}
if !o.Workarounds.UseRegularReads {
regResp, err := o.client.RegisterNodes(&ua.RegisterNodesRequest{
NodesToRegister: o.nodeIDs,
})
if err != nil {
return fmt.Errorf("registerNodes failed: %v", err)
}
o.req = &ua.ReadRequest{
MaxAge: 2000,
NodesToRead: readvalues(regResp.RegisteredNodeIDs),
TimestampsToReturn: ua.TimestampsToReturnBoth,
o.req = &ua.ReadRequest{
MaxAge: 2000,
TimestampsToReturn: ua.TimestampsToReturnBoth,
NodesToRead: readvalues(regResp.RegisteredNodeIDs),
}
} else {
var nodesToRead []*ua.ReadValueID
for _, nid := range o.nodeIDs {
nodesToRead = append(nodesToRead, &ua.ReadValueID{NodeID: nid})
}
o.req = &ua.ReadRequest{
MaxAge: 2000,
TimestampsToReturn: ua.TimestampsToReturnBoth,
NodesToRead: nodesToRead,
}
}
err = o.getData()
@ -438,7 +453,7 @@ func (o *OpcUA) getData() error {
resp, err := o.client.Read(o.req)
if err != nil {
o.ReadError.Incr(1)
return fmt.Errorf("RegisterNodes Read failed: %v", err)
return fmt.Errorf("Read failed: %w", err)
}
o.ReadSuccess.Incr(1)
for i, d := range resp.Results {

View File

@ -136,6 +136,31 @@ func TestClient1Integration(t *testing.T) {
t.Errorf("Tag: %s has value: %v", o.nodes[i].tag.FieldName, v.Value)
}
}
// test regular reads workaround
o.Workarounds.UseRegularReads = true
for i := range o.nodeData {
o.nodeData[i] = OPCData{}
}
err = Connect(&o)
if err != nil {
t.Fatalf("Connect Error: %s", err)
}
for i, v := range o.nodeData {
if v.Value != nil {
types := reflect.TypeOf(v.Value)
value := reflect.ValueOf(v.Value)
compare := fmt.Sprintf("%v", value.Interface())
if compare != testopctags[i].Want {
t.Errorf("Tag %s: Values %v for type %s does not match record", o.nodes[i].tag.FieldName, value.Interface(), types)
}
} else if testopctags[i].Want != nil {
t.Errorf("Tag: %s has value: %v", o.nodes[i].tag.FieldName, v.Value)
}
}
}
func MapOPCTag(tags OPCTags) (out NodeSettings) {

View File

@ -85,3 +85,6 @@
# [inputs.opcua.workarounds]
## Set additional valid status codes, StatusOK (0x0) is always considered valid
# additional_valid_status_codes = ["0xC0"]
## Use regular reads instead of registered reads
# use_regular_reads = false