Search in sources :

Example 1 with EBusTCPConnector

use of org.openhab.binding.ebus.internal.connection.EBusTCPConnector in project openhab1-addons by openhab.

the class EBusBinding method updated.

/*
     * (non-Javadoc)
     *
     * @see org.osgi.service.cm.ManagedService#updated(java.util.Dictionary)
     */
@Override
public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
    logger.info("Update eBus Binding configuration ...");
    if (properties == null || properties.isEmpty()) {
        throw new RuntimeException("No properties in openhab.cfg set!");
    }
    try {
        // stop last connector-thread if active
        stopConnector();
        // check to ensure that it is available
        checkConfigurationProvider();
        // clear current configuration
        configurationProvider.clear();
        // load parser from default url
        parser = new EBusTelegramParser(configurationProvider);
        URL configurationUrl = null;
        String parsers = (String) properties.get("parsers");
        if (StringUtils.isEmpty(parsers)) {
            // set to current stable configurations as default
            parsers = "common";
        }
        for (String elem : parsers.split(",")) {
            configurationUrl = null;
            // check for keyword custom to load custom configuration
            if (elem.trim().equals("custom")) {
                String parserUrl = (String) properties.get("parserUrl");
                if (parserUrl != null) {
                    logger.debug("Load custom eBus Parser with url {}", parserUrl);
                    configurationUrl = new URL(parserUrl);
                }
            } else {
                logger.debug("Load eBus Parser Configuration \"{}\" ...", elem.trim());
                String filename = "src/main/resources/" + elem.trim() + "-configuration.json";
                Bundle bundle = FrameworkUtil.getBundle(EBusBinding.class);
                configurationUrl = bundle.getResource(filename);
                if (configurationUrl == null) {
                    logger.error("Unable to load file {} ...", elem.trim() + "-configuration.json");
                }
            }
            if (configurationUrl != null) {
                configurationProvider.loadConfigurationFile(configurationUrl);
            }
        }
        // check minimal config
        if (properties.get("serialPort") != null && properties.get("hostname") != null) {
            throw new ConfigurationException("hostname", "Set property serialPort or hostname, not both!");
        }
        // use the serial connector
        if (StringUtils.isNotEmpty((String) properties.get("serialPort"))) {
            try {
                // load class by reflection to keep gnu.io (serial) optional. Declarative Services causes an
                // class not found exception, also if serial is not used!
                // FIXME: Is there a better way to avoid that a class not found exception?
                @SuppressWarnings("unchecked") Class<AbstractEBusWriteConnector> _tempClass = (Class<AbstractEBusWriteConnector>) EBusBinding.class.getClassLoader().loadClass("org.openhab.binding.ebus.internal.connection.EBusSerialConnector");
                Constructor<AbstractEBusWriteConnector> constructor = _tempClass.getDeclaredConstructor(String.class);
                connector = constructor.newInstance((String) properties.get("serialPort"));
            } catch (ClassNotFoundException e) {
                logger.error(e.toString(), e);
            } catch (NoSuchMethodException e) {
                logger.error(e.toString(), e);
            } catch (SecurityException e) {
                logger.error(e.toString(), e);
            } catch (InstantiationException e) {
                logger.error(e.toString(), e);
            } catch (IllegalAccessException e) {
                logger.error(e.toString(), e);
            } catch (IllegalArgumentException e) {
                logger.error(e.toString(), e);
            } catch (InvocationTargetException e) {
                logger.error(e.toString(), e);
            }
        } else if (StringUtils.isNotEmpty((String) properties.get("hostname"))) {
            // use the tcp-ip connector
            connector = new EBusTCPConnector((String) properties.get("hostname"), Integer.parseInt((String) properties.get("port")));
        }
        // Set eBus sender id or default 0xFF
        if (StringUtils.isNotEmpty((String) properties.get("senderId"))) {
            connector.setSenderId(EBusUtils.toByte((String) properties.get("senderId")));
        }
        if (properties.get("record") != null) {
            String debugWriterMode = (String) properties.get("record");
            logger.info("Enable CSV writer for eBUS {}", debugWriterMode);
            debugWriter = new EBusTelegramCSVWriter();
            debugWriter.openInUserData("ebus-" + debugWriterMode + ".csv");
            parser.setDebugCSVWriter(debugWriter, debugWriterMode);
        }
        // add event listener
        connector.addEBusEventListener(this);
        // start thread
        connector.start();
        // set the new connector
        commandProcessor.setConnector(connector);
        commandProcessor.setConfigurationProvider(configurationProvider);
    } catch (MalformedURLException e) {
        logger.error(e.toString(), e);
    } catch (IOException e) {
        throw new ConfigurationException("general", e.toString(), e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Bundle(org.osgi.framework.Bundle) IOException(java.io.IOException) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException) EBusTelegramParser(org.openhab.binding.ebus.internal.parser.EBusTelegramParser) AbstractEBusWriteConnector(org.openhab.binding.ebus.internal.connection.AbstractEBusWriteConnector) ConfigurationException(org.osgi.service.cm.ConfigurationException) EBusTCPConnector(org.openhab.binding.ebus.internal.connection.EBusTCPConnector) EBusTelegramCSVWriter(org.openhab.binding.ebus.internal.parser.EBusTelegramCSVWriter)

Aggregations

IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 AbstractEBusWriteConnector (org.openhab.binding.ebus.internal.connection.AbstractEBusWriteConnector)1 EBusTCPConnector (org.openhab.binding.ebus.internal.connection.EBusTCPConnector)1 EBusTelegramCSVWriter (org.openhab.binding.ebus.internal.parser.EBusTelegramCSVWriter)1 EBusTelegramParser (org.openhab.binding.ebus.internal.parser.EBusTelegramParser)1 Bundle (org.osgi.framework.Bundle)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1