MDT: fix fragment state color

This commit is contained in:
Milos Kozak 2022-07-21 23:34:39 +02:00
parent ea8e3e4ace
commit 67374301ff
3 changed files with 14 additions and 16 deletions

View file

@ -7,6 +7,7 @@ import android.content.Intent
import dagger.android.DaggerService import dagger.android.DaggerService
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.pump.common.defs.PumpDeviceState import info.nightscout.androidaps.plugins.pump.common.defs.PumpDeviceState
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkCommunicationManager 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.ble.defs.RileyLinkEncodingType
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
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.AAPSLogger
import info.nightscout.shared.logging.LTag import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
@ -85,8 +84,6 @@ abstract class RileyLinkService : DaggerService() {
} }
abstract val deviceCommunicationManager: RileyLinkCommunicationManager<*> abstract val deviceCommunicationManager: RileyLinkCommunicationManager<*>
val rileyLinkServiceState: RileyLinkServiceState?
get() = rileyLinkServiceData.rileyLinkServiceState
// Here is where the wake-lock begins: // Here is where the wake-lock begins:
// We've received a service startCommand, we grab the lock. // We've received a service startCommand, we grab the lock.
@ -94,7 +91,7 @@ abstract class RileyLinkService : DaggerService() {
fun bluetoothInit(): Boolean { fun bluetoothInit(): Boolean {
aapsLogger.debug(LTag.PUMPBTCOMM, "bluetoothInit: attempting to get an adapter") aapsLogger.debug(LTag.PUMPBTCOMM, "bluetoothInit: attempting to get an adapter")
rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.BluetoothInitializing rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothInitializing)
if (bluetoothAdapter == null) { if (bluetoothAdapter == null) {
aapsLogger.error("Unable to obtain a BluetoothAdapter.") aapsLogger.error("Unable to obtain a BluetoothAdapter.")
rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.NoBluetoothAdapter) rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.NoBluetoothAdapter)
@ -103,7 +100,7 @@ abstract class RileyLinkService : DaggerService() {
aapsLogger.error("Bluetooth is not enabled.") aapsLogger.error("Bluetooth is not enabled.")
rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.BluetoothDisabled) rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothError, RileyLinkError.BluetoothDisabled)
} else { } else {
rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.BluetoothReady rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothReady)
return true return true
} }
} }
@ -112,7 +109,7 @@ abstract class RileyLinkService : DaggerService() {
// returns true if our Rileylink configuration changed // returns true if our Rileylink configuration changed
fun reconfigureRileyLink(deviceAddress: String): Boolean { fun reconfigureRileyLink(deviceAddress: String): Boolean {
rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.RileyLinkInitializing rileyLinkServiceData.setServiceState(RileyLinkServiceState.RileyLinkInitializing)
return if (rileyLinkBLE.isConnected) { return if (rileyLinkBLE.isConnected) {
if (deviceAddress == rileyLinkServiceData.rileyLinkAddress) { if (deviceAddress == rileyLinkServiceData.rileyLinkAddress) {
aapsLogger.info(LTag.PUMPBTCOMM, "No change to RL address. Not reconnecting.") 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. // FIXME: This needs to be run in a session so that is incorruptible, has a separate thread, etc.
fun doTuneUpDevice() { fun doTuneUpDevice() {
rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.TuneUpDevice rileyLinkServiceData.setServiceState(RileyLinkServiceState.TuneUpDevice)
setPumpDeviceState(PumpDeviceState.Sleeping) setPumpDeviceState(PumpDeviceState.Sleeping)
val lastGoodFrequency = rileyLinkServiceData.lastGoodFrequency ?: sp.getDouble(RileyLinkConst.Prefs.LastGoodDeviceFrequency, 0.0) val lastGoodFrequency = rileyLinkServiceData.lastGoodFrequency ?: sp.getDouble(RileyLinkConst.Prefs.LastGoodDeviceFrequency, 0.0)
val newFrequency = deviceCommunicationManager.tuneForDevice() val newFrequency = deviceCommunicationManager.tuneForDevice()
@ -157,7 +154,7 @@ abstract class RileyLinkService : DaggerService() {
rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.TuneUpOfDeviceFailed) rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.TuneUpOfDeviceFailed)
} else { } else {
deviceCommunicationManager.clearNotConnectedCount() deviceCommunicationManager.clearNotConnectedCount()
rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.PumpConnectorReady rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorReady)
} }
} }
@ -168,7 +165,7 @@ abstract class RileyLinkService : DaggerService() {
rileyLinkServiceData.rileyLinkAddress = null rileyLinkServiceData.rileyLinkAddress = null
rileyLinkServiceData.rileyLinkName = null rileyLinkServiceData.rileyLinkName = null
} }
rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.BluetoothReady rileyLinkServiceData.setServiceState(RileyLinkServiceState.BluetoothReady)
} }
fun changeRileyLinkEncoding(encodingType: RileyLinkEncodingType?) { fun changeRileyLinkEncoding(encodingType: RileyLinkEncodingType?) {

View file

@ -27,6 +27,7 @@ class RileyLinkServiceData @Inject constructor() {
var tuneUpDone = false var tuneUpDone = false
var rileyLinkError: RileyLinkError? = null var rileyLinkError: RileyLinkError? = null
var rileyLinkServiceState: RileyLinkServiceState = RileyLinkServiceState.NotStarted var rileyLinkServiceState: RileyLinkServiceState = RileyLinkServiceState.NotStarted
private set
var lastServiceStateChange = 0L var lastServiceStateChange = 0L
private set private set
@ -62,11 +63,11 @@ class RileyLinkServiceData @Inject constructor() {
} }
@Synchronized @Synchronized
fun setServiceState(newState: RileyLinkServiceState, errorCode: RileyLinkError) { fun setServiceState(newState: RileyLinkServiceState, errorCode: RileyLinkError? = null) {
rileyLinkServiceState = newState rileyLinkServiceState = newState
lastServiceStateChange = System.currentTimeMillis() lastServiceStateChange = System.currentTimeMillis()
rileyLinkError = errorCode 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)) rileyLinkUtil.rileyLinkHistory.add(RLHistoryItem(rileyLinkServiceState, errorCode, targetDevice))
rxBus.send(EventRileyLinkDeviceStatusChange(targetDevice!!, newState, errorCode)) rxBus.send(EventRileyLinkDeviceStatusChange(targetDevice!!, newState, errorCode))
} }

