]> git.basschouten.com Git - openhab-addons.git/blob
e48ced9e9a824a97dd86cfa8120d9118fc802136
[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.GetLANConfig;
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.VeluxGwLAN;
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>Retrieve LAN configuration</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 {@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 #getLANConfig} to retrieve the current LAN configuration.</LI>
40  * </UL>
41  *
42  * @see GetLANConfig
43  * @see SlipBridgeCommunicationProtocol
44  *
45  * @author Guenther Schreiner - Initial contribution.
46  */
47 @NonNullByDefault
48 class SCgetLANConfig extends GetLANConfig implements SlipBridgeCommunicationProtocol {
49     private final Logger logger = LoggerFactory.getLogger(SCgetLANConfig.class);
50
51     private static final String DESCRIPTION = "Retrieve LAN configuration";
52     private static final Command COMMAND = Command.GW_GET_NETWORK_SETUP_REQ;
53
54     /*
55      * ===========================================================
56      * Message Content Parameters
57      */
58
59     private int cfmIpAddress;
60     private int cfmMask;
61     private int cfmDefGW;
62     private boolean cfmDHCP;
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 SlipBridgeCommunicationProtocol}.
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.name(), COMMAND.getCommand());
94         return COMMAND.getCommand();
95     }
96
97     @Override
98     public byte[] getRequestDataAsArrayOfBytes() {
99         requestData = new byte[1];
100         return requestData;
101     }
102
103     @Override
104     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
105         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
106         success = false;
107         finished = false;
108         Packet responseData = new Packet(thisResponseData);
109         switch (Command.get(responseCommand)) {
110             case GW_GET_NETWORK_SETUP_CFM:
111                 finished = true;
112                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 13)) {
113                     break;
114                 }
115                 cfmIpAddress = responseData.getFourByteValue(0);
116                 cfmMask = responseData.getFourByteValue(4);
117                 cfmDefGW = responseData.getFourByteValue(8);
118                 cfmDHCP = responseData.getOneByteValue(12) == 0 ? false : true;
119                 success = true;
120                 break;
121
122             default:
123                 KLF200Response.errorLogging(logger, responseCommand);
124                 finished = true;
125         }
126         KLF200Response.outroLogging(logger, success, finished);
127     }
128
129     @Override
130     public boolean isCommunicationFinished() {
131         return finished;
132     }
133
134     @Override
135     public boolean isCommunicationSuccessful() {
136         return success;
137     }
138
139     /*
140      * ===========================================================
141      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
142      */
143
144     @Override
145     public VeluxGwLAN getLANConfig() {
146         logger.trace("getLANConfig() called.");
147         VeluxGwLAN result = new VeluxGwLAN(Packet.intToIPAddressString(cfmIpAddress),
148                 Packet.intToIPAddressString(cfmMask), Packet.intToIPAddressString(cfmDefGW), cfmDHCP);
149         logger.debug("getLANConfig() returns {}.", result);
150         return result;
151     }
152 }