mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-06-15 15:14:26 +10:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fffcff3b74 | |||
| b336aedbc5 | |||
| 2ee2358ecc |
@@ -19,6 +19,7 @@ import '../services/message_retry_service.dart';
|
|||||||
import '../services/path_history_service.dart';
|
import '../services/path_history_service.dart';
|
||||||
import '../services/app_settings_service.dart';
|
import '../services/app_settings_service.dart';
|
||||||
import '../services/background_service.dart';
|
import '../services/background_service.dart';
|
||||||
|
import '../services/timeout_prediction_service.dart';
|
||||||
import '../services/notification_service.dart';
|
import '../services/notification_service.dart';
|
||||||
import 'meshcore_connector_usb.dart';
|
import 'meshcore_connector_usb.dart';
|
||||||
import 'meshcore_connector_tcp.dart';
|
import 'meshcore_connector_tcp.dart';
|
||||||
@@ -166,6 +167,10 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
bool _isLoadingContacts = false;
|
bool _isLoadingContacts = false;
|
||||||
bool _isLoadingChannels = false;
|
bool _isLoadingChannels = false;
|
||||||
bool _hasLoadedChannels = 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 _batteryRequested = false;
|
||||||
bool _awaitingSelfInfo = false;
|
bool _awaitingSelfInfo = false;
|
||||||
bool _hasReceivedDeviceInfo = false;
|
bool _hasReceivedDeviceInfo = false;
|
||||||
@@ -289,10 +294,6 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Contact> get allContacts => List.unmodifiable([
|
|
||||||
..._contacts,
|
|
||||||
..._discoveredContacts.where((c) => !c.isActive),
|
|
||||||
]);
|
|
||||||
List<Contact> get discoveredContacts {
|
List<Contact> get discoveredContacts {
|
||||||
return List.unmodifiable(_discoveredContacts);
|
return List.unmodifiable(_discoveredContacts);
|
||||||
}
|
}
|
||||||
@@ -672,6 +673,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
BleDebugLogService? bleDebugLogService,
|
BleDebugLogService? bleDebugLogService,
|
||||||
AppDebugLogService? appDebugLogService,
|
AppDebugLogService? appDebugLogService,
|
||||||
BackgroundService? backgroundService,
|
BackgroundService? backgroundService,
|
||||||
|
TimeoutPredictionService? timeoutPredictionService,
|
||||||
}) {
|
}) {
|
||||||
_retryService = retryService;
|
_retryService = retryService;
|
||||||
_pathHistoryService = pathHistoryService;
|
_pathHistoryService = pathHistoryService;
|
||||||
@@ -679,6 +681,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
_bleDebugLogService = bleDebugLogService;
|
_bleDebugLogService = bleDebugLogService;
|
||||||
_appDebugLogService = appDebugLogService;
|
_appDebugLogService = appDebugLogService;
|
||||||
_backgroundService = backgroundService;
|
_backgroundService = backgroundService;
|
||||||
|
_timeoutPredictionService = timeoutPredictionService;
|
||||||
_usbManager.setDebugLogService(_appDebugLogService);
|
_usbManager.setDebugLogService(_appDebugLogService);
|
||||||
_tcpConnector.setDebugLogService(_appDebugLogService);
|
_tcpConnector.setDebugLogService(_appDebugLogService);
|
||||||
|
|
||||||
@@ -693,13 +696,28 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
updateMessageCallback: _updateMessage,
|
updateMessageCallback: _updateMessage,
|
||||||
clearContactPathCallback: clearContactPath,
|
clearContactPathCallback: clearContactPath,
|
||||||
setContactPathCallback: setContactPath,
|
setContactPathCallback: setContactPath,
|
||||||
calculateTimeoutCallback: (pathLength, messageBytes) =>
|
calculateTimeoutCallback:
|
||||||
calculateTimeout(pathLength: pathLength, messageBytes: messageBytes),
|
(pathLength, messageBytes, {String? contactKey}) => calculateTimeout(
|
||||||
|
pathLength: pathLength,
|
||||||
|
messageBytes: messageBytes,
|
||||||
|
contactKey: contactKey,
|
||||||
|
),
|
||||||
getSelfPublicKeyCallback: () => _selfPublicKey,
|
getSelfPublicKeyCallback: () => _selfPublicKey,
|
||||||
prepareContactOutboundTextCallback: prepareContactOutboundText,
|
prepareContactOutboundTextCallback: prepareContactOutboundText,
|
||||||
appSettingsService: appSettingsService,
|
appSettingsService: appSettingsService,
|
||||||
debugLogService: _appDebugLogService,
|
debugLogService: _appDebugLogService,
|
||||||
recordPathResultCallback: _recordPathResult,
|
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,
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -708,9 +726,6 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
_knownContactKeys
|
_knownContactKeys
|
||||||
..clear()
|
..clear()
|
||||||
..addAll(cached.map((c) => c.publicKeyHex));
|
..addAll(cached.map((c) => c.publicKeyHex));
|
||||||
_contacts
|
|
||||||
..clear()
|
|
||||||
..addAll(cached);
|
|
||||||
for (final contact in cached) {
|
for (final contact in cached) {
|
||||||
_ensureContactSmazSettingLoaded(contact.publicKeyHex);
|
_ensureContactSmazSettingLoaded(contact.publicKeyHex);
|
||||||
}
|
}
|
||||||
@@ -1543,10 +1558,6 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
|
|
||||||
if (_activeTransport == MeshCoreTransportType.usb) {
|
if (_activeTransport == MeshCoreTransportType.usb) {
|
||||||
await _usbManager.write(data);
|
await _usbManager.write(data);
|
||||||
// Brief pause so the device firmware can process each frame before the
|
|
||||||
// next arrives. Without this, rapid-fire frames over USB can cause the
|
|
||||||
// device to miss responses (especially on reconnect).
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 10));
|
|
||||||
} else if (_activeTransport == MeshCoreTransportType.tcp) {
|
} else if (_activeTransport == MeshCoreTransportType.tcp) {
|
||||||
await _tcpConnector.write(data);
|
await _tcpConnector.write(data);
|
||||||
} else {
|
} else {
|
||||||
@@ -2509,6 +2520,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
|
|
||||||
void _handleFrame(List<int> data) {
|
void _handleFrame(List<int> data) {
|
||||||
if (data.isEmpty) return;
|
if (data.isEmpty) return;
|
||||||
|
_lastRxTime = DateTime.now();
|
||||||
|
|
||||||
final frame = Uint8List.fromList(data);
|
final frame = Uint8List.fromList(data);
|
||||||
_receivedFramesController.add(frame);
|
_receivedFramesController.add(frame);
|
||||||
@@ -2885,43 +2897,71 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate timeout for a message based on radio settings and path length
|
/// Estimate single-packet airtime in ms from radio settings, or a fallback.
|
||||||
/// Returns timeout in milliseconds, considering number of hops
|
int _estimateAirtimeMs(int messageBytes) {
|
||||||
int calculateTimeout({required int pathLength, int messageBytes = 100}) {
|
|
||||||
// If we have radio settings, use them for accurate calculation
|
|
||||||
if (_currentFreqHz != null &&
|
if (_currentFreqHz != null &&
|
||||||
_currentBwHz != null &&
|
_currentBwHz != null &&
|
||||||
_currentSf != null &&
|
_currentSf != null &&
|
||||||
_currentCr != null) {
|
_currentCr != null) {
|
||||||
final cr = _currentCr! <= 4 ? _currentCr! : _currentCr! - 4;
|
final cr = _currentCr! <= 4 ? _currentCr! : _currentCr! - 4;
|
||||||
return calculateMessageTimeout(
|
return calculateLoRaAirtime(
|
||||||
freqHz: _currentFreqHz!,
|
payloadBytes: messageBytes,
|
||||||
bwHz: _currentBwHz!,
|
spreadingFactor: _currentSf!,
|
||||||
sf: _currentSf!,
|
bandwidthHz: _currentBwHz!,
|
||||||
cr: cr,
|
codingRate: cr,
|
||||||
pathLength: pathLength,
|
lowDataRateOptimize: _currentSf! >= 11,
|
||||||
messageBytes: messageBytes,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
return 50; // fallback: ~SF7/BW125 for 100 bytes
|
||||||
|
}
|
||||||
|
|
||||||
// Fallback: Conservative estimates based on typical settings
|
/// Physics-based worst-case timeout (ceiling).
|
||||||
// Assume SF7, BW125, which gives ~50ms airtime for 100 bytes
|
int _physicsMaxTimeout(int pathLength, int airtime) {
|
||||||
const estimatedAirtime = 50;
|
|
||||||
|
|
||||||
if (pathLength < 0) {
|
if (pathLength < 0) {
|
||||||
// Flood mode: Base delay + 16× airtime
|
return 500 + (16 * airtime);
|
||||||
return 500 + (16 * estimatedAirtime);
|
|
||||||
} else {
|
} else {
|
||||||
// Direct path: Base delay + ((airtime×6 + 250ms)×(hops+1))
|
return 500 + ((airtime * 6 + 250) * (pathLength + 1));
|
||||||
return 500 + ((estimatedAirtime * 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,
|
||||||
|
}) {
|
||||||
|
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,
|
||||||
|
pathLength: pathLength,
|
||||||
|
messageBytes: messageBytes,
|
||||||
|
secondsSinceLastRx: secSinceRx,
|
||||||
|
);
|
||||||
|
if (mlTimeout != null) {
|
||||||
|
return mlTimeout.clamp(physicsMin, physicsMax);
|
||||||
|
}
|
||||||
|
|
||||||
|
return physicsMax;
|
||||||
|
}
|
||||||
|
|
||||||
void _handleContact(Uint8List frame, {bool isContact = true}) {
|
void _handleContact(Uint8List frame, {bool isContact = true}) {
|
||||||
final contact = Contact.fromFrame(frame);
|
final contact = Contact.fromFrame(frame);
|
||||||
if (contact != null) {
|
if (contact != null) {
|
||||||
_handleDiscovery(contact, frame, noNotify: true, addActive: true);
|
|
||||||
|
|
||||||
if (contact.type == advTypeRepeater) {
|
if (contact.type == advTypeRepeater) {
|
||||||
_contactUnreadCount.remove(contact.publicKeyHex);
|
_contactUnreadCount.remove(contact.publicKeyHex);
|
||||||
_unreadStore.saveContactUnreadCount(
|
_unreadStore.saveContactUnreadCount(
|
||||||
@@ -4730,12 +4770,6 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
(_autoAddRoomServers && type == advTypeRoom) ||
|
(_autoAddRoomServers && type == advTypeRoom) ||
|
||||||
(_autoAddSensors && type == advTypeSensor)) {
|
(_autoAddSensors && type == advTypeSensor)) {
|
||||||
_handleContactAdvert(newContact);
|
_handleContactAdvert(newContact);
|
||||||
_handleDiscovery(
|
|
||||||
newContact,
|
|
||||||
rawPacket,
|
|
||||||
noNotify: true,
|
|
||||||
addActive: true,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
_handleDiscovery(newContact, rawPacket);
|
_handleDiscovery(newContact, rawPacket);
|
||||||
}
|
}
|
||||||
@@ -4760,20 +4794,8 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
|
|
||||||
// CRITICAL: Preserve user's path override when contact is refreshed from device
|
// CRITICAL: Preserve user's path override when contact is refreshed from device
|
||||||
_contacts[existingIndex] = existing.copyWith(
|
_contacts[existingIndex] = existing.copyWith(
|
||||||
latitude:
|
latitude: hasLocation ? latitude : existing.latitude,
|
||||||
hasLocation &&
|
longitude: hasLocation ? longitude : existing.longitude,
|
||||||
latitude != null &&
|
|
||||||
latitude.abs() <= 90 &&
|
|
||||||
(latitude != 0 || longitude != 0)
|
|
||||||
? latitude
|
|
||||||
: existing.latitude,
|
|
||||||
longitude:
|
|
||||||
hasLocation &&
|
|
||||||
longitude != null &&
|
|
||||||
longitude.abs() <= 180 &&
|
|
||||||
(latitude != 0 || longitude != 0)
|
|
||||||
? longitude
|
|
||||||
: existing.longitude,
|
|
||||||
name: hasName ? name : existing.name,
|
name: hasName ? name : existing.name,
|
||||||
path: Uint8List.fromList(path.reversed.toList()),
|
path: Uint8List.fromList(path.reversed.toList()),
|
||||||
pathLength: path.length,
|
pathLength: path.length,
|
||||||
@@ -4844,11 +4866,11 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
try {
|
try {
|
||||||
reader.skipBytes(1); // Skip the response code byte
|
reader.skipBytes(1); // Skip the response code byte
|
||||||
final flags = reader.readByte();
|
final flags = reader.readByte();
|
||||||
_autoAddUsers = (flags & autoAddChatFlag) != 0;
|
_autoAddUsers = flags & autoAddChatFlag != 0;
|
||||||
_autoAddRepeaters = (flags & autoAddRepeaterFlag) != 0;
|
_autoAddRepeaters = flags & autoAddRepeaterFlag != 0;
|
||||||
_autoAddRoomServers = (flags & autoAddRoomServerFlag) != 0;
|
_autoAddRoomServers = flags & autoAddRoomServerFlag != 0;
|
||||||
_autoAddSensors = (flags & autoAddSensorFlag) != 0;
|
_autoAddSensors = flags & autoAddSensorFlag != 0;
|
||||||
_overwriteOldest = (flags & autoAddOverwriteOldestFlag) != 0;
|
_overwriteOldest = flags & autoAddOverwriteOldestFlag != 0;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
appLogger.error('Failed to parse auto-add config: $e', tag: 'Connector');
|
appLogger.error('Failed to parse auto-add config: $e', tag: 'Connector');
|
||||||
}
|
}
|
||||||
@@ -4858,7 +4880,6 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
Contact contact,
|
Contact contact,
|
||||||
Uint8List rawPacket, {
|
Uint8List rawPacket, {
|
||||||
bool noNotify = false,
|
bool noNotify = false,
|
||||||
bool addActive = false,
|
|
||||||
}) {
|
}) {
|
||||||
appLogger.info('Discovered new contact: ${contact.name}', tag: 'Connector');
|
appLogger.info('Discovered new contact: ${contact.name}', tag: 'Connector');
|
||||||
|
|
||||||
@@ -4879,7 +4900,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
longitude: contact.longitude,
|
longitude: contact.longitude,
|
||||||
lastSeen: contact.lastSeen,
|
lastSeen: contact.lastSeen,
|
||||||
flags: 0,
|
flags: 0,
|
||||||
isActive: addActive,
|
isActive: false,
|
||||||
);
|
);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
unawaited(_persistDiscoveredContacts());
|
unawaited(_persistDiscoveredContacts());
|
||||||
@@ -4897,7 +4918,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
longitude: contact.longitude,
|
longitude: contact.longitude,
|
||||||
lastSeen: contact.lastSeen,
|
lastSeen: contact.lastSeen,
|
||||||
lastMessageAt: contact.lastMessageAt,
|
lastMessageAt: contact.lastMessageAt,
|
||||||
isActive: addActive,
|
isActive: false,
|
||||||
flags: 0,
|
flags: 0,
|
||||||
);
|
);
|
||||||
_discoveredContacts.add(disContact);
|
_discoveredContacts.add(disContact);
|
||||||
|
|||||||
@@ -64,8 +64,6 @@ class MeshCoreUsbManager {
|
|||||||
|
|
||||||
Future<void> write(Uint8List data) => _service.write(data);
|
Future<void> write(Uint8List data) => _service.write(data);
|
||||||
|
|
||||||
Future<void> writeRaw(Uint8List data) => _service.writeRaw(data);
|
|
||||||
|
|
||||||
// --- Label management ---
|
// --- Label management ---
|
||||||
void updateConnectedLabel(String selfName) {
|
void updateConnectedLabel(String selfName) {
|
||||||
_service.updateConnectedLabel(selfName);
|
_service.updateConnectedLabel(selfName);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import 'services/app_debug_log_service.dart';
|
|||||||
import 'services/background_service.dart';
|
import 'services/background_service.dart';
|
||||||
import 'services/map_tile_cache_service.dart';
|
import 'services/map_tile_cache_service.dart';
|
||||||
import 'services/chat_text_scale_service.dart';
|
import 'services/chat_text_scale_service.dart';
|
||||||
|
import 'services/timeout_prediction_service.dart';
|
||||||
import 'storage/prefs_manager.dart';
|
import 'storage/prefs_manager.dart';
|
||||||
import 'utils/app_logger.dart';
|
import 'utils/app_logger.dart';
|
||||||
|
|
||||||
@@ -39,6 +40,7 @@ void main() async {
|
|||||||
final backgroundService = BackgroundService();
|
final backgroundService = BackgroundService();
|
||||||
final mapTileCacheService = MapTileCacheService();
|
final mapTileCacheService = MapTileCacheService();
|
||||||
final chatTextScaleService = ChatTextScaleService();
|
final chatTextScaleService = ChatTextScaleService();
|
||||||
|
final timeoutPredictionService = TimeoutPredictionService(storage);
|
||||||
|
|
||||||
// Load settings
|
// Load settings
|
||||||
await appSettingsService.loadSettings();
|
await appSettingsService.loadSettings();
|
||||||
@@ -56,6 +58,7 @@ void main() async {
|
|||||||
_registerThirdPartyLicenses();
|
_registerThirdPartyLicenses();
|
||||||
|
|
||||||
await chatTextScaleService.initialize();
|
await chatTextScaleService.initialize();
|
||||||
|
await timeoutPredictionService.initialize();
|
||||||
|
|
||||||
// Wire up connector with services
|
// Wire up connector with services
|
||||||
connector.initialize(
|
connector.initialize(
|
||||||
@@ -65,6 +68,7 @@ void main() async {
|
|||||||
bleDebugLogService: bleDebugLogService,
|
bleDebugLogService: bleDebugLogService,
|
||||||
appDebugLogService: appDebugLogService,
|
appDebugLogService: appDebugLogService,
|
||||||
backgroundService: backgroundService,
|
backgroundService: backgroundService,
|
||||||
|
timeoutPredictionService: timeoutPredictionService,
|
||||||
);
|
);
|
||||||
|
|
||||||
await connector.loadContactCache();
|
await connector.loadContactCache();
|
||||||
@@ -86,6 +90,7 @@ void main() async {
|
|||||||
appDebugLogService: appDebugLogService,
|
appDebugLogService: appDebugLogService,
|
||||||
mapTileCacheService: mapTileCacheService,
|
mapTileCacheService: mapTileCacheService,
|
||||||
chatTextScaleService: chatTextScaleService,
|
chatTextScaleService: chatTextScaleService,
|
||||||
|
timeoutPredictionService: timeoutPredictionService,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -121,6 +126,7 @@ class MeshCoreApp extends StatelessWidget {
|
|||||||
final AppDebugLogService appDebugLogService;
|
final AppDebugLogService appDebugLogService;
|
||||||
final MapTileCacheService mapTileCacheService;
|
final MapTileCacheService mapTileCacheService;
|
||||||
final ChatTextScaleService chatTextScaleService;
|
final ChatTextScaleService chatTextScaleService;
|
||||||
|
final TimeoutPredictionService timeoutPredictionService;
|
||||||
|
|
||||||
const MeshCoreApp({
|
const MeshCoreApp({
|
||||||
super.key,
|
super.key,
|
||||||
@@ -133,6 +139,7 @@ class MeshCoreApp extends StatelessWidget {
|
|||||||
required this.appDebugLogService,
|
required this.appDebugLogService,
|
||||||
required this.mapTileCacheService,
|
required this.mapTileCacheService,
|
||||||
required this.chatTextScaleService,
|
required this.chatTextScaleService,
|
||||||
|
required this.timeoutPredictionService,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -148,6 +155,7 @@ class MeshCoreApp extends StatelessWidget {
|
|||||||
ChangeNotifierProvider.value(value: chatTextScaleService),
|
ChangeNotifierProvider.value(value: chatTextScaleService),
|
||||||
Provider.value(value: storage),
|
Provider.value(value: storage),
|
||||||
Provider.value(value: mapTileCacheService),
|
Provider.value(value: mapTileCacheService),
|
||||||
|
ChangeNotifierProvider.value(value: timeoutPredictionService),
|
||||||
],
|
],
|
||||||
child: Consumer<AppSettingsService>(
|
child: Consumer<AppSettingsService>(
|
||||||
builder: (context, settingsService, child) {
|
builder: (context, settingsService, child) {
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ class AppSettings {
|
|||||||
final UnitSystem unitSystem;
|
final UnitSystem unitSystem;
|
||||||
final Set<String> mutedChannels;
|
final Set<String> mutedChannels;
|
||||||
final bool mapShowDiscoveryContacts;
|
final bool mapShowDiscoveryContacts;
|
||||||
final String tcpServerAddress;
|
|
||||||
final int tcpServerPort;
|
|
||||||
|
|
||||||
AppSettings({
|
AppSettings({
|
||||||
this.clearPathOnMaxRetry = false,
|
this.clearPathOnMaxRetry = false,
|
||||||
@@ -70,8 +68,6 @@ class AppSettings {
|
|||||||
this.unitSystem = UnitSystem.metric,
|
this.unitSystem = UnitSystem.metric,
|
||||||
Set<String>? mutedChannels,
|
Set<String>? mutedChannels,
|
||||||
this.mapShowDiscoveryContacts = true,
|
this.mapShowDiscoveryContacts = true,
|
||||||
this.tcpServerAddress = '',
|
|
||||||
this.tcpServerPort = 0,
|
|
||||||
}) : batteryChemistryByDeviceId = batteryChemistryByDeviceId ?? {},
|
}) : batteryChemistryByDeviceId = batteryChemistryByDeviceId ?? {},
|
||||||
batteryChemistryByRepeaterId = batteryChemistryByRepeaterId ?? {},
|
batteryChemistryByRepeaterId = batteryChemistryByRepeaterId ?? {},
|
||||||
mutedChannels = mutedChannels ?? {};
|
mutedChannels = mutedChannels ?? {};
|
||||||
@@ -104,8 +100,6 @@ class AppSettings {
|
|||||||
'unit_system': unitSystem.value,
|
'unit_system': unitSystem.value,
|
||||||
'muted_channels': mutedChannels.toList(),
|
'muted_channels': mutedChannels.toList(),
|
||||||
'map_show_discovery_contacts': mapShowDiscoveryContacts,
|
'map_show_discovery_contacts': mapShowDiscoveryContacts,
|
||||||
'tcp_server_address': tcpServerAddress,
|
|
||||||
'tcp_server_port': tcpServerPort,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,8 +157,6 @@ class AppSettings {
|
|||||||
{},
|
{},
|
||||||
mapShowDiscoveryContacts:
|
mapShowDiscoveryContacts:
|
||||||
json['map_show_discovery_contacts'] as bool? ?? true,
|
json['map_show_discovery_contacts'] as bool? ?? true,
|
||||||
tcpServerAddress: json['tcp_server_address'] as String? ?? '',
|
|
||||||
tcpServerPort: json['tcp_server_port'] as int? ?? 0,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,8 +187,6 @@ class AppSettings {
|
|||||||
UnitSystem? unitSystem,
|
UnitSystem? unitSystem,
|
||||||
Set<String>? mutedChannels,
|
Set<String>? mutedChannels,
|
||||||
bool? mapShowDiscoveryContacts,
|
bool? mapShowDiscoveryContacts,
|
||||||
String? tcpServerAddress,
|
|
||||||
int? tcpServerPort,
|
|
||||||
}) {
|
}) {
|
||||||
return AppSettings(
|
return AppSettings(
|
||||||
clearPathOnMaxRetry: clearPathOnMaxRetry ?? this.clearPathOnMaxRetry,
|
clearPathOnMaxRetry: clearPathOnMaxRetry ?? this.clearPathOnMaxRetry,
|
||||||
@@ -235,8 +225,6 @@ class AppSettings {
|
|||||||
mutedChannels: mutedChannels ?? this.mutedChannels,
|
mutedChannels: mutedChannels ?? this.mutedChannels,
|
||||||
mapShowDiscoveryContacts:
|
mapShowDiscoveryContacts:
|
||||||
mapShowDiscoveryContacts ?? this.mapShowDiscoveryContacts,
|
mapShowDiscoveryContacts ?? this.mapShowDiscoveryContacts,
|
||||||
tcpServerAddress: tcpServerAddress ?? this.tcpServerAddress,
|
|
||||||
tcpServerPort: tcpServerPort ?? this.tcpServerPort,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-14
@@ -65,17 +65,7 @@ class Contact {
|
|||||||
return '$pathLength hops';
|
return '$pathLength hops';
|
||||||
}
|
}
|
||||||
|
|
||||||
bool get hasLocation {
|
bool get hasLocation => latitude != null && longitude != null;
|
||||||
const double epsilon = 1e-6;
|
|
||||||
final lat = latitude ?? 0.0;
|
|
||||||
final lon = longitude ?? 0.0;
|
|
||||||
return (lat.abs() > epsilon || lon.abs() > epsilon) &&
|
|
||||||
lat >= -90.0 &&
|
|
||||||
lat <= 90.0 &&
|
|
||||||
lon >= -180.0 &&
|
|
||||||
lon <= 180.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool get isFavorite => (flags & contactFlagFavorite) != 0;
|
bool get isFavorite => (flags & contactFlagFavorite) != 0;
|
||||||
|
|
||||||
Contact copyWith({
|
Contact copyWith({
|
||||||
@@ -118,7 +108,7 @@ class Contact {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String get pathIdList {
|
String get pathIdList {
|
||||||
final pathBytes = pathBytesForDisplay;
|
final pathBytes = _pathBytesForDisplay;
|
||||||
if (pathBytes.isEmpty) return '';
|
if (pathBytes.isEmpty) return '';
|
||||||
final parts = <String>[];
|
final parts = <String>[];
|
||||||
final groupSize = pathHashSize;
|
final groupSize = pathHashSize;
|
||||||
@@ -140,7 +130,43 @@ class Contact {
|
|||||||
return "<${publicKeyHex.substring(0, 8)}...${publicKeyHex.substring(publicKeyHex.length - 8)}>";
|
return "<${publicKeyHex.substring(0, 8)}...${publicKeyHex.substring(publicKeyHex.length - 8)}>";
|
||||||
}
|
}
|
||||||
|
|
||||||
Uint8List get pathBytesForDisplay {
|
Uint8List? get traceRouteBytes {
|
||||||
|
final pathBytes = _pathBytesForDisplay;
|
||||||
|
Uint8List? traceBytes;
|
||||||
|
|
||||||
|
if (pathBytes.isEmpty) {
|
||||||
|
traceBytes = Uint8List(1);
|
||||||
|
traceBytes[0] = publicKey[0];
|
||||||
|
return traceBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == advTypeRepeater || type == advTypeRoom) {
|
||||||
|
final len = (pathBytes.length + pathBytes.length + 1);
|
||||||
|
traceBytes = Uint8List(len);
|
||||||
|
traceBytes[pathBytes.length] = publicKey[0];
|
||||||
|
for (int i = 0; i < pathBytes.length; i++) {
|
||||||
|
traceBytes[i] = pathBytes[i];
|
||||||
|
if (i < pathBytes.length) {
|
||||||
|
traceBytes[len - 1 - i] = pathBytes[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (pathBytes.length < 2) {
|
||||||
|
return pathBytes[0] == 0 ? null : pathBytes;
|
||||||
|
}
|
||||||
|
final len = (pathBytes.length + pathBytes.length - 1);
|
||||||
|
traceBytes = Uint8List(len);
|
||||||
|
for (int i = 0; i < pathBytes.length; i++) {
|
||||||
|
traceBytes[i] = pathBytes[i];
|
||||||
|
if (i < pathBytes.length - 1) {
|
||||||
|
traceBytes[len - 1 - i] = pathBytes[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return traceBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
Uint8List get _pathBytesForDisplay {
|
||||||
if (pathOverride != null) {
|
if (pathOverride != null) {
|
||||||
if (pathOverride! < 0) return Uint8List(0);
|
if (pathOverride! < 0) return Uint8List(0);
|
||||||
return pathOverrideBytes ?? Uint8List(0);
|
return pathOverrideBytes ?? Uint8List(0);
|
||||||
@@ -171,7 +197,6 @@ class Contact {
|
|||||||
double? lat, lon;
|
double? lat, lon;
|
||||||
final latRaw = reader.readInt32LE();
|
final latRaw = reader.readInt32LE();
|
||||||
final lonRaw = reader.readInt32LE();
|
final lonRaw = reader.readInt32LE();
|
||||||
|
|
||||||
if (latRaw != 0 || lonRaw != 0) {
|
if (latRaw != 0 || lonRaw != 0) {
|
||||||
lat = latRaw / 1e6;
|
lat = latRaw / 1e6;
|
||||||
lon = lonRaw / 1e6;
|
lon = lonRaw / 1e6;
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
class DeliveryObservation {
|
||||||
|
final String contactKey;
|
||||||
|
final int pathLength;
|
||||||
|
final int messageBytes;
|
||||||
|
final int secondsSinceLastRx;
|
||||||
|
final bool isFlood;
|
||||||
|
final int deliveryMs;
|
||||||
|
final DateTime timestamp;
|
||||||
|
|
||||||
|
DeliveryObservation({
|
||||||
|
required this.contactKey,
|
||||||
|
required this.pathLength,
|
||||||
|
required this.messageBytes,
|
||||||
|
required this.secondsSinceLastRx,
|
||||||
|
required this.isFlood,
|
||||||
|
required this.deliveryMs,
|
||||||
|
required this.timestamp,
|
||||||
|
});
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'contact_key': contactKey,
|
||||||
|
'path_length': pathLength,
|
||||||
|
'message_bytes': messageBytes,
|
||||||
|
'seconds_since_last_rx': secondsSinceLastRx,
|
||||||
|
'is_flood': isFlood,
|
||||||
|
'delivery_ms': deliveryMs,
|
||||||
|
'timestamp': timestamp.toIso8601String(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
factory DeliveryObservation.fromJson(Map<String, dynamic> json) {
|
||||||
|
return DeliveryObservation(
|
||||||
|
contactKey: json['contact_key'] as String,
|
||||||
|
pathLength: json['path_length'] as int,
|
||||||
|
messageBytes: json['message_bytes'] as int,
|
||||||
|
secondsSinceLastRx: json['seconds_since_last_rx'] as int? ?? 0,
|
||||||
|
isFlood: json['is_flood'] as bool,
|
||||||
|
deliveryMs: json['delivery_ms'] as int,
|
||||||
|
timestamp: DateTime.parse(json['timestamp'] as String),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,7 +40,10 @@ class ChannelMessagePathScreen extends StatelessWidget {
|
|||||||
final primaryPath = !channelMessage && !message.isOutgoing
|
final primaryPath = !channelMessage && !message.isOutgoing
|
||||||
? Uint8List.fromList(primaryPathTmp.reversed.toList())
|
? Uint8List.fromList(primaryPathTmp.reversed.toList())
|
||||||
: primaryPathTmp;
|
: primaryPathTmp;
|
||||||
final contacts = connector.allContacts;
|
final contacts = <Contact>[
|
||||||
|
...connector.contacts,
|
||||||
|
...connector.discoveredContacts,
|
||||||
|
];
|
||||||
final hops = _buildPathHops(primaryPath, contacts, l10n);
|
final hops = _buildPathHops(primaryPath, contacts, l10n);
|
||||||
final hasHopDetails = primaryPath.isNotEmpty;
|
final hasHopDetails = primaryPath.isNotEmpty;
|
||||||
final observedLabel = _formatObservedHops(
|
final observedLabel = _formatObservedHops(
|
||||||
@@ -62,9 +65,8 @@ class ChannelMessagePathScreen extends StatelessWidget {
|
|||||||
builder: (context) => PathTraceMapScreen(
|
builder: (context) => PathTraceMapScreen(
|
||||||
title: context.l10n.contacts_repeaterPathTrace,
|
title: context.l10n.contacts_repeaterPathTrace,
|
||||||
path: primaryPath,
|
path: primaryPath,
|
||||||
flipPathAround: true,
|
flipPathRound: true,
|
||||||
reversePathAround:
|
reversePathRound: !message.isOutgoing && !channelMessage,
|
||||||
!(!channelMessage && !message.isOutgoing),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -365,7 +367,10 @@ class _ChannelMessagePathMapScreenState
|
|||||||
: selectedPathTmp;
|
: selectedPathTmp;
|
||||||
|
|
||||||
final selectedIndex = _indexForPath(selectedPath, observedPaths);
|
final selectedIndex = _indexForPath(selectedPath, observedPaths);
|
||||||
final contacts = connector.allContacts;
|
final contacts = <Contact>[
|
||||||
|
...connector.contacts,
|
||||||
|
...connector.discoveredContacts,
|
||||||
|
];
|
||||||
final hops = _buildPathHops(selectedPath, contacts, context.l10n);
|
final hops = _buildPathHops(selectedPath, contacts, context.l10n);
|
||||||
|
|
||||||
final points = <LatLng>[];
|
final points = <LatLng>[];
|
||||||
|
|||||||
@@ -858,7 +858,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
builder: (context) => PathTraceMapScreen(
|
builder: (context) => PathTraceMapScreen(
|
||||||
title: context.l10n.contacts_repeaterPathTrace,
|
title: context.l10n.contacts_repeaterPathTrace,
|
||||||
path: Uint8List.fromList(pathBytes),
|
path: Uint8List.fromList(pathBytes),
|
||||||
flipPathAround: true,
|
flipPathRound: true,
|
||||||
targetContact: widget.contact,
|
targetContact: widget.contact,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -1027,7 +1027,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
final currentPathLabel = _currentPathLabel(currentContact);
|
final currentPathLabel = _currentPathLabel(currentContact);
|
||||||
|
|
||||||
// Filter out the current contact from available contacts
|
// Filter out the current contact from available contacts
|
||||||
final availableContacts = connector.allContacts
|
final availableContacts = connector.contacts
|
||||||
.where((c) => c != widget.contact)
|
.where((c) => c != widget.contact)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
|||||||
@@ -1064,7 +1064,7 @@ class _ContactsScreenState extends State<ContactsScreen>
|
|||||||
if (isRepeater) ...[
|
if (isRepeater) ...[
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.radar, color: Colors.green),
|
leading: const Icon(Icons.radar, color: Colors.green),
|
||||||
title: contact.pathBytesForDisplay.isNotEmpty
|
title: contact.pathLength > 0
|
||||||
? Text(context.l10n.contacts_pathTrace)
|
? Text(context.l10n.contacts_pathTrace)
|
||||||
: Text(context.l10n.contacts_ping),
|
: Text(context.l10n.contacts_ping),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
@@ -1072,12 +1072,10 @@ class _ContactsScreenState extends State<ContactsScreen>
|
|||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) => PathTraceMapScreen(
|
builder: (context) => PathTraceMapScreen(
|
||||||
title: contact.pathBytesForDisplay.isNotEmpty
|
title: contact.pathLength > 0
|
||||||
? context.l10n.contacts_repeaterPathTrace
|
? context.l10n.contacts_repeaterPathTrace
|
||||||
: context.l10n.contacts_repeaterPing,
|
: context.l10n.contacts_repeaterPing,
|
||||||
path: contact.pathBytesForDisplay,
|
path: contact.traceRouteBytes ?? Uint8List(0),
|
||||||
flipPathAround: true,
|
|
||||||
targetContact: contact,
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -1102,12 +1100,10 @@ class _ContactsScreenState extends State<ContactsScreen>
|
|||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) => PathTraceMapScreen(
|
builder: (context) => PathTraceMapScreen(
|
||||||
title: contact.pathBytesForDisplay.isNotEmpty
|
title: contact.pathLength > 0
|
||||||
? context.l10n.contacts_roomPathTrace
|
? context.l10n.contacts_roomPathTrace
|
||||||
: context.l10n.contacts_roomPing,
|
: context.l10n.contacts_roomPing,
|
||||||
path: contact.pathBytesForDisplay,
|
path: contact.traceRouteBytes ?? Uint8List(0),
|
||||||
flipPathAround: contact.pathBytesForDisplay.isNotEmpty,
|
|
||||||
targetContact: contact,
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -1149,8 +1145,7 @@ class _ContactsScreenState extends State<ContactsScreen>
|
|||||||
title: context.l10n.contacts_pathTraceTo(
|
title: context.l10n.contacts_pathTraceTo(
|
||||||
contact.name,
|
contact.name,
|
||||||
),
|
),
|
||||||
path: contact.pathBytesForDisplay,
|
path: contact.traceRouteBytes ?? Uint8List(0),
|
||||||
flipPathAround: true,
|
|
||||||
targetContact: contact,
|
targetContact: contact,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -137,7 +137,10 @@ class _MapScreenState extends State<MapScreen> {
|
|||||||
builder: (context, connector, settingsService, pathHistory, child) {
|
builder: (context, connector, settingsService, pathHistory, child) {
|
||||||
final tileCache = context.read<MapTileCacheService>();
|
final tileCache = context.read<MapTileCacheService>();
|
||||||
final settings = settingsService.settings;
|
final settings = settingsService.settings;
|
||||||
final allContacts = connector.allContacts;
|
final allContacts = <Contact>[
|
||||||
|
...connector.contacts,
|
||||||
|
...connector.discoveredContacts.where((c) => !c.isActive),
|
||||||
|
];
|
||||||
|
|
||||||
final contacts = settings.mapShowDiscoveryContacts
|
final contacts = settings.mapShowDiscoveryContacts
|
||||||
? allContacts
|
? allContacts
|
||||||
@@ -176,13 +179,20 @@ class _MapScreenState extends State<MapScreen> {
|
|||||||
|
|
||||||
// Filter by location
|
// Filter by location
|
||||||
final contactsWithLocation = filteredByKeyPrefix.where((c) {
|
final contactsWithLocation = filteredByKeyPrefix.where((c) {
|
||||||
return c.hasLocation;
|
if (!c.hasLocation) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return _checkLocationPlausibility(c.latitude!, c.longitude!);
|
||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
// All contacts with a known location — used as anchors regardless of
|
// All contacts with a known location — used as anchors regardless of
|
||||||
// time/key-prefix filters so that repeaters are always available.
|
// time/key-prefix filters so that repeaters are always available.
|
||||||
final allContactsWithLocation = allContacts
|
final allContactsWithLocation = allContacts
|
||||||
.where((c) => c.hasLocation)
|
.where(
|
||||||
|
(c) =>
|
||||||
|
c.hasLocation &&
|
||||||
|
_checkLocationPlausibility(c.latitude!, c.longitude!),
|
||||||
|
)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
// Compute guessed locations with caching
|
// Compute guessed locations with caching
|
||||||
|
|||||||
@@ -124,7 +124,10 @@ class _NeighborsScreenState extends State<NeighborsScreen> {
|
|||||||
|
|
||||||
void _handleNeighborsResponse(MeshCoreConnector connector, Uint8List frame) {
|
void _handleNeighborsResponse(MeshCoreConnector connector, Uint8List frame) {
|
||||||
final buffer = BufferReader(frame);
|
final buffer = BufferReader(frame);
|
||||||
final contacts = connector.allContacts;
|
final contacts = <Contact>[
|
||||||
|
...connector.contacts,
|
||||||
|
...connector.discoveredContacts,
|
||||||
|
];
|
||||||
try {
|
try {
|
||||||
final neighborCount = buffer.readUInt16LE();
|
final neighborCount = buffer.readUInt16LE();
|
||||||
final parsedNeighbors = parseNeighborsData(buffer, buffer.readUInt16LE());
|
final parsedNeighbors = parseNeighborsData(buffer, buffer.readUInt16LE());
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ class PathTraceMapScreen extends StatefulWidget {
|
|||||||
final String title;
|
final String title;
|
||||||
final Uint8List path;
|
final Uint8List path;
|
||||||
final int? repeaterId;
|
final int? repeaterId;
|
||||||
final bool flipPathAround;
|
final bool flipPathRound;
|
||||||
final bool reversePathAround;
|
final bool reversePathRound;
|
||||||
final Contact? targetContact;
|
final Contact? targetContact;
|
||||||
|
|
||||||
const PathTraceMapScreen({
|
const PathTraceMapScreen({
|
||||||
@@ -61,8 +61,8 @@ class PathTraceMapScreen extends StatefulWidget {
|
|||||||
required this.title,
|
required this.title,
|
||||||
required this.path,
|
required this.path,
|
||||||
this.repeaterId,
|
this.repeaterId,
|
||||||
this.flipPathAround = false,
|
this.flipPathRound = false,
|
||||||
this.reversePathAround = false,
|
this.reversePathRound = false,
|
||||||
this.targetContact,
|
this.targetContact,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -93,7 +93,6 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
ValueKey<String> _mapKey = const ValueKey('initial');
|
ValueKey<String> _mapKey = const ValueKey('initial');
|
||||||
double _pathDistanceMeters = 0.0;
|
double _pathDistanceMeters = 0.0;
|
||||||
bool _showNodeLabels = true;
|
bool _showNodeLabels = true;
|
||||||
Contact? _targetContact;
|
|
||||||
|
|
||||||
String _formatPathPrefixes(Uint8List pathBytes) {
|
String _formatPathPrefixes(Uint8List pathBytes) {
|
||||||
return pathBytes
|
return pathBytes
|
||||||
@@ -159,16 +158,21 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final pathTmp = widget.reversePathAround
|
final Uint8List path;
|
||||||
|
|
||||||
|
Uint8List pathTmp = widget.reversePathRound
|
||||||
? Uint8List.fromList(widget.path.reversed.toList())
|
? Uint8List.fromList(widget.path.reversed.toList())
|
||||||
: widget.path;
|
: widget.path;
|
||||||
|
|
||||||
final path = widget.flipPathAround ? buildPath(pathTmp) : pathTmp;
|
if (widget.flipPathRound) {
|
||||||
|
path = buildPath(pathTmp);
|
||||||
|
} else {
|
||||||
|
path = pathTmp;
|
||||||
|
}
|
||||||
|
|
||||||
appLogger.info(
|
appLogger.info(
|
||||||
'Initiating path trace with path: ${_formatPathPrefixes(path)}',
|
'Initiating path trace with path: ${_formatPathPrefixes(path)}',
|
||||||
tag: 'PathTraceMapScreen',
|
tag: 'PathTraceMapScreen',
|
||||||
noNotify: !mounted,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
final connector = Provider.of<MeshCoreConnector>(context, listen: false);
|
final connector = Provider.of<MeshCoreConnector>(context, listen: false);
|
||||||
@@ -259,7 +263,10 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
Map<int, Contact> pathContacts = {};
|
Map<int, Contact> pathContacts = {};
|
||||||
final contacts = connector.allContacts;
|
final contacts = <Contact>[
|
||||||
|
...connector.contacts,
|
||||||
|
...connector.discoveredContacts,
|
||||||
|
];
|
||||||
contacts.where((c) => c.type != advTypeChat).forEach((repeater) {
|
contacts.where((c) => c.type != advTypeChat).forEach((repeater) {
|
||||||
for (var repeaterData in pathData) {
|
for (var repeaterData in pathData) {
|
||||||
if (listEquals(
|
if (listEquals(
|
||||||
@@ -305,21 +312,18 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
// Compute endpoint position for the target contact.
|
// Compute endpoint position for the target contact.
|
||||||
LatLng? targetPos;
|
LatLng? targetPos;
|
||||||
bool targetGuessed = false;
|
bool targetGuessed = false;
|
||||||
_targetContact = widget.targetContact;
|
final target = widget.targetContact;
|
||||||
|
if (target != null) {
|
||||||
if (_targetContact != null) {
|
if (target.hasLocation) {
|
||||||
final tc = _targetContact!;
|
targetPos = LatLng(target.latitude!, target.longitude!);
|
||||||
if (tc.hasLocation) {
|
} else if (pathData.isNotEmpty) {
|
||||||
targetPos = LatLng(tc.latitude!, tc.longitude!);
|
|
||||||
} else if (widget.path.length > 1) {
|
|
||||||
// Infer from the last hop: average GPS contacts sharing that hop.
|
// Infer from the last hop: average GPS contacts sharing that hop.
|
||||||
// For a round-trip path (flipPathAround/reversePathAround), the target-side hop
|
// For a round-trip path (flipPathRound), the target-side hop sits
|
||||||
// sits in the middle of the symmetric sequence; .last is the local side.
|
// in the middle of the symmetric sequence; .last is the local side.
|
||||||
final lastHop = widget.reversePathAround
|
final lastHop = (widget.flipPathRound && pathData.length > 1)
|
||||||
? widget.path.first
|
? pathData[(pathData.length - 1) ~/ 2]
|
||||||
: widget.path.last;
|
: pathData.last;
|
||||||
|
final peers = connector.contacts
|
||||||
final peers = connector.allContacts
|
|
||||||
.where(
|
.where(
|
||||||
(c) =>
|
(c) =>
|
||||||
c.hasLocation &&
|
c.hasLocation &&
|
||||||
@@ -335,34 +339,12 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
peers.map((c) => c.longitude!).reduce((a, b) => a + b) /
|
peers.map((c) => c.longitude!).reduce((a, b) => a + b) /
|
||||||
peers.length;
|
peers.length;
|
||||||
const offsetDeg = 0.003;
|
const offsetDeg = 0.003;
|
||||||
final angle = (tc.publicKey[1] / 255.0) * 2 * pi;
|
final angle = (target.publicKey[1] / 255.0) * 2 * pi;
|
||||||
targetPos = LatLng(
|
targetPos = LatLng(
|
||||||
lat + offsetDeg * cos(angle),
|
lat + offsetDeg * cos(angle),
|
||||||
lon + offsetDeg * sin(angle),
|
lon + offsetDeg * sin(angle),
|
||||||
);
|
);
|
||||||
targetGuessed = true;
|
targetGuessed = true;
|
||||||
} else if (inferredPositions.containsKey(lastHop)) {
|
|
||||||
final lat = inferredPositions[lastHop]!.latitude;
|
|
||||||
final lon = inferredPositions[lastHop]!.longitude;
|
|
||||||
const offsetDeg = 0.003;
|
|
||||||
final angle = (tc.publicKey[1] / 255.0) * 2 * pi;
|
|
||||||
targetPos = LatLng(
|
|
||||||
lat + offsetDeg * cos(angle),
|
|
||||||
lon + offsetDeg * sin(angle),
|
|
||||||
);
|
|
||||||
targetGuessed = true;
|
|
||||||
} else {
|
|
||||||
// As a last resort, just place it at the same position as the last hop.
|
|
||||||
final contact = pathContacts[lastHop];
|
|
||||||
if (contact != null && contact.hasLocation) {
|
|
||||||
const offsetDeg = 0.003;
|
|
||||||
final angle = (tc.publicKey[1] / 255.0) * 2 * pi;
|
|
||||||
targetPos = LatLng(
|
|
||||||
contact.latitude! + offsetDeg * cos(angle),
|
|
||||||
contact.longitude! + offsetDeg * sin(angle),
|
|
||||||
);
|
|
||||||
targetGuessed = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -371,12 +353,7 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
|
|
||||||
_points = <LatLng>[];
|
_points = <LatLng>[];
|
||||||
_points.add(LatLng(connector.selfLatitude!, connector.selfLongitude!));
|
_points.add(LatLng(connector.selfLatitude!, connector.selfLongitude!));
|
||||||
int hopLast = 0;
|
|
||||||
int hopLastLast = 0;
|
|
||||||
for (final hop in _traceData!.pathData) {
|
for (final hop in _traceData!.pathData) {
|
||||||
if (hop == hopLastLast && widget.flipPathAround) {
|
|
||||||
break; //skip duplicate hops in round-trip paths
|
|
||||||
}
|
|
||||||
final contact = _traceData!.pathContacts[hop];
|
final contact = _traceData!.pathContacts[hop];
|
||||||
if (contact != null && contact.hasLocation) {
|
if (contact != null && contact.hasLocation) {
|
||||||
_points.add(LatLng(contact.latitude!, contact.longitude!));
|
_points.add(LatLng(contact.latitude!, contact.longitude!));
|
||||||
@@ -384,14 +361,8 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
final inferred = inferredPositions[hop];
|
final inferred = inferredPositions[hop];
|
||||||
if (inferred != null) _points.add(inferred);
|
if (inferred != null) _points.add(inferred);
|
||||||
}
|
}
|
||||||
hopLastLast = hopLast;
|
|
||||||
hopLast = hop;
|
|
||||||
}
|
|
||||||
if (targetPos != null) {
|
|
||||||
if (_targetContact != null && _targetContact!.type == advTypeChat) {
|
|
||||||
_points.add(targetPos);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (targetPos != null) _points.add(targetPos);
|
||||||
_polylines = _points.length > 1
|
_polylines = _points.length > 1
|
||||||
? [
|
? [
|
||||||
Polyline(
|
Polyline(
|
||||||
@@ -480,8 +451,7 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (_hasData)
|
if (_hasData) _buildMapPathTrace(context, tileCache),
|
||||||
_buildMapPathTrace(context, tileCache, _targetContact),
|
|
||||||
if (_points.isEmpty &&
|
if (_points.isEmpty &&
|
||||||
!_hasData &&
|
!_hasData &&
|
||||||
!_isLoading &&
|
!_isLoading &&
|
||||||
@@ -510,28 +480,17 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
List<Marker> _buildHopMarkers(
|
List<Marker> _buildHopMarkers(
|
||||||
List<int> pathData, {
|
List<int> pathData, {
|
||||||
required bool showLabels,
|
required bool showLabels,
|
||||||
required Contact? target,
|
|
||||||
}) {
|
}) {
|
||||||
final markers = <Marker>[];
|
final markers = <Marker>[];
|
||||||
int hopLast = 0;
|
|
||||||
int hopLastLast = 0;
|
|
||||||
for (final hop in pathData) {
|
for (final hop in pathData) {
|
||||||
final contact = _traceData!.pathContacts[hop];
|
final contact = _traceData!.pathContacts[hop];
|
||||||
final inferred = _inferredHopPositions[hop];
|
final inferred = _inferredHopPositions[hop];
|
||||||
final hasGps = contact != null && contact.hasLocation;
|
final hasGps = contact != null && contact.hasLocation;
|
||||||
if (hop == hopLastLast && widget.flipPathAround) {
|
if (!hasGps && inferred == null) continue;
|
||||||
continue; //skip duplicate hops in round-trip paths
|
|
||||||
}
|
|
||||||
if (!hasGps && inferred == null) {
|
|
||||||
hopLastLast = hopLast;
|
|
||||||
hopLast = hop;
|
|
||||||
continue; //skip hops with no GPS and no inferred position
|
|
||||||
}
|
|
||||||
final point = hasGps
|
final point = hasGps
|
||||||
? LatLng(contact.latitude!, contact.longitude!)
|
? LatLng(contact.latitude!, contact.longitude!)
|
||||||
: inferred!;
|
: inferred!;
|
||||||
final label = hop.toRadixString(16).padLeft(2, '0').toUpperCase();
|
final label = hop.toRadixString(16).padLeft(2, '0').toUpperCase();
|
||||||
|
|
||||||
markers.add(
|
markers.add(
|
||||||
Marker(
|
Marker(
|
||||||
point: point,
|
point: point,
|
||||||
@@ -573,8 +532,6 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
hopLastLast = hopLast;
|
|
||||||
hopLast = hop;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final selfLat = context.read<MeshCoreConnector>().selfLatitude;
|
final selfLat = context.read<MeshCoreConnector>().selfLatitude;
|
||||||
@@ -624,9 +581,9 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
|
|
||||||
// Add target contact endpoint marker.
|
// Add target contact endpoint marker.
|
||||||
final targetPos = _targetContactPosition;
|
final targetPos = _targetContactPosition;
|
||||||
if (targetPos != null && target != null && target.type == advTypeChat) {
|
if (targetPos != null) {
|
||||||
final isGuessed = _targetContactIsGuessed;
|
final isGuessed = _targetContactIsGuessed;
|
||||||
final targetName = target.name;
|
final targetName = widget.targetContact?.name ?? '?';
|
||||||
markers.add(
|
markers.add(
|
||||||
Marker(
|
Marker(
|
||||||
point: targetPos,
|
point: targetPos,
|
||||||
@@ -762,7 +719,6 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
Widget _buildMapPathTrace(
|
Widget _buildMapPathTrace(
|
||||||
BuildContext context,
|
BuildContext context,
|
||||||
MapTileCacheService tileCache,
|
MapTileCacheService tileCache,
|
||||||
Contact? target,
|
|
||||||
) {
|
) {
|
||||||
return FlutterMap(
|
return FlutterMap(
|
||||||
key: _mapKey,
|
key: _mapKey,
|
||||||
@@ -801,7 +757,6 @@ class _PathTraceMapScreenState extends State<PathTraceMapScreen> {
|
|||||||
markers: _buildHopMarkers(
|
markers: _buildHopMarkers(
|
||||||
_traceData!.pathData,
|
_traceData!.pathData,
|
||||||
showLabels: _showNodeLabels,
|
showLabels: _showNodeLabels,
|
||||||
target: target,
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import 'package:provider/provider.dart';
|
|||||||
|
|
||||||
import '../connector/meshcore_connector.dart';
|
import '../connector/meshcore_connector.dart';
|
||||||
import '../l10n/l10n.dart';
|
import '../l10n/l10n.dart';
|
||||||
import '../services/app_settings_service.dart';
|
|
||||||
import '../utils/platform_info.dart';
|
import '../utils/platform_info.dart';
|
||||||
import '../widgets/adaptive_app_bar_title.dart';
|
import '../widgets/adaptive_app_bar_title.dart';
|
||||||
import 'contacts_screen.dart';
|
import 'contacts_screen.dart';
|
||||||
@@ -28,14 +27,8 @@ class _TcpScreenState extends State<TcpScreen> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_hostController = TextEditingController(
|
_hostController = TextEditingController();
|
||||||
text: context.read<AppSettingsService>().settings.tcpServerAddress,
|
_portController = TextEditingController(text: '5000');
|
||||||
);
|
|
||||||
_portController = TextEditingController(
|
|
||||||
text: context.read<AppSettingsService>().settings.tcpServerPort > 0
|
|
||||||
? context.read<AppSettingsService>().settings.tcpServerPort.toString()
|
|
||||||
: '',
|
|
||||||
);
|
|
||||||
_connector = context.read<MeshCoreConnector>();
|
_connector = context.read<MeshCoreConnector>();
|
||||||
|
|
||||||
_connectionListener = () {
|
_connectionListener = () {
|
||||||
@@ -46,12 +39,6 @@ class _TcpScreenState extends State<TcpScreen> {
|
|||||||
if (_connector.state == MeshCoreConnectionState.connected &&
|
if (_connector.state == MeshCoreConnectionState.connected &&
|
||||||
_connector.isTcpTransportConnected &&
|
_connector.isTcpTransportConnected &&
|
||||||
!_navigatedToContacts) {
|
!_navigatedToContacts) {
|
||||||
context.read<AppSettingsService>().setTcpServerAddress(
|
|
||||||
_hostController.text,
|
|
||||||
);
|
|
||||||
context.read<AppSettingsService>().setTcpServerPort(
|
|
||||||
int.tryParse(_portController.text) ?? 0,
|
|
||||||
);
|
|
||||||
_navigatedToContacts = true;
|
_navigatedToContacts = true;
|
||||||
Navigator.of(context).pushReplacement(
|
Navigator.of(context).pushReplacement(
|
||||||
MaterialPageRoute(builder: (_) => const ContactsScreen()),
|
MaterialPageRoute(builder: (_) => const ContactsScreen()),
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ class AppDebugLogService extends ChangeNotifier {
|
|||||||
String message, {
|
String message, {
|
||||||
String tag = 'App',
|
String tag = 'App',
|
||||||
AppDebugLogLevel level = AppDebugLogLevel.info,
|
AppDebugLogLevel level = AppDebugLogLevel.info,
|
||||||
bool noNotify = false,
|
|
||||||
}) {
|
}) {
|
||||||
if (!_enabled && !kDebugMode) return;
|
if (!_enabled && !kDebugMode) return;
|
||||||
if (!_enabled) {
|
if (!_enabled) {
|
||||||
@@ -73,24 +72,22 @@ class AppDebugLogService extends ChangeNotifier {
|
|||||||
_entries.removeRange(0, _entries.length - maxEntries);
|
_entries.removeRange(0, _entries.length - maxEntries);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!noNotify) {
|
notifyListeners();
|
||||||
notifyListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Also print to console for development
|
// Also print to console for development
|
||||||
debugPrint('[$tag] $message');
|
debugPrint('[$tag] $message');
|
||||||
}
|
}
|
||||||
|
|
||||||
void info(String message, {String tag = 'App', bool noNotify = false}) {
|
void info(String message, {String tag = 'App'}) {
|
||||||
log(message, tag: tag, level: AppDebugLogLevel.info, noNotify: noNotify);
|
log(message, tag: tag, level: AppDebugLogLevel.info);
|
||||||
}
|
}
|
||||||
|
|
||||||
void warn(String message, {String tag = 'App', bool noNotify = false}) {
|
void warn(String message, {String tag = 'App'}) {
|
||||||
log(message, tag: tag, level: AppDebugLogLevel.warning, noNotify: noNotify);
|
log(message, tag: tag, level: AppDebugLogLevel.warning);
|
||||||
}
|
}
|
||||||
|
|
||||||
void error(String message, {String tag = 'App', bool noNotify = false}) {
|
void error(String message, {String tag = 'App'}) {
|
||||||
log(message, tag: tag, level: AppDebugLogLevel.error, noNotify: noNotify);
|
log(message, tag: tag, level: AppDebugLogLevel.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
|
|||||||
@@ -182,12 +182,4 @@ class AppSettingsService extends ChangeNotifier {
|
|||||||
..remove(channelName);
|
..remove(channelName);
|
||||||
await updateSettings(_settings.copyWith(mutedChannels: updated));
|
await updateSettings(_settings.copyWith(mutedChannels: updated));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> setTcpServerAddress(String value) async {
|
|
||||||
await updateSettings(_settings.copyWith(tcpServerAddress: value));
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> setTcpServerPort(int value) async {
|
|
||||||
await updateSettings(_settings.copyWith(tcpServerPort: value));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,12 +58,13 @@ class MessageRetryService extends ChangeNotifier {
|
|||||||
Function(Message)? _updateMessageCallback;
|
Function(Message)? _updateMessageCallback;
|
||||||
Function(Contact)? _clearContactPathCallback;
|
Function(Contact)? _clearContactPathCallback;
|
||||||
Function(Contact, Uint8List, int)? _setContactPathCallback;
|
Function(Contact, Uint8List, int)? _setContactPathCallback;
|
||||||
Function(int, int)? _calculateTimeoutCallback;
|
Function(int, int, {String? contactKey})? _calculateTimeoutCallback;
|
||||||
Uint8List? Function()? _getSelfPublicKeyCallback;
|
Uint8List? Function()? _getSelfPublicKeyCallback;
|
||||||
String Function(Contact, String)? _prepareContactOutboundTextCallback;
|
String Function(Contact, String)? _prepareContactOutboundTextCallback;
|
||||||
AppSettingsService? _appSettingsService;
|
AppSettingsService? _appSettingsService;
|
||||||
AppDebugLogService? _debugLogService;
|
AppDebugLogService? _debugLogService;
|
||||||
Function(String, PathSelection, bool, int?)? _recordPathResultCallback;
|
Function(String, PathSelection, bool, int?)? _recordPathResultCallback;
|
||||||
|
Function(String, int, int, int)? _onDeliveryObservedCallback;
|
||||||
|
|
||||||
MessageRetryService();
|
MessageRetryService();
|
||||||
|
|
||||||
@@ -73,12 +74,20 @@ class MessageRetryService extends ChangeNotifier {
|
|||||||
required Function(Message) updateMessageCallback,
|
required Function(Message) updateMessageCallback,
|
||||||
Function(Contact)? clearContactPathCallback,
|
Function(Contact)? clearContactPathCallback,
|
||||||
Function(Contact, Uint8List, int)? setContactPathCallback,
|
Function(Contact, Uint8List, int)? setContactPathCallback,
|
||||||
Function(int pathLength, int messageBytes)? calculateTimeoutCallback,
|
Function(int pathLength, int messageBytes, {String? contactKey})?
|
||||||
|
calculateTimeoutCallback,
|
||||||
Uint8List? Function()? getSelfPublicKeyCallback,
|
Uint8List? Function()? getSelfPublicKeyCallback,
|
||||||
String Function(Contact, String)? prepareContactOutboundTextCallback,
|
String Function(Contact, String)? prepareContactOutboundTextCallback,
|
||||||
AppSettingsService? appSettingsService,
|
AppSettingsService? appSettingsService,
|
||||||
AppDebugLogService? debugLogService,
|
AppDebugLogService? debugLogService,
|
||||||
Function(String, PathSelection, bool, int?)? recordPathResultCallback,
|
Function(String, PathSelection, bool, int?)? recordPathResultCallback,
|
||||||
|
Function(
|
||||||
|
String contactKey,
|
||||||
|
int pathLength,
|
||||||
|
int messageBytes,
|
||||||
|
int tripTimeMs,
|
||||||
|
)?
|
||||||
|
onDeliveryObservedCallback,
|
||||||
}) {
|
}) {
|
||||||
_sendMessageCallback = sendMessageCallback;
|
_sendMessageCallback = sendMessageCallback;
|
||||||
_addMessageCallback = addMessageCallback;
|
_addMessageCallback = addMessageCallback;
|
||||||
@@ -91,6 +100,7 @@ class MessageRetryService extends ChangeNotifier {
|
|||||||
_appSettingsService = appSettingsService;
|
_appSettingsService = appSettingsService;
|
||||||
_debugLogService = debugLogService;
|
_debugLogService = debugLogService;
|
||||||
_recordPathResultCallback = recordPathResultCallback;
|
_recordPathResultCallback = recordPathResultCallback;
|
||||||
|
_onDeliveryObservedCallback = onDeliveryObservedCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute expected ACK hash using same algorithm as firmware:
|
/// Compute expected ACK hash using same algorithm as firmware:
|
||||||
@@ -423,25 +433,33 @@ class MessageRetryService extends ChangeNotifier {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use device-provided timeout, or calculate from radio settings if timeout is 0 or invalid
|
// Calculate timeout: prefer ML prediction, then device-provided, then physics fallback
|
||||||
|
int pathLengthValue;
|
||||||
|
if (selection != null) {
|
||||||
|
pathLengthValue = selection.useFlood ? -1 : selection.hopCount;
|
||||||
|
if (pathLengthValue < 0) pathLengthValue = contact.pathLength;
|
||||||
|
} else if (message.pathLength != null) {
|
||||||
|
pathLengthValue = message.pathLength!;
|
||||||
|
} else {
|
||||||
|
pathLengthValue = contact.pathLength;
|
||||||
|
}
|
||||||
|
|
||||||
int actualTimeout = timeoutMs;
|
int actualTimeout = timeoutMs;
|
||||||
if (timeoutMs <= 0 && _calculateTimeoutCallback != null) {
|
if (_calculateTimeoutCallback != null) {
|
||||||
int pathLengthValue;
|
final calculated = _calculateTimeoutCallback!(
|
||||||
if (selection != null) {
|
|
||||||
pathLengthValue = selection.useFlood ? -1 : selection.hopCount;
|
|
||||||
if (pathLengthValue < 0) pathLengthValue = contact.pathLength;
|
|
||||||
} else if (message.pathLength != null) {
|
|
||||||
pathLengthValue = message.pathLength!;
|
|
||||||
} else {
|
|
||||||
pathLengthValue = contact.pathLength;
|
|
||||||
}
|
|
||||||
actualTimeout = _calculateTimeoutCallback!(
|
|
||||||
pathLengthValue,
|
pathLengthValue,
|
||||||
message.text.length,
|
message.text.length,
|
||||||
|
contactKey: contact.publicKeyHex,
|
||||||
);
|
);
|
||||||
debugPrint(
|
// calculateTimeout tries ML first, falls back to physics.
|
||||||
'Using calculated timeout: ${actualTimeout}ms for path length $pathLengthValue',
|
// Use calculated value if device didn't provide one, or if ML
|
||||||
);
|
// produced a tighter prediction than the device's estimate.
|
||||||
|
if (timeoutMs <= 0 || calculated < timeoutMs) {
|
||||||
|
actualTimeout = calculated;
|
||||||
|
debugPrint(
|
||||||
|
'Using calculated timeout: ${actualTimeout}ms for path length $pathLengthValue',
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final updatedMessage = message.copyWith(
|
final updatedMessage = message.copyWith(
|
||||||
@@ -738,6 +756,16 @@ class MessageRetryService extends ChangeNotifier {
|
|||||||
true,
|
true,
|
||||||
tripTimeMs,
|
tripTimeMs,
|
||||||
);
|
);
|
||||||
|
if (_onDeliveryObservedCallback != null &&
|
||||||
|
tripTimeMs > 0 &&
|
||||||
|
message.pathLength != null) {
|
||||||
|
_onDeliveryObservedCallback!(
|
||||||
|
contact.publicKeyHex,
|
||||||
|
message.pathLength!,
|
||||||
|
message.text.length,
|
||||||
|
tripTimeMs,
|
||||||
|
);
|
||||||
|
}
|
||||||
_onMessageResolved(matchedMessageId, contact.publicKeyHex);
|
_onMessageResolved(matchedMessageId, contact.publicKeyHex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
import '../models/delivery_observation.dart';
|
||||||
import '../models/path_history.dart';
|
import '../models/path_history.dart';
|
||||||
import '../storage/prefs_manager.dart';
|
import '../storage/prefs_manager.dart';
|
||||||
|
|
||||||
@@ -6,6 +7,7 @@ class StorageService {
|
|||||||
static const String _pathHistoryPrefix = 'path_history_';
|
static const String _pathHistoryPrefix = 'path_history_';
|
||||||
static const String _pendingMessagesKey = 'pending_messages';
|
static const String _pendingMessagesKey = 'pending_messages';
|
||||||
static const String _repeaterPasswordsKey = 'repeater_passwords';
|
static const String _repeaterPasswordsKey = 'repeater_passwords';
|
||||||
|
static const String _deliveryObservationsKey = 'delivery_observations';
|
||||||
|
|
||||||
Future<void> savePathHistory(
|
Future<void> savePathHistory(
|
||||||
String contactPubKeyHex,
|
String contactPubKeyHex,
|
||||||
@@ -122,4 +124,33 @@ class StorageService {
|
|||||||
final prefs = PrefsManager.instance;
|
final prefs = PrefsManager.instance;
|
||||||
await prefs.remove(_repeaterPasswordsKey);
|
await prefs.remove(_repeaterPasswordsKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> saveDeliveryObservations(
|
||||||
|
List<DeliveryObservation> observations,
|
||||||
|
) async {
|
||||||
|
final prefs = PrefsManager.instance;
|
||||||
|
final jsonStr = jsonEncode(observations.map((o) => o.toJson()).toList());
|
||||||
|
await prefs.setString(_deliveryObservationsKey, jsonStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<DeliveryObservation>> loadDeliveryObservations() async {
|
||||||
|
final prefs = PrefsManager.instance;
|
||||||
|
final jsonStr = prefs.getString(_deliveryObservationsKey);
|
||||||
|
|
||||||
|
if (jsonStr == null) return [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
final list = jsonDecode(jsonStr) as List;
|
||||||
|
return list
|
||||||
|
.map((e) => DeliveryObservation.fromJson(e as Map<String, dynamic>))
|
||||||
|
.toList();
|
||||||
|
} catch (e) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> clearDeliveryObservations() async {
|
||||||
|
final prefs = PrefsManager.instance;
|
||||||
|
await prefs.remove(_deliveryObservationsKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,229 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:ml_algo/ml_algo.dart';
|
||||||
|
import 'package:ml_dataframe/ml_dataframe.dart';
|
||||||
|
import '../models/delivery_observation.dart';
|
||||||
|
import 'storage_service.dart';
|
||||||
|
|
||||||
|
class _ContactStats {
|
||||||
|
int count = 0;
|
||||||
|
double _sum = 0;
|
||||||
|
|
||||||
|
void add(double ms) {
|
||||||
|
count++;
|
||||||
|
_sum += ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
double get mean => _sum / count;
|
||||||
|
}
|
||||||
|
|
||||||
|
class TimeoutPredictionService extends ChangeNotifier {
|
||||||
|
final StorageService? _storage;
|
||||||
|
|
||||||
|
static const int minObservations = 10;
|
||||||
|
static const int maxObservations = 100;
|
||||||
|
static const int _retrainInterval = 5;
|
||||||
|
// 1.5x multiplier on raw prediction to account for variance in delivery
|
||||||
|
// times — tight enough to improve on worst-case physics, loose enough
|
||||||
|
// to avoid premature timeouts from model noise.
|
||||||
|
static const double _safetyMargin = 1.5;
|
||||||
|
static const int _minContactObservations = 10;
|
||||||
|
|
||||||
|
List<DeliveryObservation> _observations = [];
|
||||||
|
LinearRegressor? _model;
|
||||||
|
List<String> _activeFeatures = [];
|
||||||
|
int _observationsSinceLastTrain = 0;
|
||||||
|
final Map<String, _ContactStats> _contactStats = {};
|
||||||
|
Timer? _persistTimer;
|
||||||
|
|
||||||
|
TimeoutPredictionService(StorageService storage) : _storage = storage;
|
||||||
|
TimeoutPredictionService.noStorage() : _storage = null;
|
||||||
|
|
||||||
|
int get observationCount => _observations.length;
|
||||||
|
bool get hasModel => _model != null;
|
||||||
|
|
||||||
|
Future<void> initialize() async {
|
||||||
|
_observations = await _storage?.loadDeliveryObservations() ?? [];
|
||||||
|
_rebuildContactStats();
|
||||||
|
|
||||||
|
if (_observations.length >= minObservations) {
|
||||||
|
_trainModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
debugPrint(
|
||||||
|
'TimeoutPrediction: initialized with ${_observations.length} observations, '
|
||||||
|
'model=${_model != null ? "ready" : "waiting for data"}',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void recordObservation({
|
||||||
|
required String contactKey,
|
||||||
|
required int pathLength,
|
||||||
|
required int messageBytes,
|
||||||
|
required int tripTimeMs,
|
||||||
|
int secondsSinceLastRx = 0,
|
||||||
|
}) {
|
||||||
|
final observation = DeliveryObservation(
|
||||||
|
contactKey: contactKey,
|
||||||
|
pathLength: pathLength,
|
||||||
|
messageBytes: messageBytes,
|
||||||
|
secondsSinceLastRx: secondsSinceLastRx,
|
||||||
|
isFlood: pathLength < 0,
|
||||||
|
deliveryMs: tripTimeMs,
|
||||||
|
timestamp: DateTime.now(),
|
||||||
|
);
|
||||||
|
|
||||||
|
_observations.add(observation);
|
||||||
|
if (_observations.length > maxObservations) {
|
||||||
|
_observations.removeAt(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
_contactStats.putIfAbsent(contactKey, () => _ContactStats());
|
||||||
|
_contactStats[contactKey]!.add(tripTimeMs.toDouble());
|
||||||
|
|
||||||
|
_observationsSinceLastTrain++;
|
||||||
|
if (_observationsSinceLastTrain >= _retrainInterval &&
|
||||||
|
_observations.length >= minObservations) {
|
||||||
|
_trainModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
_persistTimer?.cancel();
|
||||||
|
_persistTimer = Timer(const Duration(seconds: 2), () {
|
||||||
|
_storage?.saveDeliveryObservations(_observations);
|
||||||
|
});
|
||||||
|
debugPrint(
|
||||||
|
'TimeoutPrediction: recorded ${tripTimeMs}ms for $pathLength hops '
|
||||||
|
'(${_observations.length} total)',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
int? predictTimeout({
|
||||||
|
String? contactKey,
|
||||||
|
required int pathLength,
|
||||||
|
required int messageBytes,
|
||||||
|
int secondsSinceLastRx = 0,
|
||||||
|
}) {
|
||||||
|
if (_model == null) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (_activeFeatures.isEmpty) return null;
|
||||||
|
|
||||||
|
final allFeatures = {
|
||||||
|
'pathLength': pathLength.toDouble(),
|
||||||
|
'messageBytes': messageBytes.toDouble(),
|
||||||
|
'secSinceRx': secondsSinceLastRx.toDouble(),
|
||||||
|
'isFlood': pathLength < 0 ? 1.0 : 0.0,
|
||||||
|
};
|
||||||
|
final row = _activeFeatures.map((f) => allFeatures[f]!).toList();
|
||||||
|
|
||||||
|
final features = DataFrame(
|
||||||
|
[row],
|
||||||
|
headerExists: false,
|
||||||
|
header: _activeFeatures,
|
||||||
|
);
|
||||||
|
|
||||||
|
final prediction = _model!.predict(features);
|
||||||
|
final rawValue = prediction.rows.first.first;
|
||||||
|
var predictedMs = (rawValue is double)
|
||||||
|
? rawValue
|
||||||
|
: (rawValue as num).toDouble();
|
||||||
|
|
||||||
|
debugPrint(
|
||||||
|
'TimeoutPrediction: raw prediction=$predictedMs for '
|
||||||
|
'pathLength=$pathLength, messageBytes=$messageBytes, '
|
||||||
|
'features=$_activeFeatures',
|
||||||
|
);
|
||||||
|
|
||||||
|
// Sanity check: if prediction is negative or zero, fall back
|
||||||
|
if (predictedMs <= 0) return null;
|
||||||
|
|
||||||
|
// Blend with per-contact mean if enough data
|
||||||
|
if (contactKey != null) {
|
||||||
|
final stats = _contactStats[contactKey];
|
||||||
|
if (stats != null && stats.count >= _minContactObservations) {
|
||||||
|
predictedMs = 0.5 * predictedMs + 0.5 * stats.mean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connector clamps this between physics min/max bounds
|
||||||
|
final timeout = (predictedMs * _safetyMargin).ceil();
|
||||||
|
debugPrint(
|
||||||
|
'TimeoutPrediction: ML timeout ${timeout}ms '
|
||||||
|
'(raw: ${predictedMs.round()}ms, contact: $contactKey)',
|
||||||
|
);
|
||||||
|
return timeout;
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('TimeoutPrediction: prediction failed: $e');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _trainModel() {
|
||||||
|
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 allExtractors = <double Function(DeliveryObservation)>[
|
||||||
|
(o) => o.pathLength.toDouble(),
|
||||||
|
(o) => o.messageBytes.toDouble(),
|
||||||
|
(o) => o.secondsSinceLastRx.toDouble(),
|
||||||
|
(o) => o.isFlood ? 1.0 : 0.0,
|
||||||
|
];
|
||||||
|
|
||||||
|
_activeFeatures = [];
|
||||||
|
for (var i = 0; i < allNames.length; i++) {
|
||||||
|
final values = _observations.map(allExtractors[i]).toSet();
|
||||||
|
if (values.length > 1) _activeFeatures.add(allNames[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_activeFeatures.isEmpty) {
|
||||||
|
debugPrint(
|
||||||
|
'TimeoutPrediction: no features with variance, skipping training',
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final header = [..._activeFeatures, 'deliveryMs'];
|
||||||
|
final rows = _observations.map((o) {
|
||||||
|
final row = <double>[];
|
||||||
|
for (var i = 0; i < allNames.length; i++) {
|
||||||
|
if (_activeFeatures.contains(allNames[i])) {
|
||||||
|
row.add(allExtractors[i](o));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
row.add(o.deliveryMs.toDouble());
|
||||||
|
return row;
|
||||||
|
});
|
||||||
|
|
||||||
|
final data = DataFrame([header, ...rows], headerExists: true);
|
||||||
|
|
||||||
|
_model = LinearRegressor(data, 'deliveryMs');
|
||||||
|
_observationsSinceLastTrain = 0;
|
||||||
|
|
||||||
|
// Log training summary with sample predictions
|
||||||
|
final avgMs =
|
||||||
|
_observations.map((o) => o.deliveryMs).reduce((a, b) => a + b) /
|
||||||
|
_observations.length;
|
||||||
|
debugPrint(
|
||||||
|
'TimeoutPrediction: trained on ${_observations.length} observations '
|
||||||
|
'(avg: ${avgMs.round()}ms, features: $_activeFeatures)',
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint('TimeoutPrediction: training failed: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_persistTimer?.cancel();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _rebuildContactStats() {
|
||||||
|
_contactStats.clear();
|
||||||
|
for (final obs in _observations) {
|
||||||
|
_contactStats.putIfAbsent(obs.contactKey, () => _ContactStats());
|
||||||
|
_contactStats[obs.contactKey]!.add(obs.deliveryMs.toDouble());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -189,10 +189,6 @@ class UsbSerialService {
|
|||||||
serial.setStopBits1();
|
serial.setStopBits1();
|
||||||
serial.setFlowControlNone();
|
serial.setFlowControlNone();
|
||||||
serial.setRTS(false);
|
serial.setRTS(false);
|
||||||
// Toggle DTR low→high so the device sees a fresh connection even
|
|
||||||
// if the previous disconnect didn't cleanly signal DTR drop.
|
|
||||||
serial.setDTR(false);
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
|
||||||
serial.setDTR(true);
|
serial.setDTR(true);
|
||||||
_serial = serial;
|
_serial = serial;
|
||||||
// Update the normalized port name to whichever candidate succeeded.
|
// Update the normalized port name to whichever candidate succeeded.
|
||||||
@@ -253,21 +249,6 @@ class UsbSerialService {
|
|||||||
_status = UsbSerialStatus.connected;
|
_status = UsbSerialStatus.connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> writeRaw(Uint8List data) async {
|
|
||||||
if (!isConnected) {
|
|
||||||
throw StateError('USB serial port is not open');
|
|
||||||
}
|
|
||||||
if (_useAndroidUsbHost) {
|
|
||||||
try {
|
|
||||||
await _androidMethodChannel.invokeMethod<void>('write', {'data': data});
|
|
||||||
} on PlatformException catch (error) {
|
|
||||||
throw StateError(error.message ?? error.code);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_serial!.write(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> write(Uint8List data) async {
|
Future<void> write(Uint8List data) async {
|
||||||
if (!isConnected) {
|
if (!isConnected) {
|
||||||
throw StateError('USB serial port is not open');
|
throw StateError('USB serial port is not open');
|
||||||
@@ -319,7 +300,6 @@ class UsbSerialService {
|
|||||||
_serial = null;
|
_serial = null;
|
||||||
try {
|
try {
|
||||||
if (serial?.isOpen() == FlOpenStatus.open) {
|
if (serial?.isOpen() == FlOpenStatus.open) {
|
||||||
serial?.setDTR(false);
|
|
||||||
serial?.closePort();
|
serial?.closePort();
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
@@ -370,7 +350,6 @@ class UsbSerialService {
|
|||||||
final serial = _serial;
|
final serial = _serial;
|
||||||
try {
|
try {
|
||||||
if (serial?.isOpen() == FlOpenStatus.open) {
|
if (serial?.isOpen() == FlOpenStatus.open) {
|
||||||
serial?.setDTR(false);
|
|
||||||
serial?.closePort(); // synchronous C call — kills the SerialThread
|
serial?.closePort(); // synchronous C call — kills the SerialThread
|
||||||
}
|
}
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
|
|||||||
@@ -127,17 +127,6 @@ class UsbSerialService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> writeRaw(Uint8List data) async {
|
|
||||||
if (!isConnected || _writer == null) {
|
|
||||||
throw StateError('USB serial port is not open');
|
|
||||||
}
|
|
||||||
final promise = _writer!.callMethod<JSPromise<JSAny?>>(
|
|
||||||
'write'.toJS,
|
|
||||||
data.toJS,
|
|
||||||
);
|
|
||||||
await promise.toDart;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> write(Uint8List data) async {
|
Future<void> write(Uint8List data) async {
|
||||||
if (!isConnected || _writer == null) {
|
if (!isConnected || _writer == null) {
|
||||||
throw StateError('USB serial port is not open');
|
throw StateError('USB serial port is not open');
|
||||||
|
|||||||
@@ -23,23 +23,23 @@ class AppLogger {
|
|||||||
bool get isEnabled => _enabled;
|
bool get isEnabled => _enabled;
|
||||||
|
|
||||||
/// Log an info message
|
/// Log an info message
|
||||||
void info(String message, {String tag = 'App', bool noNotify = false}) {
|
void info(String message, {String tag = 'App'}) {
|
||||||
if (_enabled && _service != null) {
|
if (_enabled && _service != null) {
|
||||||
_service!.info(message, tag: tag, noNotify: noNotify);
|
_service!.info(message, tag: tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Log a warning message
|
/// Log a warning message
|
||||||
void warn(String message, {String tag = 'App', bool noNotify = false}) {
|
void warn(String message, {String tag = 'App'}) {
|
||||||
if (_enabled && _service != null) {
|
if (_enabled && _service != null) {
|
||||||
_service!.warn(message, tag: tag, noNotify: noNotify);
|
_service!.warn(message, tag: tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Log an error message
|
/// Log an error message
|
||||||
void error(String message, {String tag = 'App', bool noNotify = false}) {
|
void error(String message, {String tag = 'App'}) {
|
||||||
if (_enabled && _service != null) {
|
if (_enabled && _service != null) {
|
||||||
_service!.error(message, tag: tag, noNotify: noNotify);
|
_service!.error(message, tag: tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,10 +48,9 @@ class AppLogger {
|
|||||||
String message, {
|
String message, {
|
||||||
String tag = 'App',
|
String tag = 'App',
|
||||||
AppDebugLogLevel level = AppDebugLogLevel.info,
|
AppDebugLogLevel level = AppDebugLogLevel.info,
|
||||||
bool noNotify = false,
|
|
||||||
}) {
|
}) {
|
||||||
if (_enabled && _service != null) {
|
if (_enabled && _service != null) {
|
||||||
_service!.log(message, tag: tag, level: level, noNotify: noNotify);
|
_service!.log(message, tag: tag, level: level);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ class _PathManagementDialogState extends State<_PathManagementDialog> {
|
|||||||
builder: (context) => PathTraceMapScreen(
|
builder: (context) => PathTraceMapScreen(
|
||||||
title: context.l10n.contacts_repeaterPathTrace,
|
title: context.l10n.contacts_repeaterPathTrace,
|
||||||
path: Uint8List.fromList(pathBytes),
|
path: Uint8List.fromList(pathBytes),
|
||||||
flipPathAround: true,
|
flipPathRound: true,
|
||||||
targetContact: widget.contact,
|
targetContact: widget.contact,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -107,7 +107,7 @@ class _PathManagementDialogState extends State<_PathManagementDialog> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final pathForInput = currentContact.pathIdList;
|
final pathForInput = currentContact.pathIdList;
|
||||||
final availableContacts = connector.allContacts
|
final availableContacts = connector.contacts
|
||||||
.where((c) => c.publicKeyHex != currentContact.publicKeyHex)
|
.where((c) => c.publicKeyHex != currentContact.publicKeyHex)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
|
||||||
import '../l10n/l10n.dart';
|
import '../l10n/l10n.dart';
|
||||||
import '../models/contact.dart';
|
import '../models/contact.dart';
|
||||||
|
|
||||||
@@ -66,7 +65,7 @@ class _PathSelectionDialogState extends State<PathSelectionDialog> {
|
|||||||
|
|
||||||
void _filterValidContacts() {
|
void _filterValidContacts() {
|
||||||
_validContacts = widget.availableContacts
|
_validContacts = widget.availableContacts
|
||||||
.where((c) => c.type == advTypeRepeater || c.type == advTypeRoom)
|
.where((c) => c.type == 2 || c.type == 3)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -157,7 +157,10 @@ class _SNRIndicatorState extends State<SNRIndicator> {
|
|||||||
repeater.snr,
|
repeater.snr,
|
||||||
widget.connector.currentSf,
|
widget.connector.currentSf,
|
||||||
);
|
);
|
||||||
final allContacts = widget.connector.allContacts;
|
final allContacts = [
|
||||||
|
...widget.connector.contacts,
|
||||||
|
...widget.connector.discoveredContacts,
|
||||||
|
];
|
||||||
final name = allContacts
|
final name = allContacts
|
||||||
.where((c) => c.publicKey.first == repeater.pubkeyFirstByte)
|
.where((c) => c.publicKey.first == repeater.pubkeyFirstByte)
|
||||||
.map((c) => c.name)
|
.map((c) => c.name)
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ dependencies:
|
|||||||
material_symbols_icons: ^4.2906.0
|
material_symbols_icons: ^4.2906.0
|
||||||
web: ^1.1.1
|
web: ^1.1.1
|
||||||
flutter_svg: ^2.0.10+1
|
flutter_svg: ^2.0.10+1
|
||||||
|
ml_algo: ^16.0.0
|
||||||
|
ml_dataframe: ^1.0.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
@@ -0,0 +1,156 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:ml_algo/ml_algo.dart';
|
||||||
|
import 'package:ml_dataframe/ml_dataframe.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('LinearRegressor basic sanity check', () {
|
||||||
|
// Simple: y = 2x + 100
|
||||||
|
final data = DataFrame(
|
||||||
|
[
|
||||||
|
[1.0, 102.0],
|
||||||
|
[2.0, 104.0],
|
||||||
|
[3.0, 106.0],
|
||||||
|
[4.0, 108.0],
|
||||||
|
[5.0, 110.0],
|
||||||
|
[10.0, 120.0],
|
||||||
|
[20.0, 140.0],
|
||||||
|
[50.0, 200.0],
|
||||||
|
[0.0, 100.0],
|
||||||
|
[100.0, 300.0],
|
||||||
|
],
|
||||||
|
headerExists: false,
|
||||||
|
header: ['x', 'y'],
|
||||||
|
);
|
||||||
|
|
||||||
|
debugPrint('Training data columns: ${data.header}');
|
||||||
|
debugPrint('Training data rows: ${data.rows.length}');
|
||||||
|
|
||||||
|
final model = LinearRegressor(data, 'y');
|
||||||
|
|
||||||
|
final testDf = DataFrame(
|
||||||
|
[
|
||||||
|
[25.0],
|
||||||
|
],
|
||||||
|
headerExists: false,
|
||||||
|
header: ['x'],
|
||||||
|
);
|
||||||
|
|
||||||
|
final prediction = model.predict(testDf);
|
||||||
|
final value = prediction.rows.first.first;
|
||||||
|
debugPrint('Predict x=25 → y=$value (expected ~150)');
|
||||||
|
expect((value as num).toDouble(), closeTo(150, 5));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('LinearRegressor multi-feature with constant column produces zeros', () {
|
||||||
|
// isFlood=0 for all rows → zero-variance column → singular matrix
|
||||||
|
final data = DataFrame(
|
||||||
|
[
|
||||||
|
[0.0, 50.0, 14.0, 0.0, 1900.0],
|
||||||
|
[0.0, 80.0, 14.0, 0.0, 2200.0],
|
||||||
|
[2.0, 50.0, 14.0, 0.0, 5000.0],
|
||||||
|
[4.0, 50.0, 14.0, 0.0, 9500.0],
|
||||||
|
],
|
||||||
|
headerExists: false,
|
||||||
|
header: [
|
||||||
|
'pathLength',
|
||||||
|
'messageBytes',
|
||||||
|
'hourOfDay',
|
||||||
|
'isFlood',
|
||||||
|
'deliveryMs',
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
final model = LinearRegressor(data, 'deliveryMs');
|
||||||
|
final testDf = DataFrame(
|
||||||
|
[
|
||||||
|
[2.0, 50.0, 14.0, 0.0],
|
||||||
|
],
|
||||||
|
headerExists: false,
|
||||||
|
header: ['pathLength', 'messageBytes', 'hourOfDay', 'isFlood'],
|
||||||
|
);
|
||||||
|
final pred = model.predict(testDf).rows.first.first;
|
||||||
|
debugPrint(
|
||||||
|
'With constant isFlood column: hops=2 → ${(pred as num).round()}ms (likely 0)',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('LinearRegressor 2-feature works correctly', () {
|
||||||
|
// Just pathLength + messageBytes → deliveryMs
|
||||||
|
final data = DataFrame(
|
||||||
|
[
|
||||||
|
[0.0, 50.0, 1900.0],
|
||||||
|
[0.0, 80.0, 2200.0],
|
||||||
|
[2.0, 50.0, 5000.0],
|
||||||
|
[2.0, 80.0, 5500.0],
|
||||||
|
[4.0, 50.0, 9500.0],
|
||||||
|
[4.0, 80.0, 10000.0],
|
||||||
|
[0.0, 30.0, 1800.0],
|
||||||
|
[2.0, 30.0, 4800.0],
|
||||||
|
[4.0, 30.0, 9000.0],
|
||||||
|
[0.0, 60.0, 2000.0],
|
||||||
|
],
|
||||||
|
headerExists: false,
|
||||||
|
header: ['pathLength', 'messageBytes', 'deliveryMs'],
|
||||||
|
);
|
||||||
|
|
||||||
|
final model = LinearRegressor(data, 'deliveryMs');
|
||||||
|
|
||||||
|
for (final hops in [0.0, 2.0, 4.0]) {
|
||||||
|
final testDf = DataFrame(
|
||||||
|
[
|
||||||
|
[hops, 50.0],
|
||||||
|
],
|
||||||
|
headerExists: false,
|
||||||
|
header: ['pathLength', 'messageBytes'],
|
||||||
|
);
|
||||||
|
final pred = model.predict(testDf).rows.first.first;
|
||||||
|
debugPrint('2-feature: hops=$hops → ${(pred as num).round()}ms');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('LinearRegressor multi-feature with variance in all columns', () {
|
||||||
|
// Mix flood and direct so isFlood has variance
|
||||||
|
final data = DataFrame(
|
||||||
|
[
|
||||||
|
[0.0, 50.0, 14.0, 0.0, 1900.0],
|
||||||
|
[0.0, 80.0, 10.0, 0.0, 2200.0],
|
||||||
|
[2.0, 50.0, 16.0, 0.0, 5000.0],
|
||||||
|
[2.0, 80.0, 20.0, 0.0, 5500.0],
|
||||||
|
[4.0, 50.0, 8.0, 0.0, 9500.0],
|
||||||
|
[4.0, 80.0, 12.0, 0.0, 10000.0],
|
||||||
|
[-1.0, 40.0, 14.0, 1.0, 5000.0],
|
||||||
|
[-1.0, 60.0, 18.0, 1.0, 6500.0],
|
||||||
|
[-1.0, 30.0, 10.0, 1.0, 4000.0],
|
||||||
|
[-1.0, 80.0, 22.0, 1.0, 7000.0],
|
||||||
|
],
|
||||||
|
headerExists: false,
|
||||||
|
header: [
|
||||||
|
'pathLength',
|
||||||
|
'messageBytes',
|
||||||
|
'hourOfDay',
|
||||||
|
'isFlood',
|
||||||
|
'deliveryMs',
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
final model = LinearRegressor(data, 'deliveryMs');
|
||||||
|
|
||||||
|
for (final tc in [
|
||||||
|
[0.0, 50.0, 14.0, 0.0],
|
||||||
|
[2.0, 50.0, 14.0, 0.0],
|
||||||
|
[4.0, 50.0, 14.0, 0.0],
|
||||||
|
[-1.0, 50.0, 14.0, 1.0],
|
||||||
|
]) {
|
||||||
|
final testDf = DataFrame(
|
||||||
|
[tc],
|
||||||
|
headerExists: false,
|
||||||
|
header: ['pathLength', 'messageBytes', 'hourOfDay', 'isFlood'],
|
||||||
|
);
|
||||||
|
final pred = model.predict(testDf).rows.first.first;
|
||||||
|
debugPrint(
|
||||||
|
'4-feature: hops=${tc[0]} flood=${tc[3]} → ${(pred as num).round()}ms',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:meshcore_open/models/delivery_observation.dart';
|
||||||
|
import 'package:meshcore_open/services/timeout_prediction_service.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late TimeoutPredictionService service;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
service = TimeoutPredictionService.noStorage();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('trains on sample data and predicts sensible timeouts', () {
|
||||||
|
// Simulate realistic delivery data:
|
||||||
|
// Direct 0-hop messages: ~1500-2500ms
|
||||||
|
// 2-hop messages: ~4000-6000ms
|
||||||
|
// 4-hop messages: ~8000-12000ms
|
||||||
|
// Flood messages: ~3000-8000ms
|
||||||
|
final sampleData = [
|
||||||
|
// 0-hop direct
|
||||||
|
_obs(pathLength: 0, messageBytes: 20, deliveryMs: 1800),
|
||||||
|
_obs(pathLength: 0, messageBytes: 50, deliveryMs: 2100),
|
||||||
|
_obs(pathLength: 0, messageBytes: 80, deliveryMs: 2400),
|
||||||
|
_obs(pathLength: 0, messageBytes: 30, deliveryMs: 1925),
|
||||||
|
// 2-hop direct
|
||||||
|
_obs(pathLength: 2, messageBytes: 40, deliveryMs: 4500),
|
||||||
|
_obs(pathLength: 2, messageBytes: 60, deliveryMs: 5200),
|
||||||
|
_obs(pathLength: 2, messageBytes: 25, deliveryMs: 4100),
|
||||||
|
// 4-hop direct
|
||||||
|
_obs(pathLength: 4, messageBytes: 50, deliveryMs: 9800),
|
||||||
|
_obs(pathLength: 4, messageBytes: 30, deliveryMs: 8500),
|
||||||
|
_obs(pathLength: 4, messageBytes: 70, deliveryMs: 10570),
|
||||||
|
// Flood
|
||||||
|
_obs(pathLength: -1, messageBytes: 40, deliveryMs: 5000),
|
||||||
|
_obs(pathLength: -1, messageBytes: 60, deliveryMs: 6500),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Feed all observations
|
||||||
|
for (final obs in sampleData) {
|
||||||
|
service.recordObservation(
|
||||||
|
contactKey: obs.contactKey,
|
||||||
|
pathLength: obs.pathLength,
|
||||||
|
messageBytes: obs.messageBytes,
|
||||||
|
tripTimeMs: obs.deliveryMs,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(service.hasModel, isTrue);
|
||||||
|
expect(service.observationCount, equals(12));
|
||||||
|
|
||||||
|
// Predict for different scenarios
|
||||||
|
final direct0 = service.predictTimeout(pathLength: 0, messageBytes: 50);
|
||||||
|
final direct2 = service.predictTimeout(pathLength: 2, messageBytes: 50);
|
||||||
|
final direct4 = service.predictTimeout(pathLength: 4, messageBytes: 50);
|
||||||
|
final flood = service.predictTimeout(pathLength: -1, messageBytes: 50);
|
||||||
|
|
||||||
|
// All should return non-null (model is trained)
|
||||||
|
expect(direct0, isNotNull);
|
||||||
|
expect(direct2, isNotNull);
|
||||||
|
expect(direct4, isNotNull);
|
||||||
|
expect(flood, isNotNull);
|
||||||
|
|
||||||
|
// More hops should predict longer timeouts
|
||||||
|
expect(direct4!, greaterThan(direct2!));
|
||||||
|
expect(direct2, greaterThan(direct0!));
|
||||||
|
|
||||||
|
// All should be positive
|
||||||
|
expect(direct0, greaterThan(0));
|
||||||
|
expect(direct4, greaterThan(0));
|
||||||
|
|
||||||
|
// Print predictions for visibility
|
||||||
|
debugPrint('Predictions (with 1.5x safety margin):');
|
||||||
|
debugPrint(' 0-hop direct: ${direct0}ms');
|
||||||
|
debugPrint(' 2-hop direct: ${direct2}ms');
|
||||||
|
debugPrint(' 4-hop direct: ${direct4}ms');
|
||||||
|
debugPrint(' flood: ${flood}ms');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns null before minimum observations', () {
|
||||||
|
for (var i = 0; i < TimeoutPredictionService.minObservations - 1; i++) {
|
||||||
|
service.recordObservation(
|
||||||
|
contactKey: 'abc',
|
||||||
|
pathLength: 0,
|
||||||
|
messageBytes: 50,
|
||||||
|
tripTimeMs: 2000,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(service.hasModel, isFalse);
|
||||||
|
expect(service.predictTimeout(pathLength: 0, messageBytes: 50), isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('caps observations at maxObservations', () {
|
||||||
|
for (var i = 0; i < TimeoutPredictionService.maxObservations + 20; i++) {
|
||||||
|
service.recordObservation(
|
||||||
|
contactKey: 'abc',
|
||||||
|
pathLength: 0,
|
||||||
|
messageBytes: 50,
|
||||||
|
tripTimeMs: 2000 + i,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(
|
||||||
|
service.observationCount,
|
||||||
|
equals(TimeoutPredictionService.maxObservations),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('blends per-contact stats after enough observations', () {
|
||||||
|
// Train with mixed contacts and varied features:
|
||||||
|
// contactA is fast (0-hop), contactB is slow (2-hop)
|
||||||
|
for (var i = 0; i < 12; i++) {
|
||||||
|
service.recordObservation(
|
||||||
|
contactKey: 'contactA',
|
||||||
|
pathLength: 0,
|
||||||
|
messageBytes: 30 + i,
|
||||||
|
tripTimeMs: 1500,
|
||||||
|
);
|
||||||
|
service.recordObservation(
|
||||||
|
contactKey: 'contactB',
|
||||||
|
pathLength: 2,
|
||||||
|
messageBytes: 30 + i,
|
||||||
|
tripTimeMs: 8000,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final predA = service.predictTimeout(
|
||||||
|
contactKey: 'contactA',
|
||||||
|
pathLength: 0,
|
||||||
|
messageBytes: 50,
|
||||||
|
);
|
||||||
|
final predB = service.predictTimeout(
|
||||||
|
contactKey: 'contactB',
|
||||||
|
pathLength: 0,
|
||||||
|
messageBytes: 50,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(predA, isNotNull);
|
||||||
|
expect(predB, isNotNull);
|
||||||
|
// Contact B (slow) should have a higher predicted timeout than A (fast)
|
||||||
|
expect(predB!, greaterThan(predA!));
|
||||||
|
|
||||||
|
debugPrint('Per-contact blending:');
|
||||||
|
debugPrint(' contactA (fast): ${predA}ms');
|
||||||
|
debugPrint(' contactB (slow): ${predB}ms');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
DeliveryObservation _obs({
|
||||||
|
required int pathLength,
|
||||||
|
required int messageBytes,
|
||||||
|
required int deliveryMs,
|
||||||
|
String contactKey = 'test_contact',
|
||||||
|
}) {
|
||||||
|
return DeliveryObservation(
|
||||||
|
contactKey: contactKey,
|
||||||
|
pathLength: pathLength,
|
||||||
|
messageBytes: messageBytes,
|
||||||
|
secondsSinceLastRx: 5,
|
||||||
|
isFlood: pathLength < 0,
|
||||||
|
deliveryMs: deliveryMs,
|
||||||
|
timestamp: DateTime.now(),
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user