]> git.basschouten.com Git - openhab-addons.git/blob
f42baef7bbabcd7e04a5559a057db82a39619c9e
[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.allplay.internal.discovery;
14
15 import static org.openhab.binding.allplay.internal.AllPlayBindingConstants.SPEAKER_THING_TYPE;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.openhab.binding.allplay.internal.AllPlayBindingConstants;
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.config.discovery.DiscoveryService;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import de.kaizencode.tchaikovsky.AllPlay;
34 import de.kaizencode.tchaikovsky.exception.AllPlayException;
35 import de.kaizencode.tchaikovsky.listener.SpeakerAnnouncedListener;
36 import de.kaizencode.tchaikovsky.speaker.Speaker;
37
38 /**
39  * Discovery service to scan for AllPlay devices.
40  *
41  * @author Dominic Lerbs - Initial contribution
42  */
43 @Component(service = DiscoveryService.class, configurationPid = "discovery.allplay")
44 public class AllPlaySpeakerDiscoveryService extends AbstractDiscoveryService implements SpeakerAnnouncedListener {
45
46     private final Logger logger = LoggerFactory.getLogger(AllPlaySpeakerDiscoveryService.class);
47
48     private static final int DISCOVERY_TIMEOUT = 30;
49     private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Collections.singleton(SPEAKER_THING_TYPE);
50     private AllPlay allPlay;
51
52     public AllPlaySpeakerDiscoveryService() {
53         super(DISCOVERABLE_THING_TYPES_UIDS, DISCOVERY_TIMEOUT);
54     }
55
56     @Override
57     protected void startScan() {
58         logger.debug("Starting scan for AllPlay devices");
59         try {
60             allPlay = new AllPlay("openHAB2-discovery");
61             allPlay.addSpeakerAnnouncedListener(this);
62             allPlay.connect();
63             allPlay.discoverSpeakers();
64         } catch (AllPlayException e) {
65             logger.warn("Error while scanning for AllPlay devices", e);
66         }
67     }
68
69     @Override
70     protected void stopScan() {
71         logger.debug("Stopping scan for AllPlay devices");
72         if (allPlay != null) {
73             allPlay.removeSpeakerAnnouncedListener(this);
74             allPlay.disconnect();
75         }
76
77         super.stopScan();
78     }
79
80     @Override
81     protected void startBackgroundDiscovery() {
82         logger.trace("Starting background scan for AllPlay devices");
83         startScan();
84     }
85
86     @Override
87     protected void stopBackgroundDiscovery() {
88         logger.trace("Stopping background scan for AllPlay devices");
89         stopScan();
90     }
91
92     @Override
93     public void deactivate() {
94         removeOlderResults(getTimestampOfLastScan());
95     }
96
97     @Override
98     public void onSpeakerAnnounced(Speaker speaker) {
99         logger.debug("Speaker {} found by discovery service", speaker);
100         ThingUID thingUID = new ThingUID(AllPlayBindingConstants.SPEAKER_THING_TYPE, speaker.getId());
101
102         Map<String, Object> properties = new HashMap<>();
103         properties.put(AllPlayBindingConstants.DEVICE_ID, speaker.getId());
104         properties.put(AllPlayBindingConstants.DEVICE_NAME, speaker.getName());
105
106         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
107                 .withRepresentationProperty(AllPlayBindingConstants.DEVICE_ID).withLabel(speaker.getName()).build();
108         thingDiscovered(discoveryResult);
109     }
110 }