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