2 * Copyright (c) 2010-2020 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.digitalstrom.internal.discovery;
15 import java.util.Collections;
16 import java.util.HashMap;
20 import javax.jmdns.ServiceInfo;
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;
38 * The {@link BridgeMDNSDiscoveryParticipant} is responsible for discovering digitalSTROM-Server. It uses the central
39 * {@link MDNSDiscoveryService}.
41 * @author Michael Ochel - Initial contribution
42 * @author Matthias Siegele - Initial contribution
45 @Component(service = MDNSDiscoveryParticipant.class)
46 public class BridgeMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
48 private final Logger logger = LoggerFactory.getLogger(BridgeMDNSDiscoveryParticipant.class);
51 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52 return Collections.singleton(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE);
56 public String getServiceType() {
61 public DiscoveryResult createResult(ServiceInfo service) {
62 if (service.getApplication().contains("dssweb")) {
63 ThingUID uid = getThingUID(service);
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();
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);
84 if (dsidMap != null) {
85 dSID = dsidMap.get(JSONApiResponseKeysEnum.DSID.getKey());
87 if (StringUtils.isNotBlank(dSID)) {
88 return new ThingUID(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE, dSID);
90 logger.error("Can't get server dSID to generate thing UID. Please add the server manually.");