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