Skip to content

Commit

Permalink
Gardena: Optimized refresh after command (openhab#2353)
Browse files Browse the repository at this point in the history
* Optimized refresh after command

Signed-off-by: Gerhard Riegler <[email protected]>
  • Loading branch information
gerrieg authored and Robert Hillman committed Dec 6, 2017
1 parent a2fc40f commit 9dbd97f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void init(String id, GardenaConfig config, GardenaSmartEventListener even
*/
@Override
public void dispose() {
stopRefreshThread();
stopRefreshThread(true);
if (httpClient != null) {
try {
httpClient.stop();
Expand All @@ -159,27 +159,16 @@ public void dispose() {
* Schedules the device refresh thread.
*/
private void startRefreshThread() {
refreshThreadFuture = scheduler.scheduleWithFixedDelay(refreshDevicesThread, config.getRefresh(),
config.getRefresh(), TimeUnit.SECONDS);
refreshThreadFuture = scheduler.scheduleWithFixedDelay(refreshDevicesThread, 6, config.getRefresh(),
TimeUnit.SECONDS);
}

/**
* Stops the device refresh thread.
*/
private void stopRefreshThread() {
private void stopRefreshThread(boolean force) {
if (refreshThreadFuture != null) {
refreshThreadFuture.cancel(true);
}
}

/**
* Schedules a intermediate device refresh.
*/
private void scheduleIntermediateRefresh() {
if (refreshThreadFuture != null) {
if (refreshThreadFuture.getDelay(TimeUnit.SECONDS) > 5) {
scheduler.schedule(refreshDevicesThread, 3, TimeUnit.SECONDS);
}
refreshThreadFuture.cancel(force);
}
}

Expand Down Expand Up @@ -217,7 +206,7 @@ public Device getDevice(String deviceId) throws GardenaException {
*/
@Override
public void loadAllDevices() throws GardenaException {
stopRefreshThread();
stopRefreshThread(false);
try {
allLocations.clear();
allDevicesById.clear();
Expand Down Expand Up @@ -324,7 +313,13 @@ public void sendCommand(Device device, GardenaSmartCommandName commandName, Obje
ObjectUtils.toString(value));
String propertyUrl = String.format(URL_PROPERTY, device.getId(), ABILITY_OUTLET,
PROPERTY_BUTTON_MANUAL_OVERRIDE_TIME, device.getLocation().getId());

stopRefreshThread(false);
executeRequest(HttpMethod.PUT, propertyUrl, new SimplePropertiesWrapper(prop), NoResult.class);
device.getAbility(ABILITY_OUTLET).getProperty(PROPERTY_BUTTON_MANUAL_OVERRIDE_TIME)
.setValue(prop.getValue());
startRefreshThread();

break;
case OUTLET_VALVE:
ability = device.getAbility(ABILITY_OUTLET);
Expand All @@ -342,9 +337,10 @@ public void sendCommand(Device device, GardenaSmartCommandName commandName, Obje
}

if (command != null) {
stopRefreshThread(false);
executeRequest(HttpMethod.POST, getCommandUrl(device, ability), command, NoResult.class);
startRefreshThread();
}
scheduleIntermediateRefresh();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public String getValue() {
return value;
}

/**
* Sets the value of the property.
*/
public void setValue(String value) {
this.value = value;
}

/**
* Returns the timestamp of the property.
*/
Expand Down

0 comments on commit 9dbd97f

Please sign in to comment.