]> git.basschouten.com Git - openhab-addons.git/blob
b3b979914418d4ca2b76c5ca00689659ae7e2dd2
[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.lirc.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.openhab.binding.lirc.internal.LIRCBindingConstants;
19 import org.openhab.binding.lirc.internal.LIRCMessageListener;
20 import org.openhab.binding.lirc.internal.handler.LIRCBridgeHandler;
21 import org.openhab.binding.lirc.internal.messages.LIRCButtonEvent;
22 import org.openhab.binding.lirc.internal.messages.LIRCResponse;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  *
33  * @author Andrew Nagle - Initial contribution
34  */
35 public class LIRCRemoteDiscoveryService extends AbstractDiscoveryService implements LIRCMessageListener {
36
37     private final Logger logger = LoggerFactory.getLogger(LIRCRemoteDiscoveryService.class);
38
39     private LIRCBridgeHandler bridgeHandler;
40
41     public LIRCRemoteDiscoveryService(LIRCBridgeHandler lircBridgeHandler) {
42         super(LIRCBindingConstants.SUPPORTED_DEVICE_TYPES, LIRCBindingConstants.DISCOVERY_TIMOUT, true);
43         this.bridgeHandler = lircBridgeHandler;
44         bridgeHandler.registerMessageListener(this);
45     }
46
47     @Override
48     protected void startScan() {
49         logger.debug("Discovery service scan started");
50         bridgeHandler.startDeviceDiscovery();
51     }
52
53     @Override
54     public void onButtonPressed(ThingUID bridge, LIRCButtonEvent buttonEvent) {
55         addRemote(bridge, buttonEvent.getRemote());
56     }
57
58     @Override
59     public void onMessageReceived(ThingUID bridge, LIRCResponse message) {
60         LIRCResponse response = message;
61         String command = response.getCommand();
62         if ("LIST".equals(command) && response.isSuccess()) {
63             for (String remoteID : response.getData()) {
64                 addRemote(bridge, remoteID);
65             }
66         }
67     }
68
69     private void addRemote(ThingUID bridge, String remote) {
70         ThingTypeUID uid = LIRCBindingConstants.THING_TYPE_REMOTE;
71         ThingUID thingUID = new ThingUID(uid, bridge, remote);
72
73         logger.trace("Remote {}: Discovered new remote.", remote);
74         Map<String, Object> properties = new HashMap<>(1);
75         properties.put(LIRCBindingConstants.PROPERTY_REMOTE, remote);
76         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withLabel(remote).withBridge(bridge)
77                 .withProperties(properties).build();
78         thingDiscovered(discoveryResult);
79     }
80 }