Add disconnecting state to make sure we don't get commands during disconnect
This commit is contained in:
parent
a7d7f373de
commit
2d8288d8d9
4 changed files with 51 additions and 41 deletions
|
@ -3,5 +3,6 @@ package info.nightscout.pump.medtrum.code
|
||||||
enum class ConnectionState {
|
enum class ConnectionState {
|
||||||
CONNECTED,
|
CONNECTED,
|
||||||
DISCONNECTED,
|
DISCONNECTED,
|
||||||
CONNECTING;
|
CONNECTING,
|
||||||
|
DISCONNECTING;
|
||||||
}
|
}
|
|
@ -109,13 +109,13 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
.toObservable(EventPreferenceChange::class.java)
|
.toObservable(EventPreferenceChange::class.java)
|
||||||
.observeOn(aapsSchedulers.io)
|
.observeOn(aapsSchedulers.io)
|
||||||
.subscribe({ event ->
|
.subscribe({ event ->
|
||||||
if (event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_sn_input))) {
|
if (event.isChanged(rh.gs(R.string.key_sn_input))) {
|
||||||
medtrumPump.loadUserSettingsFromSP()
|
medtrumPump.loadUserSettingsFromSP()
|
||||||
}
|
}
|
||||||
if (event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_alarm_setting))
|
if (event.isChanged(rh.gs(R.string.key_alarm_setting))
|
||||||
|| event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_patch_expiration))
|
|| event.isChanged(rh.gs(R.string.key_patch_expiration))
|
||||||
|| event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_hourly_max_insulin))
|
|| event.isChanged(rh.gs(R.string.key_hourly_max_insulin))
|
||||||
|| event.isChanged(rh.gs(info.nightscout.pump.medtrum.R.string.key_daily_max_insulin))
|
|| event.isChanged(rh.gs(R.string.key_daily_max_insulin))
|
||||||
) {
|
) {
|
||||||
medtrumPump.loadUserSettingsFromSP()
|
medtrumPump.loadUserSettingsFromSP()
|
||||||
commandQueue.setUserOptions(object : Callback() {
|
commandQueue.setUserOptions(object : Callback() {
|
||||||
|
@ -136,6 +136,18 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
handlePumpStateUpdate(state)
|
handlePumpStateUpdate(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
scope.launch {
|
||||||
|
medtrumPump.connectionStateFlow.collect { state ->
|
||||||
|
if (medtrumPlugin.isInitialized()) {
|
||||||
|
when (state) {
|
||||||
|
ConnectionState.CONNECTED -> rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTED))
|
||||||
|
ConnectionState.DISCONNECTED -> rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED))
|
||||||
|
ConnectionState.CONNECTING -> rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTING))
|
||||||
|
ConnectionState.DISCONNECTING -> rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTING))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
medtrumPump.loadUserSettingsFromSP()
|
medtrumPump.loadUserSettingsFromSP()
|
||||||
}
|
}
|
||||||
|
@ -150,9 +162,6 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
aapsLogger.debug(LTag.PUMP, "connect: called from: $from")
|
aapsLogger.debug(LTag.PUMP, "connect: called from: $from")
|
||||||
if (currentState is IdleState) {
|
if (currentState is IdleState) {
|
||||||
medtrumPump.connectionState = ConnectionState.CONNECTING
|
medtrumPump.connectionState = ConnectionState.CONNECTING
|
||||||
if (medtrumPlugin.isInitialized()) {
|
|
||||||
rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTING))
|
|
||||||
}
|
|
||||||
return bleComm.connect(from, medtrumPump.pumpSN)
|
return bleComm.connect(from, medtrumPump.pumpSN)
|
||||||
} else {
|
} else {
|
||||||
aapsLogger.error(LTag.PUMPCOMM, "Connect attempt when in non Idle state from: $from")
|
aapsLogger.error(LTag.PUMPCOMM, "Connect attempt when in non Idle state from: $from")
|
||||||
|
@ -184,11 +193,12 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun disconnect(from: String) {
|
fun disconnect(from: String) {
|
||||||
|
medtrumPump.connectionState = ConnectionState.DISCONNECTING
|
||||||
bleComm.disconnect(from)
|
bleComm.disconnect(from)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readPumpStatus() {
|
fun readPumpStatus() {
|
||||||
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingpumpstatus)))
|
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingpumpstatus)))
|
||||||
updateTimeIfNeeded(false)
|
updateTimeIfNeeded(false)
|
||||||
loadEvents()
|
loadEvents()
|
||||||
}
|
}
|
||||||
|
@ -229,7 +239,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loadEvents(): Boolean {
|
fun loadEvents(): Boolean {
|
||||||
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingpumpstatus)))
|
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingpumpstatus)))
|
||||||
// Sync records (based on the info we have from the sync)
|
// Sync records (based on the info we have from the sync)
|
||||||
val result = syncRecords()
|
val result = syncRecords()
|
||||||
if (result) {
|
if (result) {
|
||||||
|
@ -269,7 +279,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setUserSettings(): Boolean {
|
fun setUserSettings(): Boolean {
|
||||||
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.settingpumpsettings)))
|
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.settingpumpsettings)))
|
||||||
return sendPacketAndGetResponse(SetPatchPacket(injector))
|
return sendPacketAndGetResponse(SetPatchPacket(injector))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,7 +329,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
medtrumPump.bolusStopped = true
|
medtrumPump.bolusStopped = true
|
||||||
medtrumPump.bolusStopForced = true
|
medtrumPump.bolusStopForced = true
|
||||||
aapsLogger.warn(LTag.PUMPCOMM, "Communication stopped")
|
aapsLogger.warn(LTag.PUMPCOMM, "Communication stopped")
|
||||||
bleComm.disconnect("Communication stopped")
|
disconnect("Communication stopped")
|
||||||
} else {
|
} else {
|
||||||
bolusingEvent.t = medtrumPump.bolusingTreatment
|
bolusingEvent.t = medtrumPump.bolusingTreatment
|
||||||
bolusingEvent.status = rh.gs(info.nightscout.pump.common.R.string.bolus_delivered_so_far, medtrumPump.bolusingTreatment?.insulin, medtrumPump.bolusAmountToBeDelivered)
|
bolusingEvent.status = rh.gs(info.nightscout.pump.common.R.string.bolus_delivered_so_far, medtrumPump.bolusingTreatment?.insulin, medtrumPump.bolusAmountToBeDelivered)
|
||||||
|
@ -341,7 +351,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Do not call update status directly, reconnection may be needed
|
// Do not call update status directly, reconnection may be needed
|
||||||
commandQueue.loadEvents(object : Callback() {
|
commandQueue.loadEvents(object : Callback() {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingbolusstatus)))
|
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingbolusstatus)))
|
||||||
bolusingEvent.percent = 100
|
bolusingEvent.percent = 100
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -364,7 +374,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Do not call update status directly, reconnection may be needed
|
// Do not call update status directly, reconnection may be needed
|
||||||
commandQueue.loadEvents(object : Callback() {
|
commandQueue.loadEvents(object : Callback() {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingtempbasalstatus)))
|
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingtempbasalstatus)))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -378,7 +388,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Do not call update status directly, reconnection may be needed
|
// Do not call update status directly, reconnection may be needed
|
||||||
commandQueue.loadEvents(object : Callback() {
|
commandQueue.loadEvents(object : Callback() {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
rxBus.send(EventPumpStatusChanged(rh.gs(info.nightscout.pump.medtrum.R.string.gettingtempbasalstatus)))
|
rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingtempbasalstatus)))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -590,7 +600,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// State class, Can we move this to different file?
|
// State class
|
||||||
private abstract inner class State {
|
private abstract inner class State {
|
||||||
|
|
||||||
protected var responseHandled = false
|
protected var responseHandled = false
|
||||||
|
@ -599,7 +609,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
|
|
||||||
open fun onEnter() {}
|
open fun onEnter() {}
|
||||||
open fun onIndication(data: ByteArray) {
|
open fun onIndication(data: ByteArray) {
|
||||||
aapsLogger.debug(LTag.PUMPCOMM, "onIndication: " + this.toString() + "Should not be called here!")
|
aapsLogger.warn(LTag.PUMPCOMM, "onIndication: " + this.toString() + "Should not be called here!")
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun onConnected() {
|
open fun onConnected() {
|
||||||
|
@ -609,9 +619,6 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
fun onDisconnected() {
|
fun onDisconnected() {
|
||||||
aapsLogger.debug(LTag.PUMPCOMM, "onDisconnected")
|
aapsLogger.debug(LTag.PUMPCOMM, "onDisconnected")
|
||||||
medtrumPump.connectionState = ConnectionState.DISCONNECTED
|
medtrumPump.connectionState = ConnectionState.DISCONNECTED
|
||||||
if (medtrumPlugin.isInitialized()) {
|
|
||||||
rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.DISCONNECTED))
|
|
||||||
}
|
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
|
@ -625,7 +632,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// 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
|
||||||
aapsLogger.debug(LTag.PUMPCOMM, "Medtrum Service State timeout")
|
aapsLogger.debug(LTag.PUMPCOMM, "Medtrum Service State timeout")
|
||||||
// Disconnect to cancel any outstanding commands and go back to ready state
|
// Disconnect to cancel any outstanding commands and go back to ready state
|
||||||
bleComm.disconnect("Timeout")
|
disconnect("Timeout")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -644,7 +651,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
} else {
|
} else {
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
bleComm.disconnect("onSendMessageError")
|
disconnect("onSendMessageError")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -691,7 +698,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
Notification.URGENT,
|
Notification.URGENT,
|
||||||
info.nightscout.core.ui.R.raw.alarm
|
info.nightscout.core.ui.R.raw.alarm
|
||||||
)
|
)
|
||||||
bleComm.disconnect("Unsupported pump")
|
disconnect("Unsupported pump")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
} else {
|
} else {
|
||||||
toState(GetDeviceTypeState())
|
toState(GetDeviceTypeState())
|
||||||
|
@ -700,7 +707,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Failure
|
// Failure
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
bleComm.disconnect("Failure")
|
disconnect("Failure")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -732,7 +739,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Failure
|
// Failure
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
bleComm.disconnect("Failure")
|
disconnect("Failure")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -772,7 +779,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Failure
|
// Failure
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
bleComm.disconnect("Failure")
|
disconnect("Failure")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -800,7 +807,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Failure
|
// Failure
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
bleComm.disconnect("Failure")
|
disconnect("Failure")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -830,7 +837,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Failure
|
// Failure
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
bleComm.disconnect("Failure")
|
disconnect("Failure")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -858,7 +865,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Failure
|
// Failure
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
bleComm.disconnect("Failure")
|
disconnect("Failure")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -886,7 +893,7 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Failure
|
// Failure
|
||||||
responseHandled = true
|
responseHandled = true
|
||||||
responseSuccess = false
|
responseSuccess = false
|
||||||
bleComm.disconnect("Failure")
|
disconnect("Failure")
|
||||||
toState(IdleState())
|
toState(IdleState())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -900,9 +907,6 @@ class MedtrumService : DaggerService(), BLECommCallback {
|
||||||
// Now we are fully connected and authenticated and we can start sending commands. Let AAPS know
|
// Now we are fully connected and authenticated and we can start sending commands. Let AAPS know
|
||||||
if (isConnected == false) {
|
if (isConnected == false) {
|
||||||
medtrumPump.connectionState = ConnectionState.CONNECTED
|
medtrumPump.connectionState = ConnectionState.CONNECTED
|
||||||
if (medtrumPlugin.isInitialized()) {
|
|
||||||
rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTED))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,17 +82,17 @@ class MedtrumOverviewViewModel @Inject constructor(
|
||||||
medtrumPump.connectionStateFlow.collect { state ->
|
medtrumPump.connectionStateFlow.collect { state ->
|
||||||
aapsLogger.debug(LTag.PUMP, "MedtrumViewModel connectionStateFlow: $state")
|
aapsLogger.debug(LTag.PUMP, "MedtrumViewModel connectionStateFlow: $state")
|
||||||
when (state) {
|
when (state) {
|
||||||
ConnectionState.CONNECTING -> {
|
ConnectionState.CONNECTING -> {
|
||||||
_bleStatus.postValue("{fa-bluetooth-b spin}")
|
_bleStatus.postValue("{fa-bluetooth-b spin}")
|
||||||
_canDoRefresh.postValue(false)
|
_canDoRefresh.postValue(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionState.CONNECTED -> {
|
ConnectionState.CONNECTED -> {
|
||||||
_bleStatus.postValue("{fa-bluetooth}")
|
_bleStatus.postValue("{fa-bluetooth}")
|
||||||
_canDoRefresh.postValue(false)
|
_canDoRefresh.postValue(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionState.DISCONNECTED -> {
|
ConnectionState.DISCONNECTED -> {
|
||||||
_bleStatus.postValue("{fa-bluetooth-b}")
|
_bleStatus.postValue("{fa-bluetooth-b}")
|
||||||
if (medtrumPump.pumpState > MedtrumPumpState.EJECTED && medtrumPump.pumpState < MedtrumPumpState.STOPPED) {
|
if (medtrumPump.pumpState > MedtrumPumpState.EJECTED && medtrumPump.pumpState < MedtrumPumpState.STOPPED) {
|
||||||
_canDoRefresh.postValue(true)
|
_canDoRefresh.postValue(true)
|
||||||
|
@ -100,6 +100,11 @@ class MedtrumOverviewViewModel @Inject constructor(
|
||||||
_canDoRefresh.postValue(false)
|
_canDoRefresh.postValue(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConnectionState.DISCONNECTING -> {
|
||||||
|
_bleStatus.postValue("{fa-bluetooth-b spin}")
|
||||||
|
_canDoRefresh.postValue(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
updateGUI()
|
updateGUI()
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,11 +55,11 @@ class MedtrumViewModel @Inject constructor(
|
||||||
aapsLogger.debug(LTag.PUMP, "MedtrumViewModel connectionStateFlow: $state")
|
aapsLogger.debug(LTag.PUMP, "MedtrumViewModel connectionStateFlow: $state")
|
||||||
if (patchStep.value != null) {
|
if (patchStep.value != null) {
|
||||||
when (state) {
|
when (state) {
|
||||||
ConnectionState.CONNECTED -> {
|
ConnectionState.CONNECTED -> {
|
||||||
medtrumPump.lastConnection = System.currentTimeMillis()
|
medtrumPump.lastConnection = System.currentTimeMillis()
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionState.DISCONNECTED -> {
|
ConnectionState.DISCONNECTED -> {
|
||||||
if (patchStep.value in listOf(
|
if (patchStep.value in listOf(
|
||||||
PatchStep.PRIME,
|
PatchStep.PRIME,
|
||||||
PatchStep.PRIMING,
|
PatchStep.PRIMING,
|
||||||
|
@ -84,7 +84,7 @@ class MedtrumViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionState.CONNECTING -> {
|
ConnectionState.CONNECTING, ConnectionState.DISCONNECTING -> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue