]> git.basschouten.com Git - openhab-addons.git/blob
adfbddf0603df9af4c476bb31c9294a21758b5cf
[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.onebusaway.internal.handler;
14
15 import static org.openhab.binding.onebusaway.internal.OneBusAwayBindingConstants.THING_TYPE_API;
16
17 import org.openhab.binding.onebusaway.internal.config.ApiConfiguration;
18 import org.openhab.core.thing.Bridge;
19 import org.openhab.core.thing.ChannelUID;
20 import org.openhab.core.thing.ThingStatus;
21 import org.openhab.core.thing.ThingStatusDetail;
22 import org.openhab.core.thing.ThingTypeUID;
23 import org.openhab.core.thing.binding.BaseBridgeHandler;
24 import org.openhab.core.types.Command;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link ApiHandler} is responsible for storing basic configuration data for talking to a OneBusAway API server.
30  *
31  * @author Shawn Wilsher - Initial contribution
32  */
33 public class ApiHandler extends BaseBridgeHandler {
34     public static final ThingTypeUID SUPPORTED_THING_TYPE = THING_TYPE_API;
35
36     private ApiConfiguration config;
37     private Logger logger = LoggerFactory.getLogger(ApiHandler.class);
38
39     public ApiHandler(Bridge bridge) {
40         super(bridge);
41     }
42
43     @Override
44     public void handleCommand(ChannelUID channelUID, Command command) {
45         logger.warn("The API bridge is a read-only and can not handle commands.");
46     }
47
48     @Override
49     public void initialize() {
50         logger.debug("Initializing OneBusAway bridge...");
51
52         config = loadAndCheckConfiguration();
53         if (config == null) {
54             logger.debug("Initialization of OneBusAway API bridge failed!");
55             return;
56         }
57         updateStatus(ThingStatus.ONLINE);
58     }
59
60     protected String getApiKey() {
61         return config.getApiKey();
62     }
63
64     protected String getApiServer() {
65         return config.getApiServer();
66     }
67
68     private ApiConfiguration loadAndCheckConfiguration() {
69         ApiConfiguration config = getConfigAs(ApiConfiguration.class);
70         if (config.getApiKey() == null) {
71             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "apiKey is not set");
72             return null;
73         }
74         if (config.getApiServer() == null) {
75             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "apiServer is not set");
76             return null;
77         }
78         return config;
79     }
80 }