Skip to content

Commit

Permalink
Incorporated commentd from review
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp committed Jan 12, 2021
1 parent d03174b commit f3a311a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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) {
Expand All @@ -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",
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit f3a311a

Please sign in to comment.