]> git.basschouten.com Git - openhab-addons.git/blob
b298455eead32579dc36310a463ba5ac4e629df2
[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.lcn.internal.common;
14
15 import java.util.function.BiFunction;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.lcn.internal.LcnModuleHandler;
19 import org.openhab.binding.lcn.internal.connection.ModInfo;
20 import org.openhab.binding.lcn.internal.subhandler.AbstractLcnModuleSubHandler;
21 import org.openhab.binding.lcn.internal.subhandler.LcnModuleBinarySensorSubHandler;
22 import org.openhab.binding.lcn.internal.subhandler.LcnModuleCodeSubHandler;
23 import org.openhab.binding.lcn.internal.subhandler.LcnModuleHostCommandSubHandler;
24 import org.openhab.binding.lcn.internal.subhandler.LcnModuleKeyLockTableSubHandler;
25 import org.openhab.binding.lcn.internal.subhandler.LcnModuleLedSubHandler;
26 import org.openhab.binding.lcn.internal.subhandler.LcnModuleLogicSubHandler;
27 import org.openhab.binding.lcn.internal.subhandler.LcnModuleOperatingHoursCounterSubHandler;
28 import org.openhab.binding.lcn.internal.subhandler.LcnModuleOutputSubHandler;
29 import org.openhab.binding.lcn.internal.subhandler.LcnModuleRelaySubHandler;
30 import org.openhab.binding.lcn.internal.subhandler.LcnModuleRollershutterOutputSubHandler;
31 import org.openhab.binding.lcn.internal.subhandler.LcnModuleRollershutterRelayPositionSubHandler;
32 import org.openhab.binding.lcn.internal.subhandler.LcnModuleRollershutterRelaySlatAngleSubHandler;
33 import org.openhab.binding.lcn.internal.subhandler.LcnModuleRvarLockSubHandler;
34 import org.openhab.binding.lcn.internal.subhandler.LcnModuleRvarModeSubHandler;
35 import org.openhab.binding.lcn.internal.subhandler.LcnModuleRvarSetpointSubHandler;
36 import org.openhab.binding.lcn.internal.subhandler.LcnModuleS0CounterSubHandler;
37 import org.openhab.binding.lcn.internal.subhandler.LcnModuleThresholdSubHandler;
38 import org.openhab.binding.lcn.internal.subhandler.LcnModuleVariableSubHandler;
39
40 /**
41  * Defines the supported channels of an LCN module handler.
42  *
43  * @author Fabian Wolter - Initial contribution
44  */
45 @NonNullByDefault
46 public enum LcnChannelGroup {
47     OUTPUT(4, LcnModuleOutputSubHandler::new),
48     ROLLERSHUTTEROUTPUT(1, LcnModuleRollershutterOutputSubHandler::new),
49     RELAY(8, LcnModuleRelaySubHandler::new),
50     ROLLERSHUTTERRELAY(4, LcnModuleRollershutterRelayPositionSubHandler::new),
51     ROLLERSHUTTERRELAYSLAT(4, LcnModuleRollershutterRelaySlatAngleSubHandler::new),
52     LED(12, LcnModuleLedSubHandler::new),
53     LOGIC(4, LcnModuleLogicSubHandler::new),
54     BINARYSENSOR(8, LcnModuleBinarySensorSubHandler::new),
55     VARIABLE(12, LcnModuleVariableSubHandler::new),
56     RVARSETPOINT(2, LcnModuleRvarSetpointSubHandler::new),
57     RVARMODE(2, LcnModuleRvarModeSubHandler::new),
58     RVARLOCK(2, LcnModuleRvarLockSubHandler::new),
59     THRESHOLDREGISTER1(5, LcnModuleThresholdSubHandler::new),
60     THRESHOLDREGISTER2(4, LcnModuleThresholdSubHandler::new),
61     THRESHOLDREGISTER3(4, LcnModuleThresholdSubHandler::new),
62     THRESHOLDREGISTER4(4, LcnModuleThresholdSubHandler::new),
63     S0INPUT(4, LcnModuleS0CounterSubHandler::new),
64     KEYLOCKTABLEA(8, LcnModuleKeyLockTableSubHandler::new),
65     KEYLOCKTABLEB(8, LcnModuleKeyLockTableSubHandler::new),
66     KEYLOCKTABLEC(8, LcnModuleKeyLockTableSubHandler::new),
67     KEYLOCKTABLED(8, LcnModuleKeyLockTableSubHandler::new),
68     CODE(0, LcnModuleCodeSubHandler::new),
69     OPERATINGHOURS(0, LcnModuleOperatingHoursCounterSubHandler::new),
70     HOSTCOMMAND(0, LcnModuleHostCommandSubHandler::new);
71
72     private int count;
73     private BiFunction<LcnModuleHandler, ModInfo, ? extends AbstractLcnModuleSubHandler> handlerFactory;
74
75     private LcnChannelGroup(int count,
76             BiFunction<LcnModuleHandler, ModInfo, ? extends AbstractLcnModuleSubHandler> handlerFactory) {
77         this.count = count;
78         this.handlerFactory = handlerFactory;
79     }
80
81     /**
82      * Gets the number of Channels within the channel group.
83      *
84      * @return the Channel count
85      */
86     public int getCount() {
87         return count;
88     }
89
90     /**
91      * Checks the given Channel id against the max. Channel count in this Channel group.
92      *
93      * @param number the number to check
94      * @return true, if the number is in the range
95      */
96     public boolean isValidId(int number) {
97         return number >= 0 && number < count;
98     }
99
100     /**
101      * Gets the sub handler class to handle this Channel group.
102      *
103      * @return the sub handler class
104      */
105     public AbstractLcnModuleSubHandler createSubHandler(LcnModuleHandler handler, ModInfo info) {
106         return handlerFactory.apply(handler, info);
107     }
108
109     /**
110      * Converts a given table ID into the corresponding Channel group.
111      *
112      * @param tableId to convert
113      * @return the channel group
114      * @throws LcnException when the ID is out of range
115      */
116     public static LcnChannelGroup fromTableId(int tableId) throws LcnException {
117         switch (tableId) {
118             case 0:
119                 return KEYLOCKTABLEA;
120             case 1:
121                 return KEYLOCKTABLEB;
122             case 2:
123                 return KEYLOCKTABLEC;
124             case 3:
125                 return KEYLOCKTABLED;
126             default:
127                 throw new LcnException("Unknown key table ID: " + tableId);
128         }
129     }
130 }