]> git.basschouten.com Git - openhab-addons.git/blob
b58d1c32406560e9a46475f8857157653c9f2205
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.boschshc.internal.services;
14
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeoutException;
17 import java.util.function.Consumer;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.boschshc.internal.devices.bridge.BoschSHCBridgeHandler;
22 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
23 import org.openhab.binding.boschshc.internal.services.dto.BoschSHCServiceState;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.gson.Gson;
28 import com.google.gson.JsonElement;
29
30 /**
31  * Base class of a service of a Bosch Smart Home device.
32  * The services of the devices and their official APIs can be found here: https://apidocs.bosch-smarthome.com/local/
33  * 
34  * @author Christian Oeing - Initial contribution
35  */
36 @NonNullByDefault
37 public abstract class BoschSHCService<TState extends BoschSHCServiceState> {
38
39     private final Logger logger = LoggerFactory.getLogger(BoschSHCService.class);
40
41     /**
42      * Unique service name
43      */
44     private final String serviceName;
45
46     /**
47      * Class of service state
48      */
49     private final Class<TState> stateClass;
50
51     /**
52      * gson instance to convert a class to json string and back.
53      */
54     private final Gson gson = new Gson();
55
56     /**
57      * Bridge to use for communication from/to the device
58      */
59     private @Nullable BoschSHCBridgeHandler bridgeHandler;
60
61     /**
62      * Id of device the service belongs to
63      */
64     private @Nullable String deviceId;
65
66     /**
67      * Function to call after receiving state updates from the device
68      */
69     private @Nullable Consumer<TState> stateUpdateListener;
70
71     /**
72      * Constructor
73      * 
74      * @param serviceName Unique name of the service.
75      * @param stateClass State class that this service uses for data transfers from/to the device.
76      */
77     protected BoschSHCService(String serviceName, Class<TState> stateClass) {
78         this.serviceName = serviceName;
79         this.stateClass = stateClass;
80     }
81
82     /**
83      * Initializes the service
84      * 
85      * @param bridgeHandler Bridge to use for communication from/to the device
86      * @param deviceId Id of device this service is for
87      * @param stateUpdateListener Function to call when a state update was received from the device.
88      */
89     public void initialize(BoschSHCBridgeHandler bridgeHandler, String deviceId,
90             @Nullable Consumer<TState> stateUpdateListener) {
91         this.bridgeHandler = bridgeHandler;
92         this.deviceId = deviceId;
93         this.stateUpdateListener = stateUpdateListener;
94     }
95
96     /**
97      * Returns the unique name of this service.
98      * 
99      * @return Unique name of the service.
100      */
101     public String getServiceName() {
102         return this.serviceName;
103     }
104
105     /**
106      * Returns the class of the state this service provides.
107      * 
108      * @return Class of the state this service provides.
109      */
110     public Class<TState> getStateClass() {
111         return this.stateClass;
112     }
113
114     /**
115      * Requests the current state of the service and updates it.
116      * 
117      * @throws ExecutionException
118      * @throws TimeoutException
119      * @throws InterruptedException
120      * @throws BoschSHCException
121      */
122     public void refreshState() throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
123         @Nullable
124         TState state = this.getState();
125         if (state != null) {
126             this.onStateUpdate(state);
127         }
128     }
129
130     /**
131      * Requests the current state of the device with the specified id.
132      * 
133      * @return Current state of the device.
134      * @throws ExecutionException
135      * @throws TimeoutException
136      * @throws InterruptedException
137      * @throws BoschSHCException
138      */
139     public @Nullable TState getState()
140             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
141         String deviceId = this.deviceId;
142         if (deviceId == null) {
143             return null;
144         }
145         BoschSHCBridgeHandler bridgeHandler = this.bridgeHandler;
146         if (bridgeHandler == null) {
147             return null;
148         }
149         return bridgeHandler.getState(deviceId, this.serviceName, this.stateClass);
150     }
151
152     /**
153      * Sets the state of the device with the specified id.
154      * 
155      * @param state State to set.
156      * @throws InterruptedException
157      * @throws ExecutionException
158      * @throws TimeoutException
159      */
160     public void setState(TState state) throws InterruptedException, TimeoutException, ExecutionException {
161         String deviceId = this.deviceId;
162         if (deviceId == null) {
163             return;
164         }
165         BoschSHCBridgeHandler bridgeHandler = this.bridgeHandler;
166         if (bridgeHandler == null) {
167             return;
168         }
169         bridgeHandler.putState(deviceId, this.serviceName, state);
170     }
171
172     /**
173      * A state update was received from the bridge
174      * 
175      * @param stateData Current state of service. Serialized as JSON.
176      */
177     public void onStateUpdate(JsonElement stateData) {
178         @Nullable
179         TState state = gson.fromJson(stateData, this.stateClass);
180         if (state == null) {
181             this.logger.warn("Received invalid, expected type {}", this.stateClass.getName());
182             return;
183         }
184         this.onStateUpdate(state);
185     }
186
187     /**
188      * A state update was received from the bridge.
189      * 
190      * @param state Current state of service as an instance of the state class.
191      */
192     private void onStateUpdate(TState state) {
193         Consumer<TState> stateUpdateListener = this.stateUpdateListener;
194         if (stateUpdateListener != null) {
195             stateUpdateListener.accept(state);
196         }
197     }
198 }