AndroidAPS/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.java

126 lines
5.3 KiB
Java
Raw Normal View History

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;
import android.content.Context;
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;
import info.nightscout.androidaps.data.PumpEnactResult;
2017-11-10 00:27:18 +01:00
import info.nightscout.androidaps.events.EventPumpStatusChanged;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
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 {
private static Logger log = LoggerFactory.getLogger(QueueThread.class);
CommandQueue queue;
private long connectionStartTime = 0;
private PowerManager.WakeLock mWakeLock;
2017-11-10 00:27:18 +01:00
public QueueThread(CommandQueue queue) {
super(QueueThread.class.toString());
this.queue = queue;
PowerManager powerManager = (PowerManager) MainApp.instance().getApplicationContext().getSystemService(Context.POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "QueueThread");
2017-11-10 00:27:18 +01:00
}
@Override
public final void run() {
mWakeLock.acquire();
2017-11-11 14:05:29 +01:00
MainApp.bus().post(new EventQueueChanged());
connectionStartTime = System.currentTimeMillis();
2017-11-10 00:27:18 +01:00
try {
while (true) {
2017-11-22 22:09:58 +01:00
PumpInterface pump = ConfigBuilderPlugin.getActivePump();
log.debug("Looping ...");
long secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000;
if (pump.isConnecting()) {
log.debug("State: connecting");
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.CONNECTING, (int) secondsElapsed));
SystemClock.sleep(1000);
continue;
}
2017-11-10 00:27:18 +01:00
if (!pump.isConnected() && secondsElapsed > Constants.PUMP_MAX_CONNECTION_TIME_IN_SECONDS) {
log.debug("State: timed out");
MainApp.bus().post(new EventDismissBolusprogressIfRunning(new PumpEnactResult()));
MainApp.bus().post(new EventPumpStatusChanged(MainApp.sResources.getString(R.string.connectiontimedout)));
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);
if(watchdog) {
log.debug("BT watchdog - toggeling the phonest bluetooth");
//write time
SP.putLong(R.string.key_btwatchdog_lastbark, System.currentTimeMillis());
//toggle BT
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.disable();
SystemClock.sleep(1000);
mBluetoothAdapter.enable();
SystemClock.sleep(1000);
//start over again once after watchdog barked
connectionStartTime = System.currentTimeMillis();
} else {
queue.clear();
return;
}
}
2017-11-10 00:27:18 +01:00
if (!pump.isConnected()) {
log.debug("State: connect");
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
}
if (queue.performing() == null) {
// Pickup 1st command and set performing variable
if (queue.size() > 0) {
queue.pickup();
log.debug("State: performing " + queue.performing().status());
MainApp.bus().post(new EventQueueChanged());
queue.performing().execute();
queue.resetPerforming();
MainApp.bus().post(new EventQueueChanged());
SystemClock.sleep(100);
continue;
}
}
if (queue.size() == 0 && queue.performing() == null) {
log.debug("State: queue empty. disconnect");
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTING));
pump.disconnect("Queue empty");
MainApp.bus().post(new EventPumpStatusChanged(EventPumpStatusChanged.DISCONNECTED));
return;
}
2017-11-10 00:27:18 +01:00
}
} finally {
mWakeLock.release();
2017-11-10 00:27:18 +01:00
}
}
}