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