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