- 添加Flutter项目基础文件与配置,包括.gitignore和analysis_options.yaml - 配置Android相关文件及Gradle构建脚本支持 - 新增设备状态与网络状态数据模型及相关枚举 - 实现网络检测及网速测试服务 - 创建监控状态管理Provider,实现设备状态管理和自动刷新机制 - 编写主界面HomeScreen,包括设备列表、网速仪表盘及基础交互 - 添加资源文件支持应用图标和启动画面 - 配置项目元数据文件,支持Flutter迁移及版本控制 - 新增项目README,提供入门指引和相关资源链接
120 lines
2.8 KiB
Dart
120 lines
2.8 KiB
Dart
import 'dart:async';
|
||
import 'dart:io';
|
||
import '../models/models.dart';
|
||
|
||
/// 网络检测服务
|
||
class NetworkService {
|
||
static final NetworkService _instance = NetworkService._internal();
|
||
factory NetworkService() => _instance;
|
||
NetworkService._internal();
|
||
|
||
/// Ping指定主机
|
||
Future<int?> ping(String host, {int timeout = 5000}) async {
|
||
try {
|
||
final stopwatch = Stopwatch()..start();
|
||
|
||
// 尝试建立TCP连接来检测
|
||
final socket = await Socket.connect(
|
||
host,
|
||
80,
|
||
timeout: Duration(milliseconds: timeout),
|
||
);
|
||
|
||
stopwatch.stop();
|
||
socket.destroy();
|
||
|
||
return stopwatch.elapsedMilliseconds;
|
||
} catch (e) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// 检测设备是否在线
|
||
Future<DeviceStatus> checkDevice(DeviceStatus device) async {
|
||
final stopwatch = Stopwatch()..start();
|
||
|
||
try {
|
||
final socket = await Socket.connect(
|
||
device.ip,
|
||
device.port,
|
||
timeout: const Duration(seconds: 5),
|
||
);
|
||
|
||
stopwatch.stop();
|
||
socket.destroy();
|
||
|
||
return device.copyWith(
|
||
state: DeviceState.online,
|
||
latency: stopwatch.elapsedMilliseconds,
|
||
lastCheck: DateTime.now(),
|
||
errorMessage: null,
|
||
);
|
||
} on SocketException catch (e) {
|
||
return device.copyWith(
|
||
state: DeviceState.offline,
|
||
latency: null,
|
||
lastCheck: DateTime.now(),
|
||
errorMessage: e.message,
|
||
);
|
||
} catch (e) {
|
||
return device.copyWith(
|
||
state: DeviceState.unknown,
|
||
latency: null,
|
||
lastCheck: DateTime.now(),
|
||
errorMessage: e.toString(),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 批量检测设备
|
||
Future<List<DeviceStatus>> checkAllDevices(List<DeviceStatus> devices) async {
|
||
final results = await Future.wait(
|
||
devices.map((device) => checkDevice(device)),
|
||
);
|
||
return results;
|
||
}
|
||
|
||
/// 检测网络连通性(通过多个公共DNS)
|
||
Future<bool> checkInternetConnection() async {
|
||
final hosts = [
|
||
'8.8.8.8', // Google DNS
|
||
'114.114.114.114', // 114 DNS
|
||
'223.5.5.5', // 阿里 DNS
|
||
];
|
||
|
||
for (final host in hosts) {
|
||
final latency = await ping(host);
|
||
if (latency != null) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// 获取本地IP地址
|
||
Future<String> getLocalIp() async {
|
||
try {
|
||
final interfaces = await NetworkInterface.list(
|
||
type: InternetAddressType.IPv4,
|
||
);
|
||
|
||
for (final interface in interfaces) {
|
||
for (final addr in interface.addresses) {
|
||
if (!addr.isLoopback) {
|
||
return addr.address;
|
||
}
|
||
}
|
||
}
|
||
} catch (e) {
|
||
// 忽略错误
|
||
}
|
||
return '未知';
|
||
}
|
||
|
||
/// 测量到指定主机的延迟
|
||
Future<int> measureLatency(String host) async {
|
||
final latency = await ping(host);
|
||
return latency ?? -1;
|
||
}
|
||
}
|