Search in sources :

Example 1 with CULDeviceException

use of org.openhab.io.transport.cul.CULDeviceException 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 CULDeviceException

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

the class CULSerialHandlerImpl method openHardware.

@Override
protected void openHardware() throws CULDeviceException {
    String deviceName = config.getDeviceAddress();
    logger.debug("Opening serial CUL connection for {}", deviceName);
    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(deviceName);
        if (portIdentifier.isCurrentlyOwned()) {
            throw new CULDeviceException("The port " + deviceName + " is currenty used by " + portIdentifier.getCurrentOwner());
        }
        CommPort port = portIdentifier.open(this.getClass().getName(), 2000);
        if (!(port instanceof SerialPort)) {
            throw new CULDeviceException("The device " + deviceName + " is not a serial port");
        }
        serialPort = (SerialPort) port;
        serialPort.setSerialPortParams(config.getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1, config.getParityMode());
        InputStream is = serialPort.getInputStream();
        OutputStream os = serialPort.getOutputStream();
        synchronized (serialPort) {
            br = new BufferedReader(new InputStreamReader(is));
            bw = new BufferedWriter(new OutputStreamWriter(os));
        }
        serialPort.notifyOnDataAvailable(true);
        logger.debug("Adding serial port event listener");
        serialPort.addEventListener(this);
    } catch (NoSuchPortException e) {
        throw new CULDeviceException(e);
    } catch (PortInUseException e) {
        throw new CULDeviceException(e);
    } catch (UnsupportedCommOperationException e) {
        throw new CULDeviceException(e);
    } catch (IOException e) {
        throw new CULDeviceException(e);
    } catch (TooManyListenersException e) {
        throw new CULDeviceException(e);
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) InputStreamReader(java.io.InputStreamReader) CommPortIdentifier(gnu.io.CommPortIdentifier) CULDeviceException(org.openhab.io.transport.cul.CULDeviceException) CommPort(gnu.io.CommPort) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) TooManyListenersException(java.util.TooManyListenersException) NoSuchPortException(gnu.io.NoSuchPortException) PortInUseException(gnu.io.PortInUseException) SerialPort(gnu.io.SerialPort) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter)

Example 3 with CULDeviceException

use of org.openhab.io.transport.cul.CULDeviceException 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)

Example 4 with CULDeviceException

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

the class CULNetworkHandlerImpl method openHardware.

@Override
protected void openHardware() throws CULDeviceException {
    String deviceName = config.getDeviceAddress();
    logger.debug("Trying to open CUN with deviceName {}", deviceName);
    URI uri;
    try {
        uri = new URI("cul://" + deviceName);
        String host = uri.getHost();
        int port = uri.getPort() == -1 ? CUN_DEFAULT_PORT : uri.getPort();
        if (uri.getHost() == null || uri.getPort() == -1) {
            throw new CULDeviceException("Could not parse host:port from " + deviceName);
        }
        this.address = new InetSocketAddress(host, port);
    } catch (URISyntaxException e) {
        throw new CULDeviceException("Could not parse host:port from " + deviceName, e);
    }
    thread.start();
    // is considered ready.
    while (!connected.get()) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        // Ignore
        }
    }
}
Also used : CULDeviceException(org.openhab.io.transport.cul.CULDeviceException) InetSocketAddress(java.net.InetSocketAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

CULDeviceException (org.openhab.io.transport.cul.CULDeviceException)4 CULMode (org.openhab.io.transport.cul.CULMode)2 CommPort (gnu.io.CommPort)1 CommPortIdentifier (gnu.io.CommPortIdentifier)1 NoSuchPortException (gnu.io.NoSuchPortException)1 PortInUseException (gnu.io.PortInUseException)1 SerialPort (gnu.io.SerialPort)1 UnsupportedCommOperationException (gnu.io.UnsupportedCommOperationException)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 TooManyListenersException (java.util.TooManyListenersException)1