]> git.basschouten.com Git - openhab-addons.git/blob
9559b0ad03d098c592b78baac2efd3206537a4f7
[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.am43.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.bluetooth.BluetoothBindingConstants;
23 import org.openhab.binding.bluetooth.BluetoothDevice.ConnectionState;
24 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
25 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32
33 /**
34  * This discovery participant is able to recognize AM43 devices and create discovery results for them.
35  *
36  * @author Connor Petty - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 @Component
41 public class AM43DiscoveryParticipant implements BluetoothDiscoveryParticipant {
42
43     @Override
44     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
45         return Collections.singleton(AM43BindingConstants.THING_TYPE_AM43);
46     }
47
48     @Override
49     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
50         ThingUID thingUID = getThingUID(device);
51         if (thingUID == null) {
52             return null;
53         }
54         String label = "AM43 Blind Drive Motor";
55         Map<String, Object> properties = new HashMap<>();
56         properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
57         properties.put(Thing.PROPERTY_MODEL_ID, "AM43-0.45/40-ES-EB");
58         properties.put(Thing.PROPERTY_VENDOR, "A-OK Precision motor Ltd.");
59         Integer txPower = device.getTxPower();
60         if (txPower != null) {
61             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
62         }
63
64         // Create the discovery result and add to the inbox
65         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
66                 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
67                 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
68     }
69
70     @Override
71     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
72         if (device.getConnectionState() == ConnectionState.CONNECTED
73                 && device.supportsService(AM43BindingConstants.SERVICE_UUID)) {
74             return new ThingUID(AM43BindingConstants.THING_TYPE_AM43, device.getAdapter().getUID(),
75                     device.getAddress().toString().toLowerCase().replace(":", ""));
76         }
77         return null;
78     }
79
80     @Override
81     public boolean requiresConnection(BluetoothDiscoveryDevice device) {
82         return device.getManufacturerId() == null && device.getName() != null;
83     }
84 }