add BusyException
This commit is contained in:
parent
8d2d38163a
commit
38a1d9c6bc
7 changed files with 52 additions and 33 deletions
|
@ -19,6 +19,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.pod.state.Omn
|
|||
import info.nightscout.androidaps.utils.extensions.toHex
|
||||
import io.reactivex.Observable
|
||||
import java.util.concurrent.TimeoutException
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
@ -28,8 +29,7 @@ class OmnipodDashBleManagerImpl @Inject constructor(
|
|||
private val aapsLogger: AAPSLogger,
|
||||
private val podState: OmnipodDashPodStateManager
|
||||
) : OmnipodDashBleManager {
|
||||
|
||||
// TODO: add busy AtomicBoolean
|
||||
private val busy = AtomicBoolean(false);
|
||||
private val bluetoothManager: BluetoothManager =
|
||||
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
||||
private val bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
|
||||
|
@ -41,26 +41,29 @@ class OmnipodDashBleManagerImpl @Inject constructor(
|
|||
?: myId.increment() // pod not activated
|
||||
|
||||
override fun sendCommand(cmd: Command): Observable<PodEvent> = Observable.create { emitter ->
|
||||
if (!busy.compareAndSet(false, true)) {
|
||||
throw BusyException()
|
||||
}
|
||||
try {
|
||||
val conn = connection ?: throw NotConnectedException("Not connected")
|
||||
|
||||
val session = conn.session ?: throw NotConnectedException("Missing session")
|
||||
|
||||
emitter.onNext(PodEvent.CommandSending(cmd))
|
||||
|
||||
val sendResult = session.sendCommand(cmd)
|
||||
when(sendResult) {
|
||||
when (session.sendCommand(cmd)) {
|
||||
is CommandSendErrorSending -> {
|
||||
emitter.tryOnError(CouldNotSendCommandException())
|
||||
return@create
|
||||
}
|
||||
|
||||
is CommandSendSuccess ->
|
||||
emitter.onNext(PodEvent.CommandSent(cmd))
|
||||
is CommandSendErrorConfirming ->
|
||||
emitter.onNext(PodEvent.CommandSendNotConfirmed(cmd))
|
||||
}
|
||||
|
||||
val readResult = session.readAndAckCommandResponse()
|
||||
when (readResult){
|
||||
when (val readResult = session.readAndAckCommandResponse()) {
|
||||
is CommandReceiveSuccess ->
|
||||
emitter.onNext(PodEvent.ResponseReceived(readResult.result))
|
||||
|
||||
|
@ -73,6 +76,12 @@ class OmnipodDashBleManagerImpl @Inject constructor(
|
|||
}
|
||||
}
|
||||
emitter.onComplete()
|
||||
} catch (ex: Exception) {
|
||||
disconnect()
|
||||
emitter.tryOnError(ex)
|
||||
} finally {
|
||||
busy.set(false)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getStatus(): ConnectionStatus {
|
||||
|
@ -84,6 +93,9 @@ class OmnipodDashBleManagerImpl @Inject constructor(
|
|||
}
|
||||
|
||||
override fun connect(): Observable<PodEvent> = Observable.create { emitter ->
|
||||
if (!busy.compareAndSet(false, true)) {
|
||||
throw BusyException()
|
||||
}
|
||||
try {
|
||||
emitter.onNext(PodEvent.BluetoothConnecting)
|
||||
|
||||
|
@ -110,6 +122,8 @@ class OmnipodDashBleManagerImpl @Inject constructor(
|
|||
} catch (ex: Exception) {
|
||||
disconnect()
|
||||
emitter.tryOnError(ex)
|
||||
} finally {
|
||||
busy.set(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -126,6 +140,9 @@ class OmnipodDashBleManagerImpl @Inject constructor(
|
|||
}
|
||||
|
||||
override fun pairNewPod(): Observable<PodEvent> = Observable.create { emitter ->
|
||||
if (!busy.compareAndSet(false, true)) {
|
||||
throw BusyException()
|
||||
}
|
||||
try {
|
||||
|
||||
if (podState.ltk != null) {
|
||||
|
@ -170,6 +187,8 @@ class OmnipodDashBleManagerImpl @Inject constructor(
|
|||
} catch (ex: Exception) {
|
||||
disconnect()
|
||||
emitter.tryOnError(ex)
|
||||
} finally {
|
||||
busy.set(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -46,7 +46,6 @@ class BleCommCallbacks(
|
|||
connected.await(timeoutMs.toLong(), TimeUnit.MILLISECONDS)
|
||||
} catch (e: InterruptedException) {
|
||||
aapsLogger.warn(LTag.PUMPBTCOMM,"Interrupted while waiting for Connection")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@ enum class BleCommandType(val value: Byte) {
|
|||
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
fun byValue(value: Byte): BleCommandType =
|
||||
values().firstOrNull { it.value == value }
|
||||
?: throw IllegalArgumentException("Unknown BleCommandType: $value")
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.exceptions
|
||||
|
||||
class BusyException(): Exception("Bluetooth busy")
|
|
@ -1,4 +1,4 @@
|
|||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.io
|
||||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.packet
|
||||
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.CrcMismatchException
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.IncorrectPacketException
|
|
@ -1,4 +1,4 @@
|
|||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message
|
||||
package info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.packet
|
||||
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.packet.BlePacket
|
||||
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.packet.FirstBlePacket
|
|
@ -35,7 +35,6 @@ class BleDiscoveredDevice(val scanResult: ScanResult, private val scanRecord: Sc
|
|||
}
|
||||
|
||||
@Throws(DiscoveredInvalidPodException::class)
|
||||
|
||||
private fun validatePodId() {
|
||||
val serviceUUIDs = scanRecord.serviceUuids
|
||||
val hexPodId = extractUUID16(serviceUUIDs[3]) + extractUUID16(serviceUUIDs[4])
|
||||
|
|
Loading…
Reference in a new issue