]> git.basschouten.com Git - openhab-addons.git/blob
d04a34ae02a852829be2191ba437159a66e74c2b
[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.GetFirmware;
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.VeluxGwFirmware;
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>Get Firmware Version</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 #getFirmware} to retrieve the Velux firmware version.</LI>
40  * </UL>
41  *
42  * @see GetFirmware
43  * @see SlipBridgeCommunicationProtocol
44  *
45  * @author Guenther Schreiner - Initial contribution.
46  */
47 @NonNullByDefault
48 class SCgetFirmware extends GetFirmware implements SlipBridgeCommunicationProtocol {
49     private final Logger logger = LoggerFactory.getLogger(SCgetFirmware.class);
50
51     private static final String DESCRIPTION = "Retrieve firmware version";
52     private static final Command COMMAND = Command.GW_GET_VERSION_REQ;
53
54     /*
55      * ===========================================================
56      * Message Content Parameters
57      */
58
59     private int cfmSoftwareVersionCommand = 0;
60     private int cfmSoftwareVersionWhole = 0;
61     private int cfmSoftwareVersionSub = 0;
62     private int cfmSoftwareVersionBranch = 0;
63     private int cfmSoftwareVersionBuild = 0;
64     private int cfmSoftwareVersionMicroBuild = 0;
65     private int cfmHardwareVersion = 0;
66     private int cfmProductGroup = 0;
67     private int cfmProductType = 0;
68
69     /*
70      * ===========================================================
71      * Message Objects
72      */
73
74     private byte[] requestData = new byte[0];
75
76     /*
77      * ===========================================================
78      * Result Objects
79      */
80
81     private boolean success = false;
82     private boolean finished = false;
83
84     /*
85      * ===========================================================
86      * Methods required for interface {@link SlipBridgeCommunicationProtocol}.
87      */
88
89     @Override
90     public String name() {
91         return DESCRIPTION;
92     }
93
94     @Override
95     public CommandNumber getRequestCommand() {
96         success = false;
97         finished = false;
98         logger.debug("getRequestCommand() returns {} ({}).", COMMAND.name(), COMMAND.getCommand());
99         return COMMAND.getCommand();
100     }
101
102     @Override
103     public byte[] getRequestDataAsArrayOfBytes() {
104         requestData = new byte[1];
105         return requestData;
106     }
107
108     @Override
109     public void setResponse(short responseCommand, byte[] thisResponseData, boolean isSequentialEnforced) {
110         KLF200Response.introLogging(logger, responseCommand, thisResponseData);
111         success = false;
112         finished = false;
113         Packet responseData = new Packet(thisResponseData);
114         switch (Command.get(responseCommand)) {
115             case GW_GET_VERSION_CFM:
116                 if (!KLF200Response.isLengthValid(logger, responseCommand, thisResponseData, 9)) {
117                     finished = true;
118                     break;
119                 }
120                 cfmSoftwareVersionCommand = responseData.getOneByteValue(0);
121                 cfmSoftwareVersionWhole = responseData.getOneByteValue(1);
122                 cfmSoftwareVersionSub = responseData.getOneByteValue(2);
123                 cfmSoftwareVersionBranch = responseData.getOneByteValue(3);
124                 cfmSoftwareVersionBuild = responseData.getOneByteValue(4);
125                 cfmSoftwareVersionMicroBuild = responseData.getOneByteValue(5);
126                 cfmHardwareVersion = responseData.getOneByteValue(6);
127                 cfmProductGroup = responseData.getOneByteValue(7);
128                 cfmProductType = responseData.getOneByteValue(8);
129                 success = true;
130                 finished = true;
131                 break;
132
133             default:
134                 KLF200Response.errorLogging(logger, responseCommand);
135                 finished = true;
136         }
137         KLF200Response.outroLogging(logger, success, finished);
138     }
139
140     @Override
141     public boolean isCommunicationFinished() {
142         return finished;
143     }
144
145     @Override
146     public boolean isCommunicationSuccessful() {
147         return success;
148     }
149
150     /*
151      * ===========================================================
152      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
153      */
154
155     @Override
156     public VeluxGwFirmware getFirmware() {
157         String result = String.format("Software version %d.%d.%d.%d.%d.%d, Hardware version %d.%d.%d",
158                 cfmSoftwareVersionCommand, cfmSoftwareVersionWhole, cfmSoftwareVersionSub, cfmSoftwareVersionBranch,
159                 cfmSoftwareVersionBuild, cfmSoftwareVersionMicroBuild, cfmHardwareVersion, cfmProductGroup,
160                 cfmProductType);
161         logger.trace("getFirmware() returns {}.", result);
162         return new VeluxGwFirmware(result);
163     }
164 }