Skip to content

Commit ea2ce3a

Browse files
Update deep analysis
1 parent c21b49d commit ea2ce3a

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

app/src/main/java/f/cking/software/domain/interactor/DeviceServicesFetchingPlanner.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,21 @@ class DeviceServicesFetchingPlanner(
197197
|| !recentlyChecked
198198
}
199199

200-
private fun tooMachConnections() {
201-
maxPossibleConnections = parallelProcessingBatches - 1
202-
parallelProcessingBatches = max(1, (parallelProcessingBatches * 0.5).toInt())
200+
private fun decreaseMaxConnections() {
201+
parallelProcessingBatches = max(MIN_PARALLEL_CONNECTIONS, parallelProcessingBatches - 1)
203202
}
204203

205204
private fun increaseConnections() {
206-
parallelProcessingBatches = min(max(1, (parallelProcessingBatches * 1.2).toInt()), maxPossibleConnections)
205+
parallelProcessingBatches = min(parallelProcessingBatches + 1, MAX_PARALLEL_CONNECTIONS)
207206
}
208207

209208
companion object {
210-
private const val PARALLEL_BATCH_COUNT = 7
209+
private const val PARALLEL_BATCH_COUNT = 7 // usually 7 parallel connections are stable
210+
private const val MIN_PARALLEL_CONNECTIONS = 2
211+
private const val MAX_PARALLEL_CONNECTIONS = 15
211212
private const val CHECK_INTERVAL_PER_DEVICE_MIN = 10
212213
private const val JOURNAL_REPORT_COOLDOWN_MIN = 30
213-
private const val DEVICE_FETCH_TIMEOUT_SEC = 5
214+
private const val DEVICE_FETCH_TIMEOUT_SEC = 8
214215
private const val TOTAL_FETCH_TIMEOUT_SEC = 30
215216
private const val MIN_COOLDOWN_DURATION_MINS = 1
216217
private const val TAG = "DeviceServicesFetchingPlanner"

app/src/main/java/f/cking/software/domain/interactor/FetchDeviceServiceInfo.kt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import f.cking.software.domain.model.DeviceData
99
import f.cking.software.domain.model.DeviceMetadata
1010
import f.cking.software.domain.model.DeviceMetadata.CharacteristicType
1111
import f.cking.software.domain.model.DeviceMetadata.ServiceTypes
12+
import f.cking.software.domain.model.isNullOrEmpty
1213
import f.cking.software.fromBase64
1314
import kotlinx.coroutines.Dispatchers
1415
import kotlinx.coroutines.FlowPreview
@@ -72,6 +73,12 @@ class FetchDeviceServiceInfo(
7273
}
7374
}
7475

76+
fun throwIfMetadataNotUpdated(e: Exception) {
77+
if (metadata.isNullOrEmpty() || metadata == device.metadata) {
78+
throw e
79+
}
80+
}
81+
7582
bleScannerHelper.connectToDevice(device.address)
7683
.collect { event ->
7784
when (event) {
@@ -161,38 +168,38 @@ class FetchDeviceServiceInfo(
161168
// Error handling
162169
is BleScannerHelper.DeviceConnectResult.DisconnectedWithError.UnspecifiedConnectionError -> {
163170
Timber.tag(TAG).e("Unspecified connection error from ${device.address}.")
164-
disconnect(event.gatt)
165-
throw BluetoothConnectionException.UnspecifiedConnectionError(event.errorCode)
171+
throwIfMetadataNotUpdated(BluetoothConnectionException.UnspecifiedConnectionError(event.errorCode))
172+
submitMetadata()
166173
}
167174

168175
is BleScannerHelper.DeviceConnectResult.DisconnectedWithError.ConnectionTimeout -> {
169176
Timber.tag(TAG).e("Connection timeout error from ${device.address}")
170-
disconnect(event.gatt)
171-
throw BluetoothConnectionException.ConnectionTimeoutException(event.errorCode)
177+
throwIfMetadataNotUpdated(BluetoothConnectionException.ConnectionTimeoutException(event.errorCode))
178+
submitMetadata()
172179
}
173180

174181
is BleScannerHelper.DeviceConnectResult.DisconnectedWithError.ConnectionFailedToEstablish -> {
175182
Timber.tag(TAG).e("Connection failed to establish error from ${device.address}")
176-
disconnect(event.gatt)
177-
throw BluetoothConnectionException.ConnectionFailedToEstablish(event.errorCode)
183+
throwIfMetadataNotUpdated(BluetoothConnectionException.ConnectionFailedToEstablish(event.errorCode))
184+
submitMetadata()
178185
}
179186

180187
is BleScannerHelper.DeviceConnectResult.DisconnectedWithError.ConnectionFailedBeforeInitializing -> {
181188
Timber.tag(TAG).e("Connection initializing failed error from ${device.address}")
182-
disconnect(event.gatt)
183-
throw BluetoothConnectionException.ConnectionInitializingFailed(event.errorCode)
189+
throwIfMetadataNotUpdated(BluetoothConnectionException.ConnectionInitializingFailed(event.errorCode))
190+
submitMetadata()
184191
}
185192

186193
is BleScannerHelper.DeviceConnectResult.DisconnectedWithError.ConnectionTerminated -> {
187194
Timber.tag(TAG).e("Connection terminated error from ${device.address}. Probably max GATT connections reached")
188-
disconnect(event.gatt)
189-
throw BluetoothConnectionException.ConnectionTerminated(event.errorCode)
195+
throwIfMetadataNotUpdated(BluetoothConnectionException.ConnectionTerminated(event.errorCode))
196+
submitMetadata()
190197
}
191198

192199
is BleScannerHelper.DeviceConnectResult.DisconnectedWithError.ConnectionFailedTooManyClients -> {
193200
Timber.tag(TAG).e("Connection failed due to too many clients error from ${device.address}")
194-
disconnect(event.gatt)
195-
throw BluetoothConnectionException.TooManyClients(event.errorCode)
201+
throwIfMetadataNotUpdated(BluetoothConnectionException.TooManyClients(event.errorCode))
202+
submitMetadata()
196203
}
197204

198205
else -> {

0 commit comments

Comments
 (0)