2 * Copyright (c) 2010-2023 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.HashMap;
19 import javax.jmdns.ServiceInfo;
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;
36 * The {@link BridgeMDNSDiscoveryParticipant} is responsible for discovering digitalSTROM-Server. It uses the central
37 * {@link MDNSDiscoveryService}.
39 * @author Michael Ochel - Initial contribution
40 * @author Matthias Siegele - Initial contribution
43 @Component(service = MDNSDiscoveryParticipant.class)
44 public class BridgeMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
46 private final Logger logger = LoggerFactory.getLogger(BridgeMDNSDiscoveryParticipant.class);
49 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50 return Set.of(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE);
54 public String getServiceType() {
59 public DiscoveryResult createResult(ServiceInfo service) {
60 if (service.getApplication().contains("dssweb")) {
61 ThingUID uid = getThingUID(service);
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();
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);
82 if (dsidMap != null) {
83 dSID = dsidMap.get(JSONApiResponseKeysEnum.DSID.getKey());
85 if (dSID != null && !dSID.isBlank()) {
86 return new ThingUID(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE, dSID);
88 logger.error("Can't get server dSID to generate thing UID. Please add the server manually.");