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