]> git.basschouten.com Git - openhab-addons.git/blob
fb9613f14c94d2c7bb4b4ffbeb4c4367a6beb447
[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.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 @Deprecated
49 @NonNullByDefault
50 class SCsetSceneVelocity extends SetSceneVelocity implements SlipBridgeCommunicationProtocol {
51     private final Logger logger = LoggerFactory.getLogger(SCsetSceneVelocity.class);
52
53     private static final String DESCRIPTION = "Modify Velocity of an Actuator";
54     private static final Command COMMAND = Command.GW_SET_NODE_VELOCITY_REQ;
55
56     /*
57      * ===========================================================
58      * Message Content Parameters
59      */
60
61     private int reqNodeID = -1;
62     private int reqNodeVelocity = -1;
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 BridgeCommunicationProtocol}.
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.getCommand());
94         return COMMAND.getCommand();
95     }
96
97     @Override
98     public byte[] getRequestDataAsArrayOfBytes() {
99         Packet request = new Packet(new byte[2]);
100         request.setOneByteValue(0, reqNodeID);
101         request.setOneByteValue(1, reqNodeVelocity);
102         requestData = request.toByteArray();
103         logger.trace("getRequestDataAsArrayOfBytes() data is {}.", new Packet(requestData).toString());
104         return requestData;
105     }
106
107     @Override
108     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
109         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
110         success = false;
111         finished = false;
112         Packet responseData = new Packet(thisResponseData);
113         switch (Command.get(responseCommand)) {
114             case GW_SET_NODE_VELOCITY_CFM:
115                 finished = true;
116                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 3)) {
117                     break;
118                 }
119                 int cfmStatus = responseData.getOneByteValue(0);
120                 switch (cfmStatus) {
121                     case 0:
122                         logger.trace("setResponse(): returned status: Error - Wink is rejected.");
123                         break;
124                     case 1:
125                         logger.trace("setResponse(): returned status: OK – Wink is accepted.");
126                         success = true;
127                         break;
128                     default:
129                         logger.warn("setResponse({}): returned status={} (Reserved/unknown).",
130                                 Command.get(responseCommand).toString(), cfmStatus);
131                         break;
132                 }
133                 break;
134
135             default:
136                 KLF200Response.errorLogging(logger, responseCommand);
137                 finished = true;
138         }
139         KLF200Response.outroLogging(logger, success, finished);
140     }
141
142     @Override
143     public boolean isCommunicationFinished() {
144         return finished;
145     }
146
147     @Override
148     public boolean isCommunicationSuccessful() {
149         return success;
150     }
151
152     /*
153      * ===========================================================
154      * Methods in addition to the interface {@link BridgeCommunicationProtocol}
155      * and the abstract class {@link RunProductIdentification}
156      */
157
158     /**
159      * Constructor Addon Method.
160      * <P>
161      * Passes the intended Actuator Identifier towards this class for building the request lateron.
162      *
163      * @param actuatorId as type int describing the scene to be processed.
164      * @param silent as type boolean describing the silence mode of this node.
165      * @return <b>this</b> of type {@link SCsetSceneVelocity} as class itself.
166      */
167     @Override
168     public SCsetSceneVelocity setMode(int actuatorId, boolean silent) {
169         logger.trace("setProductId({},{}) called.", actuatorId, silent);
170         this.reqNodeID = actuatorId;
171         this.reqNodeVelocity = silent ? VeluxProductVelocity.SILENT.getVelocity()
172                 : VeluxProductVelocity.FAST.getVelocity();
173         return this;
174     }
175 }