RL: tasks -> kt, prevent NPE

This commit is contained in:
Milos Kozak 2022-06-23 10:34:24 +02:00
parent 80c325fb0f
commit cec6595d0b
14 changed files with 208 additions and 395 deletions

View file

@ -1,46 +0,0 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
import info.nightscout.shared.logging.AAPSLogger;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice;
/**
* Created by geoff on 7/9/16.
*/
public class DiscoverGattServicesTask extends ServiceTask {
@Inject AAPSLogger aapsLogger;
public boolean needToConnect = false;
public DiscoverGattServicesTask(HasAndroidInjector injector) {
super(injector);
}
public DiscoverGattServicesTask(HasAndroidInjector injector, boolean needToConnect) {
super(injector);
this.needToConnect = needToConnect;
}
@Override
public void run() {
if (!isRileyLinkDevice()) {
return;
}
RileyLinkPumpDevice pumpDevice = (RileyLinkPumpDevice) activePlugin.getActivePump();
if (needToConnect) {
pumpDevice.getRileyLinkService().getRileyLinkBLE().connectGatt();
}
pumpDevice.getRileyLinkService().getRileyLinkBLE().discoverServices();
}
}

View file

@ -0,0 +1,16 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport
import info.nightscout.shared.logging.AAPSLogger
import javax.inject.Inject
class DiscoverGattServicesTask(injector: HasAndroidInjector, private val needToConnect: Boolean = false) : ServiceTask(injector) {
@Inject lateinit var aapsLogger: AAPSLogger
override fun run() {
if (needToConnect) pumpDevice?.rileyLinkService?.rileyLinkBLE?.connectGatt()
pumpDevice?.rileyLinkService?.rileyLinkBLE?.discoverServices()
}
}

View file

@ -1,126 +0,0 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks;
import android.content.Context;
import java.util.Locale;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
import info.nightscout.shared.logging.AAPSLogger;
import info.nightscout.shared.logging.LTag;
import info.nightscout.androidaps.plugins.common.ManufacturerType;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkCommunicationManager;
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.RileyLinkTargetFrequency;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkError;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkServiceState;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport;
import info.nightscout.androidaps.utils.Round;
import info.nightscout.shared.sharedPreferences.SP;
/**
* Created by geoff on 7/9/16.
* <p>
* This class is intended to be run by the Service, for the Service. Not intended for clients to run.
*/
public class InitializePumpManagerTask extends ServiceTask {
@Inject AAPSLogger aapsLogger;
//@Inject ActivePluginProvider activePlugin;
@Inject SP sp;
@Inject RileyLinkServiceData rileyLinkServiceData;
@Inject RileyLinkUtil rileyLinkUtil;
private final Context context;
public InitializePumpManagerTask(HasAndroidInjector injector, Context context) {
super(injector);
this.context = context;
}
public InitializePumpManagerTask(HasAndroidInjector injector, Context context, ServiceTransport transport) {
super(injector, transport);
this.context = context;
}
@Override
public void run() {
if (!isRileyLinkDevice()) {
return;
}
double lastGoodFrequency;
if (rileyLinkServiceData.lastGoodFrequency == null) {
lastGoodFrequency = sp.getDouble(RileyLinkConst.Prefs.LastGoodDeviceFrequency, 0.0d);
lastGoodFrequency = Math.round(lastGoodFrequency * 1000d) / 1000d;
rileyLinkServiceData.lastGoodFrequency = lastGoodFrequency;
// if (RileyLinkUtil.getRileyLinkTargetFrequency() == null) {
// String pumpFrequency = SP.getString(MedtronicConst.Prefs.PumpFrequency, null);
// }
} else {
lastGoodFrequency = rileyLinkServiceData.lastGoodFrequency;
}
/* FIXME this can apparently crash:
Fatal Exception: java.lang.ClassCastException
info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin
cannot be cast to info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice
*/
RileyLinkCommunicationManager<?> rileyLinkCommunicationManager = ((RileyLinkPumpDevice) activePlugin.getActivePump()).getRileyLinkService().getDeviceCommunicationManager();
if (activePlugin.getActivePump().manufacturer() == ManufacturerType.Medtronic) {
if ((lastGoodFrequency > 0.0d)
&& rileyLinkCommunicationManager.isValidFrequency(lastGoodFrequency)) {
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkReady);
aapsLogger.info(LTag.PUMPBTCOMM, String.format(Locale.ENGLISH, "Setting radio frequency to %.3f MHz", lastGoodFrequency));
rileyLinkCommunicationManager.setRadioFrequencyForPump(lastGoodFrequency);
boolean foundThePump = rileyLinkCommunicationManager.tryToConnectToDevice();
if (foundThePump) {
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady);
} else {
rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError,
RileyLinkError.NoContactWithDevice);
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump, context);
}
} else {
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump, context);
}
} else {
if (!Round.INSTANCE.isSame(lastGoodFrequency,
RileyLinkTargetFrequency.Omnipod.getScanFrequencies()[0])) {
lastGoodFrequency = RileyLinkTargetFrequency.Omnipod.getScanFrequencies()[0];
lastGoodFrequency = Math.round(lastGoodFrequency * 1000d) / 1000d;
rileyLinkServiceData.lastGoodFrequency = lastGoodFrequency;
}
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkReady);
rileyLinkServiceData.rileyLinkTargetFrequency = RileyLinkTargetFrequency.Omnipod; // TODO shouldn't be needed
aapsLogger.info(LTag.PUMPBTCOMM, String.format(Locale.ENGLISH, "Setting radio frequency to %.3f MHz", lastGoodFrequency));
rileyLinkCommunicationManager.setRadioFrequencyForPump(lastGoodFrequency);
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady);
}
}
}

