2 * Copyright (c) 2010-2022 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.lcn.internal.common;
15 import java.util.function.BiFunction;
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;
38 * Defines the supported channels of an LCN module handler.
40 * @author Fabian Wolter - Initial contribution
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);
67 private BiFunction<LcnModuleHandler, ModInfo, ? extends AbstractLcnModuleSubHandler> handlerFactory;
69 private LcnChannelGroup(int count,
70 BiFunction<LcnModuleHandler, ModInfo, ? extends AbstractLcnModuleSubHandler> handlerFactory) {
72 this.handlerFactory = handlerFactory;
76 * Gets the number of Channels within the channel group.
78 * @return the Channel count
80 public int getCount() {
85 * Checks the given Channel id against the max. Channel count in this Channel group.
87 * @param number the number to check
88 * @return true, if the number is in the range
90 public boolean isValidId(int number) {
91 return number >= 0 && number < count;
95 * Gets the sub handler class to handle this Channel group.
97 * @return the sub handler class
99 public AbstractLcnModuleSubHandler createSubHandler(LcnModuleHandler handler, ModInfo info) {
100 return handlerFactory.apply(handler, info);
104 * Converts a given table ID into the corresponding Channel group.
106 * @param tableId to convert
107 * @return the channel group
108 * @throws LcnException when the ID is out of range
110 public static LcnChannelGroup fromTableId(int tableId) throws LcnException {
113 return KEYLOCKTABLEA;
115 return KEYLOCKTABLEB;
117 return KEYLOCKTABLEC;
119 return KEYLOCKTABLED;
121 throw new LcnException("Unknown key table ID: " + tableId);