]> git.basschouten.com Git - openhab-addons.git/blob
06c0a918da8285b836a345169d7d6137da6f4940
[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.RunProductDiscovery;
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.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Protocol specific bridge communication supported by the Velux bridge:
26  * <B>Ask the Bridge to detect (new) products/actuators</B>
27  * <P>
28  * Common Message semantic: Communication with the bridge and (optionally) storing returned information within the class
29  * itself.
30  * <P>
31  * As 3rd level class it defines informations how to send query and 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  * There are no methods in addition to the mentioned interface.
37  *
38  * @see SlipBridgeCommunicationProtocol
39  *
40  * @author Guenther Schreiner - Initial contribution.
41  */
42 @NonNullByDefault
43 class SCrunProductDiscovery extends RunProductDiscovery implements SlipBridgeCommunicationProtocol {
44     private final Logger logger = LoggerFactory.getLogger(SCrunProductDiscovery.class);
45
46     private static final String DESCRIPTION = "Detect Products/Actuators";
47     private static final Command COMMAND = Command.GW_CS_DISCOVER_NODES_REQ;
48
49     /*
50      * ===========================================================
51      * Message Content Parameters
52      */
53
54     private int reqNodeType = 0; // NO_TYPE (All nodes except controller)
55
56     /*
57      * ===========================================================
58      * Message Objects
59      */
60
61     private byte[] requestData = new byte[0];
62
63     /*
64      * ===========================================================
65      * Result Objects
66      */
67
68     private boolean success = false;
69     private boolean finished = false;
70
71     /*
72      * ===========================================================
73      * Methods required for interface {@link BridgeCommunicationProtocol}.
74      */
75
76     @Override
77     public String name() {
78         return DESCRIPTION;
79     }
80
81     @Override
82     public CommandNumber getRequestCommand() {
83         success = false;
84         finished = false;
85         logger.debug("getRequestCommand() returns {} ({}).", COMMAND.name(), COMMAND.getCommand());
86         return COMMAND.getCommand();
87     }
88
89     @Override
90     public byte[] getRequestDataAsArrayOfBytes() {
91         logger.trace("getRequestDataAsArrayOfBytes() returns data for detection with type {}.", reqNodeType);
92         Packet request = new Packet(new byte[1]);
93         request.setOneByteValue(0, reqNodeType);
94         requestData = request.toByteArray();
95         return requestData;
96     }
97
98     @Override
99     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
100         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
101         success = false;
102         finished = false;
103         Packet responseData = new Packet(thisResponseData);
104         switch (Command.get(responseCommand)) {
105             case GW_CS_DISCOVER_NODES_CFM:
106                 logger.trace("setResponse(): received confirmation for discovery mode.");
107                 break;
108
109             case GW_CS_DISCOVER_NODES_NTF:
110                 finished = true;
111                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 131)) {
112                     break;
113                 }
114                 int ntfDiscoverStatus = responseData.getOneByteValue(130);
115                 switch (ntfDiscoverStatus) {
116                     case 0:
117                         logger.trace("setResponse(): returned status: OK. Discovered nodes. See bit array.");
118                         success = true;
119                         break;
120                     case 5:
121                         logger.warn("setResponse(): returned status: ERROR - Failed. CS not ready.");
122                         break;
123                     case 6:
124                         logger.trace(
125                                 "setResponse(): returned status: OK. But some nodes were not added to system table (e.g. System table has reached its limit).");
126                         break;
127                     case 7:
128                         logger.warn("setResponse(): returned status: ERROR - CS busy with another task.");
129                         break;
130                     default:
131                         logger.warn("setResponse({}): returned status={} (Reserved/unknown).",
132                                 Command.get(responseCommand).toString(), ntfDiscoverStatus);
133                         break;
134                 }
135                 break;
136
137             default:
138                 KLF200Response.errorLogging(logger, responseCommand);
139                 finished = true;
140         }
141         KLF200Response.outroLogging(logger, success, finished);
142     }
143
144     @Override
145     public boolean isCommunicationFinished() {
146         return finished;
147     }
148
149     @Override
150     public boolean isCommunicationSuccessful() {
151         return success;
152     }
153 }