From 7960e0a2efd47c47bad6d86ed7f0ef91343c92a2 Mon Sep 17 00:00:00 2001 From: Levi Lesches Date: Thu, 20 Jul 2023 17:24:59 -0400 Subject: [PATCH] Refactored a bit more of DashboardSocket --- lib/src/models/data/sockets.dart | 2 +- lib/src/services/socket.dart | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/src/models/data/sockets.dart b/lib/src/models/data/sockets.dart index 6edfb5a25..24d01e1ce 100644 --- a/lib/src/models/data/sockets.dart +++ b/lib/src/models/data/sockets.dart @@ -34,7 +34,7 @@ class Sockets extends Model { allowedFallthrough: {AutonomyData().messageName}, ); - /// A UDP socket for controlling rover position + /// A UDP socket for controlling the MARS subsystem. late final mars = DashboardSocket( device: Device.MARS_SERVER, onConnect: onConnect, diff --git a/lib/src/services/socket.dart b/lib/src/services/socket.dart index 78ba2bb23..adc2b65dc 100644 --- a/lib/src/services/socket.dart +++ b/lib/src/services/socket.dart @@ -50,27 +50,30 @@ class DashboardSocket extends ProtoSocket with WrapperRegistry implements Servic @override void updateSettings(UpdateSetting settings) { } + @override + void onHeartbeat(Connect heartbeat, SocketInfo source) => _heartbeats++; + @override Future checkHeartbeats() async { if (_isChecking) return; + // 1. Clear state and send a heartbeat _isChecking = true; _heartbeats = 0; + final wasConnected = isConnected; sendMessage(Connect(sender: Device.DASHBOARD, receiver: device)); + // 2. Wait a bit and count the number of responses await Future.delayed(heartbeatWaitDelay); - final wasConnected = isConnected; if (_heartbeats > 0) { - if (!wasConnected) onConnect(device); connectionStrength.value += connectionIncrement * _heartbeats; } else { - if (wasConnected) onDisconnect(device); connectionStrength.value -= connectionIncrement; } + // 3. Assess the current state connectionStrength.value = connectionStrength.value.clamp(0, 1); + if (isConnected && !wasConnected) onConnect(device); + if (wasConnected && !isConnected) onDisconnect(device); _isChecking = false; } - - @override - void onHeartbeat(Connect heartbeat, SocketInfo source) => _heartbeats++; } /// How much each successful/missed handshake is worth, as a percent.