]> git.basschouten.com Git - openhab-addons.git/blob
b8fa9b47603f82bddf53e981f48a9e2493c75447
[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}):)?(?<modifier>[<>])?(?<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     /**
61      * Returns the main GA, which is the GA to send commands to.
62      */
63     public GroupAddress getMainGA() {
64         return mainGA;
65     }
66
67     /**
68      * Returns all GAs to listen to.
69      * This includes the main GA (unless disabled by '>'), and additional listening GAs
70      * (those after the "+" symbol).
71      */
72     public Set<GroupAddress> getListenGAs() {
73         return listenGAs;
74     }
75
76     /**
77      * Returns all GAs to read from.
78      * Those GAs accept read requests to the KNX bus, i.e. they respond to a "GroupValueRead" with a
79      * "GroupValueResponse".
80      * The '&lt;' sign sets a GA as read GA.
81      */
82     public Set<GroupAddress> getReadGAs() {
83         return readGAs;
84     }
85
86     public static @Nullable GroupAddressConfiguration parse(@Nullable Object configuration) {
87         if (!(configuration instanceof String)) {
88             return null;
89         }
90
91         Matcher matcher = PATTERN_GA_CONFIGURATION.matcher(((String) configuration).replace(" ", ""));
92         if (matcher.matches()) {
93             // Listen GAs
94             String input = matcher.group("listenGAs");
95             Matcher m2 = PATTERN_LISTEN_GA.matcher(input);
96             Set<GroupAddress> listenGAs = new HashSet<>();
97             Set<GroupAddress> readGAs = new HashSet<>();
98             while (m2.find()) {
99                 String ga = m2.group("GA");
100                 try {
101                     GroupAddress groupAddress = new GroupAddress(ga);
102                     listenGAs.add(groupAddress);
103                     if (m2.group("read") != null) {
104                         readGAs.add(groupAddress);
105                     }
106                 } catch (KNXFormatException e) {
107                     LOGGER.warn("Failed to create GroupAddress from {}", ga);
108                     return null;
109                 }
110             }
111
112             // Main GA
113             String mainGA = matcher.group("mainGA");
114             try {
115                 GroupAddress groupAddress = new GroupAddress(mainGA);
116                 @Nullable
117                 String modifier = matcher.group("modifier");
118                 if (modifier == null) {
119                     // default: main GA address writes and listens
120                     listenGAs.add(groupAddress);
121                 } else if ("<".equals(modifier)) {
122                     // configured for read at startup
123                     listenGAs.add(groupAddress);
124                     readGAs.add(groupAddress);
125                 } // else (">").equals(modifier) -> write only, no action
126                 if (readGAs.size() > 1) {
127                     LOGGER.info(
128                             "Item with mainGA {} has more than one GA configured for read at startup, check configuration",
129                             groupAddress);
130                 }
131                 return new GroupAddressConfiguration(matcher.group("dpt"), groupAddress, listenGAs, readGAs);
132             } catch (KNXFormatException e) {
133                 LOGGER.warn("Failed to create GroupAddress from {}", mainGA);
134                 return null;
135             }
136         } else {
137             LOGGER.warn("Failed parsing channel configuration '{}'.", configuration);
138         }
139
140         return null;
141     }
142 }