2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.knx.internal.channel;
15 import java.util.HashSet;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import tuwien.auto.calimero.GroupAddress;
26 import tuwien.auto.calimero.KNXFormatException;
29 * Data structure representing the content of a channel's group address configuration.
31 * @author Simon Kaufmann - initial contribution and API.
35 public class GroupAddressConfiguration {
36 public static final Logger LOGGER = LoggerFactory.getLogger(GroupAddressConfiguration.class);
38 private static final Pattern PATTERN_GA_CONFIGURATION = Pattern.compile(
39 "^((?<dpt>[1-9][0-9]{0,2}\\.[0-9]{3,5}):)?(?<read><)?(?<mainGA>[0-9]{1,5}(/[0-9]{1,4}){0,2})(?<listenGAs>(\\+(<?[0-9]{1,5}(/[0-9]{1,4}){0,2}))*)$");
40 private static final Pattern PATTERN_LISTEN_GA = Pattern
41 .compile("\\+((?<read><)?(?<GA>[0-9]{1,5}(/[0-9]{1,4}){0,2}))");
43 private final @Nullable String dpt;
44 private final GroupAddress mainGA;
45 private final Set<GroupAddress> listenGAs;
46 private final Set<GroupAddress> readGAs;
48 private GroupAddressConfiguration(@Nullable String dpt, GroupAddress mainGA, Set<GroupAddress> listenGAs,
49 Set<GroupAddress> readGAs) {
52 this.listenGAs = listenGAs;
53 this.readGAs = readGAs;
56 public @Nullable String getDPT() {
60 public GroupAddress getMainGA() {
64 public Set<GroupAddress> getListenGAs() {
68 public Set<GroupAddress> getReadGAs() {
72 public static @Nullable GroupAddressConfiguration parse(@Nullable Object configuration) {
73 if (!(configuration instanceof String)) {
77 Matcher matcher = PATTERN_GA_CONFIGURATION.matcher(((String) configuration).replace(" ", ""));
78 if (matcher.matches()) {
80 String input = matcher.group("listenGAs");
81 Matcher m2 = PATTERN_LISTEN_GA.matcher(input);
82 Set<GroupAddress> listenGAs = new HashSet<>();
83 Set<GroupAddress> readGAs = new HashSet<>();
85 String ga = m2.group("GA");
87 GroupAddress groupAddress = new GroupAddress(ga);
88 listenGAs.add(groupAddress);
89 if (m2.group("read") != null) {
90 readGAs.add(groupAddress);
92 } catch (KNXFormatException e) {
93 LOGGER.warn("Failed to create GroupAddress from {}", ga);
99 String mainGA = matcher.group("mainGA");
101 GroupAddress groupAddress = new GroupAddress(mainGA);
102 listenGAs.add(groupAddress); // also listening to main GA
103 if (matcher.group("read") != null) {
104 readGAs.add(groupAddress); // also reading main GA
106 return new GroupAddressConfiguration(matcher.group("dpt"), groupAddress, listenGAs, readGAs);
107 } catch (KNXFormatException e) {
108 LOGGER.warn("Failed to create GroupAddress from {}", mainGA);
112 LOGGER.warn("Failed parsing channel configuration '{}'.", configuration);