Accessing Device Object from Module in OPC-UA Implementation


I'm currently developing a module based on the opc-ua-device example. While I've managed to access my device settings file (which extends PersistentRecord) using the GatewayContext as shown below:

PersistenceInterface pe = context.getPersistenceInterface();
PersistenceSession session = pe.getSession();
SQuery<ExampleDeviceSettings> query = new SQuery<>(ExampleDeviceSettings.META).eq(ExampleDeviceSettings.Identifier, currentIdentifier);
ExampleDeviceSettings result = session.queryOne(query);

I'm struggling to understand how I can access the actual device that extends the ManagedDevice class from my module which extends the AbstractDeviceModuleHook class.

If this is documented, kindly point me in the right direction. Otherwise, any assistance on this matter would be greatly appreciated.

Thank you.

I think you may be a little turned around.

There is no Device instance until a user creates one. If they create an instance of a device type provided by your module, the DeviceType subclass provided by the hook is used to instantiate an instance.

If for some reason you need access to Device instances after they've been instantiated, you can create a static field in your module hook class or in the Device class itself and then add/remove the devices as they are started up and shut down.

Something like this:

public static final Map<String, MyDevice> INSTANCES = new ConcurrentHashMap<>();

then retrieve the instances as needed, which should be there as long as the device is "live".

1 Like

Thanks Kevin, that helped.

I`m now using DeviceType and on method createDevice() I add my device to HashMap.
However, if the device is deleted by the user which call back can I use to remove device from my HashMap?

The device's own shutdown method should remove itself from the hashmap.

1 Like