]> git.basschouten.com Git - openhab-addons.git/blob
039a934de94c51260fc212125e5ab120db3fd527
[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.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 #getProduct} to retrieve product type.</LI>
42  * <LI>{@link #setCreatorCommand} to set the command id that identifies the API on which 'product' will be created.</LI>
43  * </UL>
44  * <P>
45  * NOTE: the class does NOT define a request as it only works as receiver.
46  *
47  * @see BridgeCommunicationProtocol
48  * @see SlipBridgeCommunicationProtocol
49  *
50  * @author Guenther Schreiner - Initial contribution.
51  */
52 @NonNullByDefault
53 public class SCgetHouseStatus extends GetHouseStatus
54         implements BridgeCommunicationProtocol, SlipBridgeCommunicationProtocol {
55     private final Logger logger = LoggerFactory.getLogger(SCgetHouseStatus.class);
56
57     private static final String DESCRIPTION = "Retrieve House Status";
58     private static final Command COMMAND = Command.GW_OPENHAB_RECEIVEONLY;
59
60     /*
61      * ===========================================================
62      * Message Objects
63      */
64
65     @SuppressWarnings("unused")
66     private byte[] requestData = new byte[0];
67
68     /*
69      * ===========================================================
70      * Result Objects
71      */
72
73     private boolean success = false;
74     private boolean finished = false;
75
76     private Command creatorCommand = Command.UNDEFTYPE;
77     private VeluxProduct product = VeluxProduct.UNKNOWN;
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         Command responseCmd = Command.get(responseCommand);
107         switch (responseCmd) {
108             case GW_NODE_STATE_POSITION_CHANGED_NTF:
109                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 20)) {
110                     break;
111                 }
112                 int ntfNodeID = responseData.getOneByteValue(0);
113                 int ntfState = responseData.getOneByteValue(1);
114                 int ntfCurrentPosition = responseData.getTwoByteValue(2);
115                 int ntfTarget = responseData.getTwoByteValue(4);
116                 FunctionalParameters ntfFunctionalParameters = FunctionalParameters.readArray(responseData, 6);
117                 int ntfRemainingTime = responseData.getTwoByteValue(14);
118                 int ntfTimeStamp = responseData.getFourByteValue(16);
119
120                 if (logger.isTraceEnabled()) {
121                     logger.trace("setResponse(): ntfNodeID={}.", ntfNodeID);
122                     logger.trace("setResponse(): ntfState={}.", ntfState);
123                     logger.trace("setResponse(): ntfCurrentPosition={}.", String.format("0x%04X", ntfCurrentPosition));
124                     logger.trace("setResponse(): ntfTarget={}.", String.format("0x%04X", ntfTarget));
125                     logger.trace("setResponse(): ntfFunctionalParameters={} (returns null).", ntfFunctionalParameters);
126                     logger.trace("setResponse(): ntfRemainingTime={}.", ntfRemainingTime);
127                     logger.trace("setResponse(): ntfTimeStamp={}.", ntfTimeStamp);
128                 }
129
130                 // this BCP returns wrong functional parameters on some (e.g. Somfy) devices so return null instead
131                 ntfFunctionalParameters = null;
132
133                 // create notification product with the returned values
134                 product = new VeluxProduct(VeluxProductName.UNKNOWN, new ProductBridgeIndex(ntfNodeID), ntfState,
135                         ntfCurrentPosition, ntfTarget, ntfFunctionalParameters, creatorCommand);
136
137                 success = true;
138                 break;
139
140             default:
141                 KLF200Response.errorLogging(logger, responseCommand);
142         }
143         KLF200Response.outroLogging(logger, success, finished);
144     }
145
146     @Override
147     public boolean isCommunicationFinished() {
148         return true;
149     }
150
151     @Override
152     public boolean isCommunicationSuccessful() {
153         return true;
154     }
155
156     /*
157      * ===========================================================
158      * Methods in addition to the interface {@link BridgeCommunicationProtocol}
159      */
160
161     public VeluxProduct getProduct() {
162         logger.trace("getProduct(): returning {}.", product);
163         return product;
164     }
165
166     /**
167      * Change the command id that identifies the API on which 'product' will be created.
168      *
169      * @param creatorCommand the API that will be used to create the product instance.
170      * @return this
171      */
172     public SCgetHouseStatus setCreatorCommand(Command creatorCommand) {
173         this.creatorCommand = creatorCommand;
174         return this;
175     }
176 }