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