2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.flicbutton.internal.discovery;
15 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.flicbutton.internal.FlicButtonBindingConstants;
20 import org.openhab.binding.flicbutton.internal.util.FlicButtonUtils;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.thing.ThingUID;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import io.flic.fliclib.javaclient.Bdaddr;
29 import io.flic.fliclib.javaclient.FlicClient;
30 import io.flic.fliclib.javaclient.GeneralCallbacks;
31 import io.flic.fliclib.javaclient.GetInfoResponseCallback;
32 import io.flic.fliclib.javaclient.enums.BdAddrType;
33 import io.flic.fliclib.javaclient.enums.BluetoothControllerState;
36 * For each configured flicd service, there is a {@link FlicSimpleclientDiscoveryServiceImpl} which will be initialized
37 * by {@link org.openhab.binding.flicbutton.internal.FlicButtonHandlerFactory}.
39 * It can scan for Flic Buttons already that are already added to fliclib-linux-hci ("verified" buttons), *
40 * but it does not support adding and verify new buttons on it's own.
41 * New buttons have to be added (verified) e.g. via simpleclient by Shortcut Labs.
42 * Background discovery listens for new buttons that are getting verified.
44 * @author Patrick Fink - Initial contribution
47 public class FlicSimpleclientDiscoveryServiceImpl extends AbstractDiscoveryService
48 implements FlicButtonDiscoveryService {
49 private final Logger logger = LoggerFactory.getLogger(FlicSimpleclientDiscoveryServiceImpl.class);
51 private boolean activated = false;
52 private ThingUID bridgeUID;
53 private @Nullable FlicClient flicClient;
55 public FlicSimpleclientDiscoveryServiceImpl(ThingUID bridgeUID) {
56 super(FlicButtonBindingConstants.SUPPORTED_THING_TYPES_UIDS, 2, true);
57 this.bridgeUID = bridgeUID;
61 public void activate(FlicClient flicClient) {
62 this.flicClient = flicClient;
68 public void deactivate() {
74 protected void startScan() {
77 discoverVerifiedButtons();
79 } catch (IOException e) {
80 logger.warn("Error occured during button discovery", e);
81 if (this.scanListener != null) {
82 scanListener.onErrorOccurred(e);
87 protected void discoverVerifiedButtons() throws IOException {
88 flicClient.getInfo(new GetInfoResponseCallback() {
90 public void onGetInfoResponse(@Nullable BluetoothControllerState bluetoothControllerState,
91 @Nullable Bdaddr myBdAddr, @Nullable BdAddrType myBdAddrType, int maxPendingConnections,
92 int maxConcurrentlyConnectedButtons, int currentPendingConnections,
93 boolean currentlyNoSpaceForNewConnection, Bdaddr @Nullable [] verifiedButtons) throws IOException {
94 for (final @Nullable Bdaddr bdaddr : verifiedButtons) {
96 flicButtonDiscovered(bdaddr);
104 protected void startBackgroundDiscovery() {
105 super.startBackgroundDiscovery();
106 flicClient.setGeneralCallbacks(new GeneralCallbacks() {
108 public void onNewVerifiedButton(@Nullable Bdaddr bdaddr) throws IOException {
109 logger.debug("A new Flic button was added by an external flicd client: {}", bdaddr);
110 if (bdaddr != null) {
111 flicButtonDiscovered(bdaddr);
118 protected void stopBackgroundDiscovery() {
119 super.stopBackgroundDiscovery();
120 if (flicClient != null) {
121 flicClient.setGeneralCallbacks(null);
126 public ThingUID flicButtonDiscovered(Bdaddr bdaddr) {
127 logger.debug("Flic Button {} discovered!", bdaddr);
128 ThingUID flicButtonUID = FlicButtonUtils.getThingUIDFromBdAddr(bdaddr, bridgeUID);
130 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(flicButtonUID).withBridge(bridgeUID)
131 .withLabel("Flic Button " + bdaddr.toString().replace(":", ""))
132 .withProperty(FlicButtonBindingConstants.CONFIG_ADDRESS, bdaddr.toString())
133 .withRepresentationProperty(FlicButtonBindingConstants.CONFIG_ADDRESS).build();
134 this.thingDiscovered(discoveryResult);
135 return flicButtonUID;