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