View file

@ -0,0 +1,65 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks
import android.content.Context
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.plugins.common.ManufacturerType
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.RileyLinkTargetFrequency
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.service.RileyLinkServiceData
import info.nightscout.androidaps.utils.Round.isSame
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP
import javax.inject.Inject
import kotlin.math.roundToLong
/**
* This class is intended to be run by the Service, for the Service. Not intended for clients to run.
*/
class InitializePumpManagerTask(injector: HasAndroidInjector, private val context: Context) : ServiceTask(injector) {
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var sp: SP
@Inject lateinit var rileyLinkServiceData: RileyLinkServiceData
@Inject lateinit var rileyLinkUtil: RileyLinkUtil
override fun run() {
if (!isRileyLinkDevice) return
var lastGoodFrequency: Double
if (rileyLinkServiceData.lastGoodFrequency == null) {
lastGoodFrequency = sp.getDouble(RileyLinkConst.Prefs.LastGoodDeviceFrequency, 0.0)
lastGoodFrequency = (lastGoodFrequency * 1000.0).roundToLong() / 1000.0
rileyLinkServiceData.lastGoodFrequency = lastGoodFrequency
} else lastGoodFrequency = rileyLinkServiceData.lastGoodFrequency
val rileyLinkCommunicationManager = pumpDevice?.rileyLinkService?.deviceCommunicationManager
if (activePlugin.activePump.manufacturer() === ManufacturerType.Medtronic) {
if (lastGoodFrequency > 0.0 && rileyLinkCommunicationManager?.isValidFrequency(lastGoodFrequency) == true) {
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkReady)
aapsLogger.info(LTag.PUMPBTCOMM, "Setting radio frequency to $lastGoodFrequency MHz")
rileyLinkCommunicationManager.setRadioFrequencyForPump(lastGoodFrequency)
if (rileyLinkCommunicationManager.tryToConnectToDevice()) rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady)
else {
rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.NoContactWithDevice)
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump, context)
}
} else rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump, context)
} else {
if (!isSame(lastGoodFrequency, RileyLinkTargetFrequency.Omnipod.scanFrequencies[0])) {
lastGoodFrequency = RileyLinkTargetFrequency.Omnipod.scanFrequencies[0]
lastGoodFrequency = (lastGoodFrequency * 1000.0).roundToLong() / 1000.0
rileyLinkServiceData.lastGoodFrequency = lastGoodFrequency
}
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkReady)
rileyLinkServiceData.rileyLinkTargetFrequency = RileyLinkTargetFrequency.Omnipod // TODO shouldn't be needed
aapsLogger.info(LTag.PUMPBTCOMM, "Setting radio frequency to $lastGoodFrequency MHz")
rileyLinkCommunicationManager?.setRadioFrequencyForPump(lastGoodFrequency)
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady)
}
}
}

