]> git.basschouten.com Git - openhab-addons.git/blob
9805a64d3f67e3b01df84ad911b0436866ca9325
[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.nikobus.internal.protocol;
14
15 import static org.openhab.binding.nikobus.internal.NikobusBindingConstants.CHANNEL_OUTPUT_PREFIX;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.thing.ChannelUID;
19
20 /**
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.
23  *
24  * @author Boris Krivonog - Initial contribution
25  */
26 @NonNullByDefault
27 public enum SwitchModuleGroup {
28
29     FIRST("12", "15", 1),
30     SECOND("17", "16", 7);
31
32     private final String statusRequest;
33     private final String statusUpdate;
34     private final int offset;
35
36     private SwitchModuleGroup(String statusRequest, String statusUpdate, int offset) {
37         this.statusRequest = statusRequest;
38         this.statusUpdate = statusUpdate;
39         this.offset = offset;
40     }
41
42     public String getStatusRequest() {
43         return statusRequest;
44     }
45
46     public String getStatusUpdate() {
47         return statusUpdate;
48     }
49
50     public int getOffset() {
51         return offset;
52     }
53
54     public int getCount() {
55         return 6;
56     }
57
58     public static SwitchModuleGroup mapFromChannel(ChannelUID channelUID) {
59         if (!channelUID.getIdWithoutGroup().startsWith(CHANNEL_OUTPUT_PREFIX)) {
60             throw new IllegalArgumentException("Unexpected channel " + channelUID.getId());
61         }
62
63         String channelNumber = channelUID.getIdWithoutGroup().substring(CHANNEL_OUTPUT_PREFIX.length());
64         return mapFromChannel(Integer.parseInt(channelNumber));
65     }
66
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));
72         }
73         return channelNumber >= SECOND.getOffset() ? SECOND : FIRST;
74     }
75 }