mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-07-11 11:07:04 +10:00
fix: address PR #296 code review feedback
- Clamp ML predictions between physics floor (raw airtime) and ceiling (worst-case formula) so model can never produce unsafe timeouts - Replace hourOfDay feature with secondsSinceLastRx for network activity - Remove unused _ContactStats.stdDev and dead model persistence code - Debounce observation writes (2s) instead of writing on every delivery - Skip recording observations when pathLength is null to avoid corrupting training data - Add comment explaining global (not per-contact) RX time tracking - Remove notifyListeners from retrain to avoid unnecessary widget rebuilds - Run dart format
This commit is contained in:
@@ -168,6 +168,8 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
bool _isLoadingChannels = false;
|
||||
bool _hasLoadedChannels = false;
|
||||
TimeoutPredictionService? _timeoutPredictionService;
|
||||
// Intentionally global (not per-contact): tracks overall network activity.
|
||||
// Frequent RX from any source indicates a busy network with more collisions.
|
||||
DateTime _lastRxTime = DateTime.now();
|
||||
bool _batteryRequested = false;
|
||||
bool _awaitingSelfInfo = false;
|
||||
@@ -694,23 +696,28 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
updateMessageCallback: _updateMessage,
|
||||
clearContactPathCallback: clearContactPath,
|
||||
setContactPathCallback: setContactPath,
|
||||
calculateTimeoutCallback: (pathLength, messageBytes, {String? contactKey}) =>
|
||||
calculateTimeout(pathLength: pathLength, messageBytes: messageBytes, contactKey: contactKey),
|
||||
calculateTimeoutCallback:
|
||||
(pathLength, messageBytes, {String? contactKey}) => calculateTimeout(
|
||||
pathLength: pathLength,
|
||||
messageBytes: messageBytes,
|
||||
contactKey: contactKey,
|
||||
),
|
||||
getSelfPublicKeyCallback: () => _selfPublicKey,
|
||||
prepareContactOutboundTextCallback: prepareContactOutboundText,
|
||||
appSettingsService: appSettingsService,
|
||||
debugLogService: _appDebugLogService,
|
||||
recordPathResultCallback: _recordPathResult,
|
||||
onDeliveryObservedCallback: (contactKey, pathLength, messageBytes, tripTimeMs) {
|
||||
final secSinceRx = DateTime.now().difference(_lastRxTime).inSeconds;
|
||||
_timeoutPredictionService?.recordObservation(
|
||||
contactKey: contactKey,
|
||||
pathLength: pathLength,
|
||||
messageBytes: messageBytes,
|
||||
tripTimeMs: tripTimeMs,
|
||||
secondsSinceLastRx: secSinceRx,
|
||||
);
|
||||
},
|
||||
onDeliveryObservedCallback:
|
||||
(contactKey, pathLength, messageBytes, tripTimeMs) {
|
||||
final secSinceRx = DateTime.now().difference(_lastRxTime).inSeconds;
|
||||
_timeoutPredictionService?.recordObservation(
|
||||
contactKey: contactKey,
|
||||
pathLength: pathLength,
|
||||
messageBytes: messageBytes,
|
||||
tripTimeMs: tripTimeMs,
|
||||
secondsSinceLastRx: secSinceRx,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2890,14 +2897,54 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate timeout for a message based on radio settings and path length
|
||||
/// Returns timeout in milliseconds, considering number of hops
|
||||
/// Estimate single-packet airtime in ms from radio settings, or a fallback.
|
||||
int _estimateAirtimeMs(int messageBytes) {
|
||||
if (_currentFreqHz != null &&
|
||||
_currentBwHz != null &&
|
||||
_currentSf != null &&
|
||||
_currentCr != null) {
|
||||
final cr = _currentCr! <= 4 ? _currentCr! : _currentCr! - 4;
|
||||
return calculateLoRaAirtime(
|
||||
payloadBytes: messageBytes,
|
||||
spreadingFactor: _currentSf!,
|
||||
bandwidthHz: _currentBwHz!,
|
||||
codingRate: cr,
|
||||
lowDataRateOptimize: _currentSf! >= 11,
|
||||
);
|
||||
}
|
||||
return 50; // fallback: ~SF7/BW125 for 100 bytes
|
||||
}
|
||||
|
||||
/// Physics-based worst-case timeout (ceiling).
|
||||
int _physicsMaxTimeout(int pathLength, int airtime) {
|
||||
if (pathLength < 0) {
|
||||
return 500 + (16 * airtime);
|
||||
} else {
|
||||
return 500 + ((airtime * 6 + 250) * (pathLength + 1));
|
||||
}
|
||||
}
|
||||
|
||||
/// Physics-based minimum timeout (floor): raw traversal time.
|
||||
int _physicsMinTimeout(int pathLength, int airtime) {
|
||||
if (pathLength < 0) {
|
||||
return airtime;
|
||||
} else {
|
||||
return airtime * (pathLength + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate timeout for a message based on radio settings and path length.
|
||||
/// Returns timeout in milliseconds, considering number of hops.
|
||||
int calculateTimeout({
|
||||
required int pathLength,
|
||||
int messageBytes = 100,
|
||||
String? contactKey,
|
||||
}) {
|
||||
// Try ML-based prediction first
|
||||
final airtime = _estimateAirtimeMs(messageBytes);
|
||||
final physicsMin = _physicsMinTimeout(pathLength, airtime);
|
||||
final physicsMax = _physicsMaxTimeout(pathLength, airtime);
|
||||
|
||||
// Try ML-based prediction, clamped between physics bounds
|
||||
final secSinceRx = DateTime.now().difference(_lastRxTime).inSeconds;
|
||||
final mlTimeout = _timeoutPredictionService?.predictTimeout(
|
||||
contactKey: contactKey,
|
||||
@@ -2905,35 +2952,11 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
messageBytes: messageBytes,
|
||||
secondsSinceLastRx: secSinceRx,
|
||||
);
|
||||
if (mlTimeout != null) return mlTimeout;
|
||||
|
||||
// If we have radio settings, use them for accurate calculation
|
||||
if (_currentFreqHz != null &&
|
||||
_currentBwHz != null &&
|
||||
_currentSf != null &&
|
||||
_currentCr != null) {
|
||||
final cr = _currentCr! <= 4 ? _currentCr! : _currentCr! - 4;
|
||||
return calculateMessageTimeout(
|
||||
freqHz: _currentFreqHz!,
|
||||
bwHz: _currentBwHz!,
|
||||
sf: _currentSf!,
|
||||
cr: cr,
|
||||
pathLength: pathLength,
|
||||
messageBytes: messageBytes,
|
||||
);
|
||||
if (mlTimeout != null) {
|
||||
return mlTimeout.clamp(physicsMin, physicsMax);
|
||||
}
|
||||
|
||||
// Fallback: Conservative estimates based on typical settings
|
||||
// Assume SF7, BW125, which gives ~50ms airtime for 100 bytes
|
||||
const estimatedAirtime = 50;
|
||||
|
||||
if (pathLength < 0) {
|
||||
// Flood mode: Base delay + 16× airtime
|
||||
return 500 + (16 * estimatedAirtime);
|
||||
} else {
|
||||
// Direct path: Base delay + ((airtime×6 + 250ms)×(hops+1))
|
||||
return 500 + ((estimatedAirtime * 6 + 250) * (pathLength + 1));
|
||||
}
|
||||
return physicsMax;
|
||||
}
|
||||
|
||||
void _handleContact(Uint8List frame, {bool isContact = true}) {
|
||||
|
||||
Reference in New Issue
Block a user