]> git.basschouten.com Git - openhab-addons.git/blob
58022510c06b2d6e921edc475b2033798800cd91
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.BridgeCommunicationProtocol;
17 import org.openhab.binding.velux.internal.bridge.common.GetHouseStatus;
18 import org.openhab.binding.velux.internal.bridge.slip.utils.KLF200Response;
19 import org.openhab.binding.velux.internal.bridge.slip.utils.Packet;
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>Retrieve House Status</B>
28  * <P>
29  * Common Message semantic: Communication from the bridge and storing returned information within the class itself.
30  * <P>
31  * As 3rd level class it defines informations how to receive answer through the
32  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
33  * as described by the interface {@link org.openhab.binding.velux.internal.bridge.slip.SlipBridgeCommunicationProtocol
34  * SlipBridgeCommunicationProtocol}.
35  * <P>
36  * Methods in addition to the mentioned interface:
37  * <UL>
38  * <LI>{@link #getNtfNodeID} to retrieve the node identifier which has been changed.</LI>
39  * <LI>{@link #getNtfState} to retrieve the state of the node which has been changed.</LI>
40  * <LI>{@link #getNtfCurrentPosition} to retrieve the actual position of this node.</LI>
41  * <LI>{@link #getNtfTarget} to retrieve the target position of this node.</LI>
42  * </UL>
43  * <P>
44  * NOTE: the class does NOT define a request as it only works as receiver.
45  *
46  * @see BridgeCommunicationProtocol
47  * @see SlipBridgeCommunicationProtocol
48  *
49  * @author Guenther Schreiner - Initial contribution.
50  */
51 @NonNullByDefault
52 class SCgetHouseStatus extends GetHouseStatus implements BridgeCommunicationProtocol, SlipBridgeCommunicationProtocol {
53     private final Logger logger = LoggerFactory.getLogger(SCgetHouseStatus.class);
54
55     private static final String DESCRIPTION = "Retrieve House Status";
56     private static final Command COMMAND = Command.GW_OPENHAB_RECEIVEONLY;
57
58     /*
59      * ===========================================================
60      * Message Objects
61      */
62
63     @SuppressWarnings("unused")
64     private byte[] requestData = new byte[0];
65
66     /*
67      * ===========================================================
68      * Result Objects
69      */
70
71     private boolean success = false;
72     private boolean finished = false;
73
74     private int ntfNodeID;
75     private int ntfState;
76     private int ntfCurrentPosition;
77     private int ntfTarget;
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         logger.debug("getRequestCommand() returns {} ({}).", COMMAND.name(), COMMAND.getCommand());
92         return COMMAND.getCommand();
93     }
94
95     @Override
96     public byte[] getRequestDataAsArrayOfBytes() {
97         return new byte[0];
98     }
99
100     @Override
101     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
102         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
103         success = false;
104         finished = true;
105         Packet responseData = new Packet(thisResponseData);
106         switch (Command.get(responseCommand)) {
107             case GW_NODE_STATE_POSITION_CHANGED_NTF:
108                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 20)) {
109                     break;
110                 }
111                 ntfNodeID = responseData.getOneByteValue(0);
112                 ntfState = responseData.getOneByteValue(1);
113                 ntfCurrentPosition = responseData.getTwoByteValue(2);
114                 ntfTarget = responseData.getTwoByteValue(4);
115                 @SuppressWarnings("unused")
116                 int ntfFP1CurrentPosition = responseData.getTwoByteValue(6);
117                 @SuppressWarnings("unused")
118                 int ntfFP2CurrentPosition = responseData.getTwoByteValue(8);
119                 @SuppressWarnings("unused")
120                 int ntfFP3CurrentPosition = responseData.getTwoByteValue(10);
121                 @SuppressWarnings("unused")
122                 int ntfFP4CurrentPosition = responseData.getTwoByteValue(12);
123                 int ntfRemainingTime = responseData.getTwoByteValue(14);
124                 int ntfTimeStamp = responseData.getFourByteValue(16);
125                 // Extracting information items
126                 logger.trace("setResponse(): ntfNodeID={}.", ntfNodeID);
127                 logger.trace("setResponse(): ntfState={}.", ntfState);
128                 logger.trace("setResponse(): ntfCurrentPosition={}.", ntfCurrentPosition);
129                 logger.trace("setResponse(): ntfTarget={}.", ntfTarget);
130                 logger.trace("setResponse(): ntfRemainingTime={}.", ntfRemainingTime);
131                 logger.trace("setResponse(): ntfTimeStamp={}.", ntfTimeStamp);
132                 success = true;
133                 break;
134
135             default:
136                 KLF200Response.errorLogging(logger, responseCommand);
137         }
138         KLF200Response.outroLogging(logger, success, finished);
139     }
140
141     @Override
142     public boolean isCommunicationFinished() {
143         return true;
144     }
145
146     @Override
147     public boolean isCommunicationSuccessful() {
148         return true;
149     }
150
151     /*
152      * ===========================================================
153      * Methods in addition to the interface {@link BridgeCommunicationProtocol}
154      */
155
156     /**
157      * @return <b>ntfNodeID</b> returns the Actuator Id as int.
158      */
159     public int getNtfNodeID() {
160         return ntfNodeID;
161     }
162
163     /**
164      * @return <b>ntfState</b> returns the state of the Actuator as int.
165      */
166     public int getNtfState() {
167         return ntfState;
168     }
169
170     /**
171      * @return <b>ntfCurrentPosition</b> returns the current position of the Actuator as int.
172      */
173     public int getNtfCurrentPosition() {
174         return ntfCurrentPosition;
175     }
176
177     /**
178      * @return <b>ntfTarget</b> returns the target position of the Actuator as int.
179      */
180     public int getNtfTarget() {
181         return ntfTarget;
182     }
183 }