]> git.basschouten.com Git - openhab-addons.git/blob
a7362539af9af3a202d7c2a5e0e555320e1a8862
[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.hccrubbishcollection.internal;
14
15 import static org.openhab.binding.hccrubbishcollection.internal.HCCRubbishCollectionBindingConstants.*;
16
17 import java.time.ZonedDateTime;
18 import java.util.concurrent.ScheduledFuture;
19 import java.util.concurrent.TimeUnit;
20
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;
36
37 /**
38  * The {@link HCCRubbishCollectionHandler} is responsible for handling commands,
39  * updating the channels and polling the API.
40  *
41  * @author Stewart Cossey - Initial contribution
42  */
43 @NonNullByDefault
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.
47
48     private final Logger logger = LoggerFactory.getLogger(HCCRubbishCollectionHandler.class);
49
50     private final HttpClient httpClient;
51     private @Nullable API api;
52
53     private @Nullable ScheduledFuture<?> refreshScheduler; // The API refresh scheduler
54     private @Nullable ScheduledFuture<?> collectionScheduler; // The Collection event trigger scheduler
55
56     /** Object disposing flag */
57     private boolean isDisposing = false;
58
59     /**
60      * Create Handler.
61      * 
62      * @param thing The thing type passed from the Handler Factory.
63      * @param httpClient The common http client provided from openHAB.
64      */
65     public HCCRubbishCollectionHandler(Thing thing, HttpClient httpClient) {
66         super(thing);
67
68         this.httpClient = httpClient;
69     }
70
71     /**
72      * Handles a command coming from openHAB.
73      * Only RefreshType is supported as all channels are read only.
74      * 
75      * @param channelUID The channel UID.
76      * @param command The command.
77      */
78     @Override
79     public void handleCommand(ChannelUID channelUID, Command command) {
80         if (command instanceof RefreshType) {
81             updateNow();
82         }
83     }
84
85     /**
86      * Refreshes the data immediately.
87      */
88     private void updateNow() {
89         if (isDisposing) {
90             return;
91         }
92
93         logger.debug("Updating data immediately");
94         stopUpdate(false);
95         startUpdate(0);
96     }
97
98     @Override
99     public void initialize() {
100         final HCCRubbishCollectionConfiguration config = getConfigAs(HCCRubbishCollectionConfiguration.class);
101
102         updateStatus(ThingStatus.UNKNOWN);
103
104         api = new API(httpClient, config.address);
105         startUpdate(0);
106     }
107
108     /**
109      * Gets the Rubbish collection data from the {@link API} and updates the
110      * channels with the data.
111      */
112     private void updateData() {
113         logger.debug("Fetching new data");
114         final API localApi = api;
115         if (localApi != null) {
116             if (isDisposing) {
117                 return;
118             }
119             if (localApi.update()) {
120                 if (isDisposing) {
121                     return;
122                 }
123                 updateStatus(ThingStatus.ONLINE); // Updates Thing to online since API update was successful.
124
125                 Integer localDay = localApi.getDay();
126                 if (localDay != null) {
127                     updateState(CHANNEL_DAY, new DecimalType(localDay));
128                 }
129
130                 ZonedDateTime localGeneralDate = localApi.getGeneralDate();
131                 if (localGeneralDate != null) {
132                     updateState(CHANNEL_BIN_GENERAL, new DateTimeType(localGeneralDate));
133                 }
134
135                 ZonedDateTime localRecyclingDate = localApi.getRecyclingDate();
136                 if (localRecyclingDate != null) {
137                     updateState(CHANNEL_BIN_RECYCLING, new DateTimeType(localRecyclingDate));
138                 }
139
140                 if (localGeneralDate != null && localRecyclingDate != null) {
141                     setupCollectionEvent(localGeneralDate, localRecyclingDate);
142                 } else {
143                     logger.debug("Cannot setup Collection Event, one or both collection dates are null.");
144                 }
145             } else {
146                 if (localApi.getErrorDetail() != ThingStatusDetail.COMMUNICATION_ERROR) {
147                     updateStatus(ThingStatus.OFFLINE, localApi.getErrorDetail(), localApi.getErrorDetailMessage());
148                     stopUpdate(false);
149                 } else {
150                     stopUpdate(true);
151                 }
152             }
153         } else {
154             logger.error("API object is null, cannot update");
155         }
156     }
157
158     /**
159      * Calculates some values for the Collection Event before setting up the
160      * Collection trigger {@link #scheduleCollectionEvent}.
161      * 
162      * @param generalDate The General Rubbish Collection Date and Time.
163      * @param recyclingDate The Recycling Collection Date and Time.
164      */
165     private void setupCollectionEvent(ZonedDateTime generalDate, ZonedDateTime recyclingDate) {
166         logger.trace("Setup Collection Trigger");
167
168         String event;
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;
174         } else {
175             logger.trace("Using Recycling Date {} for Event", recyclingDate);
176             dateTime = recyclingDate;
177             event = EVENT_RECYCLING;
178         }
179
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);
184
185         long offset = 0;
186         if (collectionEventConfig != null) {
187             offset = (long) collectionEventConfig.offset;
188         } else {
189             logger.debug("Could not get event config, default offset of {} set", offset);
190         }
191
192         ZonedDateTime offsettedDateTime = dateTime.plusMinutes(offset);
193         logger.trace("Event offset by {} minutes, new datetime {}", offset, offsettedDateTime);
194         scheduleCollectionEvent(offsettedDateTime, event);
195     }
196
197     /**
198      * Sets up the collection event trigger.
199      * 
200      * @param dateTime The Date and time to trigger the Collection Event.
201      * @param event The name of the Event to be triggered.
202      */
203     private void scheduleCollectionEvent(ZonedDateTime dateTime, String event) {
204         stopScheduleCollectionEvent(); // Stop the currently scheduled event
205
206         if (isDisposing) {
207             return;
208         }
209
210         logger.trace("Setup Collection Trigger Scheduler");
211
212         logger.trace("Local Time {}", ZonedDateTime.now());
213         long delay = dateTime.toEpochSecond() - ZonedDateTime.now().toEpochSecond();
214
215         logger.debug("Start collection scheduler, delay {} seconds ({} minutes)", delay, delay / 60);
216         if (delay > 0) {
217             collectionScheduler = scheduler.schedule(() -> {
218                 if (isDisposing) {
219                     return;
220                 }
221                 triggerChannel(TRIGGER_COLLECTION, event);
222             }, delay, TimeUnit.SECONDS);
223         } else {
224             logger.debug("Collection trigger delay already in past, ignoring");
225         }
226     }
227
228     /**
229      * Starts the data update scheduler.
230      * 
231      * @param delay The start delay in minutes. 0 executes an immediate update.
232      */
233     private void startUpdate(int delay) {
234         if (isDisposing) {
235             return;
236         }
237         logger.debug("Start refresh scheduler, delay {}", delay);
238
239         refreshScheduler = scheduler.scheduleWithFixedDelay(this::updateData, delay, DELAY_UPDATE, TimeUnit.MINUTES);
240     }
241
242     /**
243      * Stops the scheduler for the collection event trigger.
244      */
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;
251         }
252     }
253
254     /**
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.
258      * 
259      * @param networkError Set to true if a network error. False if terminating.
260      */
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;
267         }
268         if (networkError) {
269             logger.debug("Waiting {} minutes to try again", DELAY_NETWORKERROR);
270             startUpdate(DELAY_NETWORKERROR);
271         }
272     }
273
274     @Override
275     public void dispose() {
276         isDisposing = true; // Set true to exit any running functions
277         stopUpdate(false);
278         stopScheduleCollectionEvent();
279
280         super.dispose();
281     }
282 }