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