MDT refactor pass 15

This commit is contained in:
Milos Kozak 2020-04-27 21:41:24 +02:00
parent 975f61563c
commit a3278d53c3
6 changed files with 48 additions and 60 deletions

View file

@ -36,6 +36,7 @@ public abstract class RileyLinkCommunicationManager {
@Inject MedtronicPumpStatus medtronicPumpStatus;
@Inject RileyLinkServiceData rileyLinkServiceData;
@Inject ServiceTaskExecutor serviceTaskExecutor;
private final int SCAN_TIMEOUT = 1500;
@ -106,7 +107,7 @@ public abstract class RileyLinkCommunicationManager {
if (diff > ALLOWED_PUMP_UNREACHABLE) {
aapsLogger.warn(LTag.PUMPBTCOMM, "We reached max time that Pump can be unreachable. Starting Tuning.");
ServiceTaskExecutor.startTask(new WakeAndTuneTask(injector));
serviceTaskExecutor.startTask(new WakeAndTuneTask(injector));
timeoutCount = 0;
}
}
@ -222,7 +223,7 @@ public abstract class RileyLinkCommunicationManager {
public abstract boolean tryToConnectToDevice();
public double scanForDevice(double[] frequencies) {
private double scanForDevice(double[] frequencies) {
aapsLogger.info(LTag.PUMPBTCOMM, "Scanning for receiver ({})", receiverDeviceID);
wakeUp(receiverDeviceAwakeForMinutes, false);
FrequencyScanResults results = new FrequencyScanResults();

View file

@ -33,8 +33,7 @@ public class RileyLinkUtil {
private List<RLHistoryItem> historyRileyLink = new ArrayList<>();
public RileyLinkCommunicationManager rileyLinkCommunicationManager;
@Deprecated
static ServiceTask currentTask;
private ServiceTask currentTask;
private RileyLinkEncodingType encoding;
private Encoding4b6b encoding4b6b;
@ -73,8 +72,7 @@ public class RileyLinkUtil {
}
// FIXME remove ?
@Deprecated
public static void setCurrentTask(ServiceTask task) {
public void setCurrentTask(ServiceTask task) {
if (currentTask == null) {
currentTask = task;
} else {
@ -83,8 +81,7 @@ public class RileyLinkUtil {
}
@Deprecated
public static void finishCurrentTask(ServiceTask task) {
public void finishCurrentTask(ServiceTask task) {
if (task != currentTask) {
//LOG.error("finishCurrentTask: task does not match");
}

View file

@ -22,10 +22,11 @@ import javax.inject.Inject;
import dagger.android.DaggerBroadcastReceiver;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.logging.AAPSLogger;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkFirmwareVersion;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState;
@ -44,9 +45,10 @@ import info.nightscout.androidaps.utils.sharedPreferences.SP;
public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
@Inject HasAndroidInjector injector;
@Inject RileyLinkUtil rileyLinkUtil;
@Inject SP sp;
@Inject AAPSLogger aapsLogger;
@Inject RileyLinkServiceData rileyLinkServiceData;
@Inject ServiceTaskExecutor serviceTaskExecutor;
private static final Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPCOMM);
@ -102,8 +104,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
if (action == null) {
LOG.error("onReceive: null action");
} else {
if (isLoggingEnabled())
LOG.debug("Received Broadcast: " + action);
aapsLogger.debug(LTag.PUMPBTCOMM, "Received Broadcast: " + action);
if (!processBluetoothBroadcasts(action) && //
!processRileyLinkBroadcasts(action, context) && //
@ -149,8 +150,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
return true;
} else if (action.equals(RileyLinkConst.Intents.RileyLinkReady)) {
if (isLoggingEnabled())
LOG.warn("MedtronicConst.Intents.RileyLinkReady");
aapsLogger.warn(LTag.PUMPBTCOMM, "MedtronicConst.Intents.RileyLinkReady");
// sendIPCNotification(RT2Const.IPC.MSG_note_WakingPump);
serviceInstance.rileyLinkBLE.enableNotifications();
@ -161,17 +161,16 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
RileyLinkFirmwareVersion rlVersion = rileyLinkServiceData.firmwareVersion;
// if (isLoggingEnabled())
LOG.debug("RfSpy version (BLE113): " + bleVersion);
aapsLogger.debug(LTag.PUMPBTCOMM, "RfSpy version (BLE113): " + bleVersion);
serviceInstance.rileyLinkServiceData.versionBLE113 = bleVersion;
// if (isLoggingEnabled())
LOG.debug("RfSpy Radio version (CC110): " + rlVersion.name());
aapsLogger.debug(LTag.PUMPBTCOMM, "RfSpy Radio version (CC110): " + rlVersion.name());
serviceInstance.rileyLinkServiceData.versionCC110 = rlVersion;
ServiceTask task = new InitializePumpManagerTask(injector, context);
ServiceTaskExecutor.startTask(task);
if (isLoggingEnabled())
LOG.info("Announcing RileyLink open For business");
serviceTaskExecutor.startTask(task);
aapsLogger.info(LTag.PUMPBTCOMM, "Announcing RileyLink open For business");
return true;
} else if (action.equals(RileyLinkConst.Intents.RileyLinkNewAddressSet)) {
@ -200,18 +199,16 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
public boolean processBluetoothBroadcasts(String action) {
if (action.equals(RileyLinkConst.Intents.BluetoothConnected)) {
if (isLoggingEnabled())
LOG.debug("Bluetooth - Connected");
ServiceTaskExecutor.startTask(new DiscoverGattServicesTask(injector));
aapsLogger.debug(LTag.PUMPBTCOMM, "Bluetooth - Connected");
serviceTaskExecutor.startTask(new DiscoverGattServicesTask(injector));
return true;
} else if (action.equals(RileyLinkConst.Intents.BluetoothReconnected)) {
if (isLoggingEnabled())
LOG.debug("Bluetooth - Reconnecting");
aapsLogger.debug(LTag.PUMPBTCOMM, "Bluetooth - Reconnecting");
serviceInstance.bluetoothInit();
ServiceTaskExecutor.startTask(new DiscoverGattServicesTask(injector, true));
serviceTaskExecutor.startTask(new DiscoverGattServicesTask(injector, true));
return true;
} else {
@ -226,7 +223,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
if (this.broadcastIdentifiers.get("TuneUp").contains(action)) {
if (serviceInstance.getRileyLinkTargetDevice().isTuneUpEnabled()) {
ServiceTaskExecutor.startTask(new WakeAndTuneTask(injector));
serviceTaskExecutor.startTask(new WakeAndTuneTask(injector));
}
return true;
} else {
@ -252,11 +249,6 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
return false;
}
public boolean isLoggingEnabled() {
return (L.isEnabled(L.PUMPCOMM));
}
public void unregisterBroadcasts(Context context) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
}

View file

@ -4,46 +4,40 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import android.util.Log;
import javax.inject.Inject;
import javax.inject.Singleton;
import info.nightscout.androidaps.logging.AAPSLogger;
import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil;
/**
* Created by geoff on 7/9/16.
*/
@Singleton
public class ServiceTaskExecutor extends ThreadPoolExecutor {
private static final String TAG = "ServiceTaskExecutor";
private static ServiceTaskExecutor instance;
@Inject RileyLinkUtil rileyLinkUtil;
@Inject AAPSLogger aapsLogger;
private static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
static {
instance = new ServiceTaskExecutor();
}
private ServiceTaskExecutor() {
@Inject
public ServiceTaskExecutor() {
super(1, 1, 10000, TimeUnit.MILLISECONDS, taskQueue);
}
public static ServiceTaskExecutor getInstance() {
return instance;
}
public static ServiceTask startTask(ServiceTask task) {
instance.execute(task); // task will be run on async thread from pool.
public ServiceTask startTask(ServiceTask task) {
execute(task); // task will be run on async thread from pool.
return task;
}
// FIXME
protected void beforeExecute(Thread t, Runnable r) {
// This is run on either caller UI thread or Service UI thread.
ServiceTask task = (ServiceTask)r;
Log.v(TAG, "About to run task " + task.getClass().getSimpleName());
RileyLinkUtil.setCurrentTask(task);
ServiceTask task = (ServiceTask) r;
aapsLogger.debug(LTag.PUMPBTCOMM, "About to run task " + task.getClass().getSimpleName());
rileyLinkUtil.setCurrentTask(task);
task.preOp();
}
@ -51,9 +45,9 @@ public class ServiceTaskExecutor extends ThreadPoolExecutor {
// FIXME
protected void afterExecute(Runnable r, Throwable t) {
// This is run on either caller UI thread or Service UI thread.
ServiceTask task = (ServiceTask)r;
ServiceTask task = (ServiceTask) r;
task.postOp();
Log.v(TAG, "Finishing task " + task.getClass().getSimpleName());
RileyLinkUtil.finishCurrentTask(task);
aapsLogger.debug(LTag.PUMPBTCOMM, "Finishing task " + task.getClass().getSimpleName());
rileyLinkUtil.finishCurrentTask(task);
}
}

View file

@ -103,6 +103,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter
private final MedtronicPumpStatus medtronicPumpStatus;
private final MedtronicHistoryData medtronicHistoryData;
private final RileyLinkServiceData rileyLinkServiceData;
private final ServiceTaskExecutor serviceTaskExecutor;
protected static MedtronicPumpPlugin plugin = null;
private RileyLinkMedtronicService rileyLinkMedtronicService;
@ -134,7 +135,8 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter
MedtronicUtil medtronicUtil,
MedtronicPumpStatus medtronicPumpStatus,
MedtronicHistoryData medtronicHistoryData,
RileyLinkServiceData rileyLinkServiceData
RileyLinkServiceData rileyLinkServiceData,
ServiceTaskExecutor serviceTaskExecutor
) {
super(new PluginDescription() //
@ -155,6 +157,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter
this.medtronicPumpStatus = medtronicPumpStatus;
this.medtronicHistoryData = medtronicHistoryData;
this.rileyLinkServiceData = rileyLinkServiceData;
this.serviceTaskExecutor = serviceTaskExecutor;
displayConnectionMessages = false;
@ -596,7 +599,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter
if (errorCount >= 5) {
aapsLogger.error("Number of error counts was 5 or more. Starting tunning.");
setRefreshButtonEnabled(true);
ServiceTaskExecutor.startTask(new WakeAndTuneTask(getInjector()));
serviceTaskExecutor.startTask(new WakeAndTuneTask(getInjector()));
return;
}
@ -1527,7 +1530,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter
case WakeUpAndTune: {
if (rileyLinkMedtronicService.verifyConfiguration()) {
ServiceTaskExecutor.startTask(new WakeAndTuneTask(getInjector()));
serviceTaskExecutor.startTask(new WakeAndTuneTask(getInjector()));
} else {
Intent i = new Intent(context, ErrorHelperActivity.class);
i.putExtra("soundid", R.raw.boluserror);
@ -1547,7 +1550,7 @@ public class MedtronicPumpPlugin extends PumpPluginAbstract implements PumpInter
break;
case ResetRileyLinkConfiguration: {
ServiceTaskExecutor.startTask(new ResetRileyLinkConfigurationTask(getInjector()));
serviceTaskExecutor.startTask(new ResetRileyLinkConfigurationTask(getInjector()));
}
break;

View file

@ -68,6 +68,7 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager
@Inject MedtronicUtil medtronicUtil;
@Inject MedtronicPumpHistoryDecoder medtronicPumpHistoryDecoder;
@Inject RileyLinkServiceData rileyLinkServiceData;
@Inject ServiceTaskExecutor serviceTaskExecutor;
private final int MAX_COMMAND_TRIES = 3;
private final int DEFAULT_TIMEOUT = 2000;
@ -136,7 +137,7 @@ public class MedtronicCommunicationManager extends RileyLinkCommunicationManager
long diff = System.currentTimeMillis() - medtronicPumpStatus.lastConnection;
if (diff > RILEYLINK_TIMEOUT) {
ServiceTaskExecutor.startTask(new WakeAndTuneTask(injector));
serviceTaskExecutor.startTask(new WakeAndTuneTask(injector));
}
}