]> git.basschouten.com Git - openhab-addons.git/blob
39ccdbdf0431db5b6af19b0cf9c01e868e87680b
[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.digitalstrom.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18
19 import javax.jmdns.ServiceInfo;
20
21 import org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
22 import org.openhab.binding.digitalstrom.internal.lib.config.Config;
23 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.DsAPI;
24 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
25 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.impl.DsAPIImpl;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link BridgeMDNSDiscoveryParticipant} is responsible for discovering digitalSTROM-Server. It uses the central
37  * {@link org.openhab.core.config.discovery.mdns.internal.MDNSDiscoveryService}.
38  *
39  * @author Michael Ochel - Initial contribution
40  * @author Matthias Siegele - Initial contribution
41  *
42  */
43 @Component(service = MDNSDiscoveryParticipant.class)
44 public class BridgeMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
45
46     private final Logger logger = LoggerFactory.getLogger(BridgeMDNSDiscoveryParticipant.class);
47
48     @Override
49     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50         return Set.of(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE);
51     }
52
53     @Override
54     public String getServiceType() {
55         return "_tcp.local.";
56     }
57
58     @Override
59     public DiscoveryResult createResult(ServiceInfo service) {
60         if (service.getApplication().contains("dssweb")) {
61             ThingUID uid = getThingUID(service);
62
63             if (uid != null) {
64                 String hostAddress = service.getName() + "." + service.getDomain() + ".";
65                 Map<String, Object> properties = new HashMap<>(2);
66                 properties.put(DigitalSTROMBindingConstants.HOST, hostAddress);
67                 return DiscoveryResultBuilder.create(uid).withProperties(properties)
68                         .withRepresentationProperty(uid.getId()).withLabel("digitalSTROM-Server").build();
69             }
70         }
71         return null;
72     }
73
74     @Override
75     public ThingUID getThingUID(ServiceInfo service) {
76         if (service.getApplication().contains("dssweb")) {
77             String hostAddress = service.getName() + "." + service.getDomain() + ".";
78             DsAPI digitalSTROMClient = new DsAPIImpl(hostAddress, Config.DEFAULT_CONNECTION_TIMEOUT,
79                     Config.DEFAULT_READ_TIMEOUT, true);
80             Map<String, String> dsidMap = digitalSTROMClient.getDSID(null);
81             String dSID = null;
82             if (dsidMap != null) {
83                 dSID = dsidMap.get(JSONApiResponseKeysEnum.DSID.getKey());
84             }
85             if (dSID != null && !dSID.isBlank()) {
86                 return new ThingUID(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE, dSID);
87             } else {
88                 logger.error("Can't get server dSID to generate thing UID. Please add the server manually.");
89             }
90         }
91         return null;
92     }
93 }