Skip to content

Commit

Permalink
[HomeWizard] Process review comments (#9825)
Browse files Browse the repository at this point in the history
Processed the comments from the 2nd review.

Removed locks (then removed start/stop methods).

Signed-off-by: Daniël van Os <[email protected]>
Signed-off-by: Daniel <[email protected]>
  • Loading branch information
Daniel-42 committed Mar 23, 2021
1 parent 10933b9 commit 72dc25d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import java.io.IOException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -32,8 +30,6 @@
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
Expand All @@ -48,19 +44,13 @@
@NonNullByDefault
public class HomeWizardHandler extends BaseThingHandler {

private final Logger logger = LoggerFactory.getLogger(HomeWizardHandler.class);

private HomeWizardConfiguration config = new HomeWizardConfiguration();

private final Lock pollingJobLock = new ReentrantLock();
private @Nullable ScheduledFuture<?> pollingJob;
private int currentRefreshDelay = 0;

private final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();

private String apiURL = "";

private String meterModel = "";
private int meterVersion = 0;

Expand All @@ -86,9 +76,8 @@ public void handleCommand(ChannelUID channelUID, Command command) {
@Override
public void initialize() {
config = getConfigAs(HomeWizardConfiguration.class);

if (configure()) {
startPolling();
pollingJob = scheduler.scheduleWithFixedDelay(this::pollingCode, 0, config.refreshDelay, TimeUnit.SECONDS);
}
}

Expand All @@ -110,51 +99,15 @@ private boolean configure() {
}

/**
* Stop the poller unconditionally
* dispose: stop the poller
*/
@Override
public void dispose() {
stopPolling();
}

/**
* Stop the polling job
*/
private void stopPolling() {
try {
pollingJobLock.lock();
if (pollingJob != null && !pollingJob.isCancelled()) {
if (pollingJob != null) {
pollingJob.cancel(true);
}
pollingJob = null;
}
} finally {
pollingJobLock.unlock();
}
}

/**
* Start a polling job if it is not already running.
*/
private void startPolling() {
try {
pollingJobLock.lock();
boolean startPoller = false;
if (pollingJob != null) {
startPoller = pollingJob.isCancelled();
} else {
startPoller = true;
}

if (startPoller) {
currentRefreshDelay = config.refreshDelay;
pollingJob = scheduler.scheduleWithFixedDelay(this::pollingCode, 0, currentRefreshDelay,
TimeUnit.SECONDS);
}
} finally {
pollingJobLock.unlock();
var job = pollingJob;
if (job != null && !job.isCancelled()) {
job.cancel(true);
}
pollingJob = null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<description>The IP or host name of the P1 Meter.</description>
<context>network-address</context>
</parameter>
<parameter name="refreshDelay" type="integer" min="1">
<parameter name="refreshDelay" type="integer" min="1" unit="s">
<label>Refresh Interval</label>
<description>The refresh interval in seconds for polling the P1 Meter.</description>
<default>5</default>
Expand Down

0 comments on commit 72dc25d

Please sign in to comment.