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.nikobus.internal.protocol;
15 import static org.openhab.binding.nikobus.internal.NikobusBindingConstants.CHANNEL_OUTPUT_PREFIX;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.thing.ChannelUID;
21 * The {@link SwitchModuleGroup} class defines Nikobus module group used for reading status or set its new value.
22 * Nikobus module can always operate only in groups and not per-channel.
24 * @author Boris Krivonog - Initial contribution
27 public enum SwitchModuleGroup {
30 SECOND("17", "16", 7);
32 private final String statusRequest;
33 private final String statusUpdate;
34 private final int offset;
36 private SwitchModuleGroup(String statusRequest, String statusUpdate, int offset) {
37 this.statusRequest = statusRequest;
38 this.statusUpdate = statusUpdate;
42 public String getStatusRequest() {
46 public String getStatusUpdate() {
50 public int getOffset() {
54 public int getCount() {
58 public static SwitchModuleGroup mapFromChannel(ChannelUID channelUID) {
59 if (!channelUID.getIdWithoutGroup().startsWith(CHANNEL_OUTPUT_PREFIX)) {
60 throw new IllegalArgumentException("Unexpected channel " + channelUID.getId());
63 String channelNumber = channelUID.getIdWithoutGroup().substring(CHANNEL_OUTPUT_PREFIX.length());
64 return mapFromChannel(Integer.parseInt(channelNumber));
67 public static SwitchModuleGroup mapFromChannel(int channelNumber) {
68 int max = SECOND.getOffset() + SECOND.getCount();
69 if (channelNumber < FIRST.getOffset() || channelNumber > max) {
70 throw new IllegalArgumentException(String.format("Channel number should be between [%d, %d], but got %d",
71 FIRST.getOffset(), max, channelNumber));
73 return channelNumber >= SECOND.getOffset() ? SECOND : FIRST;