]> git.basschouten.com Git - openhab-addons.git/blob
951dcc39a6ac6663dc513d639cc964f756b91acf
[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.VeluxBindingConstants;
17 import org.openhab.binding.velux.internal.bridge.common.Login;
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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Protocol specific bridge communication supported by the Velux bridge:
27  * <B>Authenticate / login</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 #setPassword(String)} to define the authentication reqPassword to be used.</LI>
40  * <LI>{@link #getAuthToken()} to retrieve the authentication reqPassword.</LI>
41  * </UL>
42  *
43  * @see Login
44  * @see SlipBridgeCommunicationProtocol
45  *
46  *
47  * @author Guenther Schreiner - Initial contribution.
48  */
49 @NonNullByDefault
50 class SClogin extends Login implements SlipBridgeCommunicationProtocol {
51     private final Logger logger = LoggerFactory.getLogger(SClogin.class);
52
53     private static final String DESCRIPTION = "Authenticate / login";
54     private static final Command COMMAND = Command.GW_PASSWORD_ENTER_REQ;
55
56     /*
57      * ===========================================================
58      * Message Content Parameters
59      */
60
61     private String reqPassword = "";
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 SlipBridgeCommunicationProtocol}.
81      */
82
83     @Override
84     public String name() {
85         return DESCRIPTION;
86     }
87
88     @Override
89     public CommandNumber getRequestCommand() {
90         return COMMAND.getCommand();
91     }
92
93     @Override
94     public byte[] getRequestDataAsArrayOfBytes() {
95         requestData = new byte[32];
96         byte[] password = reqPassword.getBytes();
97         System.arraycopy(password, 0, requestData, 0, password.length);
98         return requestData;
99     }
100
101     @Override
102     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
103         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
104         success = false;
105         finished = true;
106         Packet responseData = new Packet(thisResponseData);
107         switch (Command.get(responseCommand)) {
108             case GW_PASSWORD_ENTER_CFM:
109                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 1)) {
110                     break;
111                 }
112                 int cfmStatus = responseData.getOneByteValue(0);
113                 switch (cfmStatus) {
114                     case 0:
115                         logger.info("{} bridge connection successfully established (login succeeded).",
116                                 VeluxBindingConstants.BINDING_ID);
117                         logger.debug("setResponse(): returned status: The request was successful.");
118                         success = true;
119                         break;
120                     case 1:
121                         logger.warn("{} bridge connection successfully established but login failed.",
122                                 VeluxBindingConstants.BINDING_ID);
123                         logger.debug("setResponse(): returned status: The request failed.");
124                         break;
125                     default:
126                         logger.warn("setResponse(): returned status={} (not defined).", cfmStatus);
127                         break;
128                 }
129                 break;
130
131             default:
132                 KLF200Response.errorLogging(logger, responseCommand);
133                 finished = true;
134         }
135         KLF200Response.outroLogging(logger, success, finished);
136     }
137
138     @Override
139     public boolean isCommunicationFinished() {
140         return finished;
141     }
142
143     @Override
144     public boolean isCommunicationSuccessful() {
145         return success;
146     }
147
148     /*
149      * ===========================================================
150      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
151      */
152
153     @Override
154     public void setPassword(String thisPassword) {
155         logger.trace("setPassword({}) called.", thisPassword.replaceAll(".", "*"));
156         reqPassword = thisPassword;
157         return;
158     }
159
160     @Override
161     public String getAuthToken() {
162         logger.trace("getAuthToken() called, returning {}.", reqPassword.replaceAll(".", "*"));
163         return reqPassword;
164     }
165 }