]> git.basschouten.com Git - openhab-addons.git/blob
c377be33304e4a85cd6d94d78e7e7673b138051a
[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.meteoblue.internal.handler;
14
15 import static org.openhab.binding.meteoblue.internal.MeteoBlueBindingConstants.THING_TYPE_BRIDGE;
16
17 import java.util.Collections;
18 import java.util.Set;
19
20 import org.openhab.binding.meteoblue.internal.MeteoBlueBridgeConfig;
21 import org.openhab.core.io.net.http.HttpUtil;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.BaseBridgeHandler;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link MeteoBlueBridgeHandler} is responsible for handling the
34  * bridge things created to use the meteoblue weather service.
35  *
36  * @author Chris Carman - Initial contribution
37  */
38 public class MeteoBlueBridgeHandler extends BaseBridgeHandler {
39     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_BRIDGE);
40     private final Logger logger = LoggerFactory.getLogger(MeteoBlueBridgeHandler.class);
41
42     private String apiKey;
43
44     public MeteoBlueBridgeHandler(Bridge bridge) {
45         super(bridge);
46     }
47
48     /**
49      * Initialize the bridge.
50      */
51     @Override
52     public void initialize() {
53         logger.debug("Initializing meteoblue bridge");
54
55         MeteoBlueBridgeConfig config = getConfigAs(MeteoBlueBridgeConfig.class);
56         String apiKeyTemp = config.getApiKey();
57         if (apiKeyTemp == null || apiKeyTemp.isBlank()) {
58             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
59                     "Cannot initialize meteoblue bridge. No apiKey provided.");
60             return;
61         }
62
63         apiKey = apiKeyTemp;
64
65         healthCheck();
66     }
67
68     /**
69      * No commands are supported here.
70      */
71     @Override
72     public void handleCommand(ChannelUID channelUID, Command command) {
73     }
74
75     public String getApiKey() {
76         return new String(apiKey);
77     }
78
79     private void healthCheck() {
80         String url = "http://my.meteoblue.com/packages/";
81         try {
82             HttpUtil.executeUrl("GET", url, 30 * 1000);
83             logger.trace("HealthCheck succeeded.");
84             updateStatus(ThingStatus.ONLINE);
85         } catch (Exception e) {
86             logger.trace("HealthCheck failed", e);
87             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "HealthCheck failed");
88         }
89     }
90 }