This commit is contained in:
Milos Kozak 2023-08-17 15:51:31 +02:00
commit f2d276a7e7
4 changed files with 52 additions and 34 deletions

View file

@ -49,14 +49,12 @@ class SynchronizePacket(injector: HasAndroidInjector) : MedtrumPacket(injector)
aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: fieldMask: $fieldMask") aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: fieldMask: $fieldMask")
} }
// Remove bolus fields from fieldMask if fields are present (we sync bolus trough other commands) // Remove extended bolus field from fieldMask if field is present (extended bolus is not supported)
if (fieldMask and MASK_SUSPEND != 0) { if (fieldMask and MASK_SUSPEND != 0) {
offset += 4 // If field is present, skip 4 bytes offset += 4 // If field is present, skip 4 bytes
} }
if (fieldMask and MASK_NORMAL_BOLUS != 0) { if (fieldMask and MASK_NORMAL_BOLUS != 0) {
aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: Normal bolus present removing from fieldMask") offset += 3 // If field is present, skip 3 bytes
fieldMask = fieldMask and MASK_NORMAL_BOLUS.inv()
syncData = syncData.copyOfRange(0, offset) + syncData.copyOfRange(offset + 3, syncData.size)
} }
if (fieldMask and MASK_EXTENDED_BOLUS != 0) { if (fieldMask and MASK_EXTENDED_BOLUS != 0) {
aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: Extended bolus present removing from fieldMask") aapsLogger.debug(LTag.PUMPCOMM, "SynchronizePacket: Extended bolus present removing from fieldMask")

View file

@ -164,8 +164,19 @@ class MedtrumService : DaggerService(), BLECommCallback {
if (currentState is IdleState) { if (currentState is IdleState) {
medtrumPump.connectionState = ConnectionState.CONNECTING medtrumPump.connectionState = ConnectionState.CONNECTING
return bleComm.connect(from, medtrumPump.pumpSN) return bleComm.connect(from, medtrumPump.pumpSN)
} else if (currentState is ReadyState) {
aapsLogger.error(LTag.PUMPCOMM, "Connect attempt when in ReadyState from: $from")
if (isConnected) {
aapsLogger.debug(LTag.PUMP, "connect: already connected")
return true
} else { } else {
aapsLogger.error(LTag.PUMPCOMM, "Connect attempt when in non Idle state from: $from") aapsLogger.debug(LTag.PUMP, "connect: not connected, resetting state and trying to connect")
toState(IdleState())
medtrumPump.connectionState = ConnectionState.CONNECTING
return bleComm.connect(from, medtrumPump.pumpSN)
}
} else {
aapsLogger.error(LTag.PUMPCOMM, "Connect attempt when in state: $currentState from: $from")
return false return false
} }
} }
@ -287,20 +298,22 @@ class MedtrumService : DaggerService(), BLECommCallback {
} }
fun setBolus(detailedBolusInfo: DetailedBolusInfo, t: EventOverviewBolusProgress.Treatment): Boolean { fun setBolus(detailedBolusInfo: DetailedBolusInfo, t: EventOverviewBolusProgress.Treatment): Boolean {
if (!isConnected) return false if (!isConnected) {
if (BolusProgressData.stopPressed) return false aapsLogger.warn(LTag.PUMPCOMM, "Pump not connected, not setting bolus")
return false
}
if (BolusProgressData.stopPressed) {
aapsLogger.warn(LTag.PUMPCOMM, "Bolus stop pressed, not setting bolus")
return false
}
if (!medtrumPump.bolusDone) {
aapsLogger.warn(LTag.PUMPCOMM, "Bolus already in progress, not setting new one")
return false
}
val insulin = detailedBolusInfo.insulin val insulin = detailedBolusInfo.insulin
val bolusStart = System.currentTimeMillis()
medtrumPump.bolusDone = false
medtrumPump.bolusingTreatment = t
medtrumPump.bolusAmountToBeDelivered = insulin
medtrumPump.bolusStopped = false
medtrumPump.bolusProgressLastTimeStamp = bolusStart
if (insulin > 0) { if (insulin > 0) {
val result = sendPacketAndGetResponse(SetBolusPacket(injector, insulin)) if (!sendPacketAndGetResponse(SetBolusPacket(injector, insulin))) {
if (!result) {
aapsLogger.error(LTag.PUMPCOMM, "Failed to set bolus") aapsLogger.error(LTag.PUMPCOMM, "Failed to set bolus")
commandQueue.loadEvents(null) // make sure if anything is delivered (which is highly unlikely at this point) we get it commandQueue.loadEvents(null) // make sure if anything is delivered (which is highly unlikely at this point) we get it
t.insulin = 0.0 t.insulin = 0.0
@ -312,6 +325,13 @@ class MedtrumService : DaggerService(), BLECommCallback {
return false return false
} }
val bolusStart = System.currentTimeMillis()
medtrumPump.bolusDone = false
medtrumPump.bolusingTreatment = t
medtrumPump.bolusAmountToBeDelivered = insulin
medtrumPump.bolusStopped = false
medtrumPump.bolusProgressLastTimeStamp = bolusStart
detailedBolusInfo.timestamp = bolusStart // Make sure the timestamp is set to the start of the bolus detailedBolusInfo.timestamp = bolusStart // Make sure the timestamp is set to the start of the bolus
detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history detailedBolusInfoStorage.add(detailedBolusInfo) // will be picked up on reading history
// Sync the initial bolus // Sync the initial bolus
@ -641,7 +661,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
result = currentState.waitForResponse(timeout) result = currentState.waitForResponse(timeout)
SystemClock.sleep(100) SystemClock.sleep(100)
} else { } else {
aapsLogger.error(LTag.PUMPCOMM, "Send packet attempt when in non Ready state") aapsLogger.error(LTag.PUMPCOMM, "Send packet attempt when in state: $currentState")
} }
return result return result
} }

View file

@ -103,7 +103,7 @@ class MedtrumOverviewViewModel @Inject constructor(
ConnectionState.DISCONNECTING -> { ConnectionState.DISCONNECTING -> {
_bleStatus.postValue("{fa-bluetooth-b spin}") _bleStatus.postValue("{fa-bluetooth-b spin}")
_canDoRefresh.postValue(false) _canDoRefresh.postValue(true)
} }
} }
updateGUI() updateGUI()