Search in sources :

Example 1 with JsonParserFactory

use of com.json.parsers.JsonParserFactory in project openhab1-addons by openhab.

the class MCP23017GenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    MCP23017BindingConfig config = new MCP23017BindingConfig();
    // parse bindingConfig here ...
    /*
         * Configuration string should be a json in the form:
         * Contact Test1 "Test 1" (Tests) { mcp23017="{ address:20, pin:'A0', mode:'DIGITAL_INPUT'}" }
         * Switch Test2 "Test 2" (Tests) { mcp23017="{ address:20, pin:'B0', mode:'DIGITAL_OUTPUT', defaultState:'LOW'}"
         * }
         */
    JsonParserFactory factory = JsonParserFactory.getInstance();
    JSONParser parser = factory.newJsonParser();
    @SuppressWarnings("unchecked") Map<String, Object> jsonData = parser.parseJson(bindingConfig);
    try {
        logger.debug("Process binding configuration in context {}", context);
        config.setBusAddress(Integer.parseInt((String) jsonData.get("address"), 16));
        config.setPin((Pin) MCP23017Pin.class.getField("GPIO_" + (String) jsonData.get("pin")).get(null));
        config.setPinMode(PinMode.valueOf((String) jsonData.get("mode")));
        if (item instanceof SwitchItem) {
            config.setDefaultState(PinState.valueOf((String) jsonData.get("defaultState")));
        }
    } catch (IllegalArgumentException exception) {
        final String message = "Illegal argument exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (IllegalAccessException exception) {
        final String message = "Illegal access exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (NoSuchFieldException exception) {
        final String message = "No such field exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (SecurityException exception) {
        final String message = "Security exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    }
    addBindingConfig(item, config);
}
Also used : MCP23017Pin(com.pi4j.gpio.extension.mcp.MCP23017Pin) JsonParserFactory(com.json.parsers.JsonParserFactory) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) JSONParser(com.json.parsers.JSONParser) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 2 with JsonParserFactory

use of com.json.parsers.JsonParserFactory in project openhab1-addons by openhab.

the class MCP3424GenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    MCP3424BindingConfig config = new MCP3424BindingConfig(item);
    // parse bindingConfig here ...
    /*
         * Configuration string should be a json in the form:
         * Number Test1 "Test 1" (Tests) { mcp3424="{ address:6C, pin:'CH0', gain:1, resolution:12" }
         * Dimmer Test2 "Test 2" (Tests) { mcp3424="{ address:6C, pin:'CH1', gain:1, resolution:12" }
         */
    JsonParserFactory factory = JsonParserFactory.getInstance();
    JSONParser parser = factory.newJsonParser();
    @SuppressWarnings("unchecked") Map<String, Object> jsonData = parser.parseJson(bindingConfig);
    try {
        logger.debug("processingBindingConfiguration in context {}", context);
        config.setBusAddress(Integer.parseInt((String) jsonData.get("address"), 16));
        config.setPin((Pin) MCP3424Pin.class.getField("GPIO_" + (String) jsonData.get("pin")).get(null));
    } catch (IllegalArgumentException exception) {
        final String message = "Illegal argument exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (IllegalAccessException exception) {
        final String message = "Illegal access exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (NoSuchFieldException exception) {
        final String message = "No such field exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (SecurityException exception) {
        final String message = "Security exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    }
    try {
        config.setGain(Integer.parseInt((String) jsonData.get("gain"), 10));
    } catch (Exception e) {
        logger.info("No configuration for gain. Using default: {}", config.getGain());
    }
    try {
        config.setResolution(Integer.parseInt((String) jsonData.get("resolution"), 10));
    } catch (Exception e) {
        logger.info("No configuration for resolution. Using default: {}", config.getResolution());
    }
    addBindingConfig(item, config);
}
Also used : MCP3424Pin(com.pi4j.gpio.extension.mcp.MCP3424Pin) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) JsonParserFactory(com.json.parsers.JsonParserFactory) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) JSONParser(com.json.parsers.JSONParser)

Aggregations

JSONParser (com.json.parsers.JSONParser)2 JsonParserFactory (com.json.parsers.JsonParserFactory)2 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)2 MCP23017Pin (com.pi4j.gpio.extension.mcp.MCP23017Pin)1 MCP3424Pin (com.pi4j.gpio.extension.mcp.MCP3424Pin)1 SwitchItem (org.openhab.core.library.items.SwitchItem)1