]> git.basschouten.com Git - openhab-addons.git/blob
0b131c268ceba1ac485b7c3d786fdf9019b1a144
[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 bridgeHandler) {
70                 return bridgeHandler;
71             }
72         }
73
74         return null;
75     }
76
77     @Override
78     public void dispose() {
79         logger.debug("DWDPollenflug region handler disposes.");
80     }
81
82     @Override
83     public void handleCommand(ChannelUID channelUID, Command command) {
84         if (command instanceof RefreshType) {
85             refresh();
86         }
87     }
88
89     private void refresh() {
90         DWDPollenflugBridgeHandler handler = getBridgeHandler();
91         if (handler != null) {
92             DWDPollenflug pollenflug = handler.getPollenflug();
93             if (pollenflug != null) {
94                 notifyOnUpdate(pollenflug);
95             }
96         }
97     }
98
99     public void notifyOnUpdate(DWDPollenflug pollenflug) {
100         DWDRegion region = pollenflug.getRegion(thingConfig.regionID);
101         if (region == null) {
102             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Region not found");
103             return;
104         }
105
106         updateStatus(ThingStatus.ONLINE);
107         updateProperties(region.getProperties());
108
109         region.getChannelsStateMap().forEach(this::updateState);
110     }
111 }