diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.kt b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.kt index d7efd6ca73..9e5e10cd79 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.kt +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkService.kt @@ -7,6 +7,7 @@ import android.content.Intent import dagger.android.DaggerService import dagger.android.HasAndroidInjector import info.nightscout.androidaps.interfaces.ActivePlugin +import info.nightscout.androidaps.interfaces.ResourceHelper import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.pump.common.defs.PumpDeviceState import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkCommunicationManager @@ -17,8 +18,6 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.RileyLink import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.defs.RileyLinkEncodingType 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.RileyLinkTargetDevice -import info.nightscout.androidaps.interfaces.ResourceHelper import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.LTag import info.nightscout.shared.sharedPreferences.SP @@ -85,8 +84,6 @@ abstract class RileyLinkService : DaggerService() { } abstract val deviceCommunicationManager: RileyLinkCommunicationManager<*> - val rileyLinkServiceState: RileyLinkServiceState? - get() = rileyLinkServiceData.rileyLinkServiceState // Here is where the wake-lock begins: // We've received a service startCommand, we grab the lock. @@ -94,7 +91,7 @@ abstract class RileyLinkService : DaggerService() { fun bluetoothInit(): Boolean { aapsLogger.debug(LTag.PUMPBTCOMM, "bluetoothInit: attempting to get an adapter") - rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.BluetoothInitializing + rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothInitializing) if (bluetoothAdapter == null) { aapsLogger.error("Unable to obtain a BluetoothAdapter.") rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.NoBluetoothAdapter) @@ -103,7 +100,7 @@ abstract class RileyLinkService : DaggerService() { aapsLogger.error("Bluetooth is not enabled.") rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.BluetoothDisabled) } else { - rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.BluetoothReady + rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothReady) return true } } @@ -112,7 +109,7 @@ abstract class RileyLinkService : DaggerService() { // returns true if our Rileylink configuration changed fun reconfigureRileyLink(deviceAddress: String): Boolean { - rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.RileyLinkInitializing + rileyLinkServiceData.setServiceState(RileyLinkServiceState.RileyLinkInitializing) return if (rileyLinkBLE.isConnected) { if (deviceAddress == rileyLinkServiceData.rileyLinkAddress) { aapsLogger.info(LTag.PUMPBTCOMM, "No change to RL address. Not reconnecting.") @@ -141,7 +138,7 @@ abstract class RileyLinkService : DaggerService() { // FIXME: This needs to be run in a session so that is incorruptible, has a separate thread, etc. fun doTuneUpDevice() { - rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.TuneUpDevice + rileyLinkServiceData.setServiceState(RileyLinkServiceState.TuneUpDevice) setPumpDeviceState(PumpDeviceState.Sleeping) val lastGoodFrequency = rileyLinkServiceData.lastGoodFrequency ?: sp.getDouble(RileyLinkConst.Prefs.LastGoodDeviceFrequency, 0.0) val newFrequency = deviceCommunicationManager.tuneForDevice() @@ -157,7 +154,7 @@ abstract class RileyLinkService : DaggerService() { rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.TuneUpOfDeviceFailed) } else { deviceCommunicationManager.clearNotConnectedCount() - rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.PumpConnectorReady + rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorReady) } } @@ -168,7 +165,7 @@ abstract class RileyLinkService : DaggerService() { rileyLinkServiceData.rileyLinkAddress = null rileyLinkServiceData.rileyLinkName = null } - rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.BluetoothReady + rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothReady) } fun changeRileyLinkEncoding(encodingType: RileyLinkEncodingType?) { diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.kt b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.kt index 047467fd7f..39dcf629bb 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.kt +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.kt @@ -27,6 +27,7 @@ class RileyLinkServiceData @Inject constructor() { var tuneUpDone = false var rileyLinkError: RileyLinkError? = null var rileyLinkServiceState: RileyLinkServiceState = RileyLinkServiceState.NotStarted + private set var lastServiceStateChange = 0L private set @@ -62,11 +63,11 @@ class RileyLinkServiceData @Inject constructor() { } @Synchronized - fun setServiceState(newState: RileyLinkServiceState, errorCode: RileyLinkError) { + fun setServiceState(newState: RileyLinkServiceState, errorCode: RileyLinkError? = null) { rileyLinkServiceState = newState lastServiceStateChange = System.currentTimeMillis() rileyLinkError = errorCode - aapsLogger.info(LTag.PUMP, String.format(Locale.ENGLISH, "RileyLink State Changed: $newState - Error State: ${errorCode.name}")) + aapsLogger.info(LTag.PUMP, String.format(Locale.ENGLISH, "RileyLink State Changed: $newState - Error State: ${errorCode?.name}")) rileyLinkUtil.rileyLinkHistory.add(RLHistoryItem(rileyLinkServiceState, errorCode, targetDevice)) rxBus.send(EventRileyLinkDeviceStatusChange(targetDevice!!, newState, errorCode)) } diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/InitializePumpManagerTask.kt b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/InitializePumpManagerTask.kt index 935aff66bc..cc7f946d74 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/InitializePumpManagerTask.kt +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/tasks/InitializePumpManagerTask.kt @@ -39,10 +39,10 @@ class InitializePumpManagerTask(injector: HasAndroidInjector, private val contex val rileyLinkCommunicationManager = pumpDevice?.rileyLinkService?.deviceCommunicationManager if (activePlugin.activePump.manufacturer() === ManufacturerType.Medtronic) { if (lastGoodFrequency > 0.0 && rileyLinkCommunicationManager?.isValidFrequency(lastGoodFrequency) == true) { - rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.RileyLinkReady + rileyLinkServiceData.setServiceState(RileyLinkServiceState.RileyLinkReady) aapsLogger.info(LTag.PUMPBTCOMM, "Setting radio frequency to $lastGoodFrequency MHz") rileyLinkCommunicationManager.setRadioFrequencyForPump(lastGoodFrequency) - if (rileyLinkCommunicationManager.tryToConnectToDevice()) rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.PumpConnectorReady + if (rileyLinkCommunicationManager.tryToConnectToDevice()) rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorReady) else { rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.NoContactWithDevice) rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump, context) @@ -54,11 +54,11 @@ class InitializePumpManagerTask(injector: HasAndroidInjector, private val contex lastGoodFrequency = (lastGoodFrequency * 1000.0).roundToLong() / 1000.0 rileyLinkServiceData.lastGoodFrequency = lastGoodFrequency } - rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.RileyLinkReady + rileyLinkServiceData.setServiceState(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.rileyLinkServiceState = RileyLinkServiceState.PumpConnectorReady + rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorReady) } } } \ No newline at end of file