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

View file

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

View file

@ -22,10 +22,11 @@ import javax.inject.Inject;
import dagger.android.DaggerBroadcastReceiver; import dagger.android.DaggerBroadcastReceiver;
import dagger.android.HasAndroidInjector; import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.logging.AAPSLogger;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper; 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.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.ble.defs.RileyLinkFirmwareVersion;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState; 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 { public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
@Inject HasAndroidInjector injector; @Inject HasAndroidInjector injector;
@Inject RileyLinkUtil rileyLinkUtil;
@Inject SP sp; @Inject SP sp;
@Inject AAPSLogger aapsLogger;
@Inject RileyLinkServiceData rileyLinkServiceData; @Inject RileyLinkServiceData rileyLinkServiceData;
@Inject ServiceTaskExecutor serviceTaskExecutor;
private static final Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPCOMM); private static final Logger LOG = StacktraceLoggerWrapper.getLogger(L.PUMPCOMM);
@ -102,8 +104,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
if (action == null) { if (action == null) {
LOG.error("onReceive: null action"); LOG.error("onReceive: null action");
} else { } else {
if (isLoggingEnabled()) aapsLogger.debug(LTag.PUMPBTCOMM, "Received Broadcast: " + action);
LOG.debug("Received Broadcast: " + action);
if (!processBluetoothBroadcasts(action) && // if (!processBluetoothBroadcasts(action) && //
!processRileyLinkBroadcasts(action, context) && // !processRileyLinkBroadcasts(action, context) && //
@ -149,8 +150,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
return true; return true;
} else if (action.equals(RileyLinkConst.Intents.RileyLinkReady)) { } else if (action.equals(RileyLinkConst.Intents.RileyLinkReady)) {
if (isLoggingEnabled()) aapsLogger.warn(LTag.PUMPBTCOMM, "MedtronicConst.Intents.RileyLinkReady");
LOG.warn("MedtronicConst.Intents.RileyLinkReady");
// sendIPCNotification(RT2Const.IPC.MSG_note_WakingPump); // sendIPCNotification(RT2Const.IPC.MSG_note_WakingPump);
serviceInstance.rileyLinkBLE.enableNotifications(); serviceInstance.rileyLinkBLE.enableNotifications();
@ -161,17 +161,16 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
RileyLinkFirmwareVersion rlVersion = rileyLinkServiceData.firmwareVersion; RileyLinkFirmwareVersion rlVersion = rileyLinkServiceData.firmwareVersion;
// if (isLoggingEnabled()) // if (isLoggingEnabled())
LOG.debug("RfSpy version (BLE113): " + bleVersion); aapsLogger.debug(LTag.PUMPBTCOMM, "RfSpy version (BLE113): " + bleVersion);
serviceInstance.rileyLinkServiceData.versionBLE113 = bleVersion; serviceInstance.rileyLinkServiceData.versionBLE113 = bleVersion;
// if (isLoggingEnabled()) // if (isLoggingEnabled())
LOG.debug("RfSpy Radio version (CC110): " + rlVersion.name()); aapsLogger.debug(LTag.PUMPBTCOMM, "RfSpy Radio version (CC110): " + rlVersion.name());
serviceInstance.rileyLinkServiceData.versionCC110 = rlVersion; serviceInstance.rileyLinkServiceData.versionCC110 = rlVersion;
ServiceTask task = new InitializePumpManagerTask(injector, context); ServiceTask task = new InitializePumpManagerTask(injector, context);
ServiceTaskExecutor.startTask(task); serviceTaskExecutor.startTask(task);
if (isLoggingEnabled()) aapsLogger.info(LTag.PUMPBTCOMM, "Announcing RileyLink open For business");
LOG.info("Announcing RileyLink open For business");
return true; return true;
} else if (action.equals(RileyLinkConst.Intents.RileyLinkNewAddressSet)) { } else if (action.equals(RileyLinkConst.Intents.RileyLinkNewAddressSet)) {
@ -200,18 +199,16 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
public boolean processBluetoothBroadcasts(String action) { public boolean processBluetoothBroadcasts(String action) {
if (action.equals(RileyLinkConst.Intents.BluetoothConnected)) { if (action.equals(RileyLinkConst.Intents.BluetoothConnected)) {
if (isLoggingEnabled()) aapsLogger.debug(LTag.PUMPBTCOMM, "Bluetooth - Connected");
LOG.debug("Bluetooth - Connected"); serviceTaskExecutor.startTask(new DiscoverGattServicesTask(injector));
ServiceTaskExecutor.startTask(new DiscoverGattServicesTask(injector));
return true; return true;
} else if (action.equals(RileyLinkConst.Intents.BluetoothReconnected)) { } else if (action.equals(RileyLinkConst.Intents.BluetoothReconnected)) {
if (isLoggingEnabled()) aapsLogger.debug(LTag.PUMPBTCOMM, "Bluetooth - Reconnecting");
LOG.debug("Bluetooth - Reconnecting");
serviceInstance.bluetoothInit(); serviceInstance.bluetoothInit();
ServiceTaskExecutor.startTask(new DiscoverGattServicesTask(injector, true)); serviceTaskExecutor.startTask(new DiscoverGattServicesTask(injector, true));
return true; return true;
} else { } else {
@ -226,7 +223,7 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
if (this.broadcastIdentifiers.get("TuneUp").contains(action)) { if (this.broadcastIdentifiers.get("TuneUp").contains(action)) {
if (serviceInstance.getRileyLinkTargetDevice().isTuneUpEnabled()) { if (serviceInstance.getRileyLinkTargetDevice().isTuneUpEnabled()) {
ServiceTaskExecutor.startTask(new WakeAndTuneTask(injector)); serviceTaskExecutor.startTask(new WakeAndTuneTask(injector));
} }
return true; return true;
} else { } else {
@ -252,11 +249,6 @@ public class RileyLinkBroadcastReceiver extends DaggerBroadcastReceiver {
return false; return false;
} }
public boolean isLoggingEnabled() {
return (L.isEnabled(L.PUMPCOMM));
}
public void unregisterBroadcasts(Context context) { public void unregisterBroadcasts(Context context) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(this); 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.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; 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; import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil;
/** /**
* Created by geoff on 7/9/16. * Created by geoff on 7/9/16.
*/ */
@Singleton
public class ServiceTaskExecutor extends ThreadPoolExecutor { public class ServiceTaskExecutor extends ThreadPoolExecutor {
private static final String TAG = "ServiceTaskExecutor"; @Inject RileyLinkUtil rileyLinkUtil;
private static ServiceTaskExecutor instance; @Inject AAPSLogger aapsLogger;
private static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>(); private static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
static { @Inject
instance = new ServiceTaskExecutor(); public ServiceTaskExecutor() {
}
private ServiceTaskExecutor() {
super(1, 1, 10000, TimeUnit.MILLISECONDS, taskQueue); super(1, 1, 10000, TimeUnit.MILLISECONDS, taskQueue);
} }
public ServiceTask startTask(ServiceTask task) {
public static ServiceTaskExecutor getInstance() { execute(task); // task will be run on async thread from pool.
return instance;
}
public static ServiceTask startTask(ServiceTask task) {
instance.execute(task); // task will be run on async thread from pool.
return task; return task;
} }
// FIXME // FIXME
protected void beforeExecute(Thread t, Runnable r) { protected void beforeExecute(Thread t, Runnable r) {
// This is run on either caller UI thread or Service UI thread. // This is run on either caller UI thread or Service UI thread.
ServiceTask task = (ServiceTask)r; ServiceTask task = (ServiceTask) r;
Log.v(TAG, "About to run task " + task.getClass().getSimpleName()); aapsLogger.debug(LTag.PUMPBTCOMM, "About to run task " + task.getClass().getSimpleName());
RileyLinkUtil.setCurrentTask(task); rileyLinkUtil.setCurrentTask(task);
task.preOp(); task.preOp();
} }
@ -51,9 +45,9 @@ public class ServiceTaskExecutor extends ThreadPoolExecutor {
// FIXME // FIXME
protected void afterExecute(Runnable r, Throwable t) { protected void afterExecute(Runnable r, Throwable t) {
// This is run on either caller UI thread or Service UI thread. // This is run on either caller UI thread or Service UI thread.
ServiceTask task = (ServiceTask)r; ServiceTask task = (ServiceTask) r;
task.postOp(); task.postOp();
Log.v(TAG, "Finishing task " + task.getClass().getSimpleName()); aapsLogger.debug(LTag.PUMPBTCOMM, "Finishing task " + task.getClass().getSimpleName());
RileyLinkUtil.finishCurrentTask(task); rileyLinkUtil.finishCurrentTask(task);
} }
} }

View file

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

View file

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