RileyLinkServiceData -> kt

This commit is contained in:
Milos Kozak 2022-07-01 00:33:19 +02:00
parent 23bdb57ed3
commit 9e29cbc2fe
9 changed files with 91 additions and 132 deletions

View file

@ -188,7 +188,7 @@ class MedtronicFragment : DaggerFragment() {
@Synchronized @Synchronized
private fun setDeviceStatus() { private fun setDeviceStatus() {
val resourceId = rileyLinkServiceData.rileyLinkServiceState.resourceId val resourceId = rileyLinkServiceData.rileyLinkServiceState.resourceId
val rileyLinkError = medtronicPumpPlugin.rileyLinkService?.error val rileyLinkError =rileyLinkServiceData.rileyLinkError
binding.rlStatus.text = binding.rlStatus.text =
when { when {
rileyLinkServiceData.rileyLinkServiceState == RileyLinkServiceState.NotStarted -> rh.gs(resourceId) rileyLinkServiceData.rileyLinkServiceState == RileyLinkServiceState.NotStarted -> rh.gs(resourceId)

View file

@ -375,7 +375,7 @@ public class OmnipodErosPumpPlugin extends PumpPluginBase implements Pump, Riley
} }
public boolean isRileyLinkReady() { public boolean isRileyLinkReady() {
return rileyLinkServiceData.rileyLinkServiceState.isReady(); return rileyLinkServiceData.getRileyLinkServiceState().isReady();
} }
private void handleCancelledTbr() { private void handleCancelledTbr() {
@ -1014,7 +1014,7 @@ public class OmnipodErosPumpPlugin extends PumpPluginBase implements Pump, Riley
// - RileyLink has been connecting for over RILEY_LINK_CONNECT_TIMEOUT // - RileyLink has been connecting for over RILEY_LINK_CONNECT_TIMEOUT
return (podStateManager.getLastFailedCommunication() != null && podStateManager.getLastSuccessfulCommunication().isBefore(podStateManager.getLastFailedCommunication())) || return (podStateManager.getLastFailedCommunication() != null && podStateManager.getLastSuccessfulCommunication().isBefore(podStateManager.getLastFailedCommunication())) ||
podStateManager.isSuspended() || podStateManager.isSuspended() ||
rileyLinkServiceData.rileyLinkServiceState.isError() || rileyLinkServiceData.getRileyLinkServiceState().isError() ||
// The below clause is a hack for working around the RL service state forever staying in connecting state on startup if the RL is switched off / unreachable // The below clause is a hack for working around the RL service state forever staying in connecting state on startup if the RL is switched off / unreachable
(rileyLinkServiceData.getRileyLinkServiceState().isConnecting() && rileyLinkServiceData.getLastServiceStateChange() + RILEY_LINK_CONNECT_TIMEOUT_MILLIS < currentTimeMillis); (rileyLinkServiceData.getRileyLinkServiceState().isConnecting() && rileyLinkServiceData.getLastServiceStateChange() + RILEY_LINK_CONNECT_TIMEOUT_MILLIS < currentTimeMillis);
} }

View file

@ -97,10 +97,8 @@ public class RileyLinkOmnipodService extends RileyLinkService {
/* private functions */ /* private functions */
// PumpInterface - REMOVE
public boolean isInitialized() { public boolean isInitialized() {
return rileyLinkServiceData.rileyLinkServiceState.isReady(); return rileyLinkServiceData.getRileyLinkServiceState().isReady();
} }
@Override @Override

View file

@ -72,7 +72,7 @@ class RileyLinkStatusGeneralFragment : DaggerFragment() {
} }
val rileyLinkPumpDevice = activePlugin.activePump as RileyLinkPumpDevice val rileyLinkPumpDevice = activePlugin.activePump as RileyLinkPumpDevice
val rileyLinkPumpInfo = rileyLinkPumpDevice.pumpInfo val rileyLinkPumpInfo = rileyLinkPumpDevice.pumpInfo
binding.deviceType.setText(targetDevice.resourceId) targetDevice?.resourceId?.let { binding.deviceType.setText(it) }
if (targetDevice == RileyLinkTargetDevice.MedtronicPump) { if (targetDevice == RileyLinkTargetDevice.MedtronicPump) {
binding.connectedDeviceDetails.visibility = View.VISIBLE binding.connectedDeviceDetails.visibility = View.VISIBLE
binding.configuredDeviceModel.text = activePlugin.activePump.pumpDescription.pumpType.description binding.configuredDeviceModel.text = activePlugin.activePump.pumpDescription.pumpType.description

View file

@ -153,7 +153,7 @@ class RileyLinkBroadcastReceiver : DaggerBroadcastReceiver() {
private fun processTuneUpBroadcasts(action: String): Boolean = private fun processTuneUpBroadcasts(action: String): Boolean =
if (broadcastIdentifiers["TuneUp"]?.contains(action) == true) { if (broadcastIdentifiers["TuneUp"]?.contains(action) == true) {
if (rileyLinkService?.rileyLinkTargetDevice?.isTuneUpEnabled == true) serviceTaskExecutor.startTask(WakeAndTuneTask(injector)) if (rileyLinkServiceData.targetDevice?.isTuneUpEnabled == true) serviceTaskExecutor.startTask(WakeAndTuneTask(injector))
true true
} else false } else false
} }

View file

@ -94,7 +94,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.setRileyLinkServiceState(RileyLinkServiceState.BluetoothInitializing) rileyLinkServiceData.rileyLinkServiceState = 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 +103,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.setRileyLinkServiceState(RileyLinkServiceState.BluetoothReady) rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.BluetoothReady
return true return true
} }
} }
@ -112,7 +112,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.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkInitializing) rileyLinkServiceData.rileyLinkServiceState = 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.")
@ -128,9 +128,9 @@ abstract class RileyLinkService : DaggerService() {
} }
} else { } else {
aapsLogger.debug(LTag.PUMPBTCOMM, "Using RL $deviceAddress") aapsLogger.debug(LTag.PUMPBTCOMM, "Using RL $deviceAddress")
if (rileyLinkServiceData.getRileyLinkServiceState() == RileyLinkServiceState.NotStarted) { if (rileyLinkServiceData.rileyLinkServiceState == RileyLinkServiceState.NotStarted) {
if (!bluetoothInit()) { if (!bluetoothInit()) {
aapsLogger.error("RileyLink can't get activated, Bluetooth is not functioning correctly. ${error?.name ?: "Unknown error (null)"}") aapsLogger.error("RileyLink can't get activated, Bluetooth is not functioning correctly. ${rileyLinkServiceData.rileyLinkError?.name ?: "Unknown error (null)"}")
return false return false
} }
} }
@ -141,7 +141,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.setRileyLinkServiceState(RileyLinkServiceState.TuneUpDevice) rileyLinkServiceData.rileyLinkServiceState = 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 +157,7 @@ abstract class RileyLinkService : DaggerService() {
rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.TuneUpOfDeviceFailed) rileyLinkServiceData.setServiceState(RileyLinkServiceState.PumpConnectorError, RileyLinkError.TuneUpOfDeviceFailed)
} else { } else {
deviceCommunicationManager.clearNotConnectedCount() deviceCommunicationManager.clearNotConnectedCount()
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady) rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.PumpConnectorReady
} }
} }
@ -168,22 +168,13 @@ abstract class RileyLinkService : DaggerService() {
rileyLinkServiceData.rileyLinkAddress = null rileyLinkServiceData.rileyLinkAddress = null
rileyLinkServiceData.rileyLinkName = null rileyLinkServiceData.rileyLinkName = null
} }
rileyLinkServiceData.setRileyLinkServiceState(RileyLinkServiceState.BluetoothReady) rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.BluetoothReady
} }
/**
* Get Target Device for Service
*/
val rileyLinkTargetDevice: RileyLinkTargetDevice
get() = rileyLinkServiceData.targetDevice
fun changeRileyLinkEncoding(encodingType: RileyLinkEncodingType?) { fun changeRileyLinkEncoding(encodingType: RileyLinkEncodingType?) {
rfSpy.setRileyLinkEncoding(encodingType) rfSpy.setRileyLinkEncoding(encodingType)
} }
val error: RileyLinkError?
get() = rileyLinkServiceData.rileyLinkError
fun verifyConfiguration(): Boolean { fun verifyConfiguration(): Boolean {
return verifyConfiguration(false) return verifyConfiguration(false)
} }

