]> git.basschouten.com Git - openhab-addons.git/blob
8aae21c8040e5fdd51b446454583e3905d8e6f03
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.velux.internal.VeluxBindingConstants;
21 import org.openhab.binding.velux.internal.bridge.common.Logout;
22
23 /**
24  * Specific bridge communication message supported by the Velux bridge.
25  * <P>
26  * Message semantic: Communication to deauthenticate itself, resulting in a return of current bridge state.
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 JClogout extends Logout implements JsonBridgeCommunicationProtocol {
38
39     private static final String URL = "/api/v1/auth";
40     private static final String DESCRIPTION = "deauthenticate / logout";
41
42     private Request request = new Request();
43     private Response response = new Response();
44
45     /**
46      * Bridge I/O Request message used by {@link JsonVeluxBridge} for serializing.
47      * <P>
48      * Resulting JSON:
49      *
50      * <pre>
51      * {"action":"logout","params":{}}
52      * </pre>
53      */
54     private static class Request {
55
56         @SuppressWarnings("unused")
57         private String action;
58         @SuppressWarnings("unused")
59         private Map<String, String> params;
60
61         public Request() {
62             this.action = "logout";
63             this.params = new HashMap<>();
64         }
65     }
66
67     /**
68      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
69      * methods
70      * <P>
71      * Expected JSON (sample):
72      *
73      * <pre>
74      * '{"token": "PHPnfLda71xfGlxoYEOTGQ==", "result": true, "deviceStatus": "IDLE", "data": {}, "errors": [] }'
75      * </pre>
76      */
77     private static class Response {
78         @SuppressWarnings("unused")
79         private String token = VeluxBindingConstants.UNKNOWN;
80         private boolean result;
81         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
82         @SuppressWarnings("unused")
83         private @Nullable Object data = null;
84         private String[] errors = {};
85
86         public boolean getResult() {
87             return result;
88         }
89     }
90
91     /*
92      * Methods required for interface {@link BridgeCommunicationProtocol}.
93      */
94
95     @Override
96     public String name() {
97         return DESCRIPTION;
98     }
99
100     @Override
101     public String getURL() {
102         return URL;
103     }
104
105     @Override
106     public Object getObjectOfRequest() {
107         return request;
108     }
109
110     @Override
111     public Class<Response> getClassOfResponse() {
112         return Response.class;
113     }
114
115     @Override
116     public void setResponse(Object thisResponse) {
117         response = (Response) thisResponse;
118     }
119
120     @Override
121     public boolean isCommunicationSuccessful() {
122         return response.getResult();
123     }
124
125     @Override
126     public String getDeviceStatus() {
127         return response.deviceStatus;
128     }
129
130     @Override
131     public String[] getErrors() {
132         return response.errors;
133     }
134 }