Merge upstream/dev (8 commits) [upstream-sync]
This commit is contained in:
commit
65097d053d
76 files changed
+2533
-1050
No files matched your search
@@ -843,9 +843,12 @@ class MessageRetryService extends ChangeNotifier {
|
||||
tripTimeMs,
|
||||
);
|
||||
}
|
||||
// Learn from the attempt this ACK belongs to, not the latest one: a
|
||||
// late ACK for a direct attempt can land after a retry went flood.
|
||||
if (config?.onDeliveryObserved != null &&
|
||||
tripTimeMs > 0 &&
|
||||
message.pathLength != null) {
|
||||
selection != null &&
|
||||
!ambiguousPath) {
|
||||
final outboundTextForObserved =
|
||||
config!.prepareContactOutboundText?.call(contact, message.text) ??
|
||||
message.text;
|
||||
@@ -854,7 +857,7 @@ class MessageRetryService extends ChangeNotifier {
|
||||
.length;
|
||||
config.onDeliveryObserved!(
|
||||
contact.publicKeyHex,
|
||||
message.pathLength!,
|
||||
selection.useFlood ? -1 : selection.hopCount,
|
||||
messageBytesForObserved,
|
||||
tripTimeMs,
|
||||
);
|
||||
|
||||
@@ -357,8 +357,9 @@ class PathHistoryService extends ChangeNotifier {
|
||||
|
||||
updatedPaths.removeWhere((p) => _pathsEqual(p.pathBytes, pathBytes));
|
||||
|
||||
// Make room by dropping the weakest path so new discoveries still land.
|
||||
if (existing == null && updatedPaths.length >= _maxHistoryEntries) {
|
||||
return;
|
||||
updatedPaths.remove(_lowestScoredPath(updatedPaths));
|
||||
}
|
||||
|
||||
updatedPaths.insert(0, newRecord);
|
||||
@@ -540,6 +541,17 @@ class PathHistoryService extends ChangeNotifier {
|
||||
return ranked;
|
||||
}
|
||||
|
||||
PathRecord _lowestScoredPath(List<PathRecord> paths) {
|
||||
final fastestTripMs = _getFastestKnownTripMs(paths);
|
||||
final highestRouteWeight = _getHighestKnownRouteWeight(paths);
|
||||
double score(PathRecord p) => _scorePathRecord(
|
||||
p,
|
||||
fastestTripMs: fastestTripMs,
|
||||
highestRouteWeight: highestRouteWeight,
|
||||
);
|
||||
return paths.reduce((a, b) => score(b) < score(a) ? b : a);
|
||||
}
|
||||
|
||||
int? _getFastestKnownTripMs(List<PathRecord> paths) {
|
||||
final knownTrips = paths
|
||||
.where((path) => path.tripTimeMs > 0)
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../connector/meshcore_connector.dart';
|
||||
import '../connector/meshcore_protocol.dart';
|
||||
import '../helpers/cayenne_lpp.dart';
|
||||
import '../models/contact.dart';
|
||||
import '../models/path_selection.dart';
|
||||
import '../utils/app_logger.dart';
|
||||
|
||||
enum TelemetryLocationResult { located, noLocation, failed }
|
||||
|
||||
/// Returns the first valid GPS fix in parsed Cayenne LPP telemetry, or null.
|
||||
({double latitude, double longitude})? telemetryGpsFix(
|
||||
List<Map<String, dynamic>> entries,
|
||||
) {
|
||||
for (final entry in entries) {
|
||||
final values = entry['values'];
|
||||
if (values is! Map) continue;
|
||||
final gps = values['gps'];
|
||||
if (gps is! Map) continue;
|
||||
final lat = gps['latitude'];
|
||||
final lon = gps['longitude'];
|
||||
if (lat is! num || lon is! num) continue;
|
||||
final latitude = lat.toDouble();
|
||||
final longitude = lon.toDouble();
|
||||
const double epsilon = 1e-6;
|
||||
final valid =
|
||||
(latitude.abs() > epsilon || longitude.abs() > epsilon) &&
|
||||
latitude >= -90.0 &&
|
||||
latitude <= 90.0 &&
|
||||
longitude >= -180.0 &&
|
||||
longitude <= 180.0;
|
||||
if (valid) return (latitude: latitude, longitude: longitude);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Asks [contact] for its telemetry and, when the reply carries a GPS fix,
|
||||
/// stores it on the contact so the map moves the node right away.
|
||||
Future<TelemetryLocationResult> requestTelemetryLocation(
|
||||
MeshCoreConnector connector,
|
||||
Contact contact,
|
||||
) async {
|
||||
final completer = Completer<List<Map<String, dynamic>>?>();
|
||||
// Armed just before our send: a background retry's RESP_CODE_SENT that
|
||||
// lands while the path is being prepared must not be taken for ours.
|
||||
var awaitingSent = false;
|
||||
int? tag;
|
||||
Timer? timeout;
|
||||
void finish(List<Map<String, dynamic>>? parsed) {
|
||||
if (completer.isCompleted) return;
|
||||
timeout?.cancel();
|
||||
completer.complete(parsed);
|
||||
}
|
||||
|
||||
final isChat = contact.type == advTypeChat;
|
||||
final subscription = connector.receivedFrames.listen((frame) {
|
||||
if (frame.isEmpty || completer.isCompleted) return;
|
||||
final reader = BufferReader(frame);
|
||||
try {
|
||||
final cmd = reader.readByte();
|
||||
if (cmd == respCodeSent && awaitingSent) {
|
||||
awaitingSent = false;
|
||||
reader.skipBytes(1); // reserved
|
||||
tag = reader.readUInt32LE();
|
||||
final tripTime = reader.readUInt32LE();
|
||||
timeout?.cancel();
|
||||
timeout = Timer(Duration(milliseconds: tripTime), () => finish(null));
|
||||
} else if (cmd == respCodeErr && awaitingSent) {
|
||||
awaitingSent = false;
|
||||
finish(null);
|
||||
} else if (cmd == pushCodeBinaryResponse) {
|
||||
// Binary requests always answer here. A chat telemetry reply also
|
||||
// lands here, tagged, when another request replaced it as the
|
||||
// radio's pending request.
|
||||
reader.skipBytes(1); // reserved
|
||||
if (tag == null || reader.readUInt32LE() != tag) return;
|
||||
finish(CayenneLpp.parseByChannel(reader.readRemainingBytes()));
|
||||
} else if (cmd == pushCodeTelemetryResponse && isChat) {
|
||||
reader.skipBytes(1); // reserved
|
||||
final prefix = reader.readBytes(6);
|
||||
if (!listEquals(contact.publicKey.sublist(0, 6), prefix)) return;
|
||||
finish(CayenneLpp.parseByChannel(reader.readRemainingBytes()));
|
||||
}
|
||||
} catch (e) {
|
||||
appLogger.error('Error parsing telemetry location frame: $e');
|
||||
}
|
||||
});
|
||||
|
||||
PathSelection? selection;
|
||||
try {
|
||||
selection = await connector.preparePathForContactSend(contact);
|
||||
awaitingSent = true;
|
||||
// Guards the wait for the radio's send ack; its trip time replaces this.
|
||||
timeout = Timer(const Duration(seconds: 10), () => finish(null));
|
||||
await connector.sendFrame(
|
||||
isChat
|
||||
? buildSendTelemetryReq(contact.publicKey)
|
||||
: buildSendBinaryReq(
|
||||
contact.publicKey,
|
||||
payload: buildTelemetryBinaryPayload(),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
appLogger.error('Telemetry location request failed: $e');
|
||||
finish(null);
|
||||
}
|
||||
|
||||
final parsed = await completer.future;
|
||||
await subscription.cancel();
|
||||
if (selection != null) {
|
||||
connector.recordRepeaterPathResult(
|
||||
contact,
|
||||
selection,
|
||||
parsed != null,
|
||||
null,
|
||||
);
|
||||
}
|
||||
if (parsed == null) return TelemetryLocationResult.failed;
|
||||
final fix = telemetryGpsFix(parsed);
|
||||
if (fix == null) return TelemetryLocationResult.noLocation;
|
||||
connector.updateContactLocationFromTelemetry(
|
||||
contact.publicKeyHex,
|
||||
fix.latitude,
|
||||
fix.longitude,
|
||||
);
|
||||
return TelemetryLocationResult.located;
|
||||
}
|
||||
@@ -65,7 +65,6 @@ class TimeoutPredictionService extends ChangeNotifier {
|
||||
required int pathLength,
|
||||
required int messageBytes,
|
||||
required int tripTimeMs,
|
||||
int secondsSinceLastRx = 0,
|
||||
}) {
|
||||
if (recordingSuspended) return;
|
||||
final isFlood = pathLength < 0;
|
||||
@@ -75,7 +74,6 @@ class TimeoutPredictionService extends ChangeNotifier {
|
||||
// only; isFlood carries the flood signal as a separate feature.
|
||||
pathLength: isFlood ? 0 : pathLength,
|
||||
messageBytes: messageBytes,
|
||||
secondsSinceLastRx: secondsSinceLastRx,
|
||||
isFlood: isFlood,
|
||||
deliveryMs: tripTimeMs,
|
||||
timestamp: DateTime.now(),
|
||||
@@ -110,7 +108,6 @@ class TimeoutPredictionService extends ChangeNotifier {
|
||||
String? contactKey,
|
||||
required int pathLength,
|
||||
required int messageBytes,
|
||||
int secondsSinceLastRx = 0,
|
||||
}) {
|
||||
if (_model == null) return null;
|
||||
|
||||
@@ -123,7 +120,6 @@ class TimeoutPredictionService extends ChangeNotifier {
|
||||
// prediction see the same pathLength values; isFlood carries the signal.
|
||||
'pathLength': flood ? 0.0 : pathLength.toDouble(),
|
||||
'messageBytes': messageBytes.toDouble(),
|
||||
'secSinceRx': secondsSinceLastRx.toDouble(),
|
||||
'isFlood': flood ? 1.0 : 0.0,
|
||||
};
|
||||
final row = _activeFeatures.map((f) => allFeatures[f]!).toList();
|
||||
@@ -174,13 +170,12 @@ class TimeoutPredictionService extends ChangeNotifier {
|
||||
try {
|
||||
// Build feature columns, then exclude any with zero variance
|
||||
// (ml_algo's OLS produces all-zero coefficients for singular matrices)
|
||||
final allNames = ['pathLength', 'messageBytes', 'secSinceRx', 'isFlood'];
|
||||
final allNames = ['pathLength', 'messageBytes', 'isFlood'];
|
||||
final allExtractors = <double Function(DeliveryObservation)>[
|
||||
// pathLength is already clamped to >=0 in recordObservation, but guard
|
||||
// here as well for any observations loaded from older persisted data.
|
||||
(o) => o.pathLength < 0 ? 0.0 : o.pathLength.toDouble(),
|
||||
(o) => o.messageBytes.toDouble(),
|
||||
(o) => o.secondsSinceLastRx.toDouble(),
|
||||
(o) => o.isFlood ? 1.0 : 0.0,
|
||||
];
|
||||
|
||||
|
||||
Reference in new issue
Block a user