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;
}
}
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);
}
}
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);
}
}
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
}
}
}
Aggregations