2 * Copyright (c) 2010-2023 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.semsportal.internal;
15 import static org.openhab.binding.semsportal.internal.SEMSPortalBindingConstants.*;
17 import java.time.LocalDateTime;
18 import java.util.concurrent.TimeUnit;
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;
36 * The {@link StationHandler} is responsible for handling commands, which are
37 * sent to one of the channels.
39 * @author Iwan Bron - Initial contribution
42 public class StationHandler extends BaseThingHandler {
43 private Logger logger = LoggerFactory.getLogger(StationHandler.class);
44 private static final long MAX_STATUS_AGE_MINUTES = 1;
46 private @Nullable StationStatus currentStatus;
47 private LocalDateTime lastUpdate = LocalDateTime.MIN;
49 public StationHandler(Thing thing) {
54 public void handleCommand(ChannelUID channelUID, Command command) {
56 if (command instanceof RefreshType) {
57 scheduler.execute(() -> {
59 updateChannelState(channelUID);
65 private boolean isPortalOK() {
66 PortalHandler portal = getPortal();
67 return portal != null && portal.isLoggedIn();
70 private void updateChannelState(ChannelUID channelUID) {
72 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
73 "Unable to update station info. Check Bridge status for details.");
76 switch (channelUID.getId()) {
77 case CHANNEL_CURRENT_OUTPUT:
78 updateState(channelUID.getId(), StateHelper.getCurrentOutput(currentStatus));
80 case CHANNEL_TODAY_TOTAL:
81 updateState(channelUID.getId(), StateHelper.getDayTotal(currentStatus));
83 case CHANNEL_MONTH_TOTAL:
84 updateState(channelUID.getId(), StateHelper.getMonthTotal(currentStatus));
86 case CHANNEL_OVERALL_TOTAL:
87 updateState(channelUID.getId(), StateHelper.getOverallTotal(currentStatus));
89 case CHANNEL_TODAY_INCOME:
90 updateState(channelUID.getId(), StateHelper.getDayIncome(currentStatus));
92 case CHANNEL_TOTAL_INCOME:
93 updateState(channelUID.getId(), StateHelper.getTotalIncome(currentStatus));
95 case CHANNEL_LASTUPDATE:
96 updateState(channelUID.getId(), StateHelper.getLastUpdate(currentStatus));
99 logger.debug("No mapping found for channel {}", channelUID.getId());
103 private void ensureRecentStatus() {
104 if (lastUpdate.isBefore(LocalDateTime.now().minusMinutes(MAX_STATUS_AGE_MINUTES))) {
110 public void initialize() {
111 updateStatus(ThingStatus.UNKNOWN);
112 scheduler.execute(() -> {
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.");
122 private long getUpdateInterval() {
123 PortalHandler portal = getPortal();
124 if (portal == null) {
125 return SEMSPortalBindingConstants.DEFAULT_UPDATE_INTERVAL_MINUTES;
127 return portal.getUpdateInterval();
130 private void updateStation() {
132 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
133 "Unable to update station info. Check Bridge status for details.");
136 PortalHandler portal = getPortal();
137 if (portal != null) {
139 currentStatus = portal.getStationStatus(getStationUUID());
140 StationStatus localCurrentStatus = currentStatus;
141 if (localCurrentStatus != null && localCurrentStatus.isOperational()) {
142 updateStatus(ThingStatus.ONLINE);
144 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.NONE, "Station not operational");
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());
153 logger.debug("Unable to find portal for thing {}", getThing().getUID());
157 private String getStationUUID() {
158 String uuid = getThing().getProperties().get(STATION_UUID);
160 Object uuidObj = getThing().getConfiguration().get(STATION_UUID);
161 if (uuidObj instanceof String stringValue) {
165 return uuid == null ? "" : uuid;
168 private void updateAllChannels() {
169 for (String channelName : ALL_CHANNELS) {
170 Channel channel = thing.getChannel(channelName);
171 if (channel != null) {
172 updateChannelState(channel.getUID());
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();