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