DanaRS: improve history sync

This commit is contained in:
Milos Kozak 2021-11-24 21:31:23 +01:00
parent 6c990a4423
commit 0dbfa6182f
3 changed files with 35 additions and 37 deletions

View file

@ -8,6 +8,7 @@ import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.events.EventPumpStatusChanged import info.nightscout.androidaps.events.EventPumpStatusChanged
import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.interfaces.CommandQueue
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
@ -18,7 +19,7 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP import info.nightscout.androidaps.utils.sharedPreferences.SP
class QueueThread internal constructor( class QueueThread internal constructor(
private val queue: CommandQueueImplementation, private val queue: CommandQueue,
context: Context, context: Context,
private val aapsLogger: AAPSLogger, private val aapsLogger: AAPSLogger,
private val rxBus: RxBus, private val rxBus: RxBus,

View file

@ -6,12 +6,17 @@ import info.nightscout.androidaps.dana.DanaPump
import info.nightscout.androidaps.danars.encryption.BleEncryption import info.nightscout.androidaps.danars.encryption.BleEncryption
import javax.inject.Inject import javax.inject.Inject
@Suppress("MemberVisibilityCanBePrivate")
class DanaRSPacketGeneralInitialScreenInformation( class DanaRSPacketGeneralInitialScreenInformation(
injector: HasAndroidInjector injector: HasAndroidInjector
) : DanaRSPacket(injector) { ) : DanaRSPacket(injector) {
@Inject lateinit var danaPump: DanaPump @Inject lateinit var danaPump: DanaPump
var isTempBasalInProgress = false
var isExtendedInProgress = false
var isDualBolusInProgress = false
init { init {
opCode = BleEncryption.DANAR_PACKET__OPCODE_REVIEW__INITIAL_SCREEN_INFORMATION opCode = BleEncryption.DANAR_PACKET__OPCODE_REVIEW__INITIAL_SCREEN_INFORMATION
aapsLogger.debug(LTag.PUMPCOMM, "New message") aapsLogger.debug(LTag.PUMPCOMM, "New message")
@ -22,42 +27,22 @@ class DanaRSPacketGeneralInitialScreenInformation(
failed = true failed = true
return return
} else failed = false } else failed = false
var dataIndex = DATA_START val status = intFromBuff(data, 0, 1)
var dataSize = 1
val status = byteArrayToInt(getBytes(data, dataIndex, dataSize))
danaPump.pumpSuspended = status and 0x01 == 0x01 danaPump.pumpSuspended = status and 0x01 == 0x01
val isTempBasalInProgress = status and 0x10 == 0x10 isTempBasalInProgress = status and 0x10 == 0x10
val isExtendedInProgress = status and 0x04 == 0x04 isExtendedInProgress = status and 0x04 == 0x04
val isDualBolusInProgress = status and 0x08 == 0x08 isDualBolusInProgress = status and 0x08 == 0x08
dataIndex += dataSize danaPump.dailyTotalUnits = intFromBuff(data, 1, 2) / 100.0
dataSize = 2 danaPump.maxDailyTotalUnits = intFromBuff(data, 3, 2) / 100
danaPump.dailyTotalUnits = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0 danaPump.reservoirRemainingUnits = intFromBuff(data, 5, 2) / 100.0
dataIndex += dataSize danaPump.currentBasal = intFromBuff(data, 7, 2) / 100.0
dataSize = 2 val tempBasalPercent = intFromBuff(data, 9, 1)
danaPump.maxDailyTotalUnits = (byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0).toInt() danaPump.batteryRemaining = intFromBuff(data, 10, 1)
dataIndex += dataSize val extendedBolusAbsoluteRate = intFromBuff(data, 11, 2) / 100.0
dataSize = 2 danaPump.iob = intFromBuff(data, 13, 2) / 100.0
danaPump.reservoirRemainingUnits = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0
dataIndex += dataSize
dataSize = 2
danaPump.currentBasal = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0
dataIndex += dataSize
dataSize = 1
val tempBasalPercent = byteArrayToInt(getBytes(data, dataIndex, dataSize))
dataIndex += dataSize
dataSize = 1
danaPump.batteryRemaining = byteArrayToInt(getBytes(data, dataIndex, dataSize))
dataIndex += dataSize
dataSize = 2
val extendedBolusAbsoluteRate = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0
dataIndex += dataSize
dataSize = 2
danaPump.iob = byteArrayToInt(getBytes(data, dataIndex, dataSize)) / 100.0
if (data.size >= 18) { if (data.size >= 18) {
//protocol 10+ //protocol 10+
dataIndex += dataSize danaPump.errorState = DanaPump.ErrorState[intFromBuff(data, 15, 1)]
dataSize = 1
danaPump.errorState = DanaPump.ErrorState[byteArrayToInt(getBytes(data, dataIndex, dataSize))]
?: DanaPump.ErrorState.NONE ?: DanaPump.ErrorState.NONE
aapsLogger.debug(LTag.PUMPCOMM, "ErrorState: " + danaPump.errorState.name) aapsLogger.debug(LTag.PUMPCOMM, "ErrorState: " + danaPump.errorState.name)
} }

View file

@ -338,7 +338,9 @@ class DanaRSService : DaggerService() {
fun tempBasal(percent: Int, durationInHours: Int): Boolean { fun tempBasal(percent: Int, durationInHours: Int): Boolean {
if (!isConnected) return false if (!isConnected) return false
if (danaPump.isTempBasalInProgress) { val status = DanaRSPacketGeneralInitialScreenInformation(injector)
sendMessage(status)
if (status.isTempBasalInProgress) {
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.stoppingtempbasal))) rxBus.send(EventPumpStatusChanged(rh.gs(R.string.stoppingtempbasal)))
sendMessage(DanaRSPacketBasalSetCancelTemporaryBasal(injector)) sendMessage(DanaRSPacketBasalSetCancelTemporaryBasal(injector))
SystemClock.sleep(500) SystemClock.sleep(500)
@ -348,6 +350,7 @@ class DanaRSService : DaggerService() {
sendMessage(msgTBR) sendMessage(msgTBR)
SystemClock.sleep(200) SystemClock.sleep(200)
loadEvents() loadEvents()
SystemClock.sleep(4500)
val tbr = pumpSync.expectedPumpState().temporaryBasal val tbr = pumpSync.expectedPumpState().temporaryBasal
danaPump.fromTemporaryBasal(tbr) danaPump.fromTemporaryBasal(tbr)
rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)) rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING))
@ -355,7 +358,9 @@ class DanaRSService : DaggerService() {
} }
fun highTempBasal(percent: Int): Boolean { fun highTempBasal(percent: Int): Boolean {
if (danaPump.isTempBasalInProgress) { val status = DanaRSPacketGeneralInitialScreenInformation(injector)
sendMessage(status)
if (status.isTempBasalInProgress) {
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.stoppingtempbasal))) rxBus.send(EventPumpStatusChanged(rh.gs(R.string.stoppingtempbasal)))
sendMessage(DanaRSPacketBasalSetCancelTemporaryBasal(injector)) sendMessage(DanaRSPacketBasalSetCancelTemporaryBasal(injector))
SystemClock.sleep(500) SystemClock.sleep(500)
@ -364,6 +369,7 @@ class DanaRSService : DaggerService() {
val msgTBR = DanaRSPacketAPSBasalSetTemporaryBasal(injector, percent) val msgTBR = DanaRSPacketAPSBasalSetTemporaryBasal(injector, percent)
sendMessage(msgTBR) sendMessage(msgTBR)
loadEvents() loadEvents()
SystemClock.sleep(4500)
val tbr = pumpSync.expectedPumpState().temporaryBasal val tbr = pumpSync.expectedPumpState().temporaryBasal
danaPump.fromTemporaryBasal(tbr) danaPump.fromTemporaryBasal(tbr)
rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)) rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING))
@ -375,7 +381,9 @@ class DanaRSService : DaggerService() {
aapsLogger.error(LTag.PUMPCOMM, "Wrong duration param") aapsLogger.error(LTag.PUMPCOMM, "Wrong duration param")
return false return false
} }
if (danaPump.isTempBasalInProgress) { val status = DanaRSPacketGeneralInitialScreenInformation(injector)
sendMessage(status)
if (status.isTempBasalInProgress) {
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.stoppingtempbasal))) rxBus.send(EventPumpStatusChanged(rh.gs(R.string.stoppingtempbasal)))
sendMessage(DanaRSPacketBasalSetCancelTemporaryBasal(injector)) sendMessage(DanaRSPacketBasalSetCancelTemporaryBasal(injector))
SystemClock.sleep(500) SystemClock.sleep(500)
@ -384,6 +392,7 @@ class DanaRSService : DaggerService() {
val msgTBR = DanaRSPacketAPSBasalSetTemporaryBasal(injector, percent) val msgTBR = DanaRSPacketAPSBasalSetTemporaryBasal(injector, percent)
sendMessage(msgTBR) sendMessage(msgTBR)
loadEvents() loadEvents()
SystemClock.sleep(4500)
val tbr = pumpSync.expectedPumpState().temporaryBasal val tbr = pumpSync.expectedPumpState().temporaryBasal
aapsLogger.debug(LTag.PUMPCOMM, "Expected TBR found: $tbr") aapsLogger.debug(LTag.PUMPCOMM, "Expected TBR found: $tbr")
danaPump.fromTemporaryBasal(tbr) danaPump.fromTemporaryBasal(tbr)
@ -397,6 +406,7 @@ class DanaRSService : DaggerService() {
val msgCancel = DanaRSPacketBasalSetCancelTemporaryBasal(injector) val msgCancel = DanaRSPacketBasalSetCancelTemporaryBasal(injector)
sendMessage(msgCancel) sendMessage(msgCancel)
loadEvents() loadEvents()
SystemClock.sleep(4500)
val tbr = pumpSync.expectedPumpState().temporaryBasal val tbr = pumpSync.expectedPumpState().temporaryBasal
danaPump.fromTemporaryBasal(tbr) danaPump.fromTemporaryBasal(tbr)
rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)) rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING))
@ -410,6 +420,7 @@ class DanaRSService : DaggerService() {
sendMessage(msgExtended) sendMessage(msgExtended)
SystemClock.sleep(200) SystemClock.sleep(200)
loadEvents() loadEvents()
SystemClock.sleep(4500)
val eb = pumpSync.expectedPumpState().extendedBolus val eb = pumpSync.expectedPumpState().extendedBolus
danaPump.fromExtendedBolus(eb) danaPump.fromExtendedBolus(eb)
rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)) rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING))
@ -422,6 +433,7 @@ class DanaRSService : DaggerService() {
val msgStop = DanaRSPacketBolusSetExtendedBolusCancel(injector) val msgStop = DanaRSPacketBolusSetExtendedBolusCancel(injector)
sendMessage(msgStop) sendMessage(msgStop)
loadEvents() loadEvents()
SystemClock.sleep(4500)
val eb = pumpSync.expectedPumpState().extendedBolus val eb = pumpSync.expectedPumpState().extendedBolus
danaPump.fromExtendedBolus(eb) danaPump.fromExtendedBolus(eb)
rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING)) rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING))