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