Search in sources :

Example 1 with StateDP

use of tuwien.auto.calimero.datapoint.StateDP in project openhab1-addons by openhab.

the class KNXGenericBindingProvider method parseBindingConfigString.

/**
     * This is the main method that takes care of parsing a binding configuration
     * string for a given item. It returns a collection of {@link BindingConfig}
     * instances, which hold all relevant data about the binding to KNX of an item.
     * 
     * @param item the item for which the binding configuration string is provided
     * @param bindingConfig a string which holds the binding information
     * @return a knx binding config, a collection of {@link KNXBindingConfigItem}
     *         instances, which hold all relevant data about the binding
     * @throws BindingConfigParseException if the configuration string has no valid syntax
     */
protected KNXBindingConfig parseBindingConfigString(Item item, String bindingConfig) throws BindingConfigParseException {
    KNXBindingConfig config = new KNXBindingConfig();
    String[] datapointConfigs = bindingConfig.trim().split(",");
    // we can have one datapoint per accepted command type of this item
    for (int i = 0; i < datapointConfigs.length; i++) {
        try {
            String datapointConfig = datapointConfigs[i].trim();
            KNXBindingConfigItem configItem = new KNXBindingConfigItem();
            configItem.itemName = item.getName();
            if (datapointConfig.split("<").length > 2) {
                throw new BindingConfigParseException("Only one readable GA allowed.");
            }
            String[] dataPoints = datapointConfig.split("\\+");
            for (int j = 0; j < dataPoints.length; ++j) {
                String dataPoint = dataPoints[j].trim();
                // Just skip it, it will be handle in the next iteration.
                if (dataPoint.isEmpty()) {
                    continue;
                }
                boolean isReadable = false;
                int autoRefreshTimeInSecs = 0;
                // check for the readable flag
                if (dataPoint.startsWith("<")) {
                    isReadable = true;
                    dataPoint = dataPoint.substring(1);
                    // check for the auto refresh parameter
                    if (dataPoint.startsWith("(")) {
                        int endIndex = dataPoint.indexOf(")");
                        if (endIndex > -1) {
                            dataPoint = dataPoint.substring(1);
                            if (endIndex > 1) {
                                try {
                                    autoRefreshTimeInSecs = Integer.parseInt(dataPoint.substring(0, endIndex - 1));
                                    dataPoint = dataPoint.substring(endIndex);
                                    if (autoRefreshTimeInSecs == 0) {
                                        throw new BindingConfigParseException("Autorefresh time cannot be 0.");
                                    }
                                } catch (NumberFormatException nfe) {
                                    throw new BindingConfigParseException("Autorefresh time must be a number, but was '" + dataPoint.substring(1, endIndex) + "'.");
                                }
                            } else {
                                throw new BindingConfigParseException("Autorefresh time parameter: missing time. Empty brackets are not allowed.");
                            }
                        } else {
                            throw new BindingConfigParseException("Closing ')' missing on autorefresh time parameter.");
                        }
                    }
                }
                // find the DPT for this entry
                String[] segments = dataPoint.split(":");
                Class<? extends Type> typeClass = null;
                String dptID = null;
                if (segments.length == 1) {
                    //DatapointID NOT specified in binding config, so try to guess it
                    typeClass = item.getAcceptedCommandTypes().size() > 0 ? item.getAcceptedCommandTypes().get(i) : item.getAcceptedDataTypes().size() > 1 ? item.getAcceptedDataTypes().get(i) : item.getAcceptedDataTypes().get(0);
                    dptID = getDefaultDPTId(typeClass);
                } else {
                    //DatapointID specified in binding config, so use it
                    dptID = segments[0];
                }
                if (dptID == null || dptID.trim().isEmpty()) {
                    throw new BindingConfigParseException("No DPT could be determined for the type '" + typeClass.getSimpleName() + "'.");
                }
                // check if this DPT is supported
                if (KNXCoreTypeMapper.toTypeClass(dptID) == null) {
                    throw new BindingConfigParseException("DPT " + dptID + " is not supported by the KNX binding.");
                }
                String ga = (segments.length == 1) ? segments[0].trim() : segments[1].trim();
                // determine start/stop behavior
                Boolean startStopBehavior = Boolean.FALSE;
                if (ga.endsWith(START_STOP_MARKER_SUFFIX)) {
                    startStopBehavior = Boolean.TRUE;
                    ga = ga.substring(0, ga.length() - START_STOP_MARKER_SUFFIX.length());
                }
                // create group address and datapoint
                GroupAddress groupAddress = new GroupAddress(ga);
                configItem.startStopMap.put(groupAddress, startStopBehavior);
                Datapoint dp;
                if (j != 0 || item.getAcceptedCommandTypes().size() == 0) {
                    dp = new StateDP(groupAddress, item.getName(), 0, dptID);
                } else {
                    dp = new CommandDP(groupAddress, item.getName(), 0, dptID);
                }
                // assign datapoint to configuration item
                if (configItem.mainDataPoint == null) {
                    configItem.mainDataPoint = dp;
                }
                if (isReadable) {
                    configItem.readableDataPoint = dp;
                    if (autoRefreshTimeInSecs > 0) {
                        configItem.autoRefreshInSecs = autoRefreshTimeInSecs;
                    }
                }
                if (!configItem.allDataPoints.contains(dp)) {
                    configItem.allDataPoints.add(dp);
                } else {
                    throw new BindingConfigParseException("Datapoint '" + dp.getDPT() + "' already exists for item '" + item.getName() + "'.");
                }
            }
            config.add(configItem);
        } catch (IndexOutOfBoundsException e) {
            throw new BindingConfigParseException("No more than " + i + " datapoint definitions are allowed for this item.");
        } catch (KNXFormatException e) {
            throw new BindingConfigParseException(e.getMessage());
        }
    }
    return config;
}
Also used : CommandDP(tuwien.auto.calimero.datapoint.CommandDP) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) StateDP(tuwien.auto.calimero.datapoint.StateDP) GroupAddress(tuwien.auto.calimero.GroupAddress) KNXFormatException(tuwien.auto.calimero.exception.KNXFormatException)

