feat(app): 初始化家庭网络监控Flutter应用基本结构
- 添加Flutter项目基础文件与配置,包括.gitignore和analysis_options.yaml - 配置Android相关文件及Gradle构建脚本支持 - 新增设备状态与网络状态数据模型及相关枚举 - 实现网络检测及网速测试服务 - 创建监控状态管理Provider,实现设备状态管理和自动刷新机制 - 编写主界面HomeScreen,包括设备列表、网速仪表盘及基础交互 - 添加资源文件支持应用图标和启动画面 - 配置项目元数据文件,支持Flutter迁移及版本控制 - 新增项目README,提供入门指引和相关资源链接
This commit is contained in:
127
home_monitor/lib/models/device_status.dart
Normal file
127
home_monitor/lib/models/device_status.dart
Normal file
@@ -0,0 +1,127 @@
|
||||
/// 设备状态枚举
|
||||
enum DeviceState {
|
||||
online, // 在线
|
||||
offline, // 离线
|
||||
warning, // 警告
|
||||
unknown, // 未知
|
||||
}
|
||||
|
||||
/// 设备类型枚举
|
||||
enum DeviceType {
|
||||
gateway, // 网关/主路由
|
||||
bypass, // 旁路由
|
||||
nas, // NAS存储
|
||||
server, // 服务器
|
||||
camera, // 摄像头
|
||||
printer, // 打印机
|
||||
other, // 其他设备
|
||||
}
|
||||
|
||||
/// 设备状态模型
|
||||
class DeviceStatus {
|
||||
final String id;
|
||||
final String name;
|
||||
final DeviceType type;
|
||||
final String ip;
|
||||
final int port;
|
||||
final DeviceState state;
|
||||
final int? latency; // 延迟(ms)
|
||||
final DateTime lastCheck; // 最后检测时间
|
||||
final String? errorMessage; // 错误信息
|
||||
|
||||
const DeviceStatus({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.type,
|
||||
required this.ip,
|
||||
this.port = 80,
|
||||
this.state = DeviceState.unknown,
|
||||
this.latency,
|
||||
required this.lastCheck,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
/// 从JSON创建
|
||||
factory DeviceStatus.fromJson(Map<String, dynamic> json) {
|
||||
return DeviceStatus(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
type: DeviceType.values.firstWhere(
|
||||
(e) => e.name == json['type'],
|
||||
orElse: () => DeviceType.other,
|
||||
),
|
||||
ip: json['ip'] as String,
|
||||
port: json['port'] as int? ?? 80,
|
||||
state: DeviceState.values.firstWhere(
|
||||
(e) => e.name == json['state'],
|
||||
orElse: () => DeviceState.unknown,
|
||||
),
|
||||
latency: json['latency'] as int?,
|
||||
lastCheck: DateTime.parse(json['lastCheck'] as String),
|
||||
errorMessage: json['errorMessage'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
/// 转换为JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'type': type.name,
|
||||
'ip': ip,
|
||||
'port': port,
|
||||
'state': state.name,
|
||||
'latency': latency,
|
||||
'lastCheck': lastCheck.toIso8601String(),
|
||||
'errorMessage': errorMessage,
|
||||
};
|
||||
}
|
||||
|
||||
/// 复制并更新
|
||||
DeviceStatus copyWith({
|
||||
String? id,
|
||||
String? name,
|
||||
DeviceType? type,
|
||||
String? ip,
|
||||
int? port,
|
||||
DeviceState? state,
|
||||
int? latency,
|
||||
DateTime? lastCheck,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return DeviceStatus(
|
||||
id: id ?? this.id,
|
||||
name: name ?? this.name,
|
||||
type: type ?? this.type,
|
||||
ip: ip ?? this.ip,
|
||||
port: port ?? this.port,
|
||||
state: state ?? this.state,
|
||||
latency: latency ?? this.latency,
|
||||
lastCheck: lastCheck ?? this.lastCheck,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
/// 是否在线
|
||||
bool get isOnline => state == DeviceState.online;
|
||||
|
||||
/// 获取设备类型图标名称
|
||||
String get iconName {
|
||||
switch (type) {
|
||||
case DeviceType.gateway:
|
||||
return 'router';
|
||||
case DeviceType.bypass:
|
||||
return 'alt_route';
|
||||
case DeviceType.nas:
|
||||
return 'storage';
|
||||
case DeviceType.server:
|
||||
return 'dns';
|
||||
case DeviceType.camera:
|
||||
return 'videocam';
|
||||
case DeviceType.printer:
|
||||
return 'print';
|
||||
case DeviceType.other:
|
||||
return 'devices';
|
||||
}
|
||||
}
|
||||
}
|
||||
3
home_monitor/lib/models/models.dart
Normal file
3
home_monitor/lib/models/models.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
// 模型层导出文件
|
||||
export 'device_status.dart';
|
||||
export 'network_status.dart';
|
||||
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