]> git.basschouten.com Git - openhab-addons.git/blob
14165afb473ed6c9215dd71ed1a0b97f6ba7230d
[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.BridgeCommunicationProtocol;
21 import org.openhab.binding.velux.internal.bridge.common.GetLANConfig;
22 import org.openhab.binding.velux.internal.things.VeluxGwLAN;
23
24 /**
25  * Specific bridge communication message supported by the Velux bridge.
26  * <P>
27  * Message semantic: Retrieval of LAN configuration.
28  * <P>
29  *
30  * It defines information how to send query and receive answer through the
31  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
32  * as described by the {@link org.openhab.binding.velux.internal.bridge.json.JsonBridgeCommunicationProtocol
33  * BridgeCommunicationProtocol}.
34  *
35  * @author Guenther Schreiner - Initial contribution.
36  */
37 @NonNullByDefault
38 class JCgetLANConfig extends GetLANConfig implements BridgeCommunicationProtocol, JsonBridgeCommunicationProtocol {
39
40     private static final String URL = "/api/v1/lan";
41     private static final String DESCRIPTION = "get LAN configuration";
42
43     private Request request = new Request();
44     private Response response = new Response();
45
46     /*
47      * Message Objects
48      */
49
50     /**
51      * Bridge I/O Request message used by {@link JsonVeluxBridge}
52      * for serializing.
53      * <P>
54      * Resulting JSON:
55      *
56      * <pre>
57      * {"action":"get","params":{}}
58      * </pre>
59      */
60     @NonNullByDefault
61     private static class Request {
62
63         @SuppressWarnings("unused")
64         private String action;
65
66         @SuppressWarnings("unused")
67         private Map<String, String> params;
68
69         public Request() {
70             this.action = "get";
71             this.params = new HashMap<>();
72         }
73     }
74
75     /**
76      * Bridge Communication Structure containing the network parameters.
77      * <P>
78      * Used within structure {@link JCgetLANConfig} to describe the network connectivity of the Bridge.
79      *
80      * <pre>
81      * {"ipAddress":"192.168.45.9","subnetMask":"255.255.255.0","defaultGateway":"192.168.45.129","dhcp":false}
82      * </pre>
83      */
84     @NonNullByDefault
85     private static class BCLANConfig {
86         private String ipAddress = VeluxBindingConstants.UNKNOWN;
87         private String subnetMask = VeluxBindingConstants.UNKNOWN;
88         private String defaultGateway = VeluxBindingConstants.UNKNOWN;
89         private boolean dhcp;
90
91         @Override
92         public String toString() {
93             return String.format("ipAddress=%s,subnetMask=%s,defaultGateway=%s,dhcp=%s", this.ipAddress,
94                     this.subnetMask, this.defaultGateway, this.dhcp ? "on" : "off");
95         }
96     }
97
98     /**
99      * Bridge I/O Response message used by {@link JsonVeluxBridge} for unmarshalling with including component access
100      * methods
101      * <P>
102      * Expected JSON (sample):
103      *
104      * <pre>
105      * {
106      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
107      *  "result":true,
108      *  "deviceStatus":"IDLE",
109      *  "data":"ipAddress":"192.168.45.9","subnetMask":"255.255.255.0","defaultGateway":"192.168.45.129","dhcp":false},
110      *  "errors":[]
111      * }
112      * </pre>
113      */
114     @NonNullByDefault
115     private static class Response {
116         @SuppressWarnings("unused")
117         private String token = VeluxBindingConstants.UNKNOWN;
118         private boolean result;
119         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
120         private BCLANConfig data = new BCLANConfig();
121         private String[] errors = {};
122
123         public boolean getResult() {
124             return result;
125         }
126
127         public String getDeviceStatus() {
128             return deviceStatus;
129         }
130
131         public String[] getErrors() {
132             return errors;
133         }
134     }
135
136     /*
137      * Methods required for interface {@link BridgeCommunicationProtocol}.
138      */
139
140     @Override
141     public String name() {
142         return DESCRIPTION;
143     }
144
145     @Override
146     public String getURL() {
147         return URL;
148     }
149
150     @Override
151     public Object getObjectOfRequest() {
152         return request;
153     }
154
155     @Override
156     public Class<Response> getClassOfResponse() {
157         return Response.class;
158     }
159
160     @Override
161     public void setResponse(Object response) {
162         this.response = (Response) response;
163     }
164
165     @Override
166     public boolean isCommunicationSuccessful() {
167         return response.getResult();
168     }
169
170     @Override
171     public String getDeviceStatus() {
172         return response.getDeviceStatus();
173     }
174
175     @Override
176     public String[] getErrors() {
177         return response.getErrors();
178     }
179
180     /**
181      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
182      */
183     @Override
184     public VeluxGwLAN getLANConfig() {
185         VeluxGwLAN gwLAN = new VeluxGwLAN(response.data.ipAddress, response.data.subnetMask,
186                 response.data.defaultGateway, response.data.dhcp);
187         return gwLAN;
188     }
189 }