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';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user