]> git.basschouten.com Git - openhab-addons.git/blob
da21d829e9f8ab32bc52f2be21d1285b0c623486
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.SetSceneVelocity;
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.VeluxKLFAPI.Command;
20 import org.openhab.binding.velux.internal.things.VeluxKLFAPI.CommandNumber;
21 import org.openhab.binding.velux.internal.things.VeluxProductVelocity;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Protocol specific bridge communication supported by the Velux bridge:
27  * <B>Modify Velocity of an Actuator</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 #setMode} to define the new silence mode of the intended actuator.</LI>
40  * </UL>
41  *
42  * @see SetSceneVelocity
43  * @see SlipBridgeCommunicationProtocol
44  *
45  * @author Guenther Schreiner - Initial contribution.
46  */
47 // ToDo: THIS MESSAGE EXCHANGE IS AN UNDOCUMENTED FEATURE. Check the updated Velux doc against this implementation.
48 @NonNullByDefault
49 class SCsetSceneVelocity extends SetSceneVelocity implements SlipBridgeCommunicationProtocol {
50     private final Logger logger = LoggerFactory.getLogger(SCsetSceneVelocity.class);
51
52     private static final String DESCRIPTION = "Modify Velocity of an Actuator";
53     private static final Command COMMAND = Command.GW_SET_NODE_VELOCITY_REQ;
54
55     /*
56      * ===========================================================
57      * Message Content Parameters
58      */
59
60     private int reqNodeID = -1;
61     private int reqNodeVelocity = -1;
62
63     /*
64      * ===========================================================
65      * Message Objects
66      */
67
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     /*
79      * ===========================================================
80      * Methods required for interface {@link BridgeCommunicationProtocol}.
81      */
82
83     @Override
84     public String name() {
85         return DESCRIPTION;
86     }
87
88     @Override
89     public CommandNumber getRequestCommand() {
90         success = false;
91         finished = false;
92         logger.debug("getRequestCommand() returns {}.", COMMAND.getCommand());
93         return COMMAND.getCommand();
94     }
95
96     @Override
97     public byte[] getRequestDataAsArrayOfBytes() {
98         Packet request = new Packet(new byte[2]);
99         request.setOneByteValue(0, reqNodeID);
100         request.setOneByteValue(1, reqNodeVelocity);
101         requestData = request.toByteArray();
102         logger.trace("getRequestDataAsArrayOfBytes() data is {}.", new Packet(requestData).toString());
103         return requestData;
104     }
105
106     @Override
107     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
108         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
109         success = false;
110         finished = false;
111         Packet responseData = new Packet(thisResponseData);
112         switch (Command.get(responseCommand)) {
113             case GW_SET_NODE_VELOCITY_CFM:
114                 finished = true;
115                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 3)) {
116                     break;
117                 }
118                 int cfmStatus = responseData.getOneByteValue(0);
119                 switch (cfmStatus) {
120                     case 0:
121                         logger.trace("setResponse(): returned status: Error - Wink is rejected.");
122                         break;
123                     case 1:
124                         logger.trace("setResponse(): returned status: OK – Wink is accepted.");
125                         success = true;
126                         break;
127                     default:
128                         logger.warn("setResponse({}): returned status={} (Reserved/unknown).",
129                                 Command.get(responseCommand).toString(), cfmStatus);
130                         break;
131                 }
132                 break;
133
134             default:
135                 KLF200Response.errorLogging(logger, responseCommand);
136                 finished = true;
137         }
138         KLF200Response.outroLogging(logger, success, finished);
139     }
140
141     @Override
142     public boolean isCommunicationFinished() {
143         return finished;
144     }
145
146     @Override
147     public boolean isCommunicationSuccessful() {
148         return success;
149     }
150
151     /*
152      * ===========================================================
153      * Methods in addition to the interface {@link BridgeCommunicationProtocol}
154      * and the abstract class {@link RunProductIdentification}
155      */
156
157     /**
158      * Constructor Addon Method.
159      * <P>
160      * Passes the intended Actuator Identifier towards this class for building the request lateron.
161      *
162      * @param actuatorId as type int describing the scene to be processed.
163      * @param silent as type boolean describing the silence mode of this node.
164      * @return <b>this</b> of type {@link SCsetSceneVelocity} as class itself.
165      */
166     @Override
167     public SCsetSceneVelocity setMode(int actuatorId, boolean silent) {
168         logger.trace("setProductId({},{}) called.", actuatorId, silent);
169         this.reqNodeID = actuatorId;
170         this.reqNodeVelocity = silent ? VeluxProductVelocity.SILENT.getVelocity()
171                 : VeluxProductVelocity.FAST.getVelocity();
172         return this;
173     }
174 }