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.openhab.binding.dmx.internal.Util;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * The {@link BaseDmxChannel} represents a basic DMX channel
29 * @author Jan N. Klug - Initial contribution
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;
36 protected static final Pattern CHANNEL_PATTERN = Pattern.compile("^(\\d*(?=:))?:?(\\d*)\\/?(\\d*)?$");
38 // this static declaration is needed because of the static fromString method
39 private static final Logger LOGGER = LoggerFactory.getLogger(BaseDmxChannel.class);
41 private int universeId;
42 private final int dmxChannelId;
47 * @param universeId integer for DMX universe
48 * @param dmxChannelId integer for DMX channel
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");
58 * @param dmxChannel a BaseChannel object
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);
69 * @return an integer for the DMX channel
71 public int getChannelId() {
78 * @return an integer for the DMX universe
80 public int getUniverseId() {
85 * set the DMX universe id
87 * @param universeId an integer for the new universe
89 public void setUniverseId(int universeId) {
90 this.universeId = universeId;
94 public int compareTo(BaseDmxChannel otherDmxChannel) {
95 if (otherDmxChannel == null) {
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()));
103 return universeCompare;
108 public String toString() {
109 return universeId + ":" + dmxChannelId;
113 * parse a BaseChannel list from string
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
119 public static List<BaseDmxChannel> fromString(String dmxChannelString, int defaultUniverseId)
120 throws IllegalArgumentException {
121 List<BaseDmxChannel> dmxChannels = new ArrayList<>();
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)));
136 throw new IllegalArgumentException("invalid channel definition" + singleDmxChannelString);