feat(app): 初始化家庭网络监控Flutter应用基本结构
- 添加Flutter项目基础文件与配置,包括.gitignore和analysis_options.yaml - 配置Android相关文件及Gradle构建脚本支持 - 新增设备状态与网络状态数据模型及相关枚举 - 实现网络检测及网速测试服务 - 创建监控状态管理Provider,实现设备状态管理和自动刷新机制 - 编写主界面HomeScreen,包括设备列表、网速仪表盘及基础交互 - 添加资源文件支持应用图标和启动画面 - 配置项目元数据文件,支持Flutter迁移及版本控制 - 新增项目README,提供入门指引和相关资源链接
This commit is contained in:
109
home_monitor/lib/models/network_status.dart
Normal file
109
home_monitor/lib/models/network_status.dart
Normal file
@@ -0,0 +1,109 @@
|
||||
/// 网络状态模型
|
||||
class NetworkStatus {
|
||||
final bool isConnected; // 是否连接
|
||||
final double downloadSpeed; // 下载速度 (Mbps)
|
||||
final double uploadSpeed; // 上传速度 (Mbps)
|
||||
final int latency; // 延迟 (ms)
|
||||
final String externalIp; // 外网IP
|
||||
final String localIp; // 内网IP
|
||||
final DateTime lastCheck; // 最后检测时间
|
||||
|
||||
const NetworkStatus({
|
||||
this.isConnected = false,
|
||||
this.downloadSpeed = 0,
|
||||
this.uploadSpeed = 0,
|
||||
this.latency = 0,
|
||||
this.externalIp = '',
|
||||
this.localIp = '',
|
||||
required this.lastCheck,
|
||||
});
|
||||
|
||||
/// 默认状态
|
||||
factory NetworkStatus.initial() {
|
||||
return NetworkStatus(lastCheck: DateTime.now());
|
||||
}
|
||||
|
||||
/// 从JSON创建
|
||||
factory NetworkStatus.fromJson(Map<String, dynamic> json) {
|
||||
return NetworkStatus(
|
||||
isConnected: json['isConnected'] as bool? ?? false,
|
||||
downloadSpeed: (json['downloadSpeed'] as num?)?.toDouble() ?? 0,
|
||||
uploadSpeed: (json['uploadSpeed'] as num?)?.toDouble() ?? 0,
|
||||
latency: json['latency'] as int? ?? 0,
|
||||
externalIp: json['externalIp'] as String? ?? '',
|
||||
localIp: json['localIp'] as String? ?? '',
|
||||
lastCheck: DateTime.parse(json['lastCheck'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
/// 转换为JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'isConnected': isConnected,
|
||||
'downloadSpeed': downloadSpeed,
|
||||
'uploadSpeed': uploadSpeed,
|
||||
'latency': latency,
|
||||
'externalIp': externalIp,
|
||||
'localIp': localIp,
|
||||
'lastCheck': lastCheck.toIso8601String(),
|
||||
};
|
||||
}
|
||||
|
||||
/// 复制并更新
|
||||
NetworkStatus copyWith({
|
||||
bool? isConnected,
|
||||
double? downloadSpeed,
|
||||
double? uploadSpeed,
|
||||
int? latency,
|
||||
String? externalIp,
|
||||
String? localIp,
|
||||
DateTime? lastCheck,
|
||||
}) {
|
||||
return NetworkStatus(
|
||||
isConnected: isConnected ?? this.isConnected,
|
||||
downloadSpeed: downloadSpeed ?? this.downloadSpeed,
|
||||
uploadSpeed: uploadSpeed ?? this.uploadSpeed,
|
||||
latency: latency ?? this.latency,
|
||||
externalIp: externalIp ?? this.externalIp,
|
||||
localIp: localIp ?? this.localIp,
|
||||
lastCheck: lastCheck ?? this.lastCheck,
|
||||
);
|
||||
}
|
||||
|
||||
/// 获取网络状态描述
|
||||
String get statusDescription {
|
||||
if (!isConnected) return '网络断开';
|
||||
if (latency > 200) return '网络较慢';
|
||||
if (latency > 100) return '网络一般';
|
||||
return '网络良好';
|
||||
}
|
||||
|
||||
/// 格式化下载速度
|
||||
String get downloadSpeedText {
|
||||
if (downloadSpeed >= 1000) {
|
||||
return '${(downloadSpeed / 1000).toStringAsFixed(2)} Gbps';
|
||||
}
|
||||
return '${downloadSpeed.toStringAsFixed(2)} Mbps';
|
||||
}
|
||||
|
||||
/// 格式化上传速度
|
||||
String get uploadSpeedText {
|
||||
if (uploadSpeed >= 1000) {
|
||||
return '${(uploadSpeed / 1000).toStringAsFixed(2)} Gbps';
|
||||
}
|
||||
return '${uploadSpeed.toStringAsFixed(2)} Mbps';
|
||||
}
|
||||
}
|
||||
|
||||
/// 网速历史记录
|
||||
class SpeedHistory {
|
||||
final DateTime time;
|
||||
final double download;
|
||||
final double upload;
|
||||
|
||||
const SpeedHistory({
|
||||
required this.time,
|
||||
required this.download,
|
||||
required this.upload,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user