完成 远程控制页面的手机和服务器端网络状态的数值显示

This commit is contained in:
2026-04-02 16:40:18 +08:00
parent a9f21b6460
commit 507935166e
7 changed files with 74 additions and 25 deletions

View File

@@ -22,6 +22,9 @@
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:label="飒沓机器人"
android:name="${applicationName}"

View File

@@ -1,7 +1,9 @@
import 'dart:async';
import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:dart_ping/dart_ping.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -68,7 +70,7 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
final controlMode = status.controlMode == '3' ? '远程模式' : '本地模式';
// final c = getPing('1.95.137.212'); // ⚠️ 替换为你的服务器 IP
final c = getNetworkDelay(); //
emit(state.copyWith(
runningStatusModel: state.runningStatusModel.copyWith(
voltage: voltage.toString(), // 更新电压
@@ -76,7 +78,7 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
controlMode: controlMode, // 更新控制模式
),
battery: int.tryParse(battery) ?? 0,
//ping: c, // 这里直接使用异步返回的数值
ping: await c, // 这里直接使用异步返回的数值
));
@@ -121,7 +123,7 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
// }
// });
// }
void _initPacketListener() {
Future<void> _initPacketListener() async {
print('>>> [RemoteControl] begin 初始化 0x12 监听器');
_kickOutSub?.cancel(); // 防止重复监听
@@ -191,7 +193,10 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
print('>>> [RemoteControl] ❌ 解析失败:$e');
}
});
final c = getNetworkDelay();
emit(state.copyWith(
ping: await c, // 这里直接使用异步返回的数值
));
print('>>> [RemoteControl] ✅ 0x12 监听器已建立完成');
}
@@ -362,30 +367,23 @@ class RemoteControlCubit extends Cubit<RemoteControlState> {
emit(state.copyWith(topRightIsExpanded: !state.topRightIsExpanded));
}
int getPing(String host) {
print('>>> [RemoteControl] 📡 开始 ping $host...');
Future<int> getNetworkDelay() async {
try {
// 直接 Ping 你的服务器 IP
final ping = Ping('1.95.137.212', count: 1, timeout: 1);
// 后台异步获取,不阻塞
platform.invokeMethod('ping', {
'host': host,
'count': 1,
'timeout': 2,
}).then((result) {
final success = result['success'] as bool? ?? false;
final delay = (result['delayMs'] as num?)?.toInt() ?? 100;
_currentPing = success ? delay.clamp(0, 100) : 100;
print('📶 [Ping 结果] $host - $_currentPing ms');
// 等待一次结果
final data = await ping.stream.first;
// 获取到新值后,再次 emit 更新 state
if (!isClosed) {
emit(state.copyWith(ping: _currentPing));
if (data.response != null && data.response!.time != null) {
// 返回和 cmd 一样的毫秒值
return data.response!.time!.inMilliseconds;
} else {
return 9999;
}
}).catchError((e) {
print('⚠️ [Ping 错误] $e');
_currentPing = 100;
});
return _currentPing; // 立即返回当前值
} catch (e) {
return 9999;
}
}

View File

@@ -5,6 +5,7 @@
import FlutterMacOS
import Foundation
import connectivity_plus
import device_info_plus
import file_selector_macos
import flutter_webrtc
@@ -18,6 +19,7 @@ import url_launcher_macos
import webview_flutter_wkwebview
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
ConnectivityPlusPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlusPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
FlutterWebRTCPlugin.register(with: registry.registrar(forPlugin: "FlutterWebRTCPlugin"))

View File

@@ -168,6 +168,22 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.19.1"
connectivity_plus:
dependency: "direct dev"
description:
name: connectivity_plus
sha256: b5e72753cf63becce2c61fd04dfe0f1c430cc5278b53a1342dc5ad839eab29ec
url: "https://pub.flutter-io.cn"
source: hosted
version: "6.1.5"
connectivity_plus_platform_interface:
dependency: transitive
description:
name: connectivity_plus_platform_interface
sha256: "3c09627c536d22fd24691a905cdd8b14520de69da52c7a97499c8be5284a32ed"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
convert:
dependency: transitive
description:
@@ -200,6 +216,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.9"
dart_ping:
dependency: "direct dev"
description:
name: dart_ping
sha256: "2f5418d0a5c64e53486caaac78677b25725b1e13c33c5be834ce874ea18bd24f"
url: "https://pub.flutter-io.cn"
source: hosted
version: "9.0.1"
dart_style:
dependency: transitive
description:
@@ -224,6 +248,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
dbus:
dependency: transitive
description:
name: dbus
sha256: d0c98dcd4f5169878b6cf8f6e0a52403a9dff371a3e2f019697accbf6f44a270
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.7.12"
device_info_plus:
dependency: "direct main"
description:
@@ -847,6 +879,14 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.0"
nm:
dependency: transitive
description:
name: nm
sha256: "2c9aae4127bdc8993206464fcc063611e0e36e72018696cd9631023a31b24254"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.5.0"
objective_c:
dependency: transitive
description:

View File

@@ -138,6 +138,8 @@ dev_dependencies:
# rules and activating additional ones.
flutter_lints: ^6.0.0
dart_ping: ^9.0.0
connectivity_plus: ^6.0.3
# 自动生成 ".g.dart" 文件
# 只有dev运行命令时才需要,用户运行 App 时不需要
isar_community_generator: ^3.3.0

View File

@@ -6,6 +6,7 @@
#include "generated_plugin_registrant.h"
#include <connectivity_plus/connectivity_plus_windows_plugin.h>
#include <file_selector_windows/file_selector_windows.h>
#include <flutter_webrtc/flutter_web_r_t_c_plugin.h>
#include <geolocator_windows/geolocator_windows.h>
@@ -15,6 +16,8 @@
#include <url_launcher_windows/url_launcher_windows.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
ConnectivityPlusWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin"));
FileSelectorWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FileSelectorWindows"));
FlutterWebRTCPluginRegisterWithRegistrar(

View File

@@ -3,6 +3,7 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
connectivity_plus
file_selector_windows
flutter_webrtc
geolocator_windows