]> git.basschouten.com Git - openhab-addons.git/blob
e584773588b14de1bb4e167a59e4c3c465590188
[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.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     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 = "get";
70             this.params = new HashMap<>();
71         }
72     }
73
74     /**
75      * Bridge Communication Structure containing the network parameters.
76      * <P>
77      * Used within structure {@link JCgetLANConfig} to describe the network connectivity of the Bridge.
78      *
79      * <pre>
80      * {"ipAddress":"192.168.45.9","subnetMask":"255.255.255.0","defaultGateway":"192.168.45.129","dhcp":false}
81      * </pre>
82      */
83     private static class BCLANConfig {
84         private String ipAddress = VeluxBindingConstants.UNKNOWN;
85         private String subnetMask = VeluxBindingConstants.UNKNOWN;
86         private String defaultGateway = VeluxBindingConstants.UNKNOWN;
87         private boolean dhcp;
88
89         @Override
90         public String toString() {
91             return String.format("ipAddress=%s,subnetMask=%s,defaultGateway=%s,dhcp=%s", this.ipAddress,
92                     this.subnetMask, this.defaultGateway, this.dhcp ? "on" : "off");
93         }
94     }
95
96     /**
97      * Bridge I/O Response message used by {@link JsonVeluxBridge} for unmarshalling with including component access
98      * methods
99      * <P>
100      * Expected JSON (sample):
101      *
102      * <pre>
103      * {
104      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
105      *  "result":true,
106      *  "deviceStatus":"IDLE",
107      *  "data":"ipAddress":"192.168.45.9","subnetMask":"255.255.255.0","defaultGateway":"192.168.45.129","dhcp":false},
108      *  "errors":[]
109      * }
110      * </pre>
111      */
112     private static class Response {
113         @SuppressWarnings("unused")
114         private String token = VeluxBindingConstants.UNKNOWN;
115         private boolean result;
116         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
117         private BCLANConfig data = new BCLANConfig();
118         private String[] errors = {};
119
120         public boolean getResult() {
121             return result;
122         }
123
124         public String getDeviceStatus() {
125             return deviceStatus;
126         }
127
128         public String[] getErrors() {
129             return errors;
130         }
131     }
132
133     /*
134      * Methods required for interface {@link BridgeCommunicationProtocol}.
135      */
136
137     @Override
138     public String name() {
139         return DESCRIPTION;
140     }
141
142     @Override
143     public String getURL() {
144         return URL;
145     }
146
147     @Override
148     public Object getObjectOfRequest() {
149         return request;
150     }
151
152     @Override
153     public Class<Response> getClassOfResponse() {
154         return Response.class;
155     }
156
157     @Override
158     public void setResponse(Object response) {
159         this.response = (Response) response;
160     }
161
162     @Override
163     public boolean isCommunicationSuccessful() {
164         return response.getResult();
165     }
166
167     @Override
168     public String getDeviceStatus() {
169         return response.getDeviceStatus();
170     }
171
172     @Override
173     public String[] getErrors() {
174         return response.getErrors();
175     }
176
177     /**
178      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
179      */
180     @Override
181     public VeluxGwLAN getLANConfig() {
182         VeluxGwLAN gwLAN = new VeluxGwLAN(response.data.ipAddress, response.data.subnetMask,
183                 response.data.defaultGateway, response.data.dhcp);
184         return gwLAN;
185     }
186 }