]> git.basschouten.com Git - openhab-addons.git/blob
cc6935718cd16541cbbcc8edc4513f053bd5fc1d
[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.bluetooth;
14
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.UUID;
18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.TimeUnit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * The {@link DelegateBluetoothDevice} is an abstract parent class for BluetoothDevice implementations
26  * that delegate their functions to other BluetoothDevice instances.
27  *
28  * @author Connor Petty - Initial Contribution
29  */
30 @NonNullByDefault
31 public abstract class DelegateBluetoothDevice extends BluetoothDevice {
32
33     public DelegateBluetoothDevice(BluetoothAdapter adapter, BluetoothAddress address) {
34         super(adapter, address);
35     }
36
37     protected abstract @Nullable BluetoothDevice getDelegate();
38
39     @Override
40     public @Nullable String getName() {
41         BluetoothDevice delegate = getDelegate();
42         return delegate != null ? delegate.getName() : null;
43     }
44
45     @Override
46     public @Nullable Integer getManufacturerId() {
47         BluetoothDevice delegate = getDelegate();
48         return delegate != null ? delegate.getManufacturerId() : null;
49     }
50
51     @Override
52     public @Nullable Integer getRssi() {
53         BluetoothDevice delegate = getDelegate();
54         return delegate != null ? delegate.getRssi() : null;
55     }
56
57     @Override
58     public @Nullable Integer getTxPower() {
59         BluetoothDevice delegate = getDelegate();
60         return delegate != null ? delegate.getTxPower() : null;
61     }
62
63     @Override
64     public @Nullable BluetoothService getServices(UUID uuid) {
65         BluetoothDevice delegate = getDelegate();
66         return delegate != null ? delegate.getServices(uuid) : null;
67     }
68
69     @Override
70     public Collection<BluetoothService> getServices() {
71         BluetoothDevice delegate = getDelegate();
72         return delegate != null ? delegate.getServices() : Collections.emptySet();
73     }
74
75     @Override
76     public boolean supportsService(UUID uuid) {
77         BluetoothDevice delegate = getDelegate();
78         return delegate != null ? delegate.supportsService(uuid) : false;
79     }
80
81     @Override
82     public ConnectionState getConnectionState() {
83         BluetoothDevice delegate = getDelegate();
84         return delegate != null ? delegate.getConnectionState() : ConnectionState.DISCOVERED;
85     }
86
87     @Override
88     public boolean connect() {
89         BluetoothDevice delegate = getDelegate();
90         return delegate != null ? delegate.connect() : false;
91     }
92
93     @Override
94     public boolean disconnect() {
95         BluetoothDevice delegate = getDelegate();
96         return delegate != null ? delegate.disconnect() : false;
97     }
98
99     @Override
100     public boolean discoverServices() {
101         BluetoothDevice delegate = getDelegate();
102         return delegate != null ? delegate.discoverServices() : false;
103     }
104
105     @Override
106     public CompletableFuture<byte[]> readCharacteristic(BluetoothCharacteristic characteristic) {
107         BluetoothDevice delegate = getDelegate();
108         if (delegate == null) {
109             return CompletableFuture.failedFuture(new IllegalStateException("Delegate is null"));
110         }
111         return delegate.readCharacteristic(characteristic);
112     }
113
114     @Override
115     public CompletableFuture<@Nullable Void> writeCharacteristic(BluetoothCharacteristic characteristic, byte[] value) {
116         BluetoothDevice delegate = getDelegate();
117         if (delegate == null) {
118             return CompletableFuture.failedFuture(new IllegalStateException("Delegate is null"));
119         }
120         return delegate.writeCharacteristic(characteristic, value);
121     }
122
123     @Override
124     public boolean isNotifying(BluetoothCharacteristic characteristic) {
125         BluetoothDevice delegate = getDelegate();
126         return delegate != null ? delegate.isNotifying(characteristic) : false;
127     }
128
129     @Override
130     public CompletableFuture<@Nullable Void> enableNotifications(BluetoothCharacteristic characteristic) {
131         BluetoothDevice delegate = getDelegate();
132         if (delegate == null) {
133             return CompletableFuture.failedFuture(new IllegalStateException("Delegate is null"));
134         }
135         return delegate.enableNotifications(characteristic);
136     }
137
138     @Override
139     public CompletableFuture<@Nullable Void> disableNotifications(BluetoothCharacteristic characteristic) {
140         BluetoothDevice delegate = getDelegate();
141         if (delegate == null) {
142             return CompletableFuture.failedFuture(new IllegalStateException("Delegate is null"));
143         }
144         return delegate.disableNotifications(characteristic);
145     }
146
147     @Override
148     public boolean enableNotifications(BluetoothDescriptor descriptor) {
149         BluetoothDevice delegate = getDelegate();
150         return delegate != null ? delegate.enableNotifications(descriptor) : false;
151     }
152
153     @Override
154     public boolean disableNotifications(BluetoothDescriptor descriptor) {
155         BluetoothDevice delegate = getDelegate();
156         return delegate != null ? delegate.disableNotifications(descriptor) : false;
157     }
158
159     @Override
160     protected boolean addService(BluetoothService service) {
161         BluetoothDevice delegate = getDelegate();
162         return delegate != null ? delegate.addService(service) : false;
163     }
164
165     @Override
166     protected Collection<BluetoothDeviceListener> getListeners() {
167         BluetoothDevice delegate = getDelegate();
168         return delegate != null ? delegate.getListeners() : Collections.emptySet();
169     }
170
171     @Override
172     public @Nullable BluetoothCharacteristic getCharacteristic(UUID uuid) {
173         BluetoothDevice delegate = getDelegate();
174         return delegate != null ? delegate.getCharacteristic(uuid) : null;
175     }
176
177     @Override
178     public boolean awaitConnection(long timeout, TimeUnit unit) throws InterruptedException {
179         BluetoothDevice delegate = getDelegate();
180         return delegate != null ? delegate.awaitConnection(timeout, unit) : false;
181     }
182
183     @Override
184     public boolean awaitServiceDiscovery(long timeout, TimeUnit unit) throws InterruptedException {
185         BluetoothDevice delegate = getDelegate();
186         return delegate != null ? delegate.awaitServiceDiscovery(timeout, unit) : false;
187     }
188
189     @Override
190     public boolean isServicesDiscovered() {
191         BluetoothDevice delegate = getDelegate();
192         return delegate != null ? delegate.isServicesDiscovered() : false;
193     }
194
195     @Override
196     protected void dispose() {
197         BluetoothDevice delegate = getDelegate();
198         if (delegate != null) {
199             delegate.dispose();
200         }
201     }
202 }