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.energidataservice.internal.retry;
15 import java.time.Clock;
16 import java.time.Duration;
17 import java.time.LocalTime;
18 import java.time.ZoneId;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jetty.http.HttpStatus;
22 import org.openhab.binding.energidataservice.internal.exception.DataServiceException;
23 import org.openhab.binding.energidataservice.internal.retry.strategy.ExponentialBackoff;
24 import org.openhab.binding.energidataservice.internal.retry.strategy.FixedTime;
25 import org.openhab.binding.energidataservice.internal.retry.strategy.Linear;
28 * This factory defines policies for determining appropriate {@link RetryStrategy} based
31 * @author Jacob Laursen - Initial contribution
34 public class RetryPolicyFactory {
37 * Determine {@link RetryStrategy} from {@link Throwable}.
39 * @param e thrown exception
40 * @return retry strategy
42 public static RetryStrategy fromThrowable(Throwable e) {
43 if (e instanceof DataServiceException dse) {
44 switch (dse.getHttpStatus()) {
45 case HttpStatus.TOO_MANY_REQUESTS_429:
46 return new ExponentialBackoff().withMinimum(Duration.ofMinutes(30));
48 return new ExponentialBackoff().withMinimum(Duration.ofMinutes(1)).withJitter(0.2);
52 return new ExponentialBackoff().withMinimum(Duration.ofMinutes(1)).withJitter(0.2);
56 * Default {@link RetryStrategy} with one retry per day.
57 * This is intended as a dummy strategy until replaced by a concrete one.
59 * @return retry strategy
61 public static RetryStrategy initial() {
62 return new Linear().withMinimum(Duration.ofDays(1));
66 * Determine {@link RetryStrategy} for next expected data publishing.
68 * @param localTime the time of daily data request in local time-zone
69 * @param zoneId the local time-zone
70 * @return retry strategy
72 public static RetryStrategy atFixedTime(LocalTime localTime, ZoneId zoneId) {
73 return new FixedTime(localTime, Clock.system(zoneId)).withJitter(1);
77 * Determine {@link RetryStrategy} when expected spot price data is missing.
79 * @param utcTime the time of daily data request in UTC time-zone
80 * @return retry strategy
82 public static RetryStrategy whenExpectedSpotPriceDataMissing(LocalTime localTime, ZoneId zoneId) {
83 LocalTime now = LocalTime.now(zoneId);
84 if (now.isAfter(localTime)) {
85 return new ExponentialBackoff().withMinimum(Duration.ofMinutes(10)).withJitter(0.2);
87 return atFixedTime(localTime, zoneId);