nauful / LibUA

Open-source OPC UA client and server library
Apache License 2.0
262 stars 94 forks source link

[Question] Guidance on including hardware measurements in sample server #72

Closed pspeybro closed 2 years ago

pspeybro commented 2 years ago

I was looking at the code of the example server to provide some (hardware) sensordata, but it would be nice to have a little more documentation on how to extend the example.

In the sample, the main program is updating data in the OPC UA server by calling the app.PlayRow(); from the DemoApplication. Is this the recommended way to provide external data to the OPC server? In app.PlayRow(), the "new data" is generated inside this method, but normally the data would come from somewhere else (sensors, measuring devices, calculations, ...).

Suppose I have some methods to fetch data from a few sensors/devices that are attached to my raspberry pi (System.Device.Gpio library). Where should I connect these methods to the server?

Should I just have a timer like in the sample that fetches the sensor data and then sends it to the DemoApplication with var sensordata = ReadSensor(sensorId); app.UpdateSensor(sensordata.SensorId, sensordata.Value, sensordata.Timestamp); where UpdateSensor would call MonitorNotifyDataChange(node.Id, new DataValue(...)) Or is it better to include all this inside the DemoApplication class and have this class manage the timers and read the sensors on its own?

What is the recommended way to handle Data writes to the server (for example to set a relay). Provide the DemoApplication with some callbacks (change relay pin for example) that can be called depending on the nodeId/AttributeId in HandleDataWrite?

Are there any more advanced examples out there?

nauful commented 2 years ago

Most of the devices I worked with had two general methods:

  1. Polling - Read data on a timer (similar to PlayRow). Instead of generating new data, read current data from the device, compare to current value and write if different.
  2. Message-based - Subscribe to a publishing channel on the device, and write to the UA node whenever a publish notification is recieved.

The first can run entirely inside the DemoApplication class. The second needs your callback code to call DemoApplication when updated.

pspeybro commented 2 years ago

Ok, thank you, I will look into it.