View file

@ -39,10 +39,10 @@ class InitializePumpManagerTask(injector: HasAndroidInjector, private val contex
val rileyLinkCommunicationManager = pumpDevice?.rileyLinkService?.deviceCommunicationManager val rileyLinkCommunicationManager = pumpDevice?.rileyLinkService?.deviceCommunicationManager
if (activePlugin.activePump.manufacturer() === ManufacturerType.Medtronic) { if (activePlugin.activePump.manufacturer() === ManufacturerType.Medtronic) {
if (lastGoodFrequency > 0.0 && rileyLinkCommunicationManager?.isValidFrequency(lastGoodFrequency) == true) { 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") aapsLogger.info(LTag.PUMPBTCOMM, "Setting radio frequency to $lastGoodFrequency MHz")
rileyLinkCommunicationManager.setRadioFrequencyForPump(lastGoodFrequency) rileyLinkCommunicationManager.setRadioFrequencyForPump(lastGoodFrequency)
if (rileyLinkCommunicationManager.tryToConnectToDevice()) rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.PumpConnectorReady if (rileyLinkCommunicationManager.tryToConnectToDevice()) rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorReady)
else { else {
rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.NoContactWithDevice) rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.NoContactWithDevice)
rileyLinkUtil.sendBroadcastMessage(RileyLinkConst.IPC.MSG_PUMP_tunePump, context) 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 lastGoodFrequency = (lastGoodFrequency * 1000.0).roundToLong() / 1000.0
rileyLinkServiceData.lastGoodFrequency = lastGoodFrequency rileyLinkServiceData.lastGoodFrequency = lastGoodFrequency
} }
rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.RileyLinkReady rileyLinkServiceData.setServiceState(RileyLinkServiceState.RileyLinkReady)
rileyLinkServiceData.rileyLinkTargetFrequency = RileyLinkTargetFrequency.Omnipod // TODO shouldn't be needed rileyLinkServiceData.rileyLinkTargetFrequency = RileyLinkTargetFrequency.Omnipod // TODO shouldn't be needed
aapsLogger.info(LTag.PUMPBTCOMM, "Setting radio frequency to $lastGoodFrequency MHz") aapsLogger.info(LTag.PUMPBTCOMM, "Setting radio frequency to $lastGoodFrequency MHz")
rileyLinkCommunicationManager?.setRadioFrequencyForPump(lastGoodFrequency) rileyLinkCommunicationManager?.setRadioFrequencyForPump(lastGoodFrequency)
rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.PumpConnectorReady rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorReady)
} }
} }
} }