]> git.basschouten.com Git - openhab-addons.git/blob
54fa0fb47129065afd58fa1f8203d55ae2dea91c
[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.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
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;
31
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;
36
37 /**
38  * Discovery service to scan for AllPlay devices.
39  *
40  * @author Dominic Lerbs - Initial contribution
41  */
42 @Component(service = DiscoveryService.class, configurationPid = "discovery.allplay")
43 public class AllPlaySpeakerDiscoveryService extends AbstractDiscoveryService implements SpeakerAnnouncedListener {
44
45     private final Logger logger = LoggerFactory.getLogger(AllPlaySpeakerDiscoveryService.class);
46
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;
50
51     public AllPlaySpeakerDiscoveryService() {
52         super(DISCOVERABLE_THING_TYPES_UIDS, DISCOVERY_TIMEOUT);
53     }
54
55     @Override
56     protected void startScan() {
57         logger.debug("Starting scan for AllPlay devices");
58         try {
59             allPlay = new AllPlay("openHAB2-discovery");
60             allPlay.addSpeakerAnnouncedListener(this);
61             allPlay.connect();
62             allPlay.discoverSpeakers();
63         } catch (AllPlayException e) {
64             logger.warn("Error while scanning for AllPlay devices", e);
65         }
66     }
67
68     @Override
69     protected void stopScan() {
70         logger.debug("Stopping scan for AllPlay devices");
71         if (allPlay != null) {
72             allPlay.removeSpeakerAnnouncedListener(this);
73             allPlay.disconnect();
74         }
75
76         super.stopScan();
77     }
78
79     @Override
80     protected void startBackgroundDiscovery() {
81         logger.trace("Starting background scan for AllPlay devices");
82         startScan();
83     }
84
85     @Override
86     protected void stopBackgroundDiscovery() {
87         logger.trace("Stopping background scan for AllPlay devices");
88         stopScan();
89     }
90
91     @Override
92     public void deactivate() {
93         removeOlderResults(getTimestampOfLastScan());
94     }
95
96     @Override
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());
100
101         Map<String, Object> properties = new HashMap<>();
102         properties.put(AllPlayBindingConstants.DEVICE_ID, speaker.getId());
103         properties.put(AllPlayBindingConstants.DEVICE_NAME, speaker.getName());
104
105         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
106                 .withRepresentationProperty(AllPlayBindingConstants.DEVICE_ID).withLabel(speaker.getName()).build();
107         thingDiscovered(discoveryResult);
108     }
109 }