diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt index de293443a1..ca1cecc7b3 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/MedtrumPlugin.kt @@ -5,6 +5,7 @@ import android.content.Context import android.content.Intent import android.content.ServiceConnection import android.os.IBinder +import android.text.format.DateFormat import dagger.android.HasAndroidInjector import info.nightscout.core.ui.toast.ToastUtils import info.nightscout.core.utils.fabric.FabricPrivacy @@ -31,6 +32,7 @@ import info.nightscout.interfaces.pump.defs.PumpType import info.nightscout.interfaces.queue.CommandQueue import info.nightscout.interfaces.queue.CustomCommand import info.nightscout.interfaces.ui.UiInteraction +import info.nightscout.interfaces.utils.DecimalFormatter import info.nightscout.interfaces.utils.TimeChangeType import info.nightscout.pump.medtrum.comm.enums.MedtrumPumpState import info.nightscout.pump.medtrum.ui.MedtrumOverviewFragment @@ -222,7 +224,7 @@ import kotlin.math.round get() = medtrumPump.reservoir override val batteryLevel: Int - get() = 0 // TODO + get() = 0 // We cannot determine battery level (yet) @Synchronized override fun deliverTreatment(detailedBolusInfo: DetailedBolusInfo): PumpEnactResult { @@ -317,7 +319,43 @@ import kotlin.math.round } override fun getJSONStatus(profile: Profile, profileName: String, version: String): JSONObject { - return JSONObject() // TODO + val now = System.currentTimeMillis() + if (medtrumPump.lastConnection + 60 * 60 * 1000L < System.currentTimeMillis()) { + return JSONObject() + } + val pumpJson = JSONObject() + val status = JSONObject() + val extended = JSONObject() + try { + status.put( + "status", if (!isSuspended()) "normal" + else if (isInitialized() && isSuspended()) "suspended" + else "no active patch" + ) + status.put("timestamp", dateUtil.toISOString(medtrumPump.lastConnection)) + if (medtrumPump.lastBolusTime != 0L) { + extended.put("lastBolus", dateUtil.dateAndTimeString(medtrumPump.lastBolusTime)) + extended.put("lastBolusAmount", medtrumPump.lastBolusAmount) + } + val tb = pumpSync.expectedPumpState().temporaryBasal + if (tb != null) { + extended.put("TempBasalAbsoluteRate", tb.convertedToAbsolute(now, profile)) + extended.put("TempBasalStart", dateUtil.dateAndTimeString(tb.timestamp)) + extended.put("TempBasalRemaining", tb.plannedRemainingMinutes) + } + extended.put("BaseBasalRate", baseBasalRate) + try { + extended.put("ActiveProfile", profileName) + } catch (ignored: Exception) { + } + pumpJson.put("status", status) + pumpJson.put("extended", extended) + pumpJson.put("reservoir", medtrumPump.reservoir.toInt()) + pumpJson.put("clock", dateUtil.toISOString(now)) + } catch (e: JSONException) { + aapsLogger.error(LTag.PUMP, "Unhandled exception: $e") + } + return pumpJson } override fun manufacturer(): ManufacturerType { @@ -336,7 +374,20 @@ import kotlin.math.round get() = PumpDescription(medtrumPump.pumpType) override fun shortStatus(veryShort: Boolean): String { - return ""// TODO + var ret = "" + if (medtrumPump.lastConnection != 0L) { + val agoMillis = System.currentTimeMillis() - medtrumPump.lastConnection + val agoMin = (agoMillis / 60.0 / 1000.0).toInt() + ret += "LastConn: $agoMin minAgo\n" + } + if (medtrumPump.lastBolusTime != 0L) + ret += "LastBolus: ${DecimalFormatter.to2Decimal(medtrumPump.lastBolusAmount)}U @${DateFormat.format("HH:mm", medtrumPump.lastBolusTime)}\n" + + if (medtrumPump.tempBasalInProgress) + ret += "Temp: ${medtrumPump.temporaryBasalToString()}\n" + + ret += "Res: ${DecimalFormatter.to0Decimal(medtrumPump.reservoir)}U\n" + return ret } override val isFakingTempsByExtendedBoluses: Boolean = false @@ -375,7 +426,7 @@ import kotlin.math.round } override fun setUserOptions(): PumpEnactResult { - if (!isInitialized()) { + if (!isInitialized()) { val result = PumpEnactResult(injector).success(false) result.comment = "pump not initialized" return result 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 35b7ccb442..5110ee1e27 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 @@ -68,19 +68,13 @@ class MedtrumPump @Inject constructor( private var _lastBasalType: MutableStateFlow = MutableStateFlow(BasalType.NONE) val lastBasalTypeFlow: StateFlow = _lastBasalType - var lastBasalType: BasalType + val lastBasalType: BasalType get() = _lastBasalType.value - set(value) { - _lastBasalType.value = value - } private val _lastBasalRate = MutableStateFlow(0.0) val lastBasalRateFlow: StateFlow = _lastBasalRate - var lastBasalRate: Double + val lastBasalRate: Double get() = _lastBasalRate.value - set(value) { - _lastBasalRate.value = value - } private val _reservoir = MutableStateFlow(0.0) val reservoirFlow: StateFlow = _reservoir @@ -141,6 +135,22 @@ class MedtrumPump @Inject constructor( sp.putString(R.string.key_actual_basal_profile, encodedString ?: "") } + private var _lastBolusTime = 0L // Time in ms! + var lastBolusTime: Long + get() = _lastBolusTime + set(value) { + _lastBolusTime = value + sp.putLong(R.string.key_last_bolus_time, value) + } + + private var _lastBolusAmount = 0.0 + var lastBolusAmount: Double + get() = _lastBolusAmount + set(value) { + _lastBolusAmount = value + sp.putDouble(R.string.key_last_bolus_amount, value) + } + private var _lastConnection = 0L // Time in ms! var lastConnection: Long get() = _lastConnection @@ -181,7 +191,6 @@ class MedtrumPump @Inject constructor( var lastTimeReceivedFromPump = 0L // Time in ms! // TODO: Consider removing as is not used? var suspendTime = 0L // Time in ms! - var patchAge = 0L // Time in seconds?! // TODO: Not used @@ -196,11 +205,18 @@ class MedtrumPump @Inject constructor( var bolusStopForced = false // bolus forced to stop by user var bolusDone = false // success end - // Last basal status update - // TODO maybe make basal parameters private? So we are forced to update trough handleBasalStatusUpdate - var lastBasalSequence = 0 - var lastBasalPatchId = 0L - var lastBasalStartTime = 0L + // Last basal status update (from pump) + private var _lastBasalSequence = 0 + val lastBasalSequence: Int + get() = _lastBasalSequence + + private var _lastBasalPatchId = 0L + val lastBasalPatchId: Long + get() = _lastBasalPatchId + + private var _lastBasalStartTime = 0L + val lastBasalStartTime: Long + get() = _lastBasalStartTime val baseBasalRate: Double get() = getHourlyBasalFromMedtrumProfileArray(actualBasalProfile, dateUtil.now()) @@ -225,6 +241,8 @@ class MedtrumPump @Inject constructor( // Load stuff from SP _patchSessionToken = sp.getLong(R.string.key_session_token, 0L) _lastConnection = sp.getLong(R.string.key_last_connection, 0L) + _lastBolusTime = sp.getLong(R.string.key_last_bolus_time, 0L) + _lastBolusAmount = sp.getDouble(R.string.key_last_bolus_amount, 0.0) _currentSequenceNumber = sp.getInt(R.string.key_current_sequence_number, 0) _patchId = sp.getLong(R.string.key_patch_id, 0L) _syncedSequenceNumber = sp.getInt(R.string.key_synced_sequence_number, 0) @@ -243,7 +261,7 @@ class MedtrumPump @Inject constructor( fun loadUserSettingsFromSP() { desiredPatchExpiration = sp.getBoolean(info.nightscout.pump.medtrum.R.string.key_patch_expiration, false) - val alarmSettingCode = sp.getString(info.nightscout.pump.medtrum.R.string.key_alarm_setting, AlarmSetting.LIGHT_VIBRATE_AND_BEEP.code.toString())?.toByte() + val alarmSettingCode = sp.getString(info.nightscout.pump.medtrum.R.string.key_alarm_setting, AlarmSetting.LIGHT_VIBRATE_AND_BEEP.code.toString()).toByte() desiredAlarmSetting = AlarmSetting.values().firstOrNull { it.code == alarmSettingCode } ?: AlarmSetting.LIGHT_VIBRATE_AND_BEEP desiredHourlyMaxInsulin = sp.getInt(info.nightscout.pump.medtrum.R.string.key_hourly_max_insulin, 40) desiredDailyMaxInsulin = sp.getInt(info.nightscout.pump.medtrum.R.string.key_daily_max_insulin, 180) @@ -370,17 +388,17 @@ class MedtrumPump @Inject constructor( } // Update medtrum pump state - lastBasalType = basalType - lastBasalRate = basalRate - lastBasalSequence = basalSequence + _lastBasalType.value = basalType + _lastBasalRate.value = basalRate + _lastBasalSequence = basalSequence if (basalSequence > currentSequenceNumber) { currentSequenceNumber = basalSequence } - lastBasalPatchId = basalPatchId + _lastBasalPatchId = basalPatchId if (basalPatchId != patchId) { aapsLogger.error(LTag.PUMP, "handleBasalStatusUpdate: WTF? PatchId in status update does not match current patchId!") } - lastBasalStartTime = basalStartTime + _lastBasalStartTime = basalStartTime } fun handleStopStatusUpdate(stopSequence: Int, stopPatchId: Long) { @@ -418,4 +436,8 @@ class MedtrumPump @Inject constructor( "handleBasalStatusUpdate: ${if (newRecord) "**NEW** " else ""}EVENT TEMP_START (FAKE)" ) } + fun temporaryBasalToString(): String { + if (!tempBasalInProgress) return "" + return tempBasalAbsoluteRate.toString() + "U/h" + } } diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt index 600f2223cf..265e7b3ae9 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/comm/packets/GetRecordPacket.kt @@ -125,7 +125,12 @@ class GetRecordPacket(injector: HasAndroidInjector, private val recordIndex: Int // detailedInfo can be from another similar record. Reinsert detailedBolusInfoStorage.add(detailedBolusInfo) } + if (bolusStartTime > medtrumPump.lastBolusTime) { + medtrumPump.lastBolusTime = bolusStartTime + medtrumPump.lastBolusAmount = bolusNormalDelivered + } } else { + // TODO: at least record the bolus aapsLogger.error( LTag.PUMPCOMM, "from record: EVENT BOLUS ${dateUtil.dateAndTimeString(bolusStartTime)} ($bolusStartTime) " + "Bolus type: $bolusType not supported" 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 d33de4aa71..85a2db7fba 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 @@ -53,6 +53,10 @@ class MedtrumOverviewViewModel @Inject constructor( val lastConnectionMinAgo: LiveData get() = _lastConnectionMinAgo + private val _lastBolus = SingleLiveEvent() + val lastBolus: LiveData + get() = _lastBolus + private val _activeAlarms = SingleLiveEvent() val activeAlarms: LiveData get() = _activeAlarms @@ -64,7 +68,7 @@ class MedtrumOverviewViewModel @Inject constructor( private val _fwVersion = SingleLiveEvent() val fwVersion: LiveData get() = _fwVersion - + private val _patchNo = SingleLiveEvent() val patchNo: LiveData get() = _patchNo @@ -148,6 +152,21 @@ class MedtrumOverviewViewModel @Inject constructor( val agoMilliseconds = System.currentTimeMillis() - medtrumPump.lastConnection val agoMinutes = agoMilliseconds / 1000 / 60 _lastConnectionMinAgo.postValue(rh.gs(info.nightscout.shared.R.string.minago, agoMinutes)) + if (medtrumPump.lastBolusTime != 0L) { + val agoMilliseconds = System.currentTimeMillis() - medtrumPump.lastBolusTime + val agoHours = agoMilliseconds.toDouble() / 60.0 / 60.0 / 1000.0 + if (agoHours < 6) + // max 6h back + _lastBolus.postValue( + dateUtil.timeString(medtrumPump.lastBolusTime) + " " + dateUtil.sinceString(medtrumPump.lastBolusTime, rh) + " " + rh.gs( + info.nightscout.interfaces.R.string + .format_insulin_units, medtrumPump + .lastBolusAmount + ) + ) + else + _lastBolus.postValue("") + } // TODO: Update these values // _activeAlarms.postValue(rh.gs(R.string.active_alarms, pump.activeAlarms)) 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 1c3fb68e39..9634d4fc49 100644 --- a/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml +++ b/pump/medtrum/src/main/res/layout/fragment_medtrum_overview.xml @@ -34,6 +34,7 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - medtrumpump_settings pump_state last_connection + last_bolus_time + last_bolus_amount medtrum_session_token patch_id device_type @@ -35,9 +37,7 @@ Last connected Pump state Active alarms - Reservoir %.2f U - Battery %.2f V Basal type Basal rate diff --git a/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml b/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml index c87d2384d2..9195ccdf9f 100644 --- a/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml +++ b/pump/medtrum/src/main/res/xml/pref_medtrum_pump.xml @@ -9,7 +9,7 @@