]> git.basschouten.com Git - openhab-addons.git/blob
76b52c0026962fc0ab07f036dd060e55762208f4
[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.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     @NonNullByDefault
60     private static class Request {
61
62         @SuppressWarnings("unused")
63         private String action;
64
65         @SuppressWarnings("unused")
66         private Map<String, String> params;
67
68         public Request() {
69             this.action = "getFirmware";
70             this.params = new HashMap<>();
71         }
72     }
73
74     /**
75      * Bridge Communication Structure containing the version of the firmware.
76      * <P>
77      * Used within structure {@link JCgetFirmware} to describe the software of the Bridge.
78      */
79     @NonNullByDefault
80     private static class BCfirmwareVersion {
81         /*
82          * "version": "0.1.1.0.41.0"
83          */
84         private String version = VeluxBindingConstants.UNKNOWN;
85     }
86
87     /**
88      * Bridge I/O Response message used by {@link org.openhab.binding.velux.internal.bridge.json.JsonVeluxBridge} for
89      * deserializing with including component access methods
90      * <P>
91      * Expected JSON (sample):
92      *
93      * <pre>
94      * {
95      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
96      *  "result":true,
97      *  "deviceStatus":"IDLE",
98      *  "data":{"version":"0.1.1.0.41.0"},
99      *  "errors":[]
100      * }
101      * </pre>
102      */
103     @NonNullByDefault
104     private static class Response {
105         @SuppressWarnings("unused")
106         private String token = VeluxBindingConstants.UNKNOWN;
107         private boolean result;
108         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
109         private BCfirmwareVersion data = new BCfirmwareVersion();
110         private String[] errors = {};
111     }
112
113     /*
114      * Methods required for interface {@link BridgeCommunicationProtocol}.
115      */
116
117     @Override
118     public String name() {
119         return DESCRIPTION;
120     }
121
122     @Override
123     public String getURL() {
124         return URL;
125     }
126
127     @Override
128     public Object getObjectOfRequest() {
129         return request;
130     }
131
132     @Override
133     public Class<Response> getClassOfResponse() {
134         return Response.class;
135     }
136
137     @Override
138     public void setResponse(Object thisResponse) {
139         response = (Response) thisResponse;
140     }
141
142     @Override
143     public boolean isCommunicationSuccessful() {
144         return response.result;
145     }
146
147     @Override
148     public String getDeviceStatus() {
149         return response.deviceStatus;
150     }
151
152     @Override
153     public String[] getErrors() {
154         return response.errors;
155     }
156
157     /**
158      * Methods in addition to interface {@link JsonBridgeCommunicationProtocol}.
159      */
160     @Override
161     public VeluxGwFirmware getFirmware() {
162         VeluxGwFirmware gwFirmware = new VeluxGwFirmware(response.data.version);
163         return gwFirmware;
164     }
165 }