View file

@ -1,103 +0,0 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service;
import java.util.Locale;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.pump.common.events.EventRileyLinkDeviceStatusChange;
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.RileyLinkTargetFrequency;
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.data.RLHistoryItem;
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.shared.logging.AAPSLogger;
import info.nightscout.shared.logging.LTag;
/**
* Created by andy on 16/05/2018.
*/
// FIXME encapsulation
@Singleton
public class RileyLinkServiceData {
@Inject AAPSLogger aapsLogger;
@Inject RileyLinkUtil rileyLinkUtil;
@Inject RxBus rxBus;
boolean tuneUpDone = false;
@Nullable public RileyLinkError rileyLinkError;
public RileyLinkServiceState rileyLinkServiceState = RileyLinkServiceState.NotStarted;
private long lastServiceStateChange = 0L;
@Nullable public RileyLinkFirmwareVersion firmwareVersion; // here we have "compatibility level" version
@Nullable public RileyLinkTargetFrequency rileyLinkTargetFrequency;
@Nullable public String rileyLinkAddress;
@Nullable public String rileyLinkName;
@Nullable public Integer batteryLevel;
public boolean showBatteryLevel = false;
long lastTuneUpTime = 0L;
@Nullable public Double lastGoodFrequency;
// bt version
@Nullable public String versionBLE113;
// radio version
@Nullable public String versionCC110;
// orangeLink
public boolean isOrange = false;
@Nullable public String versionOrangeFirmware;
@Nullable public String versionOrangeHardware;
public RileyLinkTargetDevice targetDevice;
// Medtronic Pump
public String pumpID;
public byte[] pumpIDBytes;
@Inject
public RileyLinkServiceData() {
}
public void setPumpID(String pumpId, byte[] pumpIdBytes) {
this.pumpID = pumpId;
this.pumpIDBytes = pumpIdBytes;
}
public void setRileyLinkServiceState(RileyLinkServiceState newState) {
setServiceState(newState, null);
}
public RileyLinkServiceState getRileyLinkServiceState() {
return workWithServiceState(null, null, false);
}
public void setServiceState(RileyLinkServiceState newState, RileyLinkError errorCode) {
workWithServiceState(newState, errorCode, true);
}
public long getLastServiceStateChange() {
return lastServiceStateChange;
}
private synchronized RileyLinkServiceState workWithServiceState(RileyLinkServiceState newState, RileyLinkError errorCode, boolean set) {
if (set) {
rileyLinkServiceState = newState;
lastServiceStateChange = System.currentTimeMillis();
this.rileyLinkError = errorCode;
aapsLogger.info(LTag.PUMP, String.format(Locale.ENGLISH, "RileyLink State Changed: %s %s", newState, errorCode == null ? "" : " - Error State: " + errorCode.name()));
rileyLinkUtil.getRileyLinkHistory().add(new RLHistoryItem(rileyLinkServiceState, errorCode, targetDevice));
rxBus.send(new EventRileyLinkDeviceStatusChange(targetDevice, newState, errorCode));
return null;
} else {
return rileyLinkServiceState;
}
}
}

