private String password = "";
private int refresh = 30;
private int statusTimeout = 300;
+ private int retries = 10;
+ private int retryDelay = 1000;
public String getEmail() {
return email;
return statusTimeout;
}
+ public int getRetries() {
+ return retries;
+ }
+
+ public int getRetryDelay() {
+ return retryDelay;
+ }
+
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
+
+ public void setRetries(int retries) {
+ this.retries = retries;
+ }
+
+ public void setRetryDelay(int retryDelay) {
+ this.retryDelay = retryDelay;
+ }
}
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
+import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
*/
private @Nullable ScheduledFuture<?> reconciliationFuture;
+ // List of futures used for command retries
+ private Collection<ScheduledFuture<?>> retryFutures = new ConcurrentLinkedQueue<ScheduledFuture<?>>();
+
/**
* List of executions
*/
logger.debug("Doing cleanup");
stopPolling();
executions.clear();
+ // cancel all scheduled retries
+ retryFutures.forEach(x -> x.cancel(false));
+
try {
httpClient.stop();
} catch (Exception e) {
return;
}
- Boolean result = sendCommandInternal(io, command, params, url);
+ removeFinishedRetries();
+
+ boolean result = sendCommandInternal(io, command, params, url);
if (!result) {
- sendCommandInternal(io, command, params, url);
+ scheduleRetry(io, command, params, url, thingConfig.getRetries());
+ }
+ }
+
+ private void repeatSendCommandInternal(String io, String command, String params, String url, int retries) {
+ logger.debug("Retrying command, retries left: {}", retries);
+ boolean result = sendCommandInternal(io, command, params, url);
+ if (!result && (retries > 0)) {
+ scheduleRetry(io, command, params, url, retries - 1);
}
}
- private Boolean sendCommandInternal(String io, String command, String params, String url) {
+ private boolean sendCommandInternal(String io, String command, String params, String url) {
String value = params.equals("[]") ? command : params.replace("\"", "");
String urlParameters = "{\"label\":\"" + getThingLabelByURL(io) + " - " + value
+ " - OH2\",\"actions\":[{\"deviceURL\":\"" + io + "\",\"commands\":[{\"name\":\"" + command
return false;
}
+ private void removeFinishedRetries() {
+ retryFutures.removeIf(x -> x.isDone());
+ logger.debug("Currently {} retries are scheduled.", retryFutures.size());
+ }
+
+ private void scheduleRetry(String io, String command, String params, String url, int retries) {
+ retryFutures.add(scheduler.schedule(() -> {
+ repeatSendCommandInternal(io, command, params, url, retries);
+ }, thingConfig.getRetryDelay(), TimeUnit.MILLISECONDS));
+ }
+
private String getThingLabelByURL(String io) {
Thing th = getThingByDeviceUrl(io);
if (th != null) {