From f3a311aa8cb21296015fad1ab91b368ed9c41d77 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Tue, 12 Jan 2021 08:10:04 +0100 Subject: [PATCH] Incorporated commentd from review Signed-off-by: Christoph Weitkamp --- .../internal/callmonitor/CallMonitor.java | 29 ++++++------------- .../avmfritz/internal/handler/BoxHandler.java | 11 +++---- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/callmonitor/CallMonitor.java b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/callmonitor/CallMonitor.java index 148688ce5ae5e..d986662ef93fa 100644 --- a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/callmonitor/CallMonitor.java +++ b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/callmonitor/CallMonitor.java @@ -69,16 +69,18 @@ public CallMonitor(String ip, BoxHandler handler, ScheduledExecutorService sched thread.start(); this.monitorThread = thread; }, 0, 2, TimeUnit.HOURS); + // initialize states of call monitor channels + resetChannels(); } /** - * Refresh channels. + * Reset channels. */ - public void refreshChannels() { - CallMonitorThread thread = this.monitorThread; - if (thread != null) { - thread.resetChannels(); - } + public void resetChannels() { + handler.updateState(CHANNEL_CALL_INCOMING, UnDefType.UNDEF); + handler.updateState(CHANNEL_CALL_OUTGOING, UnDefType.UNDEF); + handler.updateState(CHANNEL_CALL_ACTIVE, UnDefType.UNDEF); + handler.updateState(CHANNEL_CALL_STATE, CALL_STATE_IDLE); } /** @@ -100,9 +102,6 @@ public class CallMonitorThread extends Thread { // time to wait before reconnecting private long reconnectTime = 60000L; - public CallMonitorThread() { - } - @Override public void run() { while (!interrupted) { @@ -114,7 +113,7 @@ public void run() { reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); // reset the retry interval reconnectTime = 60000L; - } catch (Exception e) { + } catch (IOException e) { handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Cannot connect to Fritz!Box call monitor - make sure to enable it by dialing '#96*5'!"); logger.debug("Error attempting to connect to FritzBox. Retrying in {} seconds", @@ -160,16 +159,6 @@ public void run() { } } - /** - * Resets states of call monitor channels - */ - public void resetChannels() { - handler.updateState(CHANNEL_CALL_INCOMING, UnDefType.UNDEF); - handler.updateState(CHANNEL_CALL_OUTGOING, UnDefType.UNDEF); - handler.updateState(CHANNEL_CALL_ACTIVE, UnDefType.UNDEF); - handler.updateState(CHANNEL_CALL_STATE, CALL_STATE_IDLE); - } - /** * Close socket and stop running thread. */ diff --git a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/BoxHandler.java b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/BoxHandler.java index 1c17b63c6e26e..284d728d1c529 100644 --- a/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/BoxHandler.java +++ b/bundles/org.openhab.binding.avmfritz/src/main/java/org/openhab/binding/avmfritz/internal/handler/BoxHandler.java @@ -15,7 +15,6 @@ import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*; import java.util.Set; -import java.util.concurrent.TimeUnit; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -60,8 +59,6 @@ protected void manageConnections() { CallMonitor cm = this.callMonitor; if (cm == null && callChannelsLinked()) { this.callMonitor = new CallMonitor(config.ipAddress, this, scheduler); - // initialize states of call monitor channels - scheduler.schedule(this::refreshCallChannels, refreshInterval, TimeUnit.SECONDS); } else if (cm != null && !callChannelsLinked()) { cm.dispose(); this.callMonitor = null; @@ -101,15 +98,15 @@ public void updateState(String channelID, State state) { @Override public void handleRefreshCommand() { - refreshCallChannels(); + refreshCallMonitorChannels(); super.handleRefreshCommand(); } - private void refreshCallChannels() { + private void refreshCallMonitorChannels() { CallMonitor cm = this.callMonitor; - if (cm != null && callChannelsLinked()) { + if (cm != null) { // initialize states of call monitor channels - cm.refreshChannels(); + cm.resetChannels(); } } }