Skip to content

Commit

Permalink
Refactored a bit more of DashboardSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Lesches committed Jul 20, 2023
1 parent 99c8a2f commit 7960e0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/src/models/data/sockets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions lib/src/services/socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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<void>.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.
Expand Down

0 comments on commit 7960e0a

Please sign in to comment.