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