]> git.basschouten.com Git - openhab-addons.git/blob
62f788d2108d493716114c1407358e0538cfa539
[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.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     @NonNullByDefault
55     private static class Request {
56
57         @SuppressWarnings("unused")
58         private String action;
59         @SuppressWarnings("unused")
60         private Map<String, String> params;
61
62         public Request() {
63             this.action = "logout";
64             this.params = new HashMap<>();
65         }
66     }
67
68     /**
69      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
70      * methods
71      * <P>
72      * Expected JSON (sample):
73      *
74      * <pre>
75      * '{"token": "PHPnfLda71xfGlxoYEOTGQ==", "result": true, "deviceStatus": "IDLE", "data": {}, "errors": [] }'
76      * </pre>
77      */
78     @NonNullByDefault
79     private static class Response {
80         @SuppressWarnings("unused")
81         private String token = VeluxBindingConstants.UNKNOWN;
82         private boolean result;
83         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
84         @SuppressWarnings("unused")
85         private @Nullable Object data = null;
86         private String[] errors = {};
87
88         public boolean getResult() {
89             return result;
90         }
91     }
92
93     /*
94      * Methods required for interface {@link BridgeCommunicationProtocol}.
95      */
96
97     @Override
98     public String name() {
99         return DESCRIPTION;
100     }
101
102     @Override
103     public String getURL() {
104         return URL;
105     }
106
107     @Override
108     public Object getObjectOfRequest() {
109         return request;
110     }
111
112     @Override
113     public Class<Response> getClassOfResponse() {
114         return Response.class;
115     }
116
117     @Override
118     public void setResponse(Object thisResponse) {
119         response = (Response) thisResponse;
120     }
121
122     @Override
123     public boolean isCommunicationSuccessful() {
124         return response.getResult();
125     }
126
127     @Override
128     public String getDeviceStatus() {
129         return response.deviceStatus;
130     }
131
132     @Override
133     public String[] getErrors() {
134         return response.errors;
135     }
136 }