2017-11-10 00:27:18 +01:00
|
|
|
package info.nightscout.androidaps.queue;
|
|
|
|
|
2017-12-01 13:57:53 +01:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
2017-11-11 23:03:31 +01:00
|
|
|
import android.content.Context;
|
2018-09-01 22:45:15 +02:00
|
|
|
import android.content.ContextWrapper;
|
2017-11-11 23:03:31 +01:00
|
|
|
import android.os.PowerManager;
|
2017-11-10 00:27:18 +01:00
|
|
|
import android.os.SystemClock;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2017-11-11 14:05:29 +01:00
|
|
|
import info.nightscout.androidaps.Constants;
|
2017-11-10 00:27:18 +01:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2017-11-11 14:05:29 +01:00
|
|
|
import info.nightscout.androidaps.R;
|
2017-11-10 00:27:18 +01:00
|
|
|
import info.nightscout.androidaps.events.EventPumpStatusChanged;
|
|
|
|
import info.nightscout.androidaps.interfaces.PumpInterface;
|
2018-07-29 14:39:56 +02:00
|
|
|
import info.nightscout.androidaps.logging.L;
|
2017-11-10 00:27:18 +01:00
|
|
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
2017-11-11 23:03:31 +01:00
|
|
|
import info.nightscout.androidaps.plugins.Overview.events.EventDismissBolusprogressIfRunning;
|
2017-11-11 14:05:29 +01:00
|
|
|
import info.nightscout.androidaps.queue.events.EventQueueChanged;
|
2017-12-01 13:57:53 +01:00
|
|
|
import info.nightscout.utils.SP;
|
2017-11-10 00:27:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 09.11.2017.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class QueueThread extends Thread {
|
2018-07-29 14:39:56 +02:00
|
|
|
private Logger log = LoggerFactory.getLogger(L.PUMPQUEUE);
|
2017-11-10 00:27:18 +01:00
|
|
|
|
2017-12-06 17:11:34 +01:00
|
|
|
private CommandQueue queue;
|
2017-11-10 00:27:18 +01:00
|
|
|
|
2017-12-20 18:26:47 +01:00
|
|
|
private long lastCommandTime = 0;
|
2017-12-05 07:34:34 +01:00
|
|
|
private boolean connectLogged = false;
|
2018-04-01 12:34:41 +02:00
|
|
|
public boolean waitingForDisconnect = false;
|
2017-11-10 00:27:18 +01:00
|
|
|
|
2017-11-11 23:03:31 +01:00
|
|
|
private PowerManager.WakeLock mWakeLock;
|
|
|
|
|
2018-09-01 22:45:15 +02:00
|
|
|
QueueThread(CommandQueue queue) {
|
2018-01-21 22:51:30 +01:00
|
|
|
super();
|
2017-11-10 00:27:18 +01:00
|
|
|
|
|
|
|
this.queue = queue;
|
2018-09-01 22:45:15 +02:00
|
|
|
Context context = MainApp.instance().getApplicationContext();
|
|
|
|
if (context != null) {
|
|
|
|
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "QueueThread");
|
|
|
|
}
|
2017-11-10 00:27:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final void run() {
|
2017-11-11 23:03:31 +01:00
|
|
|
mWakeLock.acquire();
|
2017-11-11 14:05:29 +01:00
|
|
|
MainApp.bus().post(new EventQueueChanged());
|
2017-12-20 19:42:13 +01:00
|
|
|
long connectionStartTime = lastCommandTime = System.currentTimeMillis();
|
2017-11-10 00:27:18 +01:00
|
|
|
|
2017-11-11 23:03:31 +01:00
|
|
|
try {
|
|
|
|
while (true) {
|
2018-09-17 17:43:42 +02:00
|
|
|
PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump();
|
2018-04-12 09:53:14 +02:00
|
|
|
if (pump == null) {
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("pump == null");
|
2018-05-02 13:59:27 +02:00
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.pumpNotInitialized)));
|
2018-04-12 09:53:14 +02:00
|
|
|
SystemClock.sleep(1000);
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-11 23:03:31 +01:00
|
|
|
long secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000;
|
2017-11-10 00:27:18 +01:00
|
|
|
|
2017-11-11 23:03:31 +01:00
|
|
|
if (!pump.isConnected() && secondsElapsed > Constants.PUMP_MAX_CONNECTION_TIME_IN_SECONDS) {
|
2017-12-19 14:47:55 +01:00
|
|
|
MainApp.bus().post(new EventDismissBolusprogressIfRunning(null));
|
2018-05-02 13:59:27 +02:00
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(MainApp.gs(R.string.connectiontimedout)));
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("timed out");
|
2017-11-11 23:03:31 +01:00
|
|
|
pump.stopConnecting();
|
2017-12-01 13:57:53 +01:00
|
|
|
|
|
|
|
//BLUETOOTH-WATCHDOG
|
|
|
|
boolean watchdog = SP.getBoolean(R.string.key_btwatchdog, false);
|
|
|
|
long last_watchdog = SP.getLong(R.string.key_btwatchdog_lastbark, 0l);
|
|
|
|
watchdog = watchdog && System.currentTimeMillis() - last_watchdog > (Constants.MIN_WATCHDOG_INTERVAL_IN_SECONDS * 1000);
|
2018-07-27 12:17:29 +02:00
|
|
|
if (watchdog) {
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("BT watchdog - toggeling the phonest bluetooth");
|
2017-12-01 13:57:53 +01:00
|
|
|
//write time
|
|
|
|
SP.putLong(R.string.key_btwatchdog_lastbark, System.currentTimeMillis());
|
|
|
|
//toggle BT
|
2017-12-25 20:39:14 +01:00
|
|
|
pump.stopConnecting();
|
|
|
|
pump.disconnect("watchdog");
|
|
|
|
SystemClock.sleep(1000);
|
2017-12-01 13:57:53 +01:00
|
|
|
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
mBluetoothAdapter.disable();
|
|
|
|
SystemClock.sleep(1000);
|
|
|
|
mBluetoothAdapter.enable();
|
|
|
|
SystemClock.sleep(1000);
|
|
|
|
//start over again once after watchdog barked
|
2017-12-25 20:39:14 +01:00
|
|
|
//Notification notification = new Notification(Notification.OLD_NSCLIENT, "Watchdog", Notification.URGENT);
|
|
|
|
//MainApp.bus().post(new EventNewNotification(notification));
|
2017-12-20 20:55:14 +01:00
|
|
|
connectionStartTime = lastCommandTime = System.currentTimeMillis();
|
2017-12-25 20:39:14 +01:00
|
|
|
pump.connect("watchdog");
|
2017-12-01 13:57:53 +01:00
|
|
|
} else {
|
|
|
|
queue.clear();
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("no connection possible");
|
2017-12-25 20:39:14 +01:00
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING));
|
|
|
|
pump.disconnect("Queue empty");
|
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED));
|
2017-12-01 13:57:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-11-11 23:03:31 +01:00
|
|
|
}
|
2017-11-10 00:27:18 +01:00
|
|
|
|
2018-08-21 17:25:20 +02:00
|
|
|
if (pump.isHandshakeInProgress()) {
|
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
|
|
|
log.debug("handshaking " + secondsElapsed);
|
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.HANDSHAKING, (int) secondsElapsed));
|
|
|
|
SystemClock.sleep(100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-25 20:39:14 +01:00
|
|
|
if (pump.isConnecting()) {
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("connecting " + secondsElapsed);
|
2017-12-25 20:39:14 +01:00
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed));
|
|
|
|
SystemClock.sleep(1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-11 23:03:31 +01:00
|
|
|
if (!pump.isConnected()) {
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("connect");
|
2017-11-11 23:03:31 +01:00
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed));
|
|
|
|
pump.connect("Connection needed");
|
|
|
|
SystemClock.sleep(1000);
|
2017-11-11 14:05:29 +01:00
|
|
|
continue;
|
2017-11-10 00:27:18 +01:00
|
|
|
}
|
|
|
|
|
2017-11-11 23:03:31 +01:00
|
|
|
if (queue.performing() == null) {
|
2017-12-05 07:34:34 +01:00
|
|
|
if (!connectLogged) {
|
|
|
|
connectLogged = true;
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("connection time " + secondsElapsed + "s");
|
2017-12-05 07:34:34 +01:00
|
|
|
}
|
2017-11-11 23:03:31 +01:00
|
|
|
// Pickup 1st command and set performing variable
|
|
|
|
if (queue.size() > 0) {
|
|
|
|
queue.pickup();
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("performing " + queue.performing().status());
|
2017-11-11 23:03:31 +01:00
|
|
|
MainApp.bus().post(new EventQueueChanged());
|
|
|
|
queue.performing().execute();
|
|
|
|
queue.resetPerforming();
|
|
|
|
MainApp.bus().post(new EventQueueChanged());
|
2017-12-20 18:26:47 +01:00
|
|
|
lastCommandTime = System.currentTimeMillis();
|
2017-11-11 23:03:31 +01:00
|
|
|
SystemClock.sleep(100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queue.size() == 0 && queue.performing() == null) {
|
2017-12-20 18:26:47 +01:00
|
|
|
long secondsFromLastCommand = (System.currentTimeMillis() - lastCommandTime) / 1000;
|
|
|
|
if (secondsFromLastCommand >= 5) {
|
2018-04-01 12:34:41 +02:00
|
|
|
waitingForDisconnect = true;
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("queue empty. disconnect");
|
2017-12-20 18:26:47 +01:00
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING));
|
|
|
|
pump.disconnect("Queue empty");
|
|
|
|
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED));
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("disconnected");
|
2017-12-20 18:26:47 +01:00
|
|
|
return;
|
|
|
|
} else {
|
2018-07-29 14:39:56 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
2018-07-27 12:17:29 +02:00
|
|
|
log.debug("waiting for disconnect");
|
2017-12-20 18:26:47 +01:00
|
|
|
SystemClock.sleep(1000);
|
|
|
|
}
|
2017-11-11 23:03:31 +01:00
|
|
|
}
|
2017-11-10 00:27:18 +01:00
|
|
|
}
|
2017-11-11 23:03:31 +01:00
|
|
|
} finally {
|
|
|
|
mWakeLock.release();
|
2018-08-21 19:53:59 +02:00
|
|
|
if (L.isEnabled(L.PUMPQUEUE))
|
|
|
|
log.debug("thread end");
|
2017-11-10 00:27:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|