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.allplay.internal.discovery;
15 import static org.openhab.binding.allplay.internal.AllPlayBindingConstants.SPEAKER_THING_TYPE;
17 import java.util.HashMap;
21 import org.openhab.binding.allplay.internal.AllPlayBindingConstants;
22 import org.openhab.core.config.discovery.AbstractDiscoveryService;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import de.kaizencode.tchaikovsky.AllPlay;
33 import de.kaizencode.tchaikovsky.exception.AllPlayException;
34 import de.kaizencode.tchaikovsky.listener.SpeakerAnnouncedListener;
35 import de.kaizencode.tchaikovsky.speaker.Speaker;
38 * Discovery service to scan for AllPlay devices.
40 * @author Dominic Lerbs - Initial contribution
42 @Component(service = DiscoveryService.class, configurationPid = "discovery.allplay")
43 public class AllPlaySpeakerDiscoveryService extends AbstractDiscoveryService implements SpeakerAnnouncedListener {
45 private final Logger logger = LoggerFactory.getLogger(AllPlaySpeakerDiscoveryService.class);
47 private static final int DISCOVERY_TIMEOUT = 30;
48 private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Set.of(SPEAKER_THING_TYPE);
49 private AllPlay allPlay;
51 public AllPlaySpeakerDiscoveryService() {
52 super(DISCOVERABLE_THING_TYPES_UIDS, DISCOVERY_TIMEOUT);
56 protected void startScan() {
57 logger.debug("Starting scan for AllPlay devices");
59 allPlay = new AllPlay("openHAB2-discovery");
60 allPlay.addSpeakerAnnouncedListener(this);
62 allPlay.discoverSpeakers();
63 } catch (AllPlayException e) {
64 logger.warn("Error while scanning for AllPlay devices", e);
69 protected void stopScan() {
70 logger.debug("Stopping scan for AllPlay devices");
71 if (allPlay != null) {
72 allPlay.removeSpeakerAnnouncedListener(this);
80 protected void startBackgroundDiscovery() {
81 logger.trace("Starting background scan for AllPlay devices");
86 protected void stopBackgroundDiscovery() {
87 logger.trace("Stopping background scan for AllPlay devices");
92 public void deactivate() {
93 removeOlderResults(getTimestampOfLastScan());
97 public void onSpeakerAnnounced(Speaker speaker) {
98 logger.debug("Speaker {} found by discovery service", speaker);
99 ThingUID thingUID = new ThingUID(AllPlayBindingConstants.SPEAKER_THING_TYPE, speaker.getId());
101 Map<String, Object> properties = new HashMap<>();
102 properties.put(AllPlayBindingConstants.DEVICE_ID, speaker.getId());
103 properties.put(AllPlayBindingConstants.DEVICE_NAME, speaker.getName());
105 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
106 .withRepresentationProperty(AllPlayBindingConstants.DEVICE_ID).withLabel(speaker.getName()).build();
107 thingDiscovered(discoveryResult);