Example 2 with StateDP

use of tuwien.auto.calimero.datapoint.StateDP in project openhab1-addons by openhab.

the class KNXGenericBindingProviderTest method testParseBindingConfig.

@Test
public void testParseBindingConfig() throws BindingConfigParseException, KNXFormatException {
    // method under Test
    KNXBindingConfig bindingConfigs = provider.parseBindingConfigString(item1, "<4/2/10+0/2/10, 5.005:4/2/11+0/2/11, +4/2/12, 4/2/13");
    // Assertions
    assertEquals(4, bindingConfigs.size());
    for (KNXBindingConfigItem bindingConfig : bindingConfigs) {
        assertEquals("item1", bindingConfig.itemName);
    }
    assertNotNull(bindingConfigs.get(0).readableDataPoint);
    assertNull(bindingConfigs.get(1).readableDataPoint);
    assertNull(bindingConfigs.get(2).readableDataPoint);
    assertNull(bindingConfigs.get(3).readableDataPoint);
    assertTrue(bindingConfigs.get(0).allDataPoints.contains(new GroupAddress("4/2/10")));
    assertTrue(bindingConfigs.get(0).allDataPoints.contains(new GroupAddress("0/2/10")));
    assertTrue(bindingConfigs.get(1).allDataPoints.contains(new GroupAddress("4/2/11")));
    assertTrue(bindingConfigs.get(1).allDataPoints.contains(new GroupAddress("0/2/11")));
    assertTrue(bindingConfigs.get(2).allDataPoints.contains(new GroupAddress("4/2/12")));
    assertTrue(bindingConfigs.get(3).allDataPoints.contains(new GroupAddress("4/2/13")));
    assertEquals(true, bindingConfigs.get(0).mainDataPoint instanceof CommandDP);
    assertEquals(true, bindingConfigs.get(1).mainDataPoint instanceof CommandDP);
    assertEquals(true, bindingConfigs.get(2).mainDataPoint instanceof StateDP);
    assertEquals(true, bindingConfigs.get(3).mainDataPoint instanceof CommandDP);
}
Also used : KNXBindingConfigItem(org.openhab.binding.knx.internal.config.KNXGenericBindingProvider.KNXBindingConfigItem) KNXBindingConfig(org.openhab.binding.knx.internal.config.KNXGenericBindingProvider.KNXBindingConfig) StateDP(tuwien.auto.calimero.datapoint.StateDP) GroupAddress(tuwien.auto.calimero.GroupAddress) CommandDP(tuwien.auto.calimero.datapoint.CommandDP) Test(org.junit.Test)

Aggregations

GroupAddress (tuwien.auto.calimero.GroupAddress)2 CommandDP (tuwien.auto.calimero.datapoint.CommandDP)2 StateDP (tuwien.auto.calimero.datapoint.StateDP)2 Test (org.junit.Test)1 KNXBindingConfig (org.openhab.binding.knx.internal.config.KNXGenericBindingProvider.KNXBindingConfig)1 KNXBindingConfigItem (org.openhab.binding.knx.internal.config.KNXGenericBindingProvider.KNXBindingConfigItem)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1 Datapoint (tuwien.auto.calimero.datapoint.Datapoint)1 KNXFormatException (tuwien.auto.calimero.exception.KNXFormatException)1