]> git.basschouten.com Git - openhab-addons.git/blob
bc4475599022678e47087ca2aabb920a79e778ae
[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 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     @NonNullByDefault
46     private static class ParamsRunScene {
47         @SuppressWarnings("unused")
48         private int id;
49     }
50
51     /**
52      * Bridge I/O Request message used by {@link JsonVeluxBridge}
53      * for serializing.
54      * <P>
55      * Resulting JSON (sample):
56      *
57      * <pre>
58      * {"action":"run","params":{"id":9}}
59      * </pre>
60      */
61     @NonNullByDefault
62     private static class Request {
63         @SuppressWarnings("unused")
64         private String action;
65         private ParamsRunScene params;
66
67         public Request() {
68             this.action = "run";
69             this.params = new ParamsRunScene();
70         }
71     }
72
73     /**
74      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
75      * methods
76      * <P>
77      * Expected JSON (sample):
78      *
79      * <pre>
80      * {
81      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
82      *  "result":true,
83      *  "deviceStatus":"IDLE",
84      *  "data":{},
85      *  "errors":[]
86      * }
87      * </pre>
88      */
89     @NonNullByDefault
90     private static class Response {
91         @SuppressWarnings("unused")
92         private String token = VeluxBindingConstants.UNKNOWN;
93         private boolean result;
94         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
95         @SuppressWarnings("unused")
96         private @Nullable Object data;
97         private String[] errors = {};
98
99         public boolean getResult() {
100             return result;
101         }
102     }
103
104     /*
105      * Methods required for interface {@link BridgeCommunicationProtocol}.
106      */
107
108     @Override
109     public String name() {
110         return DESCRIPTION;
111     }
112
113     @Override
114     public String getURL() {
115         return URL;
116     }
117
118     @Override
119     public Object getObjectOfRequest() {
120         return request;
121     }
122
123     @Override
124     public Class<Response> getClassOfResponse() {
125         return Response.class;
126     }
127
128     @Override
129     public void setResponse(Object response) {
130         this.response = (Response) response;
131     }
132
133     @Override
134     public boolean isCommunicationSuccessful() {
135         return response.getResult();
136     }
137
138     @Override
139     public String getDeviceStatus() {
140         return response.deviceStatus;
141     }
142
143     @Override
144     public String[] getErrors() {
145         return response.errors;
146     }
147
148     /*
149      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
150      */
151     @Override
152     public JCrunScene setSceneId(int id) {
153         request.params.id = id;
154         return this;
155     }
156 }