]> git.basschouten.com Git - openhab-addons.git/blob
4f44bfebc70b4512a9a8d87c2fed70ef203ad0a5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.awattar.internal.handler;
14
15 import static org.openhab.binding.awattar.internal.AwattarBindingConstants.BINDING_ID;
16 import static org.openhab.binding.awattar.internal.AwattarBindingConstants.CHANNEL_GROUP_CURRENT;
17 import static org.openhab.binding.awattar.internal.AwattarBindingConstants.CHANNEL_MARKET_GROSS;
18 import static org.openhab.binding.awattar.internal.AwattarBindingConstants.CHANNEL_MARKET_NET;
19 import static org.openhab.binding.awattar.internal.AwattarBindingConstants.CHANNEL_TOTAL_GROSS;
20 import static org.openhab.binding.awattar.internal.AwattarBindingConstants.CHANNEL_TOTAL_NET;
21 import static org.openhab.binding.awattar.internal.AwattarUtil.getCalendarForHour;
22 import static org.openhab.binding.awattar.internal.AwattarUtil.getMillisToNextMinute;
23
24 import java.math.BigDecimal;
25 import java.math.RoundingMode;
26 import java.time.ZonedDateTime;
27 import java.util.concurrent.ScheduledFuture;
28 import java.util.concurrent.TimeUnit;
29
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.binding.awattar.internal.AwattarPrice;
33 import org.openhab.core.i18n.TimeZoneProvider;
34 import org.openhab.core.library.types.DecimalType;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Channel;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingStatusDetail;
41 import org.openhab.core.thing.binding.BaseThingHandler;
42 import org.openhab.core.thing.type.ChannelKind;
43 import org.openhab.core.types.Command;
44 import org.openhab.core.types.RefreshType;
45 import org.openhab.core.types.State;
46 import org.openhab.core.types.UnDefType;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * The {@link AwattarPriceHandler} is responsible for handling commands, which are
52  * sent to one of the channels.
53  *
54  * @author Wolfgang Klimt - Initial contribution
55  */
56 @NonNullByDefault
57 public class AwattarPriceHandler extends BaseThingHandler {
58     private static final int THING_REFRESH_INTERVAL = 60;
59     private final Logger logger = LoggerFactory.getLogger(AwattarPriceHandler.class);
60
61     private final TimeZoneProvider timeZoneProvider;
62     private @Nullable ScheduledFuture<?> thingRefresher;
63
64     public AwattarPriceHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
65         super(thing);
66         this.timeZoneProvider = timeZoneProvider;
67     }
68
69     @Override
70     public void handleCommand(ChannelUID channelUID, Command command) {
71         if (command instanceof RefreshType) {
72             refreshChannel(channelUID);
73         } else {
74             logger.debug("Binding {} only supports refresh command", BINDING_ID);
75         }
76     }
77
78     /**
79      * Initialize the binding and start the refresh job.
80      * The refresh job runs once after initialization and afterward every hour.
81      */
82
83     @Override
84     public void initialize() {
85         synchronized (this) {
86             ScheduledFuture<?> localRefresher = thingRefresher;
87             if (localRefresher == null || localRefresher.isCancelled()) {
88                 /*
89                  * The scheduler is required to run exactly at minute borders, hence we can't use scheduleWithFixedDelay
90                  * here
91                  */
92                 thingRefresher = scheduler.scheduleAtFixedRate(this::refreshChannels,
93                         getMillisToNextMinute(1, timeZoneProvider), THING_REFRESH_INTERVAL, TimeUnit.SECONDS);
94             }
95         }
96         updateStatus(ThingStatus.UNKNOWN);
97     }
98
99     @Override
100     public void dispose() {
101         ScheduledFuture<?> localRefresher = thingRefresher;
102         if (localRefresher != null) {
103             localRefresher.cancel(true);
104             thingRefresher = null;
105         }
106     }
107
108     public void refreshChannels() {
109         updateStatus(ThingStatus.ONLINE);
110         for (Channel channel : getThing().getChannels()) {
111             ChannelUID channelUID = channel.getUID();
112             if (ChannelKind.STATE.equals(channel.getKind()) && channelUID.isInGroup() && channelUID.getGroupId() != null
113                     && isLinked(channelUID)) {
114                 refreshChannel(channel.getUID());
115             }
116         }
117     }
118
119     public void refreshChannel(ChannelUID channelUID) {
120         State state = UnDefType.UNDEF;
121         Bridge bridge = getBridge();
122         if (bridge == null) {
123             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/error.bridge.missing");
124             return;
125         }
126         AwattarBridgeHandler bridgeHandler = (AwattarBridgeHandler) bridge.getHandler();
127         if (bridgeHandler == null) {
128             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/error.bridge.missing");
129             return;
130         }
131         String group = channelUID.getGroupId();
132         if (group == null) {
133             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
134                     "@text/error.channelgroup.missing");
135             return;
136         }
137
138         ZonedDateTime target;
139
140         if (group.equals(CHANNEL_GROUP_CURRENT)) {
141             target = ZonedDateTime.now(bridgeHandler.getTimeZone());
142         } else if (group.startsWith("today")) {
143             target = getCalendarForHour(Integer.parseInt(group.substring(5)), bridgeHandler.getTimeZone());
144         } else if (group.startsWith("tomorrow")) {
145             target = getCalendarForHour(Integer.parseInt(group.substring(8)), bridgeHandler.getTimeZone()).plusDays(1);
146         } else {
147             logger.warn("Unsupported channel group {}", group);
148             updateState(channelUID, state);
149             return;
150         }
151
152         AwattarPrice price = bridgeHandler.getPriceFor(target.toInstant().toEpochMilli());
153
154         if (price == null) {
155             logger.trace("No price found for hour {}", target.toString());
156             updateState(channelUID, state);
157             return;
158         }
159
160         String channelId = channelUID.getIdWithoutGroup();
161         switch (channelId) {
162             case CHANNEL_MARKET_NET:
163                 state = toDecimalType(price.netPrice());
164                 break;
165             case CHANNEL_MARKET_GROSS:
166                 state = toDecimalType(price.grossPrice());
167                 break;
168             case CHANNEL_TOTAL_NET:
169                 state = toDecimalType(price.netTotal());
170                 break;
171             case CHANNEL_TOTAL_GROSS:
172                 state = toDecimalType(price.grossTotal());
173                 break;
174             default:
175                 logger.warn("Unknown channel id {} for Thing type {}", channelUID, getThing().getThingTypeUID());
176         }
177         updateState(channelUID, state);
178     }
179
180     private DecimalType toDecimalType(Double value) {
181         BigDecimal bd = BigDecimal.valueOf(value);
182         return new DecimalType(bd.setScale(2, RoundingMode.HALF_UP));
183     }
184 }