From 082b36552064af52c7713c4a4e17916ff8bf8c93 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Sat, 17 Jun 2023 08:44:04 +0200 Subject: [PATCH] Added more info to overview --- .../nightscout/pump/medtrum/MedtrumPump.kt | 42 +++++++++++++++++-- .../medtrum/comm/packets/AuthorizePacket.kt | 16 ++++--- .../comm/packets/NotificationPacket.kt | 8 +++- .../pump/medtrum/services/MedtrumService.kt | 6 +-- .../ui/viewmodel/MedtrumOverviewViewModel.kt | 6 +-- .../res/layout/fragment_medtrum_overview.xml | 40 ++++++++++++++++++ pump/medtrum/src/main/res/values/strings.xml | 5 +++ .../comm/packets/AuthorizePacketTest.kt | 4 +- 8 files changed, 106 insertions(+), 21 deletions(-) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt index 3fa0b709c5..35b7ccb442 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPump.kt @@ -90,6 +90,15 @@ class MedtrumPump @Inject constructor( _reservoir.value = value } + var batteryVoltage_A = 0.0 // Not used in UI + private val _batteryVoltage_B = MutableStateFlow(0.0) + val batteryVoltage_BFlow: StateFlow = _batteryVoltage_B + var batteryVoltage_B: Double + get() = _batteryVoltage_B.value + set(value) { + _batteryVoltage_B.value = value + } + /** Stuff stored in SP */ private var _patchSessionToken = 0L var patchSessionToken: Long @@ -140,19 +149,41 @@ class MedtrumPump @Inject constructor( sp.putLong(R.string.key_last_connection, value) } + private var _deviceType: Int = 0 // As reported by pump + var deviceType: Int + get() = _deviceType + set(value) { + _deviceType = value + sp.putInt(R.string.key_device_type, value) + } + + private var _swVersion: String = "" // As reported by pump + var swVersion: String + get() = _swVersion + set(value) { + _swVersion = value + sp.putString(R.string.key_sw_version, value) + } + + private var _patchStartTime = 0L // Time in ms! + var patchStartTime: Long + get() = _patchStartTime + set(value) { + _patchStartTime = value + sp.putLong(R.string.key_patch_start_time, value) + } + private var _pumpSN = 0L val pumpSN: Long get() = _pumpSN - val pumpType: PumpType = PumpType.MEDTRUM_NANO // TODO, type based on pumpSN or pump activation/connection + val pumpType: PumpType = PumpType.MEDTRUM_NANO // TODO, type based on deviceType from pump var lastTimeReceivedFromPump = 0L // Time in ms! // TODO: Consider removing as is not used? var suspendTime = 0L // Time in ms! - var patchStartTime = 0L // Time in ms! // TODO: save in SP + var patchAge = 0L // Time in seconds?! // TODO: Not used - var batteryVoltage_A = 0.0 - var batteryVoltage_B = 0.0 var alarmFlags = 0 var alarmParameter = 0 @@ -198,6 +229,9 @@ class MedtrumPump @Inject constructor( _patchId = sp.getLong(R.string.key_patch_id, 0L) _syncedSequenceNumber = sp.getInt(R.string.key_synced_sequence_number, 0) _pumpState.value = MedtrumPumpState.fromByte(sp.getInt(R.string.key_pump_state, MedtrumPumpState.NONE.state.toInt()).toByte()) + _deviceType = sp.getInt(R.string.key_device_type, 0) + _swVersion = sp.getString(R.string.key_sw_version, "") + _patchStartTime = sp.getLong(R.string.key_patch_start_time, 0L) val encodedString = sp.getString(R.string.key_actual_basal_profile, "0") try { diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/AuthorizePacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/AuthorizePacket.kt index 4cf10a580c..fce1d8c869 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/AuthorizePacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/AuthorizePacket.kt @@ -6,15 +6,13 @@ import info.nightscout.pump.medtrum.comm.enums.CommandType.AUTH_REQ import info.nightscout.pump.medtrum.encryption.Crypt import info.nightscout.pump.medtrum.extension.toByteArray import info.nightscout.pump.medtrum.extension.toInt +import info.nightscout.rx.logging.LTag import javax.inject.Inject class AuthorizePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) { @Inject lateinit var medtrumPump: MedtrumPump - var deviceType: Int = 0 - var swVersion: String = "" - companion object { private const val RESP_DEVICE_TYPE_START = 7 @@ -41,10 +39,18 @@ class AuthorizePacket(injector: HasAndroidInjector) : MedtrumPacket(injector) { override fun handleResponse(data: ByteArray): Boolean { val success = super.handleResponse(data) if (success) { - deviceType = data.copyOfRange(RESP_DEVICE_TYPE_START, RESP_DEVICE_TYPE_END).toInt() - swVersion = "" + data.copyOfRange(RESP_VERSION_X_START, RESP_VERSION_X_END).toInt() + "." + data.copyOfRange(RESP_VERSION_Y_START, RESP_VERSION_Y_END).toInt() + "." + data.copyOfRange( + val deviceType = data.copyOfRange(RESP_DEVICE_TYPE_START, RESP_DEVICE_TYPE_END).toInt() + val swVersion = "" + data.copyOfRange(RESP_VERSION_X_START, RESP_VERSION_X_END).toInt() + "." + data.copyOfRange(RESP_VERSION_Y_START, RESP_VERSION_Y_END).toInt() + "." + data.copyOfRange( RESP_VERSION_Z_START, RESP_VERSION_Z_END ).toInt() + + if (medtrumPump.deviceType != deviceType) { + medtrumPump.deviceType = deviceType + } + if (medtrumPump.swVersion != swVersion) { + medtrumPump.swVersion = swVersion + } + aapsLogger.debug(LTag.PUMPCOMM, "GetDeviceTypeState: deviceType: ${deviceType}, swVersion: ${swVersion}") } return success } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt index c8f1a215fa..1072086ca9 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/NotificationPacket.kt @@ -188,8 +188,12 @@ class NotificationPacket(val injector: HasAndroidInjector) { if (fieldMask and MASK_START_TIME != 0) { aapsLogger.debug(LTag.PUMPCOMM, "Start time notification received") - medtrumPump.patchStartTime = MedtrumTimeUtil().convertPumpTimeToSystemTimeMillis(data.copyOfRange(offset, offset + 4).toLong()) - aapsLogger.debug(LTag.PUMPCOMM, "Patch start time: ${medtrumPump.patchStartTime}") + val patchStartTime = MedtrumTimeUtil().convertPumpTimeToSystemTimeMillis(data.copyOfRange(offset, offset + 4).toLong()) + if (medtrumPump.patchStartTime != patchStartTime) { + aapsLogger.debug(LTag.PUMPCOMM, "Patch start time changed from ${medtrumPump.patchStartTime} to $patchStartTime") + medtrumPump.patchStartTime = patchStartTime + } + aapsLogger.debug(LTag.PUMPCOMM, "Patch start time: ${patchStartTime}") offset += 4 } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt index 00647d05cc..39f84d208f 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt @@ -586,11 +586,7 @@ class MedtrumService : DaggerService(), BLECommCallback { if (mPacket?.handleResponse(data) == true) { // Succes! responseHandled = true - responseSuccess = true - // TODO Get pump version info - val deviceType = (mPacket as AuthorizePacket).deviceType - val swVersion = (mPacket as AuthorizePacket).swVersion - aapsLogger.debug(LTag.PUMPCOMM, "GetDeviceTypeState: deviceType: $deviceType swVersion: $swVersion") // TODO remove me later + responseSuccess = true toState(GetDeviceTypeState()) } else if (mPacket?.failed == true) { // Failure diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt index f2dfa87af9..d33de4aa71 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/ui/viewmodel/MedtrumOverviewViewModel.kt @@ -151,9 +151,9 @@ class MedtrumOverviewViewModel @Inject constructor( // TODO: Update these values // _activeAlarms.postValue(rh.gs(R.string.active_alarms, pump.activeAlarms)) - // _pumpType.postValue(rh.gs(R.string.pump_type, pump.pumpType)) - // _fwVersion.postValue(rh.gs(R.string.fw_version, pump.fwVersion)) - // _patchNo.postValue(rh.gs(R.string.patch_no, pump.patchNo)) + _pumpType.postValue(medtrumPump.deviceType.toString()) + _fwVersion.postValue(medtrumPump.swVersion) + _patchNo.postValue(medtrumPump.patchId.toString()) if (medtrumPump.desiredPatchExpiration) { val expiry = medtrumPump.patchStartTime + T.hours(84).msecs() diff --git a/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml b/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml index c9f6cd5be7..1c3fb68e39 100644 --- a/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml +++ b/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml @@ -276,6 +276,46 @@ android:textSize="14sp" /> + + + + + + + + + + last_connection medtrum_session_token patch_id + device_type + sw_version + patch_start_time actual_basal_profile current_sequence_number synced_sequence_number @@ -34,6 +37,8 @@ Active alarms Reservoir %.2f U + Battery + %.2f V Basal type Basal rate %.2f U/h diff --git a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/AuthorizePacketTest.kt b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/AuthorizePacketTest.kt index 7c07339220..697d439e25 100644 --- a/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/AuthorizePacketTest.kt +++ b/pump/medtrum/src/test/java/info/nightscout/pump/medtrum/comm/packets/AuthorizePacketTest.kt @@ -59,8 +59,8 @@ class AuthorizePacketTest : MedtrumTestBase() { val swString = "$swVerX.$swVerY.$swVerZ" assertTrue(result) assertFalse(packet.failed) - assertEquals(deviceType, packet.deviceType) - assertEquals(swString, packet.swVersion) + assertEquals(deviceType, medtrumPump.deviceType) + assertEquals(swString, medtrumPump.swVersion) } @Test fun handleResponseGivenResponseWhenMessageTooShortThenResultFalse() {