]> git.basschouten.com Git - openhab-addons.git/blob
d5170cd071df5a7451b23a42fb6d88de5a13d7b1
[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.concurrent.CompletableFuture;
16 import java.util.concurrent.atomic.AtomicBoolean;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.bluetooth.BluetoothCharacteristic.GattCharacteristic;
21 import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification;
22
23 /**
24  * Mock implementation of a {@link BluetoothDevice}.
25  *
26  * @author Connor Petty - Initial contribution
27  */
28 @NonNullByDefault
29 public class MockBluetoothDevice extends BaseBluetoothDevice {
30
31     private AtomicBoolean servicesDiscovered = new AtomicBoolean(false);
32
33     /**
34      * This is the name that returned in the DEVICE_NAME characteristic
35      */
36     private @Nullable String deviceName = null;
37
38     public MockBluetoothDevice(BluetoothAdapter adapter, BluetoothAddress address) {
39         super(adapter, address);
40     }
41
42     @Override
43     public boolean connect() {
44         this.connectionState = ConnectionState.CONNECTED;
45         notifyListeners(BluetoothEventType.CONNECTION_STATE,
46                 new BluetoothConnectionStatusNotification(ConnectionState.CONNECTED));
47
48         discoverServices();
49
50         return true;
51     }
52
53     @Override
54     public boolean discoverServices() {
55         if (!servicesDiscovered.getAndSet(true)) {
56             populateServices();
57             notifyListeners(BluetoothEventType.SERVICES_DISCOVERED);
58         }
59         return true;
60     }
61
62     protected void populateServices() {
63         if (deviceName != null) {
64             BluetoothService service = new BluetoothService(BluetoothService.GattService.DEVICE_INFORMATION.getUUID());
65             service.addCharacteristic(new BluetoothCharacteristic(GattCharacteristic.DEVICE_NAME.getUUID(), 0));
66             addService(service);
67         }
68     }
69
70     @Override
71     public boolean disconnect() {
72         return true;
73     }
74
75     @Override
76     public boolean readCharacteristic(BluetoothCharacteristic characteristic) {
77         if (characteristic.getGattCharacteristic() == GattCharacteristic.DEVICE_NAME) {
78             characteristic.setValue(deviceName);
79             notifyListeners(BluetoothEventType.CHARACTERISTIC_READ_COMPLETE, characteristic,
80                     BluetoothCompletionStatus.SUCCESS);
81             return true;
82         }
83         return false;
84     }
85
86     public void setDeviceName(String deviceName) {
87         this.deviceName = deviceName;
88     }
89
90     @Override
91     protected void notifyListeners(BluetoothEventType event, Object... args) {
92         CompletableFuture.runAsync(() -> super.notifyListeners(event, args));
93     }
94
95     @Override
96     public boolean writeCharacteristic(BluetoothCharacteristic characteristic) {
97         return false;
98     }
99
100     @Override
101     public boolean enableNotifications(BluetoothCharacteristic characteristic) {
102         return false;
103     }
104
105     @Override
106     public boolean disableNotifications(BluetoothCharacteristic characteristic) {
107         return false;
108     }
109
110     @Override
111     public boolean enableNotifications(BluetoothDescriptor descriptor) {
112         return false;
113     }
114
115     @Override
116     public boolean disableNotifications(BluetoothDescriptor descriptor) {
117         return false;
118     }
119 }