mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 15:24:35 +01:00
使用ServerInfo储存信息
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:ssh2/ssh2.dart';
|
||||
import 'package:toolbox/core/extension/stringx.dart';
|
||||
import 'package:toolbox/core/provider_base.dart';
|
||||
import 'package:toolbox/data/model/disk_info.dart';
|
||||
import 'package:toolbox/data/model/server.dart';
|
||||
import 'package:toolbox/data/model/server_private_info.dart';
|
||||
import 'package:toolbox/data/model/server_status.dart';
|
||||
import 'package:toolbox/data/model/tcp_status.dart';
|
||||
@@ -11,12 +12,8 @@ import 'package:toolbox/data/store/server.dart';
|
||||
import 'package:toolbox/locator.dart';
|
||||
|
||||
class ServerProvider extends BusyProvider {
|
||||
List<ServerPrivateInfo> _servers = [];
|
||||
List<ServerStatus> _serversStatus = [];
|
||||
List<SSHClient> _clients = [];
|
||||
|
||||
List<ServerPrivateInfo> get servers => _servers;
|
||||
List<ServerStatus> get serversStatus => _serversStatus;
|
||||
List<ServerInfo> _servers = [];
|
||||
List<ServerInfo> get servers => _servers;
|
||||
|
||||
ServerStatus get emptyStatus => ServerStatus(
|
||||
cpuPercent: 0,
|
||||
@@ -36,41 +33,39 @@ class ServerProvider extends BusyProvider {
|
||||
|
||||
Future<void> loadLocalData() async {
|
||||
setBusyState(true);
|
||||
_servers = locator<ServerStore>().fetch();
|
||||
initStatusList();
|
||||
final infos = locator<ServerStore>().fetch();
|
||||
_servers = List.generate(infos.length, (index) => genInfo(infos[index]));
|
||||
setBusyState(false);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void initStatusList() {
|
||||
_clients = List.generate(
|
||||
_servers.length,
|
||||
(idx) => _fillClient(idx));
|
||||
_serversStatus = List.generate(_servers.length, (idx) => _fillStatus(idx));
|
||||
ServerInfo genInfo(ServerPrivateInfo spi) {
|
||||
return ServerInfo(
|
||||
spi,
|
||||
emptyStatus,
|
||||
SSHClient(
|
||||
host: spi.ip!,
|
||||
port: spi.port!,
|
||||
username: spi.user!,
|
||||
passwordOrKey: spi.authorization));
|
||||
}
|
||||
|
||||
SSHClient _fillClient(int idx) {
|
||||
if (idx < _clients.length) {
|
||||
return _clients[idx];
|
||||
}
|
||||
SSHClient genClient(ServerPrivateInfo spi) {
|
||||
return SSHClient(
|
||||
host: _servers[idx].ip!,
|
||||
port: _servers[idx].port!,
|
||||
username: _servers[idx].user!,
|
||||
passwordOrKey: _servers[idx].authorization,
|
||||
);
|
||||
}
|
||||
|
||||
ServerStatus _fillStatus(int idx) {
|
||||
if (idx < _serversStatus.length) {
|
||||
return _serversStatus[idx];
|
||||
}
|
||||
return emptyStatus;
|
||||
host: spi.ip!,
|
||||
port: spi.port!,
|
||||
username: spi.user!,
|
||||
passwordOrKey: spi.authorization);
|
||||
}
|
||||
|
||||
Future<void> refreshData() async {
|
||||
_serversStatus = await Future.wait(
|
||||
_servers.map((s) => _getData(s, _servers.indexOf(s))));
|
||||
final _serversStatus = await Future.wait(
|
||||
_servers.map((s) => _getData(s.info, _servers.indexOf(s))));
|
||||
int idx = 0;
|
||||
for (var item in _serversStatus) {
|
||||
_servers[idx].status = item;
|
||||
idx++;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -81,28 +76,27 @@ class ServerProvider extends BusyProvider {
|
||||
}
|
||||
|
||||
void addServer(ServerPrivateInfo info) {
|
||||
_servers.add(info);
|
||||
_servers.add(genInfo(info));
|
||||
locator<ServerStore>().put(info);
|
||||
initStatusList();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void delServer(ServerPrivateInfo info) {
|
||||
_servers.remove(info);
|
||||
_servers.removeWhere((e) => e.info == info);
|
||||
locator<ServerStore>().delete(info);
|
||||
initStatusList();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void updateServer(ServerPrivateInfo old, ServerPrivateInfo newInfo) {
|
||||
_servers[_servers.indexOf(old)] = newInfo;
|
||||
final idx = _servers.indexWhere((e) => e.info == old);
|
||||
_servers[idx].info = newInfo;
|
||||
_servers[idx].client = genClient(newInfo);
|
||||
locator<ServerStore>().update(old, newInfo);
|
||||
initStatusList();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<ServerStatus> _getData(ServerPrivateInfo info, int idx) async {
|
||||
final client = _clients[idx];
|
||||
final client = _servers[idx].client;
|
||||
if (!(await client.isConnected())) {
|
||||
await client.connect();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
class BuildData {
|
||||
static const String name = "ToolBox";
|
||||
static const int build = 10;
|
||||
static const String engine = "Flutter 2.5.0 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision 4cc385b4b8 (10 days ago) • 2021-09-07 23:01:49 -0700\nEngine • revision f0826da7ef\nTools • Dart 2.14.0\n";
|
||||
static const String engine =
|
||||
"Flutter 2.5.0 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision 4cc385b4b8 (10 days ago) • 2021-09-07 23:01:49 -0700\nEngine • revision f0826da7ef\nTools • Dart 2.14.0\n";
|
||||
static const String buildAt = "2021-09-18 17:25:30.528659";
|
||||
static const int modifications = 9;
|
||||
}
|
||||
|
||||
@@ -31,10 +31,6 @@ class _ServerPageState extends State<ServerPage>
|
||||
final usernameController = TextEditingController();
|
||||
final passwordController = TextEditingController();
|
||||
final keyController = TextEditingController();
|
||||
final ipFocusNode = FocusNode();
|
||||
final portFocusNode = FocusNode();
|
||||
final usernameFocusNode = FocusNode();
|
||||
final passwordFocusNode = FocusNode();
|
||||
|
||||
late ServerProvider serverProvider;
|
||||
final cachedServerStatus = <ServerStatus?>[];
|
||||
@@ -73,7 +69,7 @@ class _ServerPageState extends State<ServerPage>
|
||||
children: [
|
||||
const SizedBox(height: 13),
|
||||
...pro.servers.map((e) => _buildEachServerCard(
|
||||
pro.serversStatus[pro.servers.indexOf(e)], e))
|
||||
pro.servers[pro.servers.indexOf(e)].status, e.info))
|
||||
],
|
||||
));
|
||||
})),
|
||||
@@ -134,32 +130,21 @@ class _ServerPageState extends State<ServerPage>
|
||||
controller: nameController,
|
||||
keyboardType: TextInputType.text,
|
||||
decoration: _buildDecoration('名称'),
|
||||
onSubmitted: (_) =>
|
||||
FocusScope.of(context).requestFocus(ipFocusNode),
|
||||
),
|
||||
TextField(
|
||||
controller: ipController,
|
||||
focusNode: ipFocusNode,
|
||||
keyboardType: TextInputType.text,
|
||||
decoration: _buildDecoration('IP'),
|
||||
onSubmitted: (_) =>
|
||||
FocusScope.of(context).requestFocus(usernameFocusNode),
|
||||
),
|
||||
TextField(
|
||||
controller: portController,
|
||||
focusNode: portFocusNode,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: _buildDecoration('Port'),
|
||||
onSubmitted: (_) =>
|
||||
FocusScope.of(context).requestFocus(usernameFocusNode),
|
||||
),
|
||||
TextField(
|
||||
controller: usernameController,
|
||||
focusNode: usernameFocusNode,
|
||||
keyboardType: TextInputType.text,
|
||||
decoration: _buildDecoration('用户名'),
|
||||
onSubmitted: (_) =>
|
||||
FocusScope.of(context).requestFocus(passwordFocusNode),
|
||||
),
|
||||
TextField(
|
||||
controller: keyController,
|
||||
@@ -169,7 +154,6 @@ class _ServerPageState extends State<ServerPage>
|
||||
),
|
||||
TextField(
|
||||
controller: passwordController,
|
||||
focusNode: passwordFocusNode,
|
||||
obscureText: true,
|
||||
keyboardType: TextInputType.text,
|
||||
decoration: _buildDecoration('密码'),
|
||||
@@ -221,7 +205,8 @@ class _ServerPageState extends State<ServerPage>
|
||||
memData[1] = memData.last;
|
||||
memData.last = mem1;
|
||||
|
||||
final rootDisk = ss.disk!.firstWhere((element) => element!.mountLocation == '/');
|
||||
final rootDisk =
|
||||
ss.disk!.firstWhere((element) => element!.mountLocation == '/');
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -261,8 +246,10 @@ class _ServerPageState extends State<ServerPage>
|
||||
data: memData,
|
||||
)
|
||||
]),
|
||||
_buildIOData('Net', 'Conn:\n' + ss.tcp!.maxConn!.toString(), 'Fail:\n' + ss.tcp!.fail.toString()),
|
||||
_buildIOData('Disk', 'Total:\n' + rootDisk!.size!, 'Used:\n' + rootDisk.usedPercent.toString() + '%')
|
||||
_buildIOData('Net', 'Conn:\n' + ss.tcp!.maxConn!.toString(),
|
||||
'Fail:\n' + ss.tcp!.fail.toString()),
|
||||
_buildIOData('Disk', 'Total:\n' + rootDisk!.size!,
|
||||
'Used:\n' + rootDisk.usedPercent.toString() + '%')
|
||||
],
|
||||
),
|
||||
],
|
||||
@@ -270,7 +257,8 @@ class _ServerPageState extends State<ServerPage>
|
||||
}
|
||||
|
||||
Widget _buildIOData(String title, String up, String down) {
|
||||
final statusTextStyle = TextStyle(fontSize: 11, color: _theme.textTheme.bodyText1!.color!.withAlpha(177));
|
||||
final statusTextStyle = TextStyle(
|
||||
fontSize: 11, color: _theme.textTheme.bodyText1!.color!.withAlpha(177));
|
||||
return SizedBox(
|
||||
width: _media.size.width * 0.2,
|
||||
height: _media.size.height * 0.1,
|
||||
@@ -315,10 +303,12 @@ class _ServerPageState extends State<ServerPage>
|
||||
children: [
|
||||
DonutPieChart(series),
|
||||
Positioned.fill(
|
||||
child: Center(child: Text(
|
||||
child: Center(
|
||||
child: Text(
|
||||
'${percent.toStringAsFixed(1)}%\n',
|
||||
textAlign: TextAlign.center,
|
||||
),),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
child: Text(title, textAlign: TextAlign.center),
|
||||
|
||||
Reference in New Issue
Block a user