2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.coronastats.internal.handler;
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;
33 * The {@link CoronaStatsCountryHandler} is the handler for country thing
35 * @author Johannes Ott - Initial contribution
38 public class CoronaStatsCountryHandler extends BaseThingHandler {
39 private final Logger logger = LoggerFactory.getLogger(CoronaStatsCountryHandler.class);
41 private CoronaStatsCountryConfiguration thingConfig = new CoronaStatsCountryConfiguration();
43 public CoronaStatsCountryHandler(Thing thing) {
48 public void initialize() {
49 thingConfig = getConfigAs(CoronaStatsCountryConfiguration.class);
50 logger.debug("Initializing Corona Stats country handler for country code {}", thingConfig.getCountryCode());
52 if (thingConfig.isValid()) {
53 CoronaStatsWorldHandler handler = getBridgeHandler();
54 if (handler == null) {
55 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge handler missing");
57 updateStatus(ThingStatus.ONLINE);
60 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No valid country code given.");
65 public void dispose() {
66 logger.debug("CoronaStats country handler disposes.");
70 public void handleCommand(ChannelUID channelUID, Command command) {
71 if (command instanceof RefreshType) {
76 private void refresh() {
77 CoronaStatsWorldHandler handler = getBridgeHandler();
78 if (handler != null) {
79 CoronaStats coronaStats = handler.getCoronaStats();
80 if (coronaStats != null) {
81 notifyOnUpdate(coronaStats);
86 private @Nullable CoronaStatsWorldHandler getBridgeHandler() {
87 Bridge bridge = getBridge();
89 ThingHandler handler = bridge.getHandler();
90 if (handler instanceof CoronaStatsWorldHandler) {
91 return (CoronaStatsWorldHandler) handler;
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");
105 updateStatus(ThingStatus.ONLINE);
106 updateProperties(country.getProperties());
108 country.getChannelsStateMap().forEach(this::updateState);