View file

@ -1,19 +0,0 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport;
/**
* Created by geoff on 7/10/16.
*/
public class PumpTask extends ServiceTask {
public PumpTask(HasAndroidInjector injector) {
super(injector);
}
public PumpTask(HasAndroidInjector injector, ServiceTransport transport) {
super(injector, transport);
}
}

View file

@ -0,0 +1,7 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport
open class PumpTask(injector: HasAndroidInjector) : ServiceTask(injector)

View file

@ -1,43 +0,0 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.interfaces.ActivePlugin;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.pump.common.events.EventRefreshButtonState;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RFSpy;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice;
/**
* Created by andy on 9/16/18.
*/
public class ResetRileyLinkConfigurationTask extends PumpTask {
@Inject ActivePlugin activePlugin;
@Inject RxBus rxBus;
@Inject RFSpy rfSpy;
public ResetRileyLinkConfigurationTask(HasAndroidInjector injector) {
super(injector);
}
@Override
public void run() {
if (!isRileyLinkDevice()) {
return;
}
RileyLinkPumpDevice rileyLinkPumpDevice = (RileyLinkPumpDevice) activePlugin.getActivePump();
rxBus.send(new EventRefreshButtonState(false));
rileyLinkPumpDevice.setBusy(true);
rfSpy.resetRileyLinkConfiguration();
rileyLinkPumpDevice.setBusy(false);
rxBus.send(new EventRefreshButtonState(true));
}
}

View file

@ -0,0 +1,22 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.pump.common.events.EventRefreshButtonState
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RFSpy
import javax.inject.Inject
class ResetRileyLinkConfigurationTask(injector: HasAndroidInjector) : PumpTask(injector) {
@Inject lateinit var rxBus: RxBus
@Inject lateinit var rfSpy: RFSpy
override fun run() {
if (!isRileyLinkDevice) return
rxBus.send(EventRefreshButtonState(false))
pumpDevice?.setBusy(true)
rfSpy.resetRileyLinkConfiguration()
pumpDevice?.setBusy(false)
rxBus.send(EventRefreshButtonState(true))
}
}

View file

@ -1,71 +0,0 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.interfaces.ActivePlugin;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport;
/**
* Created by geoff on 7/9/16.
*/
public class ServiceTask implements Runnable {
@Inject protected ActivePlugin activePlugin;
public boolean completed = false;
protected ServiceTransport mTransport;
protected HasAndroidInjector injector;
public ServiceTask(HasAndroidInjector injector) {
this.injector = injector;
injector.androidInjector().inject(this);
init(new ServiceTransport());
}
public ServiceTask(HasAndroidInjector injector, ServiceTransport transport) {
this.injector = injector;
injector.androidInjector().inject(this);
init(transport);
}
public void init(ServiceTransport transport) {
mTransport = transport;
}
@Override
public void run() {
}
public void preOp() {
// This function is called by UI thread before running asynch thread.
}
public void postOp() {
// This function is called by UI thread after running asynch thread.
}
public ServiceTransport getServiceTransport() {
return mTransport;
}
/*
* protected void sendResponse(ServiceResult result) {
* RoundtripService.getInstance().sendServiceTransportResponse(mTransport,result);
* }
*/
public boolean isRileyLinkDevice() {
return (activePlugin.getActivePump() instanceof RileyLinkPumpDevice);
}
}

View file

@ -0,0 +1,34 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.data.ServiceTransport
import javax.inject.Inject
@Suppress("LeakingThis")
open class ServiceTask constructor(val injector: HasAndroidInjector, val serviceTransport: ServiceTransport = ServiceTransport()) : Runnable {
@Inject lateinit var activePlugin: ActivePlugin
var completed = false
init {
injector.androidInjector().inject(this)
}
override fun run() {}
// This function is called by UI thread before running async thread.
fun preOp() {}
// This function is called by UI thread after running async thread.
fun postOp() {}
val isRileyLinkDevice: Boolean
get() = activePlugin.activePump is RileyLinkPumpDevice
val pumpDevice: RileyLinkPumpDevice?
get() = if (isRileyLinkDevice) activePlugin.activePump as RileyLinkPumpDevice else null
}

