]> git.basschouten.com Git - openhab-addons.git/blob
351c20a9ad9d8a12639031a913b9d9f814f780bb
[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.dwdpollenflug.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.dwdpollenflug.internal.config.DWDPollenflugRegionConfiguration;
18 import org.openhab.binding.dwdpollenflug.internal.dto.DWDPollenflug;
19 import org.openhab.binding.dwdpollenflug.internal.dto.DWDRegion;
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 DWDPollenflugRegionHandler} is the handler for bridge thing
34  *
35  * @author Johannes Ott - Initial contribution
36  */
37 @NonNullByDefault
38 public class DWDPollenflugRegionHandler extends BaseThingHandler {
39
40     private final Logger logger = LoggerFactory.getLogger(DWDPollenflugRegionHandler.class);
41
42     private DWDPollenflugRegionConfiguration thingConfig = new DWDPollenflugRegionConfiguration();
43
44     public DWDPollenflugRegionHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public void initialize() {
50         logger.debug("Initializing DWD Pollenflug region handler");
51         thingConfig = getConfigAs(DWDPollenflugRegionConfiguration.class);
52
53         if (thingConfig.isValid()) {
54             DWDPollenflugBridgeHandler handler = getBridgeHandler();
55             if (handler == null) {
56                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge handler missing");
57             } else {
58                 updateStatus(ThingStatus.ONLINE);
59             }
60         } else {
61             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No valid region id given.");
62         }
63     }
64
65     private @Nullable DWDPollenflugBridgeHandler getBridgeHandler() {
66         Bridge bridge = getBridge();
67         if (bridge != null) {
68             ThingHandler handler = bridge.getHandler();
69             if (handler instanceof DWDPollenflugBridgeHandler) {
70                 DWDPollenflugBridgeHandler bridgeHandler = (DWDPollenflugBridgeHandler) handler;
71                 return bridgeHandler;
72             }
73         }
74
75         return null;
76     }
77
78     @Override
79     public void dispose() {
80         logger.debug("DWDPollenflug region handler disposes.");
81     }
82
83     @Override
84     public void handleCommand(ChannelUID channelUID, Command command) {
85         if (command instanceof RefreshType) {
86             refresh();
87         }
88     }
89
90     private void refresh() {
91         DWDPollenflugBridgeHandler handler = getBridgeHandler();
92         if (handler != null) {
93             DWDPollenflug pollenflug = handler.getPollenflug();
94             if (pollenflug != null) {
95                 notifyOnUpdate(pollenflug);
96             }
97         }
98     }
99
100     public void notifyOnUpdate(DWDPollenflug pollenflug) {
101         DWDRegion region = pollenflug.getRegion(thingConfig.regionID);
102         if (region == null) {
103             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Region not found");
104             return;
105         }
106
107         updateStatus(ThingStatus.ONLINE);
108         updateProperties(region.getProperties());
109
110         region.getChannelsStateMap().forEach(this::updateState);
111     }
112 }