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