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.velux.internal.things;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * <B>Velux</B> product characteristics: GatewayState Description.
28 * "https://velcdn.azureedge.net/~/media/com/api/klf200/technical%20specification%20for%20klf%20200%20api.pdf#page=19">KLF200
29 * GatewayState value Description</a>
31 * Methods in handle this type of information:
34 * <LI>{@link #VeluxGwState(byte, byte)} to convert a value into the complete characteristic.</LI>
35 * <LI>{@link #toString()} to retrieve the human-readable description of the complete characteristic.</LI>
36 * <LI>{@link #toDescription()} to retrieve the verbose human-readable description of the complete characteristic..</LI>
37 * <LI>{@link #getSubState()} to retrieve a Velux value as part of the complete characteristic.</LI>
39 * In addition, the subtype are accessible via:
41 * <LI>{@link VeluxGatewayState#getStateValue()} to retrieve the Velux value of the characteristic.</LI>
42 * <LI>{@link VeluxGatewayState#getStateDescription()} to retrieve the human-readable description of the
43 * characteristic.</LI>
44 * <LI>{@link VeluxGatewayState#get(int)} to convert a value into the characteristic.</LI>
45 * <LI>{@link VeluxGatewaySubState#getStateValue()} to retrieve the Velux value of the characteristic.</LI>
46 * <LI>{@link VeluxGatewaySubState#getStateDescription()} to retrieve the human-readable description of the
47 * characteristic.</LI>
48 * <LI>{@link VeluxGatewaySubState#get(int)} to convert a value into the characteristic.</LI>
53 * @author Guenther Schreiner - initial contribution.
56 public class VeluxGwState {
57 private final Logger logger = LoggerFactory.getLogger(VeluxGwState.class);
61 public enum VeluxGatewayState {
62 UNDEFTYPE(-1, "Unkwown state."),
63 GW_S_TEST(0, "Test mode."),
64 GW_S_GWM_EMPTY(1, "Gateway mode, no actuator nodes in the system table."),
65 GW_S_GWM(2, "Gateway mode, with one or more actuator nodes in the system table."),
66 GW_S_BM_UNCONFIG(3, "Beacon mode, not configured by a remote controller."),
67 GW_S_BM(4, "Beacon mode, has been configured by a remote controller."),
68 GW_STATE_RESERVED(255, "Reserved");
72 private int stateValue;
73 private String stateDescription;
75 // Reverse-lookup map for getting a VeluxProductVelocity from a value.
76 private static final Map<Integer, VeluxGatewayState> LOOKUPTYPEID2ENUM = Stream.of(VeluxGatewayState.values())
77 .collect(Collectors.toMap(VeluxGatewayState::getStateValue, Function.identity()));
81 private VeluxGatewayState(int stateValue, String stateDescription) {
82 this.stateValue = stateValue;
83 this.stateDescription = stateDescription;
86 // Class access methods
88 public int getStateValue() {
92 public String getStateDescription() {
93 return stateDescription;
96 public static VeluxGatewayState get(int stateValue) {
97 return LOOKUPTYPEID2ENUM.getOrDefault(stateValue, VeluxGatewayState.UNDEFTYPE);
101 public enum VeluxGatewaySubState {
102 UNDEFTYPE(-1, "Unknown state."),
103 GW_SS_IDLE(0, "Idle state."),
104 GW_SS_P1(1, "Performing task in Configuration Service handler."),
105 GW_SS_P2(2, "Performing Scene Configuration."),
106 GW_SS_P3(3, "Performing Information Service Configuration."),
107 GW_SS_P4(4, "Performing Contact input Configuration."),
108 GW_SS_PFF(88, "Reserved");
112 private int stateValue;
113 private String stateDescription;
115 // Reverse-lookup map for getting a VeluxGatewayState from a TypeId
116 private static final Map<Integer, VeluxGatewaySubState> LOOKUPTYPEID2ENUM = Stream
117 .of(VeluxGatewaySubState.values())
118 .collect(Collectors.toMap(VeluxGatewaySubState::getStateValue, Function.identity()));
122 private VeluxGatewaySubState(int stateValue, String stateDescription) {
123 this.stateValue = stateValue;
124 this.stateDescription = stateDescription;
127 // Class access methods
129 public int getStateValue() {
133 public String getStateDescription() {
134 return stateDescription;
137 public static VeluxGatewaySubState get(int stateValue) {
138 return LOOKUPTYPEID2ENUM.getOrDefault(stateValue, VeluxGatewaySubState.UNDEFTYPE);
144 private VeluxGatewayState gwState;
145 private VeluxGatewaySubState gwSubState;
149 public VeluxGwState(byte stateValue, byte subStateValue) {
150 logger.trace("VeluxGwState() created.");
152 this.gwState = VeluxGatewayState.get(stateValue);
153 this.gwSubState = VeluxGatewaySubState.get(subStateValue);
156 // Class access methods
159 public String toString() {
160 return this.gwState.name().concat("/").concat(this.gwSubState.name());
163 public String toDescription() {
164 return this.gwState.getStateDescription().concat(", ").concat(this.gwSubState.getStateDescription());
167 public byte getSubState() {
168 return (byte) this.gwSubState.getStateValue();