]> git.basschouten.com Git - openhab-addons.git/blob
bd3f9c819a611d8c87a0808a941458086b7efa86
[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.dmx.internal.multiverse;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import java.util.stream.IntStream;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.dmx.internal.Util;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link BaseDmxChannel} represents a basic DMX channel
30  *
31  * @author Jan N. Klug - Initial contribution
32  */
33 @NonNullByDefault
34 public class BaseDmxChannel implements Comparable<BaseDmxChannel> {
35     public static final int MIN_CHANNEL_ID = 1;
36     public static final int MAX_CHANNEL_ID = 512;
37
38     protected static final Pattern CHANNEL_PATTERN = Pattern.compile("^(\\d*(?=:))?:?(\\d*)\\/?(\\d*)?$");
39
40     // this static declaration is needed because of the static fromString method
41     private static final Logger LOGGER = LoggerFactory.getLogger(BaseDmxChannel.class);
42
43     private int universeId;
44     private final int dmxChannelId;
45
46     /**
47      * BaseChannel object
48      *
49      * @param universeId integer for DMX universe
50      * @param dmxChannelId integer for DMX channel
51      */
52     public BaseDmxChannel(int universeId, int dmxChannelId) {
53         this.universeId = universeId;
54         this.dmxChannelId = Util.coerceToRange(dmxChannelId, MIN_CHANNEL_ID, MAX_CHANNEL_ID, LOGGER, "channelId");
55     }
56
57     /**
58      * copy constructor
59      *
60      * @param dmxChannel a BaseChannel object
61      */
62     public BaseDmxChannel(BaseDmxChannel dmxChannel) {
63         this.universeId = dmxChannel.getUniverseId();
64         this.dmxChannelId = dmxChannel.getChannelId();
65         LOGGER.trace("created DMX channel {} in universe {} ", dmxChannelId, universeId);
66     }
67
68     /**
69      * get DMX channel
70      *
71      * @return an integer for the DMX channel
72      */
73     public int getChannelId() {
74         return dmxChannelId;
75     }
76
77     /**
78      * get DMX universe
79      *
80      * @return a integer for the DMX universe
81      */
82     public int getUniverseId() {
83         return universeId;
84     }
85
86     /**
87      * set the DMX universe id
88      *
89      * @param universeId an integer for the new universe
90      */
91     public void setUniverseId(int universeId) {
92         this.universeId = universeId;
93     }
94
95     @Override
96     public int compareTo(@Nullable BaseDmxChannel otherDmxChannel) {
97         if (otherDmxChannel == null) {
98             return -1;
99         }
100         int universeCompare = Integer.compare(getUniverseId(), otherDmxChannel.getUniverseId());
101         if (universeCompare == 0) {
102             return Integer.compare(getChannelId(), otherDmxChannel.getChannelId());
103         } else {
104             return universeCompare;
105         }
106     }
107
108     @Override
109     public String toString() {
110         return universeId + ":" + dmxChannelId;
111     }
112
113     /**
114      * parse a BaseChannel list from string
115      *
116      * @param dmxChannelString channel string in format [universe:]channel[/width],...
117      * @param defaultUniverseId default id to use if universe not specified
118      * @return a List of BaseChannels
119      */
120     public static List<BaseDmxChannel> fromString(String dmxChannelString, int defaultUniverseId)
121             throws IllegalArgumentException {
122         List<BaseDmxChannel> dmxChannels = new ArrayList<>();
123
124         Stream.of(dmxChannelString.split(",")).forEach(singleDmxChannelString -> {
125             int dmxChannelId, dmxChannelWidth;
126             Matcher channelMatch = CHANNEL_PATTERN.matcher(singleDmxChannelString);
127             if (channelMatch.matches()) {
128                 final int universeId = (channelMatch.group(1) == null) ? defaultUniverseId
129                         : Integer.parseInt(channelMatch.group(1));
130                 dmxChannelWidth = "".equals(channelMatch.group(3)) ? 1 : Integer.parseInt(channelMatch.group(3));
131                 dmxChannelId = Integer.parseInt(channelMatch.group(2));
132                 LOGGER.trace("parsed channel string {} to universe {}, id {}, width {}", singleDmxChannelString,
133                         universeId, dmxChannelId, dmxChannelWidth);
134                 IntStream.range(dmxChannelId, dmxChannelId + dmxChannelWidth)
135                         .forEach(c -> dmxChannels.add(new BaseDmxChannel(universeId, c)));
136             } else {
137                 throw new IllegalArgumentException("invalid channel definition" + singleDmxChannelString);
138             }
139         });
140
141         return dmxChannels;
142     }
143 }