fix state when new thread was not started during waiting for pump disconnection

This commit is contained in:
Milos Kozak 2018-04-01 12:34:41 +02:00
parent 5fa56b22c9
commit 89ee73ef7d
2 changed files with 13 additions and 1 deletions

View file

@ -107,10 +107,12 @@ public class CommandQueue {
private synchronized void inject(Command command) { private synchronized void inject(Command command) {
// inject as a first command // inject as a first command
log.debug("QUEUE: Adding as first: " + command.getClass().getSimpleName() + " - " + command.status());
queue.addFirst(command); queue.addFirst(command);
} }
private synchronized void add(Command command) { private synchronized void add(Command command) {
log.debug("QUEUE: Adding: " + command.getClass().getSimpleName() + " - " + command.status());
queue.add(command); queue.add(command);
} }
@ -142,9 +144,16 @@ public class CommandQueue {
// After new command added to the queue // After new command added to the queue
// start thread again if not already running // start thread again if not already running
protected synchronized void notifyAboutNewCommand() { protected synchronized void notifyAboutNewCommand() {
while (thread != null && thread.getState() != Thread.State.TERMINATED && thread.waitingForDisconnect) {
log.debug("QUEUE: Waiting for previous thread finish");
SystemClock.sleep(500);
}
if (thread == null || thread.getState() == Thread.State.TERMINATED) { if (thread == null || thread.getState() == Thread.State.TERMINATED) {
thread = new QueueThread(this); thread = new QueueThread(this);
thread.start(); thread.start();
log.debug("QUEUE: Starting new thread");
} else {
log.debug("QUEUE: Thread is already running");
} }
} }
@ -321,7 +330,7 @@ public class CommandQueue {
MainApp.bus().post(new EventDismissNotification(Notification.BASAL_VALUE_BELOW_MINIMUM)); MainApp.bus().post(new EventDismissNotification(Notification.BASAL_VALUE_BELOW_MINIMUM));
if (isThisProfileSet(profile)) { if (isThisProfileSet(profile)) {
log.debug("Correct profile already set"); log.debug("QUEUE: Correct profile already set");
if (callback != null) if (callback != null)
callback.result(new PumpEnactResult().success(true).enacted(false)).run(); callback.result(new PumpEnactResult().success(true).enacted(false)).run();
return false; return false;

View file

@ -32,6 +32,7 @@ public class QueueThread extends Thread {
private long lastCommandTime = 0; private long lastCommandTime = 0;
private boolean connectLogged = false; private boolean connectLogged = false;
public boolean waitingForDisconnect = false;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
@ -130,10 +131,12 @@ public class QueueThread extends Thread {
if (queue.size() == 0 && queue.performing() == null) { if (queue.size() == 0 && queue.performing() == null) {
long secondsFromLastCommand = (System.currentTimeMillis() - lastCommandTime) / 1000; long secondsFromLastCommand = (System.currentTimeMillis() - lastCommandTime) / 1000;
if (secondsFromLastCommand >= 5) { if (secondsFromLastCommand >= 5) {
waitingForDisconnect = true;
log.debug("QUEUE: queue empty. disconnect"); log.debug("QUEUE: queue empty. disconnect");
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING)); MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING));
pump.disconnect("Queue empty"); pump.disconnect("Queue empty");
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED)); MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED));
log.debug("QUEUE: disconnected");
return; return;
} else { } else {
log.debug("QUEUE: waiting for disconnect"); log.debug("QUEUE: waiting for disconnect");