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.hccrubbishcollection.internal;
15 import static org.openhab.binding.hccrubbishcollection.internal.HCCRubbishCollectionBindingConstants.*;
17 import java.time.ZonedDateTime;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.core.library.types.DateTimeType;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link HCCRubbishCollectionHandler} is responsible for handling commands,
39 * updating the channels and polling the API.
41 * @author Stewart Cossey - Initial contribution
44 public class HCCRubbishCollectionHandler extends BaseThingHandler {
45 private static final int DELAY_NETWORKERROR = 3; // On network error tries again in 3 minutes.
46 private static final int DELAY_UPDATE = 480; // Polls API every 8 hours.
48 private final Logger logger = LoggerFactory.getLogger(HCCRubbishCollectionHandler.class);
50 private final HttpClient httpClient;
51 private @Nullable API api;
53 private @Nullable ScheduledFuture<?> refreshScheduler; // The API refresh scheduler
54 private @Nullable ScheduledFuture<?> collectionScheduler; // The Collection event trigger scheduler
56 /** Object disposing flag */
57 private boolean isDisposing = false;
62 * @param thing The thing type passed from the Handler Factory.
63 * @param httpClient The common http client provided from openHAB.
65 public HCCRubbishCollectionHandler(Thing thing, HttpClient httpClient) {
68 this.httpClient = httpClient;
72 * Handles a command coming from openHAB.
73 * Only RefreshType is supported as all channels are read only.
75 * @param channelUID The channel UID.
76 * @param command The command.
79 public void handleCommand(ChannelUID channelUID, Command command) {
80 if (command instanceof RefreshType) {
86 * Refreshes the data immediately.
88 private void updateNow() {
93 logger.debug("Updating data immediately");
99 public void initialize() {
100 final HCCRubbishCollectionConfiguration config = getConfigAs(HCCRubbishCollectionConfiguration.class);
102 updateStatus(ThingStatus.UNKNOWN);
104 api = new API(httpClient, config.address);
109 * Gets the Rubbish collection data from the {@link API} and updates the
110 * channels with the data.
112 private void updateData() {
113 logger.debug("Fetching new data");
114 final API localApi = api;
115 if (localApi != null) {
119 if (localApi.update()) {
123 updateStatus(ThingStatus.ONLINE); // Updates Thing to online since API update was successful.
125 Integer localDay = localApi.getDay();
126 if (localDay != null) {
127 updateState(CHANNEL_DAY, new DecimalType(localDay));
130 ZonedDateTime localGeneralDate = localApi.getGeneralDate();
131 if (localGeneralDate != null) {
132 updateState(CHANNEL_BIN_GENERAL, new DateTimeType(localGeneralDate));
135 ZonedDateTime localRecyclingDate = localApi.getRecyclingDate();
136 if (localRecyclingDate != null) {
137 updateState(CHANNEL_BIN_RECYCLING, new DateTimeType(localRecyclingDate));
140 if (localGeneralDate != null && localRecyclingDate != null) {
141 setupCollectionEvent(localGeneralDate, localRecyclingDate);
143 logger.debug("Cannot setup Collection Event, one or both collection dates are null.");
146 if (localApi.getErrorDetail() != ThingStatusDetail.COMMUNICATION_ERROR) {
147 updateStatus(ThingStatus.OFFLINE, localApi.getErrorDetail(), localApi.getErrorDetailMessage());
154 logger.error("API object is null, cannot update");
159 * Calculates some values for the Collection Event before setting up the
160 * Collection trigger {@link #scheduleCollectionEvent}.
162 * @param generalDate The General Rubbish Collection Date and Time.
163 * @param recyclingDate The Recycling Collection Date and Time.
165 private void setupCollectionEvent(ZonedDateTime generalDate, ZonedDateTime recyclingDate) {
166 logger.trace("Setup Collection Trigger");
169 ZonedDateTime dateTime;
170 if (generalDate.compareTo(recyclingDate) < 0) {
171 logger.trace("Using General Date {} for Event", generalDate);
172 dateTime = generalDate;
173 event = EVENT_GENERAL;
175 logger.trace("Using Recycling Date {} for Event", recyclingDate);
176 dateTime = recyclingDate;
177 event = EVENT_RECYCLING;
180 logger.trace("Loading channel config");
181 Channel collectionTriggerChannel = getThing().getChannel(TRIGGER_COLLECTION);
182 HCCRubbishCollectionEventConfiguration collectionEventConfig = (collectionTriggerChannel == null) ? null
183 : collectionTriggerChannel.getConfiguration().as(HCCRubbishCollectionEventConfiguration.class);
186 if (collectionEventConfig != null) {
187 offset = (long) collectionEventConfig.offset;
189 logger.debug("Could not get event config, default offset of {} set", offset);
192 ZonedDateTime offsettedDateTime = dateTime.plusMinutes(offset);
193 logger.trace("Event offset by {} minutes, new datetime {}", offset, offsettedDateTime);
194 scheduleCollectionEvent(offsettedDateTime, event);
198 * Sets up the collection event trigger.
200 * @param dateTime The Date and time to trigger the Collection Event.
201 * @param event The name of the Event to be triggered.
203 private void scheduleCollectionEvent(ZonedDateTime dateTime, String event) {
204 stopScheduleCollectionEvent(); // Stop the currently scheduled event
210 logger.trace("Setup Collection Trigger Scheduler");
212 logger.trace("Local Time {}", ZonedDateTime.now());
213 long delay = dateTime.toEpochSecond() - ZonedDateTime.now().toEpochSecond();
215 logger.debug("Start collection scheduler, delay {} seconds ({} minutes)", delay, delay / 60);
217 collectionScheduler = scheduler.schedule(() -> {
221 triggerChannel(TRIGGER_COLLECTION, event);
222 }, delay, TimeUnit.SECONDS);
224 logger.debug("Collection trigger delay already in past, ignoring");
229 * Starts the data update scheduler.
231 * @param delay The start delay in minutes. 0 executes an immediate update.
233 private void startUpdate(int delay) {
237 logger.debug("Start refresh scheduler, delay {}", delay);
239 refreshScheduler = scheduler.scheduleWithFixedDelay(this::updateData, delay, DELAY_UPDATE, TimeUnit.MINUTES);
243 * Stops the scheduler for the collection event trigger.
245 private void stopScheduleCollectionEvent() {
246 ScheduledFuture<?> localCollectionScheduler = collectionScheduler;
247 logger.debug("Stopping Collection Trigger Scheduler");
248 if (localCollectionScheduler != null) {
249 localCollectionScheduler.cancel(true);
250 collectionScheduler = null;
255 * Stop the data update scheduler (stops updating data). If stopping due to a
256 * network error, then resets the update scheduler {@link #startUpdate(int)}
257 * with an initial delay to wait a short period then try again.
259 * @param networkError Set to true if a network error. False if terminating.
261 private void stopUpdate(boolean networkError) {
262 final ScheduledFuture<?> localRefreshScheduler = refreshScheduler;
263 logger.debug("Stopping updater scheduler, networkError = {}", networkError);
264 if (localRefreshScheduler != null) {
265 localRefreshScheduler.cancel(true);
266 refreshScheduler = null;
269 logger.debug("Waiting {} minutes to try again", DELAY_NETWORKERROR);
270 startUpdate(DELAY_NETWORKERROR);
275 public void dispose() {
276 isDisposing = true; // Set true to exit any running functions
278 stopScheduleCollectionEvent();