]> git.basschouten.com Git - openhab-addons.git/blob
a24ff68c54029baf1f963999faeed33c09b996e8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.knx.internal.channel;
14
15 import java.util.HashSet;
16 import java.util.Set;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import tuwien.auto.calimero.GroupAddress;
26 import tuwien.auto.calimero.KNXFormatException;
27
28 /**
29  * Data structure representing the content of a channel's group address configuration.
30  *
31  * @author Simon Kaufmann - initial contribution and API.
32  *
33  */
34 @NonNullByDefault
35 public class GroupAddressConfiguration {
36     public static final Logger LOGGER = LoggerFactory.getLogger(GroupAddressConfiguration.class);
37
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}))");
42
43     private final @Nullable String dpt;
44     private final GroupAddress mainGA;
45     private final Set<GroupAddress> listenGAs;
46     private final Set<GroupAddress> readGAs;
47
48     private GroupAddressConfiguration(@Nullable String dpt, GroupAddress mainGA, Set<GroupAddress> listenGAs,
49             Set<GroupAddress> readGAs) {
50         this.dpt = dpt;
51         this.mainGA = mainGA;
52         this.listenGAs = listenGAs;
53         this.readGAs = readGAs;
54     }
55
56     public @Nullable String getDPT() {
57         return dpt;
58     }
59
60     public GroupAddress getMainGA() {
61         return mainGA;
62     }
63
64     public Set<GroupAddress> getListenGAs() {
65         return listenGAs;
66     }
67
68     public Set<GroupAddress> getReadGAs() {
69         return readGAs;
70     }
71
72     public static @Nullable GroupAddressConfiguration parse(@Nullable Object configuration) {
73         if (!(configuration instanceof String)) {
74             return null;
75         }
76
77         Matcher matcher = PATTERN_GA_CONFIGURATION.matcher(((String) configuration).replace(" ", ""));
78         if (matcher.matches()) {
79             // Listen GAs
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<>();
84             while (m2.find()) {
85                 String ga = m2.group("GA");
86                 try {
87                     GroupAddress groupAddress = new GroupAddress(ga);
88                     listenGAs.add(groupAddress);
89                     if (m2.group("read") != null) {
90                         readGAs.add(groupAddress);
91                     }
92                 } catch (KNXFormatException e) {
93                     LOGGER.warn("Failed to create GroupAddress from {}", ga);
94                     return null;
95                 }
96             }
97
98             // Main GA
99             String mainGA = matcher.group("mainGA");
100             try {
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
105                 }
106                 return new GroupAddressConfiguration(matcher.group("dpt"), groupAddress, listenGAs, readGAs);
107             } catch (KNXFormatException e) {
108                 LOGGER.warn("Failed to create GroupAddress from {}", mainGA);
109                 return null;
110             }
111         } else {
112             LOGGER.warn("Failed parsing channel configuration '{}'.", configuration);
113         }
114
115         return null;
116     }
117 }