]> git.basschouten.com Git - openhab-addons.git/blob
7eed6b1faad15f04f2dee6ac23b71fefbb44fd19
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.openhab.binding.velux.internal.things.VeluxProduct;
23 import org.openhab.binding.velux.internal.things.VeluxProduct.ProductBridgeIndex;
24 import org.openhab.binding.velux.internal.things.VeluxProductName;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Protocol specific bridge communication supported by the Velux bridge:
30  * <B>Retrieve House Status</B>
31  * <P>
32  * Common Message semantic: Communication from the bridge and storing returned information within the class itself.
33  * <P>
34  * As 3rd level class it defines informations how to receive answer through the
35  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
36  * as described by the interface {@link org.openhab.binding.velux.internal.bridge.slip.SlipBridgeCommunicationProtocol
37  * SlipBridgeCommunicationProtocol}.
38  * <P>
39  * Methods in addition to the mentioned interface:
40  * <UL>
41  * <LI>{@link #getNtfNodeID} to retrieve the node identifier which has been changed.</LI>
42  * <LI>{@link #getNtfState} to retrieve the state of the node which has been changed.</LI>
43  * <LI>{@link #getNtfCurrentPosition} to retrieve the actual position of this node.</LI>
44  * <LI>{@link #getNtfTarget} to retrieve the target position of this node.</LI>
45  * </UL>
46  * <P>
47  * NOTE: the class does NOT define a request as it only works as receiver.
48  *
49  * @see BridgeCommunicationProtocol
50  * @see SlipBridgeCommunicationProtocol
51  *
52  * @author Guenther Schreiner - Initial contribution.
53  */
54 @NonNullByDefault
55 public class SCgetHouseStatus extends GetHouseStatus
56         implements BridgeCommunicationProtocol, SlipBridgeCommunicationProtocol {
57     private final Logger logger = LoggerFactory.getLogger(SCgetHouseStatus.class);
58
59     private static final String DESCRIPTION = "Retrieve House Status";
60     private static final Command COMMAND = Command.GW_OPENHAB_RECEIVEONLY;
61
62     /*
63      * ===========================================================
64      * Message Objects
65      */
66
67     @SuppressWarnings("unused")
68     private byte[] requestData = new byte[0];
69
70     /*
71      * ===========================================================
72      * Result Objects
73      */
74
75     private boolean success = false;
76     private boolean finished = false;
77
78     private Command creatorCommand = Command.UNDEFTYPE;
79     private VeluxProduct product = VeluxProduct.UNKNOWN;
80
81     /*
82      * ===========================================================
83      * Methods required for interface {@link SlipBridgeCommunicationProtocol}.
84      */
85
86     @Override
87     public String name() {
88         return DESCRIPTION;
89     }
90
91     @Override
92     public CommandNumber getRequestCommand() {
93         logger.debug("getRequestCommand() returns {} ({}).", COMMAND.name(), COMMAND.getCommand());
94         return COMMAND.getCommand();
95     }
96
97     @Override
98     public byte[] getRequestDataAsArrayOfBytes() {
99         return new byte[0];
100     }
101
102     @Override
103     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
104         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
105         success = false;
106         finished = true;
107         Packet responseData = new Packet(thisResponseData);
108         Command responseCmd = Command.get(responseCommand);
109         switch (responseCmd) {
110             case GW_NODE_STATE_POSITION_CHANGED_NTF:
111                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 20)) {
112                     break;
113                 }
114                 int ntfNodeID = responseData.getOneByteValue(0);
115                 int ntfState = responseData.getOneByteValue(1);
116                 int ntfCurrentPosition = responseData.getTwoByteValue(2);
117                 int ntfTarget = responseData.getTwoByteValue(4);
118                 FunctionalParameters ntfFunctionalParameters = FunctionalParameters.readArray(responseData, 6);
119                 int ntfRemainingTime = responseData.getTwoByteValue(14);
120                 int ntfTimeStamp = responseData.getFourByteValue(16);
121
122                 if (logger.isTraceEnabled()) {
123                     logger.trace("setResponse(): ntfNodeID={}.", ntfNodeID);
124                     logger.trace("setResponse(): ntfState={}.", ntfState);
125                     logger.trace("setResponse(): ntfCurrentPosition={}.", String.format("0x%04X", ntfCurrentPosition));
126                     logger.trace("setResponse(): ntfTarget={}.", String.format("0x%04X", ntfTarget));
127                     logger.trace("setResponse(): ntfFunctionalParameters={} (returns null).", ntfFunctionalParameters);
128                     logger.trace("setResponse(): ntfRemainingTime={}.", ntfRemainingTime);
129                     logger.trace("setResponse(): ntfTimeStamp={}.", ntfTimeStamp);
130                 }
131
132                 // this BCP returns wrong functional parameters on some (e.g. Somfy) devices so return null instead
133                 ntfFunctionalParameters = null;
134
135                 // create notification product with the returned values
136                 product = new VeluxProduct(VeluxProductName.UNKNOWN, new ProductBridgeIndex(ntfNodeID), ntfState,
137                         ntfCurrentPosition, ntfTarget, ntfFunctionalParameters, creatorCommand);
138
139                 success = true;
140                 break;
141
142             default:
143                 KLF200Response.errorLogging(logger, responseCommand);
144         }
145         KLF200Response.outroLogging(logger, success, finished);
146     }
147
148     @Override
149     public boolean isCommunicationFinished() {
150         return true;
151     }
152
153     @Override
154     public boolean isCommunicationSuccessful() {
155         return true;
156     }
157
158     /*
159      * ===========================================================
160      * Methods in addition to the interface {@link BridgeCommunicationProtocol}
161      */
162
163     public VeluxProduct getProduct() {
164         logger.trace("getProduct(): returning {}.", product);
165         return product;
166     }
167
168     /**
169      * Change the command id that identifies the API on which 'product' will be created.
170      *
171      * @param creatorCommand the API that will be used to create the product instance.
172      * @return this
173      */
174     public SCgetHouseStatus setCreatorCommand(Command creatorCommand) {
175         this.creatorCommand = creatorCommand;
176         return this;
177     }
178 }