]> git.basschouten.com Git - openhab-addons.git/blob
50d1ce026c29a70213d9dd231583e91e33204dbf
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.velux.internal.VeluxBindingConstants;
18 import org.openhab.binding.velux.internal.bridge.common.RunScene;
19
20 /**
21  * Specific bridge communication message supported by the Velux bridge.
22  * <P>
23  * Message semantic: Trigger activation of a specific scene, resulting in a return of current bridge state.
24  * <P>
25  *
26  * It defines information how to send query and receive answer through the
27  * {@link org.openhab.binding.velux.internal.bridge.VeluxBridgeProvider VeluxBridgeProvider}
28  * as described by the {@link org.openhab.binding.velux.internal.bridge.json.JsonBridgeCommunicationProtocol
29  * BridgeCommunicationProtocol}.
30  *
31  * @author Guenther Schreiner - Initial contribution.
32  */
33 @NonNullByDefault
34 class JCrunScene extends RunScene implements JsonBridgeCommunicationProtocol {
35
36     private static final String URL = "/api/v1/scenes";
37     private static final String DESCRIPTION = "run Scene";
38
39     private Request request = new Request();
40     private Response response = new Response();
41
42     /*
43      * Message Objects
44      */
45     private static class ParamsRunScene {
46         @SuppressWarnings("unused")
47         private int id;
48     }
49
50     /**
51      * Bridge I/O Request message used by {@link JsonVeluxBridge}
52      * for serializing.
53      * <P>
54      * Resulting JSON (sample):
55      *
56      * <pre>
57      * {"action":"run","params":{"id":9}}
58      * </pre>
59      */
60     private static class Request {
61         @SuppressWarnings("unused")
62         private String action;
63         private ParamsRunScene params;
64
65         public Request() {
66             this.action = "run";
67             this.params = new ParamsRunScene();
68         }
69     }
70
71     /**
72      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
73      * methods
74      * <P>
75      * Expected JSON (sample):
76      *
77      * <pre>
78      * {
79      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
80      *  "result":true,
81      *  "deviceStatus":"IDLE",
82      *  "data":{},
83      *  "errors":[]
84      * }
85      * </pre>
86      */
87     private static class Response {
88         @SuppressWarnings("unused")
89         private String token = VeluxBindingConstants.UNKNOWN;
90         private boolean result;
91         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
92         @SuppressWarnings("unused")
93         private @Nullable Object data;
94         private String[] errors = {};
95
96         public boolean getResult() {
97             return result;
98         }
99     }
100
101     /*
102      * Methods required for interface {@link BridgeCommunicationProtocol}.
103      */
104
105     @Override
106     public String name() {
107         return DESCRIPTION;
108     }
109
110     @Override
111     public String getURL() {
112         return URL;
113     }
114
115     @Override
116     public Object getObjectOfRequest() {
117         return request;
118     }
119
120     @Override
121     public Class<Response> getClassOfResponse() {
122         return Response.class;
123     }
124
125     @Override
126     public void setResponse(Object response) {
127         this.response = (Response) response;
128     }
129
130     @Override
131     public boolean isCommunicationSuccessful() {
132         return response.getResult();
133     }
134
135     @Override
136     public String getDeviceStatus() {
137         return response.deviceStatus;
138     }
139
140     @Override
141     public String[] getErrors() {
142         return response.errors;
143     }
144
145     /*
146      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
147      */
148     @Override
149     public JCrunScene setSceneId(int id) {
150         request.params.id = id;
151         return this;
152     }
153 }