Improve tbr syncing
This commit is contained in:
parent
4a1eded8e5
commit
9860c7aac3
5 changed files with 63 additions and 22 deletions
|
@ -173,7 +173,10 @@ import kotlin.math.round
|
||||||
override fun getPumpStatus(reason: String) {
|
override fun getPumpStatus(reason: String) {
|
||||||
aapsLogger.debug(LTag.PUMP, "Medtrum getPumpStatus - reason:$reason")
|
aapsLogger.debug(LTag.PUMP, "Medtrum getPumpStatus - reason:$reason")
|
||||||
if (isInitialized()) {
|
if (isInitialized()) {
|
||||||
medtrumService?.readPumpStatus()
|
val connectionOK = medtrumService?.readPumpStatus() ?: false
|
||||||
|
if (connectionOK == false) {
|
||||||
|
aapsLogger.error(LTag.PUMP, "Medtrum getPumpStatus failed")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,22 @@
|
||||||
package info.nightscout.pump.medtrum.comm.packets
|
package info.nightscout.pump.medtrum.comm.packets
|
||||||
|
|
||||||
import dagger.android.HasAndroidInjector
|
import dagger.android.HasAndroidInjector
|
||||||
|
import info.nightscout.interfaces.pump.PumpSync
|
||||||
import info.nightscout.pump.medtrum.MedtrumPump
|
import info.nightscout.pump.medtrum.MedtrumPump
|
||||||
import info.nightscout.pump.medtrum.comm.enums.CommandType.CANCEL_TEMP_BASAL
|
import info.nightscout.pump.medtrum.comm.enums.CommandType.CANCEL_TEMP_BASAL
|
||||||
import info.nightscout.pump.medtrum.comm.enums.BasalType
|
import info.nightscout.pump.medtrum.comm.enums.BasalType
|
||||||
import info.nightscout.pump.medtrum.extension.toInt
|
import info.nightscout.pump.medtrum.extension.toInt
|
||||||
import info.nightscout.pump.medtrum.extension.toLong
|
import info.nightscout.pump.medtrum.extension.toLong
|
||||||
import info.nightscout.pump.medtrum.util.MedtrumTimeUtil
|
import info.nightscout.pump.medtrum.util.MedtrumTimeUtil
|
||||||
|
import info.nightscout.rx.logging.LTag
|
||||||
|
import info.nightscout.shared.utils.DateUtil
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class CancelTempBasalPacket(injector: HasAndroidInjector) : MedtrumPacket(injector) {
|
class CancelTempBasalPacket(injector: HasAndroidInjector) : MedtrumPacket(injector) {
|
||||||
|
|
||||||
@Inject lateinit var medtrumPump: MedtrumPump
|
@Inject lateinit var medtrumPump: MedtrumPump
|
||||||
|
@Inject lateinit var pumpSync: PumpSync
|
||||||
|
@Inject lateinit var dateUtil: DateUtil
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
|
@ -42,6 +47,17 @@ class CancelTempBasalPacket(injector: HasAndroidInjector) : MedtrumPacket(inject
|
||||||
val basalStartTime = MedtrumTimeUtil().convertPumpTimeToSystemTimeMillis(data.copyOfRange(RESP_BASAL_START_TIME_START, RESP_BASAL_START_TIME_END).toLong())
|
val basalStartTime = MedtrumTimeUtil().convertPumpTimeToSystemTimeMillis(data.copyOfRange(RESP_BASAL_START_TIME_START, RESP_BASAL_START_TIME_END).toLong())
|
||||||
|
|
||||||
medtrumPump.handleBasalStatusUpdate(basalType, basalRate, basalSequence, basalPatchId, basalStartTime)
|
medtrumPump.handleBasalStatusUpdate(basalType, basalRate, basalSequence, basalPatchId, basalStartTime)
|
||||||
|
|
||||||
|
pumpSync.syncStopTemporaryBasalWithPumpId(
|
||||||
|
timestamp = basalStartTime, // Time of normal basal start = time of tbr end
|
||||||
|
endPumpId = basalStartTime,
|
||||||
|
pumpType = medtrumPump.pumpType,
|
||||||
|
pumpSerial = medtrumPump.pumpSN.toString(radix = 16)
|
||||||
|
)
|
||||||
|
aapsLogger.warn(
|
||||||
|
LTag.PUMPCOMM,
|
||||||
|
"CancelTempBasalPacket: EVENT TEMP_END ($basalType) ${dateUtil.dateAndTimeString(basalStartTime)} ($basalStartTime) "
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return success
|
return success
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,7 +74,9 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
// TODO: Test and further increase?
|
// TODO: Test and further increase?
|
||||||
private const val COMMAND_TIMEOUT_SEC: Long = 60
|
private const val COMMAND_DEFAULT_TIMEOUT_SEC: Long = 60
|
||||||
|
private const val COMMAND_SYNC_TIMEOUT_SEC: Long = 120
|
||||||
|
private const val COMMAND_CONNECTING_TIMEOUT_SEC: Long = 30
|
||||||
}
|
}
|
||||||
|
|
||||||
val timeUtil = MedtrumTimeUtil()
|
val timeUtil = MedtrumTimeUtil()
|
||||||
|
@ -142,7 +144,12 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deactivatePatch(): Boolean {
|
fun deactivatePatch(): Boolean {
|
||||||
return sendPacketAndGetResponse(StopPatchPacket(injector))
|
var result = true
|
||||||
|
if (medtrumPump.tempBasalInProgress) {
|
||||||
|
result = sendPacketAndGetResponse(CancelTempBasalPacket(injector))
|
||||||
|
}
|
||||||
|
if (result) result = sendPacketAndGetResponse(StopPatchPacket(injector))
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stopConnecting() {
|
fun stopConnecting() {
|
||||||
|
@ -227,8 +234,13 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
}
|
}
|
||||||
if (result) result = sendPacketAndGetResponse(SetTempBasalPacket(injector, absoluteRate, durationInMinutes))
|
if (result) result = sendPacketAndGetResponse(SetTempBasalPacket(injector, absoluteRate, durationInMinutes))
|
||||||
|
|
||||||
// Get history records, this will update the pump state
|
// Get history records, this will update the prevoius basals
|
||||||
if (result) result = syncRecords()
|
// Do not call update status directly, reconnection may be needed
|
||||||
|
commandQueue.readStatus(rh.gs(info.nightscout.pump.medtrum.R.string.gettingtempbasalstatus), object : Callback() {
|
||||||
|
override fun run() {
|
||||||
|
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingtempbasalstatus)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -236,8 +248,13 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
fun cancelTempBasal(): Boolean {
|
fun cancelTempBasal(): Boolean {
|
||||||
var result = sendPacketAndGetResponse(CancelTempBasalPacket(injector))
|
var result = sendPacketAndGetResponse(CancelTempBasalPacket(injector))
|
||||||
|
|
||||||
// Get history records, this will update the pump state
|
// Get history records, this will update the prevoius basals
|
||||||
if (result) result = syncRecords()
|
// Do not call update status directly, reconnection may be needed
|
||||||
|
commandQueue.readStatus(rh.gs(info.nightscout.pump.medtrum.R.string.gettingtempbasalstatus), object : Callback() {
|
||||||
|
override fun run() {
|
||||||
|
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingtempbasalstatus)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -266,11 +283,13 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
/** This gets the history records from the pump */
|
/** This gets the history records from the pump */
|
||||||
private fun syncRecords(): Boolean {
|
private fun syncRecords(): Boolean {
|
||||||
aapsLogger.debug(LTag.PUMP, "syncRecords: called!, syncedSequenceNumber: ${medtrumPump.syncedSequenceNumber}, currentSequenceNumber: ${medtrumPump.currentSequenceNumber}")
|
aapsLogger.debug(LTag.PUMP, "syncRecords: called!, syncedSequenceNumber: ${medtrumPump.syncedSequenceNumber}, currentSequenceNumber: ${medtrumPump.currentSequenceNumber}")
|
||||||
var result = false
|
var result = true
|
||||||
// Note: medtrum app fetches all records when they sync?
|
// Note: medtrum app fetches all records when they sync?
|
||||||
for (sequence in medtrumPump.syncedSequenceNumber..medtrumPump.currentSequenceNumber) {
|
if (medtrumPump.syncedSequenceNumber < medtrumPump.currentSequenceNumber) {
|
||||||
result = sendPacketAndGetResponse(GetRecordPacket(injector, sequence))
|
for (sequence in (medtrumPump.syncedSequenceNumber + 1)..medtrumPump.currentSequenceNumber) {
|
||||||
if (!result) break
|
result = sendPacketAndGetResponse(GetRecordPacket(injector, sequence))
|
||||||
|
if (result == false) break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -324,13 +343,13 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
currentState.onEnter()
|
currentState.onEnter()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sendPacketAndGetResponse(packet: MedtrumPacket): Boolean {
|
private fun sendPacketAndGetResponse(packet: MedtrumPacket, timeout: Long = COMMAND_DEFAULT_TIMEOUT_SEC): Boolean {
|
||||||
var result = false
|
var result = false
|
||||||
if (currentState is ReadyState) {
|
if (currentState is ReadyState) {
|
||||||
toState(CommandState())
|
toState(CommandState())
|
||||||
mPacket = packet
|
mPacket = packet
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
result = currentState.waitForResponse()
|
result = currentState.waitForResponse(timeout)
|
||||||
} else {
|
} else {
|
||||||
aapsLogger.error(LTag.PUMPCOMM, "Send packet attempt when in non Ready state")
|
aapsLogger.error(LTag.PUMPCOMM, "Send packet attempt when in non Ready state")
|
||||||
}
|
}
|
||||||
|
@ -363,9 +382,9 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun waitForResponse(): Boolean {
|
fun waitForResponse(timeout: Long): Boolean {
|
||||||
val startTime = System.currentTimeMillis()
|
val startTime = System.currentTimeMillis()
|
||||||
val timeoutMillis = T.secs(COMMAND_TIMEOUT_SEC).msecs()
|
val timeoutMillis = T.secs(timeout).msecs()
|
||||||
while (!responseHandled) {
|
while (!responseHandled) {
|
||||||
if (System.currentTimeMillis() - startTime > timeoutMillis) {
|
if (System.currentTimeMillis() - startTime > timeoutMillis) {
|
||||||
// If we haven't received a response in the specified time, assume the command failed
|
// If we haven't received a response in the specified time, assume the command failed
|
||||||
|
@ -409,7 +428,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
mPacket = AuthorizePacket(injector)
|
mPacket = AuthorizePacket(injector)
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
scope.launch {
|
scope.launch {
|
||||||
waitForResponse()
|
waitForResponse(COMMAND_CONNECTING_TIMEOUT_SEC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -441,7 +460,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
mPacket = GetDeviceTypePacket(injector)
|
mPacket = GetDeviceTypePacket(injector)
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
scope.launch {
|
scope.launch {
|
||||||
waitForResponse()
|
waitForResponse(COMMAND_CONNECTING_TIMEOUT_SEC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -473,7 +492,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
mPacket = GetTimePacket(injector)
|
mPacket = GetTimePacket(injector)
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
scope.launch {
|
scope.launch {
|
||||||
waitForResponse()
|
waitForResponse(COMMAND_CONNECTING_TIMEOUT_SEC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -514,7 +533,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
mPacket = SetTimePacket(injector)
|
mPacket = SetTimePacket(injector)
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
scope.launch {
|
scope.launch {
|
||||||
waitForResponse()
|
waitForResponse(COMMAND_CONNECTING_TIMEOUT_SEC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -542,7 +561,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
mPacket = SetTimeZonePacket(injector)
|
mPacket = SetTimeZonePacket(injector)
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
scope.launch {
|
scope.launch {
|
||||||
waitForResponse()
|
waitForResponse(COMMAND_CONNECTING_TIMEOUT_SEC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -570,7 +589,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
mPacket = SynchronizePacket(injector)
|
mPacket = SynchronizePacket(injector)
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
scope.launch {
|
scope.launch {
|
||||||
waitForResponse()
|
waitForResponse(COMMAND_CONNECTING_TIMEOUT_SEC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -598,7 +617,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
mPacket = SubscribePacket(injector)
|
mPacket = SubscribePacket(injector)
|
||||||
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
mPacket?.getRequest()?.let { bleComm.sendMessage(it) }
|
||||||
scope.launch {
|
scope.launch {
|
||||||
waitForResponse()
|
waitForResponse(COMMAND_CONNECTING_TIMEOUT_SEC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@
|
||||||
|
|
||||||
<string name="waitingforestimatedbolusend">Waiting for bolus end. Remaining %1$d sec.</string>
|
<string name="waitingforestimatedbolusend">Waiting for bolus end. Remaining %1$d sec.</string>
|
||||||
<string name="gettingbolusstatus">Getting bolus status</string>
|
<string name="gettingbolusstatus">Getting bolus status</string>
|
||||||
|
<string name="gettingtempbasalstatus">Getting temporary basal status</string>
|
||||||
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
@ -16,6 +16,8 @@ class CancelTempBasalPacketTest : MedtrumTestBase() {
|
||||||
if (it is CancelTempBasalPacket) {
|
if (it is CancelTempBasalPacket) {
|
||||||
it.aapsLogger = aapsLogger
|
it.aapsLogger = aapsLogger
|
||||||
it.medtrumPump = medtrumPump
|
it.medtrumPump = medtrumPump
|
||||||
|
it.pumpSync = pumpSync
|
||||||
|
it.dateUtil = dateUtil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue