]> git.basschouten.com Git - openhab-addons.git/blob
ee7d766e29958c7617552fccf58d3a501f3a7f73
[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.bridge.slip;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.velux.internal.bridge.common.GetDeviceStatus;
17 import org.openhab.binding.velux.internal.bridge.slip.utils.KLF200Response;
18 import org.openhab.binding.velux.internal.bridge.slip.utils.Packet;
19 import org.openhab.binding.velux.internal.things.VeluxGwState;
20 import org.openhab.binding.velux.internal.things.VeluxKLFAPI.Command;
21 import org.openhab.binding.velux.internal.things.VeluxKLFAPI.CommandNumber;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Protocol specific bridge communication supported by the Velux bridge:
27  * <B>Get Bridge Device Status</B>
28  * <P>
29  * Common Message semantic: Communication with the bridge and (optionally) storing returned information within the class
30  * itself.
31  * <P>
32  * As 3rd level class it defines informations how to send query and receive answer through the
33  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
34  * as described by the interface {@link org.openhab.binding.velux.internal.bridge.slip.SlipBridgeCommunicationProtocol
35  * SlipBridgeCommunicationProtocol}.
36  * <P>
37  * Methods in addition to the mentioned interface:
38  * <UL>
39  * <LI>{@link #getState} to retrieve the Velux gateway status.</LI>
40  * </UL>
41  *
42  * @see GetDeviceStatus
43  * @see SlipBridgeCommunicationProtocol
44  *
45  * @author Guenther Schreiner - Initial contribution.
46  */
47 @NonNullByDefault
48 class SCgetDeviceStatus extends GetDeviceStatus implements SlipBridgeCommunicationProtocol {
49     private final Logger logger = LoggerFactory.getLogger(SCgetDeviceStatus.class);
50
51     private static final String DESCRIPTION = "Get Bridge Device Status";
52     private static final Command COMMAND = Command.GW_GET_STATE_REQ;
53
54     /*
55      * ===========================================================
56      * Message Content Parameters
57      */
58
59     private int cfmGatewayState;
60     private int cfmSubState;
61     @SuppressWarnings("unused")
62     private int cfmStateData;
63
64     /*
65      * ===========================================================
66      * Message Objects
67      */
68
69     private byte[] requestData = new byte[0];
70
71     /*
72      * ===========================================================
73      * Result Objects
74      */
75
76     private boolean success = false;
77     private boolean finished = false;
78
79     /*
80      * ===========================================================
81      * Methods required for interface {@link SlipBridgeCommunicationProtocol}.
82      */
83
84     @Override
85     public String name() {
86         return DESCRIPTION;
87     }
88
89     @Override
90     public CommandNumber getRequestCommand() {
91         success = false;
92         finished = false;
93         logger.debug("getRequestCommand() returns {} ({}).", COMMAND.name(), COMMAND.getCommand());
94         return COMMAND.getCommand();
95     }
96
97     @Override
98     public byte[] getRequestDataAsArrayOfBytes() {
99         logger.trace("getRequestDataAsArrayOfBytes() returns data.");
100         requestData = new byte[0];
101         return requestData;
102     }
103
104     @Override
105     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
106         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
107         success = false;
108         finished = false;
109         Packet responseData = new Packet(thisResponseData);
110         switch (Command.get(responseCommand)) {
111             case GW_GET_STATE_CFM:
112                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 6)) {
113                     finished = true;
114                     break;
115                 }
116                 cfmGatewayState = responseData.getOneByteValue(0);
117                 cfmSubState = responseData.getOneByteValue(1);
118                 cfmStateData = responseData.getFourByteValue(2);
119                 finished = true;
120                 success = true;
121                 break;
122
123             default:
124                 KLF200Response.errorLogging(logger, responseCommand);
125                 finished = true;
126         }
127         KLF200Response.outroLogging(logger, success, finished);
128     }
129
130     @Override
131     public boolean isCommunicationFinished() {
132         return finished;
133     }
134
135     @Override
136     public boolean isCommunicationSuccessful() {
137         return success;
138     }
139
140     /*
141      * ===========================================================
142      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
143      */
144
145     @Override
146     public VeluxGwState getState() {
147         VeluxGwState thisGwState = new VeluxGwState((byte) cfmGatewayState, (byte) cfmSubState);
148         logger.trace("getState() returns {} ({}).", thisGwState, thisGwState.toDescription());
149         return thisGwState;
150     }
151 }