From 096b2e93a310c8c912c62e88f7e903711422cec9 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 20 Dec 2017 18:26:47 +0100 Subject: [PATCH] wait a few sec before disconnection for another command --- .../androidaps/queue/QueueThread.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java index 2c568cdccd..26597144b8 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java @@ -29,6 +29,7 @@ public class QueueThread extends Thread { CommandQueue queue; private long connectionStartTime = 0; + private long lastCommandTime = 0; private boolean connectLogged = false; private PowerManager.WakeLock mWakeLock; @@ -45,7 +46,7 @@ public class QueueThread extends Thread { public final void run() { mWakeLock.acquire(); MainApp.bus().post(new EventQueueChanged()); - connectionStartTime = System.currentTimeMillis(); + connectionStartTime = lastCommandTime = System.currentTimeMillis(); try { while (true) { @@ -107,17 +108,24 @@ public class QueueThread extends Thread { queue.performing().execute(); queue.resetPerforming(); MainApp.bus().post(new EventQueueChanged()); + lastCommandTime = System.currentTimeMillis(); SystemClock.sleep(100); continue; } } if (queue.size() == 0 && queue.performing() == null) { - log.debug("QUEUE: queue empty. disconnect"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); - pump.disconnect("Queue empty"); - MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); - return; + long secondsFromLastCommand = (System.currentTimeMillis() - lastCommandTime) / 1000; + if (secondsFromLastCommand >= 5) { + log.debug("QUEUE: queue empty. disconnect"); + MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); + pump.disconnect("Queue empty"); + MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); + return; + } else { + log.debug("QUEUE: waiting for disconnect"); + SystemClock.sleep(1000); + } } } } finally {