]> git.basschouten.com Git - openhab-addons.git/blob
5d35febd6fcf38b946367db8601937f2dc07d212
[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.json;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.velux.internal.VeluxBindingConstants;
20 import org.openhab.binding.velux.internal.bridge.common.GetFirmware;
21 import org.openhab.binding.velux.internal.things.VeluxGwFirmware;
22
23 /**
24  * Specific bridge communication message supported by the Velux bridge.
25  * <P>
26  * Message semantic: Retrieval of Bridge configuration.
27  * <P>
28  *
29  * It defines information how to send query and receive answer through the
30  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
31  * as described by the {@link org.openhab.binding.velux.internal.bridge.json.JsonBridgeCommunicationProtocol
32  * BridgeCommunicationProtocol}.
33  *
34  * @author Guenther Schreiner - Initial contribution.
35  */
36 @NonNullByDefault
37 class JCgetFirmware extends GetFirmware implements JsonBridgeCommunicationProtocol {
38
39     private static final String URL = "/api/v1/settings";
40     private static final String DESCRIPTION = "get firmware version";
41
42     private Request request = new Request();
43     private Response response = new Response();
44
45     /*
46      * Message Objects
47      */
48
49     /**
50      * Bridge I/O Request message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge
51      * JsonVeluxBridge} for serializing.
52      * <P>
53      * Resulting JSON:
54      *
55      * <pre>
56      * {"action":"getFirmware","params":{}}
57      * </pre>
58      */
59     private static class Request {
60
61         @SuppressWarnings("unused")
62         private String action;
63
64         @SuppressWarnings("unused")
65         private Map<String, String> params;
66
67         public Request() {
68             this.action = "getFirmware";
69             this.params = new HashMap<>();
70         }
71     }
72
73     /**
74      * Bridge Communication Structure containing the version of the firmware.
75      * <P>
76      * Used within structure {@link JCgetFirmware} to describe the software of the Bridge.
77      */
78     private static class BCfirmwareVersion {
79         /*
80          * "version": "0.1.1.0.41.0"
81          */
82         private String version = VeluxBindingConstants.UNKNOWN;
83     }
84
85     /**
86      * Bridge I/O Response message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge} for
87      * deserializing with including component access methods
88      * <P>
89      * Expected JSON (sample):
90      *
91      * <pre>
92      * {
93      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
94      *  "result":true,
95      *  "deviceStatus":"IDLE",
96      *  "data":{"version":"0.1.1.0.41.0"},
97      *  "errors":[]
98      * }
99      * </pre>
100      */
101     private static class Response {
102         @SuppressWarnings("unused")
103         private String token = VeluxBindingConstants.UNKNOWN;
104         private boolean result;
105         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
106         private BCfirmwareVersion data = new BCfirmwareVersion();
107         private String[] errors = {};
108     }
109
110     /*
111      * Methods required for interface {@link BridgeCommunicationProtocol}.
112      */
113
114     @Override
115     public String name() {
116         return DESCRIPTION;
117     }
118
119     @Override
120     public String getURL() {
121         return URL;
122     }
123
124     @Override
125     public Object getObjectOfRequest() {
126         return request;
127     }
128
129     @Override
130     public Class<Response> getClassOfResponse() {
131         return Response.class;
132     }
133
134     @Override
135     public void setResponse(Object thisResponse) {
136         response = (Response) thisResponse;
137     }
138
139     @Override
140     public boolean isCommunicationSuccessful() {
141         return response.result;
142     }
143
144     @Override
145     public String getDeviceStatus() {
146         return response.deviceStatus;
147     }
148
149     @Override
150     public String[] getErrors() {
151         return response.errors;
152     }
153
154     /**
155      * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
156      */
157     @Override
158     public VeluxGwFirmware getFirmware() {
159         VeluxGwFirmware gwFirmware = new VeluxGwFirmware(response.data.version);
160         return gwFirmware;
161     }
162 }