# GatewayHook Powers

**URL:** <https://forum.inductiveautomation.com/t/gatewayhook-powers/9963>\
**Category:** 3rd Party Modules\
**Created:** [April 28, 2015, 1:54pm UTC](https://forum.inductiveautomation.com/t/gatewayhook-powers/9963 "2015-04-28T13:54:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nifemi](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nifemi](https://forum.inductiveautomation.com/u/nifemi)\
**Post date:** [April 28, 2015, 1:54pm UTC](https://forum.inductiveautomation.com/t/gatewayhook-powers/9963/1 "2015-04-28T13:54:04Z")

</div>

I have been attempting to start an installed driver and create a new device from inside the GatewayHook of my module, and  
I’ve had no success so far. Is this within the powers of the GatewayContext? I’d appreciate any clarification on what gateway activities I can achieve using the context, and any other tips in this area.

Thank you for your time.

---

<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:** [April 28, 2015, 2:48pm UTC](https://forum.inductiveautomation.com/t/gatewayhook-powers/9963/2 "2015-04-28T14:48:15Z")

</div>

There are no artificial limitations in place when programming in gateway scope. You can do this, it’s just not easy. What do you have so far?

---

<div class="post-metadata">

**Author:** ![nifemi](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nifemi](https://forum.inductiveautomation.com/u/nifemi)\
**Post date:** [April 28, 2015, 3:21pm UTC](https://forum.inductiveautomation.com/t/gatewayhook-powers/9963/3 "2015-04-28T15:21:53Z")

</div>

Thanks for your reply, I would say so far that “not easy” is an understatement… 🙂

I started by trying to go through the context, but no dice there. I am now looking at the DeviceRecord and DeviceConfiguration classes

---

<div class="post-metadata">

**Author:** ![nifemi](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nifemi](https://forum.inductiveautomation.com/u/nifemi)\
**Post date:** [April 29, 2015, 10:20am UTC](https://forum.inductiveautomation.com/t/gatewayhook-powers/9963/4 "2015-04-29T10:20:28Z")

</div>

Update:

I am now looking at recreating the process that takes place when a device is created in the gateway. I’m extending DriverContext, and creating a new DeviceSettingsRecord. The idea is to attempt to retrieve the DriverType, and create a new device that way.

---

<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:** [April 29, 2015, 4:16pm UTC](https://forum.inductiveautomation.com/t/gatewayhook-powers/9963/5 "2015-04-29T16:16:59Z")

</div>

[quote=“nifemi”]Update:

I am now looking at recreating the process that takes place when a device is created in the gateway. I’m extending DriverContext, and creating a new DeviceSettingsRecord. The idea is to attempt to retrieve the DriverType, and create a new device that way.[/quote]

You don’t need to extend DriverContext or anything, but creating new settings records is probably the right approach. I’ll try and whip up some example code that should push you in the right direction…

---

<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:** [April 29, 2015, 4:29pm UTC](https://forum.inductiveautomation.com/t/gatewayhook-powers/9963/6 "2015-04-29T16:29:59Z")

</div>

This is only going to work if you’re trying to add a driver that your module has provided, not one that another module provides.

deviceType is the type id that your DriverType registers the driver under.  
context is the GatewayContext  
MySettingsRecord is whatever the driver’s settings record class is.

```auto
    public void addDevice(String deviceType) {
        PersistenceSession session = context.getPersistenceInterface().getSession();

        try {
            DeviceSettingsRecord deviceRecord = context.getPersistentInterface()
                    .createNew(DeviceSettingsRecord.META, session.getDataSet());

            deviceRecord.setName(...);
            deviceRecord.setEnabled(...);
            deviceRecord.setType(deviceType);

            MySettingsRecord r = context.getPersistenceInterface()
                    .createNew(MyDriverSettings.META, session.getDataSet());

            r.setReference(MyDriverSettings.DeviceSettings, deviceRecord);
            r.set(...); // Set all your other properties

            session.commitAndDetachDataSet();

            context.getPersistenceInterface().notifyRecordAdded(deviceRecord);
        } catch (Exception e) {
            // Do something here...
        }
    }
```

---

<div class="post-metadata">

**Author:** ![nifemi](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@nifemi](https://forum.inductiveautomation.com/u/nifemi)\
**Post date:** [April 30, 2015, 9:43am UTC](https://forum.inductiveautomation.com/t/gatewayhook-powers/9963/7 "2015-04-30T09:43:20Z")

</div>

Thanks!!
