]> git.basschouten.com Git - openhab-addons.git/blob
90cd515f6d850404abbff3f4b20661be6d99f369
[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.airquality.internal.handler;
14
15 import java.util.Collection;
16 import java.util.Collections;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.airquality.internal.api.ApiBridge;
21 import org.openhab.binding.airquality.internal.discovery.AirQualityDiscoveryService;
22 import org.openhab.core.i18n.LocationProvider;
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.binding.BaseBridgeHandler;
28 import org.openhab.core.thing.binding.ThingHandlerService;
29 import org.openhab.core.types.Command;
30
31 /**
32  * The {@link AirQualityBridgeHandler} is responsible for handling communication
33  * with the service via the API.
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  */
37 @NonNullByDefault
38 public class AirQualityBridgeHandler extends BaseBridgeHandler {
39     private final LocationProvider locationProvider;
40     private @Nullable ApiBridge apiBridge;
41
42     public AirQualityBridgeHandler(Bridge bridge, LocationProvider locationProvider) {
43         super(bridge);
44         this.locationProvider = locationProvider;
45     }
46
47     @Override
48     public void initialize() {
49         String apiKey = (String) getConfig().get("apiKey");
50         if (apiKey != null && apiKey.length() != 0) {
51             apiBridge = new ApiBridge(apiKey);
52             updateStatus(ThingStatus.ONLINE);
53         } else {
54             apiBridge = null;
55             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/null-or-empty-api-key");
56         }
57     }
58
59     @Override
60     public void handleCommand(ChannelUID channelUID, Command command) {
61         // We do nothing
62     }
63
64     public @Nullable ApiBridge getApiBridge() {
65         return apiBridge;
66     }
67
68     @Override
69     public Collection<Class<? extends ThingHandlerService>> getServices() {
70         return Collections.singleton(AirQualityDiscoveryService.class);
71     }
72
73     public LocationProvider getLocationProvider() {
74         return locationProvider;
75     }
76 }