]> git.basschouten.com Git - openhab-addons.git/blob
0a062aa622b59826e4c318d9de829e9fd6ad07df
[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.coronastats.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.coronastats.internal.config.CoronaStatsCountryConfiguration;
18 import org.openhab.binding.coronastats.internal.dto.CoronaStats;
19 import org.openhab.binding.coronastats.internal.dto.CoronaStatsCountry;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link CoronaStatsCountryHandler} is the handler for country thing
34  *
35  * @author Johannes Ott - Initial contribution
36  */
37 @NonNullByDefault
38 public class CoronaStatsCountryHandler extends BaseThingHandler {
39     private final Logger logger = LoggerFactory.getLogger(CoronaStatsCountryHandler.class);
40
41     private CoronaStatsCountryConfiguration thingConfig = new CoronaStatsCountryConfiguration();
42
43     public CoronaStatsCountryHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void initialize() {
49         thingConfig = getConfigAs(CoronaStatsCountryConfiguration.class);
50         logger.debug("Initializing Corona Stats country handler for country code {}", thingConfig.getCountryCode());
51
52         if (thingConfig.isValid()) {
53             CoronaStatsWorldHandler handler = getBridgeHandler();
54             if (handler == null) {
55                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge handler missing");
56             } else {
57                 updateStatus(ThingStatus.ONLINE);
58             }
59         } else {
60             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No valid country code given.");
61         }
62     }
63
64     @Override
65     public void dispose() {
66         logger.debug("CoronaStats country handler disposes.");
67     }
68
69     @Override
70     public void handleCommand(ChannelUID channelUID, Command command) {
71         if (command instanceof RefreshType) {
72             refresh();
73         }
74     }
75
76     private void refresh() {
77         CoronaStatsWorldHandler handler = getBridgeHandler();
78         if (handler != null) {
79             CoronaStats coronaStats = handler.getCoronaStats();
80             if (coronaStats != null) {
81                 notifyOnUpdate(coronaStats);
82             }
83         }
84     }
85
86     private @Nullable CoronaStatsWorldHandler getBridgeHandler() {
87         Bridge bridge = getBridge();
88         if (bridge != null) {
89             ThingHandler handler = bridge.getHandler();
90             if (handler instanceof CoronaStatsWorldHandler) {
91                 return (CoronaStatsWorldHandler) handler;
92             }
93         }
94
95         return null;
96     }
97
98     public void notifyOnUpdate(CoronaStats coronaStats) {
99         CoronaStatsCountry country = coronaStats.getCountry(thingConfig.getCountryCode());
100         if (country == null) {
101             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Country not found");
102             return;
103         }
104
105         updateStatus(ThingStatus.ONLINE);
106         updateProperties(country.getProperties());
107
108         country.getChannelsStateMap().forEach(this::updateState);
109     }
110 }