]> git.basschouten.com Git - openhab-addons.git/blob
75521107fbe73fc79631ba418b5a9b3880eae4a7
[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.semsportal.internal;
14
15 import static org.openhab.binding.semsportal.internal.SEMSPortalBindingConstants.*;
16
17 import java.time.LocalDateTime;
18 import java.util.concurrent.TimeUnit;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.semsportal.internal.dto.StationStatus;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.binding.BaseThingHandler;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link StationHandler} is responsible for handling commands, which are
37  * sent to one of the channels.
38  *
39  * @author Iwan Bron - Initial contribution
40  */
41 @NonNullByDefault
42 public class StationHandler extends BaseThingHandler {
43     private Logger logger = LoggerFactory.getLogger(StationHandler.class);
44     private static final long MAX_STATUS_AGE_MINUTES = 1;
45
46     private @Nullable StationStatus currentStatus;
47     private LocalDateTime lastUpdate = LocalDateTime.MIN;
48
49     public StationHandler(Thing thing) {
50         super(thing);
51     }
52
53     @Override
54     public void handleCommand(ChannelUID channelUID, Command command) {
55         if (isPortalOK()) {
56             if (command instanceof RefreshType) {
57                 scheduler.execute(() -> {
58                     ensureRecentStatus();
59                     updateChannelState(channelUID);
60                 });
61             }
62         }
63     }
64
65     private boolean isPortalOK() {
66         PortalHandler portal = getPortal();
67         return portal != null && portal.isLoggedIn();
68     }
69
70     private void updateChannelState(ChannelUID channelUID) {
71         if (!isPortalOK()) {
72             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
73                     "Unable to update station info. Check Bridge status for details.");
74             return;
75         }
76         switch (channelUID.getId()) {
77             case CHANNEL_CURRENT_OUTPUT:
78                 updateState(channelUID.getId(), StateHelper.getCurrentOutput(currentStatus));
79                 break;
80             case CHANNEL_TODAY_TOTAL:
81                 updateState(channelUID.getId(), StateHelper.getDayTotal(currentStatus));
82                 break;
83             case CHANNEL_MONTH_TOTAL:
84                 updateState(channelUID.getId(), StateHelper.getMonthTotal(currentStatus));
85                 break;
86             case CHANNEL_OVERALL_TOTAL:
87                 updateState(channelUID.getId(), StateHelper.getOverallTotal(currentStatus));
88                 break;
89             case CHANNEL_TODAY_INCOME:
90                 updateState(channelUID.getId(), StateHelper.getDayIncome(currentStatus));
91                 break;
92             case CHANNEL_TOTAL_INCOME:
93                 updateState(channelUID.getId(), StateHelper.getTotalIncome(currentStatus));
94                 break;
95             case CHANNEL_LASTUPDATE:
96                 updateState(channelUID.getId(), StateHelper.getLastUpdate(currentStatus));
97                 break;
98             default:
99                 logger.debug("No mapping found for channel {}", channelUID.getId());
100         }
101     }
102
103     private void ensureRecentStatus() {
104         if (lastUpdate.isBefore(LocalDateTime.now().minusMinutes(MAX_STATUS_AGE_MINUTES))) {
105             updateStation();
106         }
107     }
108
109     @Override
110     public void initialize() {
111         updateStatus(ThingStatus.UNKNOWN);
112         scheduler.execute(() -> {
113             try {
114                 scheduler.scheduleWithFixedDelay(() -> ensureRecentStatus(), 0, getUpdateInterval(), TimeUnit.MINUTES);
115             } catch (Exception e) {
116                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
117                         "Unable to update station info. Check Bridge status for details.");
118             }
119         });
120     }
121
122     private long getUpdateInterval() {
123         PortalHandler portal = getPortal();
124         if (portal == null) {
125             return SEMSPortalBindingConstants.DEFAULT_UPDATE_INTERVAL_MINUTES;
126         }
127         return portal.getUpdateInterval();
128     }
129
130     private void updateStation() {
131         if (!isPortalOK()) {
132             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
133                     "Unable to update station info. Check Bridge status for details.");
134             return;
135         }
136         PortalHandler portal = getPortal();
137         if (portal != null) {
138             try {
139                 currentStatus = portal.getStationStatus(getStationUUID());
140                 StationStatus localCurrentStatus = currentStatus;
141                 if (localCurrentStatus != null && localCurrentStatus.isOperational()) {
142                     updateStatus(ThingStatus.ONLINE);
143                 } else {
144                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.NONE, "Station not operational");
145                 }
146                 updateAllChannels();
147             } catch (CommunicationException commEx) {
148                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, commEx.getMessage());
149             } catch (ConfigurationException confEx) {
150                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, confEx.getMessage());
151             }
152         } else {
153             logger.debug("Unable to find portal for thing {}", getThing().getUID());
154         }
155     }
156
157     private String getStationUUID() {
158         String uuid = getThing().getProperties().get(STATION_UUID);
159         if (uuid == null) {
160             Object uuidObj = getThing().getConfiguration().get(STATION_UUID);
161             if (uuidObj instanceof String) {
162                 uuid = (String) uuidObj;
163             }
164         }
165         return uuid == null ? "" : uuid;
166     }
167
168     private void updateAllChannels() {
169         for (String channelName : ALL_CHANNELS) {
170             Channel channel = thing.getChannel(channelName);
171             if (channel != null) {
172                 updateChannelState(channel.getUID());
173             }
174         }
175     }
176
177     private @Nullable PortalHandler getPortal() {
178         Bridge bridge = getBridge();
179         if (bridge != null && bridge.getHandler() != null && bridge.getHandler() instanceof PortalHandler) {
180             return (PortalHandler) bridge.getHandler();
181         }
182         return null;
183     }
184 }