Skip to content

Commit

Permalink
Rename waitDeviceSessionToOpen to waitDeviceSessionWithFtpToOpen and …
Browse files Browse the repository at this point in the history
…update it so that it waits for FTP client to be ready polarofficial#496
  • Loading branch information
korzonkiee committed Dec 6, 2024
1 parent 734c1f1 commit 5ddc048
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS

doFactoryReset(identifier, true)
.andThen(Completable.timer(30, TimeUnit.SECONDS))
.andThen(waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, waitForDeviceDownSeconds = 10L))
.andThen(waitDeviceSessionWithFtpToOpen(identifier, factoryResetMaxWaitTimeSeconds, waitForDeviceDownSeconds = 10L))
.andThen(Completable.timer(5, TimeUnit.SECONDS))
.andThen(
Flowable.fromIterable(firmwareFiles)
Expand All @@ -2015,7 +2015,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
BleLogger.d(TAG, "Starting finalization of firmware update")
Completable.timer(rebootTriggeredWaitTimeSeconds, TimeUnit.SECONDS)
.andThen(
waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 120L)
waitDeviceSessionWithFtpToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 120L)
.andThen(
Completable.fromCallable {
BleLogger.d(TAG, "Restoring backup to device after version ${firmwareUpdateResponse.version}")
Expand All @@ -2031,7 +2031,7 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}
.concatMap { status ->
if (status is FirmwareUpdateStatus.FinalizingFwUpdate) {
waitDeviceSessionToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 60L)
waitDeviceSessionWithFtpToOpen(identifier, factoryResetMaxWaitTimeSeconds, if (isDeviceSensor) 0L else 60L)
.andThen(Flowable.just(FirmwareUpdateStatus.FwUpdateCompletedSuccessfully(firmwareUpdateResponse.version)))
} else {
Flowable.just(status)
Expand Down Expand Up @@ -2403,12 +2403,12 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
}, BackpressureStrategy.BUFFER)
}

