# Accessing Device Object from Module in OPC-UA Implementation

**URL:** <https://forum.inductiveautomation.com/t/accessing-device-object-from-module-in-opc-ua-implementation/78385>\
**Category:** Module Development\
**Created:** [August 25, 2023, 11:49am UTC](https://forum.inductiveautomation.com/t/accessing-device-object-from-module-in-opc-ua-implementation/78385 "2023-08-25T11:49:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Bergman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dan_bergman/32/51525_2.png) [@Dan\_Bergman](https://forum.inductiveautomation.com/u/Dan_Bergman)\
**Post date:** [August 25, 2023, 11:49am UTC](https://forum.inductiveautomation.com/t/accessing-device-object-from-module-in-opc-ua-implementation/78385/1 "2023-08-25T11:49:50Z")

</div>

Hello,

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 25, 2023, 11:54am UTC](https://forum.inductiveautomation.com/t/accessing-device-object-from-module-in-opc-ua-implementation/78385/2 "2023-08-25T11:54:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 25, 2023, 12:01pm UTC](https://forum.inductiveautomation.com/t/accessing-device-object-from-module-in-opc-ua-implementation/78385/3 "2023-08-25T12:01:40Z")

</div>

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:

```java
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".

---

<div class="post-metadata">

**Author:** ![Dan\_Bergman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dan_bergman/32/51525_2.png) [@Dan\_Bergman](https://forum.inductiveautomation.com/u/Dan_Bergman)\
**Post date:** [August 30, 2023, 2:21pm UTC](https://forum.inductiveautomation.com/t/accessing-device-object-from-module-in-opc-ua-implementation/78385/4 "2023-08-30T14:21:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 30, 2023, 2:25pm UTC](https://forum.inductiveautomation.com/t/accessing-device-object-from-module-in-opc-ua-implementation/78385/5 "2023-08-30T14:25:53Z")

</div>

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