|
@ -8,6 +8,7 @@ import net.aiterp.git.ykonsole2.domain.runtime.* |
|
|
import net.aiterp.git.ykonsole2.infrastructure.drivers.abstracts.ActiveDriver |
|
|
import net.aiterp.git.ykonsole2.infrastructure.drivers.abstracts.ActiveDriver |
|
|
import net.aiterp.git.ykonsole2.infrastructure.iconsole.* |
|
|
import net.aiterp.git.ykonsole2.infrastructure.iconsole.* |
|
|
import net.aiterp.git.ykonsole2.application.logging.log |
|
|
import net.aiterp.git.ykonsole2.application.logging.log |
|
|
|
|
|
import java.time.Instant |
|
|
import java.util.* |
|
|
import java.util.* |
|
|
import kotlin.time.Duration |
|
|
import kotlin.time.Duration |
|
|
import kotlin.time.Duration.Companion.milliseconds |
|
|
import kotlin.time.Duration.Companion.milliseconds |
|
@ -20,7 +21,11 @@ class IConsole : ActiveDriver() { |
|
|
private var running = false |
|
|
private var running = false |
|
|
private var connected = false |
|
|
private var connected = false |
|
|
|
|
|
|
|
|
|
|
|
private var tryingSince: Instant? = null |
|
|
|
|
|
|
|
|
private var maxLevel = 20 |
|
|
private var maxLevel = 20 |
|
|
|
|
|
private var lastCals = 0 |
|
|
|
|
|
private var lastMeters = 0 |
|
|
|
|
|
|
|
|
private val queue = Collections.synchronizedSet<Request?>(LinkedHashSet()) |
|
|
private val queue = Collections.synchronizedSet<Request?>(LinkedHashSet()) |
|
|
|
|
|
|
|
@ -30,6 +35,8 @@ class IConsole : ActiveDriver() { |
|
|
private var btCommandInput: BluetoothGattCharacteristic? = null |
|
|
private var btCommandInput: BluetoothGattCharacteristic? = null |
|
|
|
|
|
|
|
|
private fun onConnect(device: Device, output: FlowBus<Event>) { |
|
|
private fun onConnect(device: Device, output: FlowBus<Event>) { |
|
|
|
|
|
tryingSince = Instant.now() |
|
|
|
|
|
|
|
|
if (!device.connectionString.startsWith("iconsole:")) { |
|
|
if (!device.connectionString.startsWith("iconsole:")) { |
|
|
logger.info("Ignoring non-iConsole $device") |
|
|
logger.info("Ignoring non-iConsole $device") |
|
|
return |
|
|
return |
|
@ -112,10 +119,13 @@ class IConsole : ActiveDriver() { |
|
|
|
|
|
|
|
|
if (currentTime > lastTime) { |
|
|
if (currentTime > lastTime) { |
|
|
lastTime = currentTime |
|
|
lastTime = currentTime |
|
|
|
|
|
lastCals = maxOf(res.calories, lastCals) |
|
|
|
|
|
lastMeters = maxOf((res.distance * 1000).toInt(), lastMeters) |
|
|
|
|
|
|
|
|
output.emitBlocking(ValuesReceived(listOf( |
|
|
output.emitBlocking(ValuesReceived(listOf( |
|
|
Time(lastTime), |
|
|
Time(lastTime), |
|
|
Calories(res.calories), |
|
|
|
|
|
Distance((res.distance * 1000).toInt()), |
|
|
|
|
|
|
|
|
Calories(lastCals), |
|
|
|
|
|
Distance(lastMeters), |
|
|
Level(res.level), |
|
|
Level(res.level), |
|
|
))) |
|
|
))) |
|
|
} |
|
|
} |
|
@ -130,11 +140,23 @@ class IConsole : ActiveDriver() { |
|
|
|
|
|
|
|
|
override fun onScanStopped() { |
|
|
override fun onScanStopped() { |
|
|
logger.info("Scan stopped") |
|
|
logger.info("Scan stopped") |
|
|
|
|
|
|
|
|
|
|
|
tryingSince?.let { ts -> |
|
|
|
|
|
if (ts.isBefore(Instant.now().minusSeconds(20))) { |
|
|
|
|
|
central?.stopScan() |
|
|
|
|
|
tryingSince = null |
|
|
|
|
|
runBlocking { |
|
|
|
|
|
output.emit(ErrorOccurred("Connection timeout after 20 seconds")) |
|
|
|
|
|
output.emit(Disconnected) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
override fun onDiscoveredPeripheral(peripheral: BluetoothPeripheral, scanResult: ScanResult) { |
|
|
override fun onDiscoveredPeripheral(peripheral: BluetoothPeripheral, scanResult: ScanResult) { |
|
|
logger.info("Connecting to ${peripheral.name} (${peripheral.address})...") |
|
|
logger.info("Connecting to ${peripheral.name} (${peripheral.address})...") |
|
|
|
|
|
|
|
|
|
|
|
tryingSince = null |
|
|
central?.connectPeripheral(peripheral, cbPeripheral) |
|
|
central?.connectPeripheral(peripheral, cbPeripheral) |
|
|
central?.stopScan() |
|
|
central?.stopScan() |
|
|
} |
|
|
} |
|
@ -154,6 +176,8 @@ class IConsole : ActiveDriver() { |
|
|
|
|
|
|
|
|
private fun onDisconnect() { |
|
|
private fun onDisconnect() { |
|
|
lastTime = 0 |
|
|
lastTime = 0 |
|
|
|
|
|
lastCals = 0 |
|
|
|
|
|
lastMeters = 0 |
|
|
running = false |
|
|
running = false |
|
|
connected = false |
|
|
connected = false |
|
|
|
|
|
|
|
|