private fun waitDeviceSessionToOpen(
private fun waitDeviceSessionWithFtpToOpen(
deviceId: String,
timeoutSeconds: Long,
waitForDeviceDownSeconds: Long = 0L
): Completable {
BleLogger.d(TAG, "waitDeviceSessionToOpen(), seconds: $timeoutSeconds, waitForDeviceDownSeconds: $waitForDeviceDownSeconds")
BleLogger.d(TAG, "waitDeviceSessionWithFtpToOpen(): seconds $timeoutSeconds, waitForDeviceDownSeconds $waitForDeviceDownSeconds")
val pollIntervalSeconds = 5L

return Observable.timer(waitForDeviceDownSeconds, TimeUnit.SECONDS)
Expand All @@ -2419,15 +2419,20 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
.takeUntil(Observable.timer(timeoutSeconds, TimeUnit.SECONDS))
.timeout(timeoutSeconds, TimeUnit.SECONDS)
.subscribe({
if (deviceSessionState == DeviceSessionState.SESSION_OPEN) {
BleLogger.d(TAG, "Session opened, deviceId: $deviceId")
val isSessionOpen = deviceSessionState == DeviceSessionState.SESSION_OPEN
val isFtpClientReady = isFtpClientReady(deviceId)

BleLogger.d(TAG, "waitDeviceSessionWithFtpToOpen(): isSessionOpen $isSessionOpen, isFtpClientReady $isFtpClientReady")

if (isSessionOpen && isFtpClientReady) {
BleLogger.d(TAG, "waitDeviceSessionWithFtpToOpen(): completed")
disposable?.dispose()
emitter.onComplete()
} else {
BleLogger.d(TAG, "Waiting for device session to open, deviceId: $deviceId, current state: $deviceSessionState")
BleLogger.d(TAG, "waitDeviceSessionWithFtpToOpen(): current state $deviceSessionState")
}
}, { error ->
BleLogger.e(TAG, "Timeout reached while waiting for device session to open, deviceId: $deviceId")
BleLogger.e(TAG, "waitDeviceSessionWithFtpToOpen(): error $error")
emitter.onError(error)
})
emitter.setCancellable {
Expand All @@ -2436,6 +2441,22 @@ class BDBleApiImpl private constructor(context: Context, features: Set<PolarBleS
})
}

private fun isFtpClientReady(identifier: String): Boolean {
try {
val session = sessionPsFtpClientReady(identifier)

val client = session.fetchClient(BlePsFtpUtils.RFC77_PFTP_SERVICE) as BlePsFtpClient?
if (client != null) {
return true
}

return false
}
catch (error: Throwable) {
return false
}
}

override fun deleteStoredDeviceData(identifier: String, dataType: PolarStoredDataType, until: LocalDate?): Completable {

fileDeletionMap.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2335,7 +2335,7 @@ extension PolarBleApiImpl: PolarBleApi {
BleLogger.trace("Firmware update is in finalizing stage")

BleLogger.trace("Device rebooting")
self.waitDeviceSessionToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
self.waitDeviceSessionWithFtpToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
.do(onSubscribe: {
BleLogger.trace("Waiting for device session to open after reboot with timeout: \(rebootMaxWaitTimeSeconds) seconds")
})
Expand All @@ -2350,7 +2350,7 @@ extension PolarBleApiImpl: PolarBleApi {
}
.flatMap { _ in
BleLogger.trace("Waiting for device session to open after factory reset with timeout: \(factoryResetMaxWaitTimeSeconds) seconds")
return self.waitDeviceSessionToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
return self.waitDeviceSessionWithFtpToOpen(deviceId: identifier, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10)
.do(onSubscribe: {
BleLogger.trace("Waiting for device session to open post factory reset")
})
Expand Down Expand Up @@ -3060,7 +3060,7 @@ extension PolarBleApiImpl: PolarBleApi {
let factoryResetMaxWaitTimeSeconds: TimeInterval = 6 * 60
BleLogger.trace("Write FW to device")
return doFactoryReset(deviceId, preservePairingInformation: true)
.andThen(waitDeviceSessionToOpen(deviceId: deviceId, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10))
.andThen(waitDeviceSessionWithFtpToOpen(deviceId: deviceId, timeoutSeconds: Int(factoryResetMaxWaitTimeSeconds), waitForDeviceDownSeconds: 10))
.andThen(Observable.create { observer in
do {
let session = try self.sessionFtpClientReady(deviceId)
Expand Down Expand Up @@ -3109,9 +3109,8 @@ extension PolarBleApiImpl: PolarBleApi {
})
}


private func waitDeviceSessionToOpen(deviceId: String, timeoutSeconds: Int, waitForDeviceDownSeconds: Int = 0) -> Completable {
BleLogger.trace("Wait for device session to open, timeoutSeconds: \(timeoutSeconds), waitForDeviceDownSeconds: \(waitForDeviceDownSeconds)")
private func waitDeviceSessionWithFtpToOpen(deviceId: String, timeoutSeconds: Int, waitForDeviceDownSeconds: Int = 0) -> Completable {
BleLogger.trace("waitDeviceSessionWithFtpToOpen(): timeoutSeconds \(timeoutSeconds), waitForDeviceDownSeconds \(waitForDeviceDownSeconds)")
let pollIntervalSeconds = 5

return Completable.create { emitter in
Expand All @@ -3123,22 +3122,39 @@ extension PolarBleApiImpl: PolarBleApi {
}
.timeout(RxTimeInterval.seconds(timeoutSeconds), scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
if self.deviceSessionState == BleDeviceSession.DeviceSessionState.sessionOpen {
BleLogger.trace("Session opened, deviceId: \(deviceId)")
let isSessionOpen = self.deviceSessionState == BleDeviceSession.DeviceSessionState.sessionOpen
let isFtpClientReady = self.isFtpClientReady(deviceId)

BleLogger.trace("waitDeviceSessionWithFtpToOpen(): isSessionOpen \(isSessionOpen), isFTPReady \(isFtpClientReady)")

if isSessionOpen && isFtpClientReady {
BleLogger.trace("waitDeviceSessionWithFtpToOpen(): completed")
disposable?.dispose()
emitter(.completed)
} else {
BleLogger.trace("Waiting for device session to open, deviceId: \(deviceId), current state: \(String(describing: self.deviceSessionState))")
BleLogger.trace("waitDeviceSessionWithFtpToOpen(): current state \(String(describing: self.deviceSessionState))")
}
}, onError: { error in
BleLogger.trace("Timeout reached while waiting for device session to open, deviceId: \(deviceId)")
BleLogger.error("waitDeviceSessionWithFtpToOpen(): error \(error)")
emitter(.error(error))
})
return Disposables.create {
disposable?.dispose()
}
}
}

private func isFtpClientReady(_ identifier: String) -> Bool {
do {
let session = try self.sessionFtpClientReady(identifier)
guard let client = session.fetchGattClient(BlePsFtpClient.PSFTP_SERVICE) as? BlePsFtpClient else {
return false
}
return true
} catch _ {
return false
}
}


private func processAccData(
Expand Down

0 comments on commit 5ddc048

Please sign in to comment.