]> git.basschouten.com Git - openhab-addons.git/blob
66f87f94d851515ab16a627bd6def7d6c79fffb0
[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.net.HttpURLConnection;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20
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;
38
39 /**
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}.
43  *
44  * @author Michael Ochel - Initial contribution
45  * @author Matthias Siegele - Initial contribution
46  */
47 @Component(service = DiscoveryService.class, configurationPid = "discovery.digitalstrom")
48 public class BridgeDiscoveryService extends AbstractDiscoveryService {
49
50     private final Logger logger = LoggerFactory.getLogger(BridgeDiscoveryService.class);
51     public static final String HOST_ADDRESS = "dss.local.";
52
53     private final Runnable resultCreater = new Runnable() {
54
55         @Override
56         public void run() {
57             createResult();
58         }
59
60         private void createResult() {
61             ThingUID uid = getThingUID();
62
63             if (uid != null) {
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);
69             }
70         }
71
72         private ThingUID getThingUID() {
73             DsAPI digitalSTROMClient = new DsAPIImpl(HOST_ADDRESS, Config.DEFAULT_CONNECTION_TIMEOUT,
74                     Config.DEFAULT_READ_TIMEOUT, true);
75             String dSID = null;
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());
83                     }
84                     if (StringUtils.isNotBlank(dSID)) {
85                         return new ThingUID(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE, dSID);
86                     } else {
87                         logger.error("Can't get server dSID to generate ThingUID. Please add the server manually.");
88                     }
89             }
90             return null;
91         }
92     };
93
94     /**
95      * Creates a new {@link BridgeDiscoveryService}.
96      */
97     public BridgeDiscoveryService() {
98         super(new HashSet<>(Arrays.asList(DigitalSTROMBindingConstants.THING_TYPE_DSS_BRIDGE)), 10, false);
99     }
100
101     @Activate
102     @Override
103     protected void activate(Map<String, Object> configProperties) {
104         super.activate(configProperties);
105     }
106
107     @Deactivate
108     @Override
109     protected void deactivate() {
110         super.deactivate();
111     }
112
113     @Modified
114     @Override
115     protected void modified(Map<String, Object> configProperties) {
116         super.modified(configProperties);
117     }
118
119     @Override
120     protected void startScan() {
121         scheduler.execute(resultCreater);
122     }
123 }