]> git.basschouten.com Git - openhab-addons.git/blob
e96f30f2d561eaf4b30389050f07ccdb407dbfd7
[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.SetSceneVelocity;
19
20 /**
21  * Specific bridge communication message supported by the Velux bridge.
22  * <P>
23  * Message semantic: setting of scene silent mode, 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 JCsetSceneVelocity extends SetSceneVelocity implements JsonBridgeCommunicationProtocol {
35
36     private static final String URL = "/api/v1/scenes";
37     private static final String DESCRIPTION = "modify silent mode";
38
39     private Request request = new Request();
40     private Response response = new Response();
41
42     private static int productId;
43     private static boolean silentMode;
44
45     /*
46      * Message Objects
47      */
48     private static class ParamsRunScene {
49         @SuppressWarnings("unused")
50         private int id;
51         @SuppressWarnings("unused")
52         private boolean silent;
53
54         private ParamsRunScene(int id, boolean silent) {
55             this.id = id;
56             this.silent = silent;
57         }
58     }
59
60     /**
61      * Bridge I/O Request message used by {@link JsonVeluxBridge}
62      * for serializing.
63      * <P>
64      * Resulting JSON (sample):
65      *
66      * <pre>
67      * {"action":"setSilentMode","params":{"id":9,"silent":false}}}
68      * </pre>
69      */
70     private static class Request {
71         @SuppressWarnings("unused")
72         private String action;
73         @SuppressWarnings("unused")
74         private ParamsRunScene params;
75
76         public Request() {
77             this.action = "setSilentMode";
78             this.params = new ParamsRunScene(JCsetSceneVelocity.productId, JCsetSceneVelocity.silentMode);
79         }
80     }
81
82     /**
83      * Bridge I/O Response message used by {@link JsonVeluxBridge} for deserializing with including component access
84      * methods
85      * <P>
86      * Expected JSON (sample):
87      *
88      * <pre>
89      * {
90      *  "token":"RHIKGlJyZhidI/JSK0a2RQ==",
91      *  "result":true,
92      *  "deviceStatus":"IDLE",
93      *  "data":{},
94      *  "errors":[]
95      * }
96      * </pre>
97      */
98     private static class Response {
99         @SuppressWarnings("unused")
100         private String token = VeluxBindingConstants.UNKNOWN;
101         private boolean result;
102         private String deviceStatus = VeluxBindingConstants.UNKNOWN;
103         @SuppressWarnings("unused")
104         private @Nullable Object data;
105         private String[] errors = {};
106
107         public boolean getResult() {
108             return result;
109         }
110     }
111
112     /*
113      * Methods required for interface {@link BridgeCommunicationProtocol}.
114      */
115
116     @Override
117     public String name() {
118         return DESCRIPTION;
119     }
120
121     @Override
122     public String getURL() {
123         return URL;
124     }
125
126     @Override
127     public Object getObjectOfRequest() {
128         return request;
129     }
130
131     @Override
132     public Class<Response> getClassOfResponse() {
133         return Response.class;
134     }
135
136     @Override
137     public void setResponse(Object response) {
138         this.response = (Response) response;
139     }
140
141     @Override
142     public boolean isCommunicationSuccessful() {
143         return response.getResult();
144     }
145
146     @Override
147     public String getDeviceStatus() {
148         return response.deviceStatus;
149     }
150
151     @Override
152     public String[] getErrors() {
153         return response.errors;
154     }
155
156     /*
157      * Methods in addition to interface {@link BridgeCommunicationProtocol}.
158      */
159     @Override
160     public JCsetSceneVelocity setMode(int id, boolean silent) {
161         JCsetSceneVelocity.productId = id;
162         JCsetSceneVelocity.silentMode = silent;
163         return this;
164     }
165 }