-
Notifications
You must be signed in to change notification settings - Fork 39
/
BluetoothGattInteractor.kt
110 lines (98 loc) · 4.51 KB
/
BluetoothGattInteractor.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.ruuvi.station.bluetooth.domain
import com.ruuvi.station.bluetooth.BluetoothInteractor
import com.ruuvi.station.bluetooth.IRuuviGattListener
import com.ruuvi.station.bluetooth.LogReading
import com.ruuvi.station.bluetooth.model.GattSyncStatus
import com.ruuvi.station.bluetooth.model.SyncProgress
import com.ruuvi.station.database.domain.SensorHistoryRepository
import com.ruuvi.station.database.domain.SensorSettingsRepository
import com.ruuvi.station.database.domain.TagRepository
import com.ruuvi.station.database.tables.TagSensorReading
import com.ruuvi.station.firebase.domain.FirebaseInteractor
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import timber.log.Timber
import java.util.*
class BluetoothGattInteractor (
private val interactor: BluetoothInteractor,
private val tagRepository: TagRepository,
private val sensorSettingsRepository: SensorSettingsRepository,
private val sensorHistoryRepository: SensorHistoryRepository,
private val firebaseInteractor: FirebaseInteractor
) {
private val syncStatus = MutableStateFlow<GattSyncStatus?> (null)
val syncStatusFlow: StateFlow<GattSyncStatus?> = syncStatus
fun readLogs(sensorId: String, from: Date?) {
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.CONNECTING))
val found= interactor.readLogs(sensorId, from, object : IRuuviGattListener {
override fun connected(state: Boolean) {
if (state) {
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.CONNECTED))
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.READING_INFO))
} else {
if (syncStatus.value?.syncProgress == SyncProgress.SAVING_DATA) {
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.DONE))
} else {
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.DISCONNECTED))
}
}
}
override fun deviceInfo(model: String, fw: String, canReadLogs: Boolean) {
if (canReadLogs) {
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.READING_DATA, model, fw))
} else {
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.NOT_SUPPORTED, model, fw))
}
}
override fun dataReady(data: List<LogReading>) {
setSyncStatus(GattSyncStatus(
sensorId,
SyncProgress.SAVING_DATA,
readDataSize = data.size
))
saveGattReadings(sensorId, data)
}
override fun syncProgress(syncedDataPoints: Int) {
// Throttle UI updates, this might have to be tweaked.
// Maybe a time based thing would be better?
if (syncedDataPoints % 10 == 0) {
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.READING_DATA, syncedDataPoints = syncedDataPoints))
}
}
override fun heartbeat(raw: String) {
}
})
if (!found)
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.NOT_FOUND))
}
fun disconnect(id: String): Boolean {
return interactor.disconnect(id)
}
fun setSyncStatus(status: GattSyncStatus) {
Timber.d("syncStatusFlow sent $status")
syncStatus.value = status
}
fun saveGattReadings(sensorId: String, data: List<LogReading>) {
Timber.d("saveGattReadings")
firebaseInteractor.logGattSync(data.size)
val tagReadingList = mutableListOf<TagSensorReading>()
val sensorSettings = sensorSettingsRepository.getSensorSettings(sensorId)
data.forEach { logReading ->
val reading = TagSensorReading()
reading.ruuviTagId = sensorId
reading.temperature = logReading.temperature
reading.humidity = logReading.humidity
reading.pressure = logReading.pressure
reading.createdAt = logReading.date
sensorSettings?.calibrateSensor(reading)
tagReadingList.add(reading)
}
sensorHistoryRepository.bulkInsert(sensorId, tagReadingList)
updateLastSync(sensorId, Date())
}
fun updateLastSync(sensorId: String, date: Date?) =
sensorSettingsRepository.updateLastSync(sensorId, date)
fun resetGattStatus(sensorId: String) {
setSyncStatus(GattSyncStatus(sensorId, SyncProgress.STILL))
}
}