| .. | .. |
|---|
| 81 | 81 | String? connectedHost; // The host we're currently connected to |
|---|
| 82 | 82 | String? connectedVia; // "Local", "VPN", "Remote", "Bonjour", "Scan" |
|---|
| 83 | 83 | void Function(Map<String, dynamic> message)? onMessage; |
|---|
| 84 | + /// Called when the server sends a debug_state_request on pailot/control/out. |
|---|
| 85 | + /// The handler should read current session state and call [publishDebugStateResponse]. |
|---|
| 86 | + void Function(String requestId)? onDebugStateRequest; |
|---|
| 84 | 87 | void Function()? onOpen; |
|---|
| 85 | 88 | void Function()? onClose; |
|---|
| 86 | 89 | void Function()? onReconnecting; |
|---|
| .. | .. |
|---|
| 625 | 628 | |
|---|
| 626 | 629 | // pailot/control/out — command responses |
|---|
| 627 | 630 | if (topic == 'pailot/control/out') { |
|---|
| 631 | + // Intercept debug_state_request — handle locally, do not forward to UI. |
|---|
| 632 | + if (json['type'] == 'debug_state_request') { |
|---|
| 633 | + final requestId = json['requestId'] as String?; |
|---|
| 634 | + if (requestId != null) { |
|---|
| 635 | + _mqttLog('debug_state_request received (requestId=$requestId)'); |
|---|
| 636 | + onDebugStateRequest?.call(requestId); |
|---|
| 637 | + } |
|---|
| 638 | + return; |
|---|
| 639 | + } |
|---|
| 628 | 640 | onMessage?.call(json); |
|---|
| 629 | 641 | return; |
|---|
| 630 | 642 | } |
|---|
| .. | .. |
|---|
| 686 | 698 | } |
|---|
| 687 | 699 | } |
|---|
| 688 | 700 | |
|---|
| 701 | + /// Publish a debug_state_response back to the broker on pailot/control/in. |
|---|
| 702 | + /// Call this from the [onDebugStateRequest] handler once session state is collected. |
|---|
| 703 | + void publishDebugStateResponse({ |
|---|
| 704 | + required String requestId, |
|---|
| 705 | + required String? activeSessionId, |
|---|
| 706 | + required List<Map<String, dynamic>> sessions, |
|---|
| 707 | + String? appVersion, |
|---|
| 708 | + String? platform, |
|---|
| 709 | + }) { |
|---|
| 710 | + _publish('pailot/control/in', { |
|---|
| 711 | + 'msgId': const Uuid().v4(), |
|---|
| 712 | + 'type': 'command', |
|---|
| 713 | + 'command': 'debug_state_response', |
|---|
| 714 | + 'requestId': requestId, |
|---|
| 715 | + 'activeSessionId': activeSessionId, |
|---|
| 716 | + 'sessions': sessions, |
|---|
| 717 | + if (appVersion != null) 'appVersion': appVersion, |
|---|
| 718 | + if (platform != null) 'platform': platform, |
|---|
| 719 | + 'ts': DateTime.now().millisecondsSinceEpoch, |
|---|
| 720 | + }, MqttQos.atLeastOnce); |
|---|
| 721 | + _mqttLog('debug_state_response sent for requestId=$requestId sessions=${sessions.length}'); |
|---|
| 722 | + } |
|---|
| 723 | + |
|---|
| 689 | 724 | /// Send a message — routes to the appropriate MQTT topic based on content. |
|---|
| 690 | 725 | void send(Map<String, dynamic> message) { |
|---|
| 691 | 726 | final type = message['type'] as String?; |
|---|