Search in sources :

Example 1 with GroupAddress

use of tuwien.auto.calimero.GroupAddress 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 GroupAddress

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

the class KNXBusReaderSchedulerTest method createDP.

/**
     * Convenience method creating a Datapoint
     * 
     * @param dpt datapoint type
     * @return a new CommandDP
     * @throws KNXFormatException
     */
private Datapoint createDP(String dpt) throws KNXFormatException {
    dpCount++;
    int mainNumber = Integer.parseInt(dpt.substring(0, dpt.indexOf('.')));
    return new CommandDP(new GroupAddress("1/1/" + dpCount), "test" + dpCount, mainNumber, dpt);
}
Also used : CommandDP(tuwien.auto.calimero.datapoint.CommandDP) GroupAddress(tuwien.auto.calimero.GroupAddress) Datapoint(tuwien.auto.calimero.datapoint.Datapoint)

Example 3 with GroupAddress

use of tuwien.auto.calimero.GroupAddress 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)

Example 4 with GroupAddress

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

the class KNXBinding method readFromKNX.

/**
     * Handles the given {@link ProcessEvent}. After finding the corresponding
     * Item (by iterating through all known group addresses) this Item is updated.
     * Each item is added to a special list to identify and avoid echo's in
     * the <code>receiveUpdate</code> and <code>receiveCommand</code> methods.
     *
     * @param e the {@link ProcessEvent} to handle.
     */
private void readFromKNX(ProcessEvent e) {
    try {
        GroupAddress destination = e.getDestination();
        byte[] asdu = e.getASDU();
        if (asdu.length == 0) {
            return;
        }
        String[] itemList = getItemNames(destination);
        if (itemList.length == 0) {
            logger.debug("Received telegram for unknown group address {}", destination.toString());
        }
        for (String itemName : itemList) {
            Iterable<Datapoint> datapoints = getDatapoints(itemName, destination);
            if (datapoints != null) {
                for (Datapoint datapoint : datapoints) {
                    Type type = getType(datapoint, asdu);
                    if (type != null) {
                        if (type instanceof Command && isStartStopEnabled(itemName, destination, datapoint)) {
                            if (isDimmerThreadRunning(itemName) && type == IncreaseDecreaseType.INCREASE) {
                                stopDimmerThread(itemName);
                            } else {
                                startDimmerThread(destination, itemName, (Command) type);
                            }
                        } else {
                            sendTypeToItemButNotToKnx(destination, itemName, type);
                        }
                    } else {
                        final char[] hexCode = "0123456789ABCDEF".toCharArray();
                        StringBuilder sb = new StringBuilder(2 + asdu.length * 2);
                        sb.append("0x");
                        for (byte b : asdu) {
                            sb.append(hexCode[(b >> 4) & 0xF]);
                            sb.append(hexCode[(b & 0xF)]);
                        }
                        logger.debug("Ignoring KNX bus data: couldn't transform to an openHAB type (not supported). Destination='{}', datapoint='{}', data='{}'", new Object[] { destination.toString(), datapoint.toString(), sb.toString() });
                    }
                }
            }
        }
    } catch (RuntimeException re) {
        logger.error("Error while receiving event from KNX bus: " + re.toString());
    }
}
Also used : Datapoint(tuwien.auto.calimero.datapoint.Datapoint) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) Type(org.openhab.core.types.Type) Command(org.openhab.core.types.Command) GroupAddress(tuwien.auto.calimero.GroupAddress)

Aggregations

GroupAddress (tuwien.auto.calimero.GroupAddress)4 CommandDP (tuwien.auto.calimero.datapoint.CommandDP)3 Datapoint (tuwien.auto.calimero.datapoint.Datapoint)3 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 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)1 Command (org.openhab.core.types.Command)1 Type (org.openhab.core.types.Type)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1 KNXFormatException (tuwien.auto.calimero.exception.KNXFormatException)1