View file

@ -0,0 +1,73 @@
package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.pump.common.events.EventRileyLinkDeviceStatusChange
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.RileyLinkTargetFrequency
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.data.RLHistoryItem
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.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
/**
* Created by andy on 16/05/2018.
*/
@Singleton
class RileyLinkServiceData @Inject constructor() {
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var rileyLinkUtil: RileyLinkUtil
@Inject lateinit var rxBus: RxBus
var tuneUpDone = false
var rileyLinkError: RileyLinkError? = null
var rileyLinkServiceState: RileyLinkServiceState = RileyLinkServiceState.NotStarted
var lastServiceStateChange = 0L
private set
// here we have "compatibility level" version
@JvmField var firmwareVersion: RileyLinkFirmwareVersion? = null
@JvmField var rileyLinkTargetFrequency: RileyLinkTargetFrequency? = null
@JvmField var rileyLinkAddress: String? = null
@JvmField var rileyLinkName: String? = null
@JvmField var batteryLevel: Int? = null
var showBatteryLevel = false
var lastTuneUpTime = 0L
var lastGoodFrequency: Double? = null
// bt version
var versionBLE113: String? = null
// radio version
@JvmField var versionCC110: String? = null
// orangeLink
var isOrange = false
var versionOrangeFirmware: String? = null
var versionOrangeHardware: String? = null
@JvmField var targetDevice: RileyLinkTargetDevice? = null
// Medtronic Pump
var pumpID: String? = null
var pumpIDBytes: ByteArray = byteArrayOf(0, 0, 0)
fun setPumpID(pumpId: String?, pumpIdBytes: ByteArray) {
pumpID = pumpId
pumpIDBytes = pumpIdBytes
}
@Synchronized
fun setServiceState(newState: RileyLinkServiceState, errorCode: RileyLinkError) {
rileyLinkServiceState = newState
lastServiceStateChange = System.currentTimeMillis()
rileyLinkError = errorCode
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))
}
}

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.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkReady) rileyLinkServiceData.rileyLinkServiceState = 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.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady) if (rileyLinkCommunicationManager.tryToConnectToDevice()) rileyLinkServiceData.rileyLinkServiceState = 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.setRileyLinkServiceState(RileyLinkServiceState.RileyLinkReady) rileyLinkServiceData.rileyLinkServiceState = 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.setRileyLinkServiceState(RileyLinkServiceState.PumpConnectorReady) rileyLinkServiceData.rileyLinkServiceState = RileyLinkServiceState.PumpConnectorReady
} }
} }
} }