View file

@ -1,53 +0,0 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Singleton;
import info.nightscout.shared.logging.AAPSLogger;
import info.nightscout.shared.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 {
@Inject RileyLinkUtil rileyLinkUtil;
@Inject AAPSLogger aapsLogger;
private static final LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
@Inject
public ServiceTaskExecutor() {
super(1, 1, 10000, TimeUnit.MILLISECONDS, taskQueue);
}
public ServiceTask startTask(ServiceTask task) {
execute(task); // task will be run on async thread from pool.
return task;
}
// FIXME
@Override protected void beforeExecute(Thread t, Runnable r) {
// This is run on either caller UI thread or Service UI thread.
ServiceTask task = (ServiceTask) r;
aapsLogger.debug(LTag.PUMPBTCOMM, "About to run task " + task.getClass().getSimpleName());
rileyLinkUtil.setCurrentTask(task);
task.preOp();
}
// FIXME
@Override protected void afterExecute(Runnable r, Throwable t) {
// This is run on either caller UI thread or Service UI thread.
ServiceTask task = (ServiceTask) r;
task.postOp();
aapsLogger.debug(LTag.PUMPBTCOMM, "Finishing task " + task.getClass().getSimpleName());
rileyLinkUtil.finishCurrentTask(task);
}
}

View file

@ -0,0 +1,45 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class ServiceTaskExecutor @Inject constructor() : ThreadPoolExecutor(1, 1, 10000, TimeUnit.MILLISECONDS, taskQueue) {
@Inject lateinit var rileyLinkUtil: RileyLinkUtil
@Inject lateinit var aapsLogger: AAPSLogger
companion object {
private val taskQueue = LinkedBlockingQueue<Runnable>()
}
fun startTask(task: ServiceTask): ServiceTask {
execute(task) // task will be run on async thread from pool.
return task
}
// FIXME
override fun beforeExecute(t: Thread, r: Runnable) {
// This is run on either caller UI thread or Service UI thread.
val task = r as ServiceTask
aapsLogger.debug(LTag.PUMPBTCOMM, "About to run task ${task.javaClass.simpleName}")
rileyLinkUtil.setCurrentTask(task)
task.preOp()
}
// FIXME
override fun afterExecute(r: Runnable, t: Throwable) {
// This is run on either caller UI thread or Service UI thread.
val task = r as ServiceTask
task.postOp()
aapsLogger.debug(LTag.PUMPBTCOMM, "Finishing task ${task.javaClass.simpleName}")
rileyLinkUtil.finishCurrentTask(task)
}
}

View file

@ -1,37 +0,0 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.pump.common.events.EventRefreshButtonState;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice;
/**
* Created by geoff on 7/16/16.
*/
public class WakeAndTuneTask extends PumpTask {
//@Inject ActivePluginProvider activePlugin;
@Inject RxBus rxBus;
private static final String TAG = "WakeAndTuneTask";
public WakeAndTuneTask(HasAndroidInjector injector) {
super(injector);
}
@Override
public void run() {
if (!isRileyLinkDevice()) {
return;
}
RileyLinkPumpDevice pumpDevice = (RileyLinkPumpDevice) activePlugin.getActivePump();
rxBus.send(new EventRefreshButtonState(false));
pumpDevice.setBusy(true);
pumpDevice.getRileyLinkService().doTuneUpDevice();
pumpDevice.setBusy(false);
rxBus.send(new EventRefreshButtonState(true));
}
}

View file

@ -0,0 +1,19 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.tasks
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.pump.common.events.EventRefreshButtonState
import javax.inject.Inject
class WakeAndTuneTask(injector: HasAndroidInjector) : PumpTask(injector) {
@Inject lateinit var rxBus: RxBus
override fun run() {
rxBus.send(EventRefreshButtonState(false))
pumpDevice?.setBusy(true)
pumpDevice?.rileyLinkService?.doTuneUpDevice()
pumpDevice?.setBusy(false)
rxBus.send(EventRefreshButtonState(true))
}
}