digitaltwinconsortium / UA-CloudTwin

A cloud-based Digital Twin Definition Language (DTDL) adapter for OPC UA data.
MIT License
23 stars 8 forks source link

Index out of range in updateAssetTelemetry in the ADTClient #5

Closed dammen closed 1 year ago

dammen commented 1 year ago

https://github.com/digitaltwinconsortium/UA-CloudTwin/blob/420384cddc93a8c9e09c93015126bc431198ba31/Applications/ADTClient.cs#L264

When running locally the ADTClient gets an index out of range error on line 264. After inspecting the array, it seems that you have written the code for 1-based indexed arrays.

Are my assumption correct ? And if so, is there a particular reason for this ?

If not, is there anything wrong with the telemetryName that is passed into the function: "opc.tcp://111.111.11.111:1111_11111111_somename;sensor-name;i=11111"

barnstee commented 1 year ago

Thanks for reporting, that was a plain "off by 1" bug. I've just checked in a fix. :-)

dammen commented 1 year ago

Thanks for the quick reply. I see that you have changed the condition check, but I belive you need to change the indexing of the parts-array instead. At least in our case the first element is endpointUrl, second is DisplayName and the third element is the node id. For example: ["opc.tcp://111.111.11.111:1111_11111111_somename", "sensor-name", "i=11111"]. Thus to get the correct properties in the digital twin we needed to rewrite the code as following:

 if (parts.Length > 1)
  {
      updateTwinData.AppendReplace("/OPCUADisplayName", parts[1]);
  }

  if (parts.Length > 2)
  {
      updateTwinData.AppendReplace("/OPCUANodeId", parts[0] + parts[2]);
  }

Edit: I have a branch with the changes applied, but I dont have permission to push to the repo. If you give me access, then I can create a pull request for the issue. There is also some other problems we have encountered which I then also could add PR for.

barnstee commented 1 year ago

OK, I see what the problem is. You are setting the name property in the metadata message to the EndpointUrl of your OPC UA server, while UA Cloud Twin expects OPCUAApplicationURI;OPCUANamespaceURI. The EndpointURL of an OPC UA server is not globally unique while the combination of OPCUAApplicationURI;OPCUANamespaceURI is. Please modify our OPC UA Publisher accordingly.

I've added a section in the readme explaining all this stuff. :-)

dammen commented 1 year ago

okay, then it makes sense. Thanks for clarifying this!

barnstee commented 1 year ago

Closing as the issue was resolved.