Search in sources :

Example 1 with CULMode

use of org.openhab.io.transport.cul.CULMode in project openhab1-addons by openhab.

the class CULManager method getOpenCULHandler.

/**
     * Get CULHandler for the given device in the given mode. The same
     * CULHandler can be returned multiple times if you ask multiple times for
     * the same device in the same mode. It is not possible to obtain a
     * CULHandler of an already openend device for another RF mode.
     *
     * @param config
     *            The configuration for the handler.
     * @return A CULHandler to communicate with the culfw based device.
     * @throws CULDeviceException
     */
public <T extends CULConfig> CULHandlerInternal<T> getOpenCULHandler(T config) throws CULDeviceException {
    CULMode mode = config.getMode();
    String deviceName = config.getDeviceName();
    logger.debug("Trying to open device " + deviceName + " in mode " + mode.toString());
    synchronized (openDevices) {
        if (openDevices.containsKey(deviceName)) {
            @SuppressWarnings("unchecked") CULHandlerInternal<T> handler = (CULHandlerInternal<T>) openDevices.get(deviceName);
            if (handler.getConfig().equals(config)) {
                logger.debug("Device " + deviceName + " is already open in mode " + mode.toString() + ", returning already openend handler");
                return handler;
            } else {
                throw new CULDeviceException("The device " + deviceName + " is already open in mode " + mode.toString());
            }
        }
        CULHandlerInternal<T> handler = createNewHandler(config);
        openDevices.put(deviceName, handler);
        return handler;
    }
}
Also used : CULDeviceException(org.openhab.io.transport.cul.CULDeviceException) CULMode(org.openhab.io.transport.cul.CULMode)

Example 2 with CULMode

use of org.openhab.io.transport.cul.CULMode in project openhab1-addons by openhab.

the class CULManager method createNewHandler.

private <T extends CULConfig> CULHandlerInternal<T> createNewHandler(T config) throws CULDeviceException {
    String deviceType = config.getDeviceType();
    CULMode mode = config.getMode();
    logger.debug("Searching class for device type " + deviceType);
    @SuppressWarnings("unchecked") Class<? extends CULHandlerInternal<T>> culHandlerclass = (Class<? extends CULHandlerInternal<T>>) deviceTypeClasses.get(deviceType);
    if (culHandlerclass == null) {
        throw new CULDeviceException("No class for the device type " + deviceType + " is registred");
    }
    Class<?>[] constructorParametersTypes = { CULConfig.class };
    Object[] parameters = { config };
    try {
        Constructor<? extends CULHandlerInternal<T>> culHanlderConstructor = culHandlerclass.getConstructor(constructorParametersTypes);
        CULHandlerInternal<T> culHandler = culHanlderConstructor.newInstance(parameters);
        List<String> initCommands = mode.getCommands();
        if (!(culHandler instanceof CULHandlerInternal)) {
            logger.error("Class " + culHandlerclass.getCanonicalName() + " does not implement the internal interface");
            throw new CULDeviceException("This CULHandler class does not implement the internal interface: " + culHandlerclass.getCanonicalName());
        }
        CULHandlerInternal<?> internalHandler = culHandler;
        internalHandler.open();
        for (String command : initCommands) {
            internalHandler.sendWithoutCheck(command);
        }
        return culHandler;
    } catch (SecurityException e1) {
        throw new CULDeviceException("Not allowed to access the constructor ", e1);
    } catch (NoSuchMethodException e1) {
        throw new CULDeviceException("Can't find the constructor to build the CULHandler", e1);
    } catch (IllegalArgumentException e) {
        throw new CULDeviceException("Invalid arguments for constructor. CULConfig: " + config, e);
    } catch (InstantiationException e) {
        throw new CULDeviceException("Can't instantiate CULHandler object", e);
    } catch (IllegalAccessException e) {
        throw new CULDeviceException("Can't instantiate CULHandler object", e);
    } catch (InvocationTargetException e) {
        throw new CULDeviceException("Can't instantiate CULHandler object", e);
    } catch (CULCommunicationException e) {
        throw new CULDeviceException("Can't initialise RF mode", e);
    }
}
Also used : CULDeviceException(org.openhab.io.transport.cul.CULDeviceException) CULCommunicationException(org.openhab.io.transport.cul.CULCommunicationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CULMode(org.openhab.io.transport.cul.CULMode)

Aggregations

CULDeviceException (org.openhab.io.transport.cul.CULDeviceException)2 CULMode (org.openhab.io.transport.cul.CULMode)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CULCommunicationException (org.openhab.io.transport.cul.CULCommunicationException)1