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.dmx.internal.multiverse;
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;
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;
29 * The {@link BaseDmxChannel} represents a basic DMX channel
31 * @author Jan N. Klug - Initial contribution
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;
38 protected static final Pattern CHANNEL_PATTERN = Pattern.compile("^(\\d*(?=:))?:?(\\d*)\\/?(\\d*)?$");
40 // this static declaration is needed because of the static fromString method
41 private static final Logger LOGGER = LoggerFactory.getLogger(BaseDmxChannel.class);
43 private int universeId;
44 private final int dmxChannelId;
49 * @param universeId integer for DMX universe
50 * @param dmxChannelId integer for DMX channel
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");
60 * @param dmxChannel a BaseChannel object
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);
71 * @return an integer for the DMX channel
73 public int getChannelId() {
80 * @return a integer for the DMX universe
82 public int getUniverseId() {
87 * set the DMX universe id
89 * @param universeId an integer for the new universe
91 public void setUniverseId(int universeId) {
92 this.universeId = universeId;
96 public int compareTo(@Nullable BaseDmxChannel otherDmxChannel) {
97 if (otherDmxChannel == null) {
100 int universeCompare = Integer.valueOf(getUniverseId()).compareTo(otherDmxChannel.getUniverseId());
101 if (universeCompare == 0) {
102 return Integer.compare(getChannelId(), otherDmxChannel.getChannelId());
104 return universeCompare;
109 public String toString() {
110 return universeId + ":" + dmxChannelId;
114 * parse a BaseChannel list from string
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
120 public static List<BaseDmxChannel> fromString(String dmxChannelString, int defaultUniverseId)
121 throws IllegalArgumentException {
122 List<BaseDmxChannel> dmxChannels = new ArrayList<>();
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 = channelMatch.group(3).equals("") ? 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)));
137 throw new IllegalArgumentException("invalid channel definition" + singleDmxChannelString);