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