]> git.basschouten.com Git - openhab-addons.git/blob
1e08448a0ae63c3ddbef3f2149575c1b69269291
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.flicbutton.internal.discovery;
14
15 import java.io.IOException;
16
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;
27
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;
34
35 /**
36  * For each configured flicd service, there is a {@link FlicSimpleclientDiscoveryServiceImpl} which will be initialized
37  * by {@link org.openhab.binding.flicbutton.internal.FlicButtonHandlerFactory}.
38  *
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.
43  *
44  * @author Patrick Fink - Initial contribution
45  */
46 @NonNullByDefault
47 public class FlicSimpleclientDiscoveryServiceImpl extends AbstractDiscoveryService
48         implements FlicButtonDiscoveryService {
49     private final Logger logger = LoggerFactory.getLogger(FlicSimpleclientDiscoveryServiceImpl.class);
50
51     private boolean activated = false;
52     private ThingUID bridgeUID;
53     private @Nullable FlicClient flicClient;
54
55     public FlicSimpleclientDiscoveryServiceImpl(ThingUID bridgeUID) {
56         super(FlicButtonBindingConstants.SUPPORTED_THING_TYPES_UIDS, 2, true);
57         this.bridgeUID = bridgeUID;
58     }
59
60     @Override
61     public void activate(FlicClient flicClient) {
62         this.flicClient = flicClient;
63         activated = true;
64         super.activate(null);
65     }
66
67     @Override
68     public void deactivate() {
69         activated = false;
70         super.deactivate();
71     }
72
73     @Override
74     protected void startScan() {
75         try {
76             if (activated) {
77                 discoverVerifiedButtons();
78             }
79         } catch (IOException e) {
80             logger.warn("Error occured during button discovery", e);
81             if (this.scanListener != null) {
82                 scanListener.onErrorOccurred(e);
83             }
84         }
85     }
86
87     protected void discoverVerifiedButtons() throws IOException {
88         flicClient.getInfo(new GetInfoResponseCallback() {
89             @Override
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) {
95                     if (bdaddr != null) {
96                         flicButtonDiscovered(bdaddr);
97                     }
98                 }
99             }
100         });
101     }
102
103     @Override
104     protected void startBackgroundDiscovery() {
105         super.startBackgroundDiscovery();
106         flicClient.setGeneralCallbacks(new GeneralCallbacks() {
107             @Override
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);
112                 }
113             }
114         });
115     }
116
117     @Override
118     protected void stopBackgroundDiscovery() {
119         super.stopBackgroundDiscovery();
120         if (flicClient != null) {
121             flicClient.setGeneralCallbacks(null);
122         }
123     }
124
125     @Override
126     public ThingUID flicButtonDiscovered(Bdaddr bdaddr) {
127         logger.debug("Flic Button {} discovered!", bdaddr);
128         ThingUID flicButtonUID = FlicButtonUtils.getThingUIDFromBdAddr(bdaddr, bridgeUID);
129
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;
136     }
137 }