2 * Copyright (c) 2010-2022 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.net.HttpURLConnection;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.HashSet;
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.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Deactivate;
34 import org.osgi.service.component.annotations.Modified;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link BridgeDiscoveryService} is responsible for discovering digitalSTROM-Server, if the server is in the
40 * local network and is reachable through "dss.local." with default port number "8080". It uses the central
41 * {@link AbstractDiscoveryService}.
43 * @author Michael Ochel - Initial contribution
44 * @author Matthias Siegele - Initial contribution
46 @Component(service = DiscoveryService.class, configurationPid = "discovery.digitalstrom")
47 public class BridgeDiscoveryService extends AbstractDiscoveryService {
49 private final Logger logger = LoggerFactory.getLogger(BridgeDiscoveryService.class);
50 public static final String HOST_ADDRESS = "dss.local.";
52 private final Runnable resultCreater = new Runnable() {
59 private void createResult() {
60 ThingUID uid = getThingUID();
63 Map<String, Object> properties = new HashMap<>(2);
64 properties.put(DigitalSTROMBindingConstants.HOST, HOST_ADDRESS);
65 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
66 .withLabel("digitalSTROM-Server").build();
67 thingDiscovered(result);
71 private ThingUID getThingUID() {
72 DsAPI digitalSTROMClient = new DsAPIImpl(HOST_ADDRESS, Config.DEFAULT_CONNECTION_TIMEOUT,
73 Config.DEFAULT_READ_TIMEOUT, true);
75 switch (digitalSTROMClient.checkConnection("123")) {
76 case HttpURLConnection.HTTP_OK:
77 case HttpURLConnection.HTTP_UNAUTHORIZED:
78 case HttpURLConnection.HTTP_FORBIDDEN:
79 Map<String, String> dsidMap = digitalSTROMClient.getDSID(null);
80 if (dsidMap != null) {
81 dSID = dsidMap.get(JSONApiResponseKeysEnum.DSID.getKey());
83 if (dSID != null && !dSID.isBlank()) {
84 return new ThingUID(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE, dSID);
86 logger.error("Can't get server dSID to generate ThingUID. Please add the server manually.");
94 * Creates a new {@link BridgeDiscoveryService}.
96 public BridgeDiscoveryService() {
97 super(new HashSet<>(Arrays.asList(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE)), 10, false);
102 protected void activate(Map<String, Object> configProperties) {
103 super.activate(configProperties);
108 protected void deactivate() {
114 protected void modified(Map<String, Object> configProperties) {
115 super.modified(configProperties);
119 protected void startScan() {
120 scheduler.execute(resultCreater);