]> git.basschouten.com Git - openhab-addons.git/blob
a05c0373feaad35e604a459c5454d277645664c4
[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.velux.internal.things;
14
15 import java.util.Map;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * <B>Velux</B> product characteristics: GatewayState Description.
26  * <P>
27  * See <a href=
28  * "https://velcdn.azureedge.net/~/media/com/api/klf200/technical%20specification%20for%20klf%20200%20api.pdf#page=19">KLF200
29  * GatewayState value Description</a>
30  * <P>
31  * Methods in handle this type of information:
32  * *
33  * <UL>
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>
38  * </UL>
39  * In addition, the subtype are accessible via:
40  * <UL>
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>
49  * </UL>
50  *
51  * @see VeluxKLFAPI
52  *
53  * @author Guenther Schreiner - initial contribution.
54  */
55 @NonNullByDefault
56 public class VeluxGwState {
57     private final Logger logger = LoggerFactory.getLogger(VeluxGwState.class);
58
59     // Type definition
60
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");
69
70         // Class internal
71
72         private int stateValue;
73         private String stateDescription;
74
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()));
78
79         // Constructor
80
81         private VeluxGatewayState(int stateValue, String stateDescription) {
82             this.stateValue = stateValue;
83             this.stateDescription = stateDescription;
84         }
85
86         // Class access methods
87
88         public int getStateValue() {
89             return stateValue;
90         }
91
92         public String getStateDescription() {
93             return stateDescription;
94         }
95
96         public static VeluxGatewayState get(int stateValue) {
97             return LOOKUPTYPEID2ENUM.getOrDefault(stateValue, VeluxGatewayState.UNDEFTYPE);
98         }
99     }
100
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");
109
110         // Class internal
111
112         private int stateValue;
113         private String stateDescription;
114
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()));
119
120         // Constructor
121
122         private VeluxGatewaySubState(int stateValue, String stateDescription) {
123             this.stateValue = stateValue;
124             this.stateDescription = stateDescription;
125         }
126
127         // Class access methods
128
129         public int getStateValue() {
130             return stateValue;
131         }
132
133         public String getStateDescription() {
134             return stateDescription;
135         }
136
137         public static VeluxGatewaySubState get(int stateValue) {
138             return LOOKUPTYPEID2ENUM.getOrDefault(stateValue, VeluxGatewaySubState.UNDEFTYPE);
139         }
140     }
141
142     // Class internal
143
144     private VeluxGatewayState gwState;
145     private VeluxGatewaySubState gwSubState;
146
147     // Constructor
148
149     public VeluxGwState(byte stateValue, byte subStateValue) {
150         logger.trace("VeluxGwState() created.");
151
152         this.gwState = VeluxGatewayState.get(stateValue);
153         this.gwSubState = VeluxGatewaySubState.get(subStateValue);
154     }
155
156     // Class access methods
157
158     @Override
159     public String toString() {
160         return this.gwState.name().concat("/").concat(this.gwSubState.name());
161     }
162
163     public String toDescription() {
164         return this.gwState.getStateDescription().concat(", ").concat(this.gwSubState.getStateDescription());
165     }
166
167     public byte getSubState() {
168         return (byte) this.gwSubState.getStateValue();
169     }
170 }