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