]> git.basschouten.com Git - openhab-addons.git/blob
98251ae0f760069d0b9a6b93ff7cbd6c27018b12
[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.bluetooth.roaming.internal;
14
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Objects;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.CopyOnWriteArrayList;
21 import java.util.concurrent.atomic.AtomicReference;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.bluetooth.BluetoothAdapter;
26 import org.openhab.binding.bluetooth.BluetoothAddress;
27 import org.openhab.binding.bluetooth.BluetoothCharacteristic;
28 import org.openhab.binding.bluetooth.BluetoothCompletionStatus;
29 import org.openhab.binding.bluetooth.BluetoothDescriptor;
30 import org.openhab.binding.bluetooth.BluetoothDevice;
31 import org.openhab.binding.bluetooth.BluetoothDeviceListener;
32 import org.openhab.binding.bluetooth.DelegateBluetoothDevice;
33 import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
34 import org.openhab.binding.bluetooth.notification.BluetoothScanNotification;
35
36 /**
37  * The {@link RoamingBluetoothDevice} acts as a roaming device by delegating
38  * its operations to actual adapters.
39  *
40  * @author Connor Petty - Initial contribution
41  */
42 @NonNullByDefault
43 public class RoamingBluetoothDevice extends DelegateBluetoothDevice {
44
45     private final Map<BluetoothDevice, Listener> devices = new ConcurrentHashMap<>();
46
47     private final List<BluetoothDeviceListener> eventListeners = new CopyOnWriteArrayList<>();
48
49     private final AtomicReference<@Nullable BluetoothDevice> currentDelegateRef = new AtomicReference<>();
50
51     protected RoamingBluetoothDevice(RoamingBridgeHandler roamingAdapter, BluetoothAddress address) {
52         super(roamingAdapter, address);
53     }
54
55     public void addBluetoothDevice(BluetoothDevice device) {
56         device.addListener(Objects.requireNonNull(devices.computeIfAbsent(device, Listener::new)));
57     }
58
59     public void removeBluetoothDevice(BluetoothDevice device) {
60         BluetoothDeviceListener listener = devices.remove(device);
61         if (listener != null) {
62             device.removeListener(listener);
63         }
64     }
65
66     @Override
67     protected Collection<BluetoothDeviceListener> getListeners() {
68         return eventListeners;
69     }
70
71     @Override
72     protected @Nullable BluetoothDevice getDelegate() {
73         BluetoothDevice newDelegate = null;
74         int newRssi = Integer.MIN_VALUE;
75         for (BluetoothDevice device : devices.keySet()) {
76             ConnectionState state = device.getConnectionState();
77             if (state == ConnectionState.CONNECTING || state == ConnectionState.CONNECTED) {
78                 newDelegate = device;
79                 break;
80             }
81             Integer rssi = device.getRssi();
82             if (rssi != null && (newDelegate == null || rssi > newRssi)) {
83                 newRssi = rssi;
84                 newDelegate = device;
85             }
86         }
87         BluetoothDevice oldDelegate = currentDelegateRef.getAndSet(newDelegate);
88         if (oldDelegate != newDelegate) { // using reference comparison is valid in this case
89             notifyListeners(BluetoothEventType.ADAPTER_CHANGED, getAdapter(newDelegate));
90         }
91         return newDelegate;
92     }
93
94     private BluetoothAdapter getAdapter(@Nullable BluetoothDevice delegate) {
95         if (delegate != null) {
96             return delegate.getAdapter();
97         }
98         // as a last resort we return our "actual" adapter
99         return super.getAdapter();
100     }
101
102     @Override
103     public BluetoothAdapter getAdapter() {
104         return getAdapter(currentDelegateRef.get());
105     }
106
107     private class Listener implements BluetoothDeviceListener {
108
109         private BluetoothDevice device;
110
111         public Listener(BluetoothDevice device) {
112             this.device = device;
113         }
114
115         @Override
116         public void onScanRecordReceived(BluetoothScanNotification scanNotification) {
117             if (device == getDelegate()) {
118                 notifyListeners(BluetoothEventType.SCAN_RECORD, scanNotification);
119             }
120         }
121
122         @Override
123         public void onConnectionStateChange(BluetoothConnectionStatusNotification connectionNotification) {
124             if (device == getDelegate()) {
125                 notifyListeners(BluetoothEventType.CONNECTION_STATE, connectionNotification);
126             }
127         }
128
129         @Override
130         public void onServicesDiscovered() {
131             device.getServices().forEach(RoamingBluetoothDevice.this::addService);
132             if (device == getDelegate()) {
133                 notifyListeners(BluetoothEventType.SERVICES_DISCOVERED);
134             }
135         }
136
137         @Override
138         public void onCharacteristicReadComplete(BluetoothCharacteristic characteristic,
139                 BluetoothCompletionStatus status) {
140             if (device == getDelegate()) {
141                 notifyListeners(BluetoothEventType.CHARACTERISTIC_READ_COMPLETE, characteristic, status);
142             }
143         }
144
145         @Override
146         public void onCharacteristicWriteComplete(BluetoothCharacteristic characteristic,
147                 BluetoothCompletionStatus status) {
148             if (device == getDelegate()) {
149                 notifyListeners(BluetoothEventType.CHARACTERISTIC_WRITE_COMPLETE, characteristic);
150             }
151         }
152
153         @Override
154         public void onCharacteristicUpdate(BluetoothCharacteristic characteristic) {
155             if (device == getDelegate()) {
156                 notifyListeners(BluetoothEventType.CHARACTERISTIC_UPDATED, characteristic);
157             }
158         }
159
160         @Override
161         public void onDescriptorUpdate(BluetoothDescriptor bluetoothDescriptor) {
162             if (device == getDelegate()) {
163                 notifyListeners(BluetoothEventType.DESCRIPTOR_UPDATED, bluetoothDescriptor);
164             }
165         }
166
167         @Override
168         public void onAdapterChanged(BluetoothAdapter adapter) {
169             // do nothing since we are the ones that are supposed to trigger this
170         }
171     }
172 }