GatewayHook Powers

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.

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?

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

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

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=“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…

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.

    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...
        }
    }

Thanks!!