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.Collections;
18 import java.util.HashMap;
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;
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;
39 * Discovery service to scan for AllPlay devices.
41 * @author Dominic Lerbs - Initial contribution
43 @Component(service = DiscoveryService.class, configurationPid = "discovery.allplay")
44 public class AllPlaySpeakerDiscoveryService extends AbstractDiscoveryService implements SpeakerAnnouncedListener {
46 private final Logger logger = LoggerFactory.getLogger(AllPlaySpeakerDiscoveryService.class);
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;
52 public AllPlaySpeakerDiscoveryService() {
53 super(DISCOVERABLE_THING_TYPES_UIDS, DISCOVERY_TIMEOUT);
57 protected void startScan() {
58 logger.debug("Starting scan for AllPlay devices");
60 allPlay = new AllPlay("openHAB2-discovery");
61 allPlay.addSpeakerAnnouncedListener(this);
63 allPlay.discoverSpeakers();
64 } catch (AllPlayException e) {
65 logger.warn("Error while scanning for AllPlay devices", e);
70 protected void stopScan() {
71 logger.debug("Stopping scan for AllPlay devices");
72 if (allPlay != null) {
73 allPlay.removeSpeakerAnnouncedListener(this);
81 protected void startBackgroundDiscovery() {
82 logger.trace("Starting background scan for AllPlay devices");
87 protected void stopBackgroundDiscovery() {
88 logger.trace("Stopping background scan for AllPlay devices");
93 public void deactivate() {
94 removeOlderResults(getTimestampOfLastScan());
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());
102 Map<String, Object> properties = new HashMap<>();
103 properties.put(AllPlayBindingConstants.DEVICE_ID, speaker.getId());
104 properties.put(AllPlayBindingConstants.DEVICE_NAME, speaker.getName());
106 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
107 .withRepresentationProperty(AllPlayBindingConstants.DEVICE_ID).withLabel(speaker.getName()).build();
108 thingDiscovered(discoveryResult);