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/extension/stringx.dart';
|
||||||
import 'package:toolbox/core/provider_base.dart';
|
import 'package:toolbox/core/provider_base.dart';
|
||||||
import 'package:toolbox/data/model/disk_info.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_private_info.dart';
|
||||||
import 'package:toolbox/data/model/server_status.dart';
|
import 'package:toolbox/data/model/server_status.dart';
|
||||||
import 'package:toolbox/data/model/tcp_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';
|
import 'package:toolbox/locator.dart';
|
||||||
|
|
||||||
class ServerProvider extends BusyProvider {
|
class ServerProvider extends BusyProvider {
|
||||||
List<ServerPrivateInfo> _servers = [];
|
List<ServerInfo> _servers = [];
|
||||||
List<ServerStatus> _serversStatus = [];
|
List<ServerInfo> get servers => _servers;
|
||||||
List<SSHClient> _clients = [];
|
|
||||||
|
|
||||||
List<ServerPrivateInfo> get servers => _servers;
|
|
||||||
List<ServerStatus> get serversStatus => _serversStatus;
|
|
||||||
|
|
||||||
ServerStatus get emptyStatus => ServerStatus(
|
ServerStatus get emptyStatus => ServerStatus(
|
||||||
cpuPercent: 0,
|
cpuPercent: 0,
|
||||||
@@ -36,41 +33,39 @@ class ServerProvider extends BusyProvider {
|
|||||||
|
|
||||||
Future<void> loadLocalData() async {
|
Future<void> loadLocalData() async {
|
||||||
setBusyState(true);
|
setBusyState(true);
|
||||||
_servers = locator<ServerStore>().fetch();
|
final infos = locator<ServerStore>().fetch();
|
||||||
initStatusList();
|
_servers = List.generate(infos.length, (index) => genInfo(infos[index]));
|
||||||
setBusyState(false);
|
setBusyState(false);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void initStatusList() {
|
ServerInfo genInfo(ServerPrivateInfo spi) {
|
||||||
_clients = List.generate(
|
return ServerInfo(
|
||||||
_servers.length,
|
spi,
|
||||||
(idx) => _fillClient(idx));
|
emptyStatus,
|
||||||
_serversStatus = List.generate(_servers.length, (idx) => _fillStatus(idx));
|
SSHClient(
|
||||||
|
host: spi.ip!,
|
||||||
|
port: spi.port!,
|
||||||
|
username: spi.user!,
|
||||||
|
passwordOrKey: spi.authorization));
|
||||||
}
|
}
|
||||||
|
|
||||||
SSHClient _fillClient(int idx) {
|
SSHClient genClient(ServerPrivateInfo spi) {
|
||||||
if (idx < _clients.length) {
|
|
||||||
return _clients[idx];
|
|
||||||
}
|
|
||||||
return SSHClient(
|
return SSHClient(
|
||||||
host: _servers[idx].ip!,
|
host: spi.ip!,
|
||||||
port: _servers[idx].port!,
|
port: spi.port!,
|
||||||
username: _servers[idx].user!,
|
username: spi.user!,
|
||||||
passwordOrKey: _servers[idx].authorization,
|
passwordOrKey: spi.authorization);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerStatus _fillStatus(int idx) {
|
|
||||||
if (idx < _serversStatus.length) {
|
|
||||||
return _serversStatus[idx];
|
|
||||||
}
|
|
||||||
return emptyStatus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> refreshData() async {
|
Future<void> refreshData() async {
|
||||||
_serversStatus = await Future.wait(
|
final _serversStatus = await Future.wait(
|
||||||
_servers.map((s) => _getData(s, _servers.indexOf(s))));
|
_servers.map((s) => _getData(s.info, _servers.indexOf(s))));
|
||||||
|
int idx = 0;
|
||||||
|
for (var item in _serversStatus) {
|
||||||
|
_servers[idx].status = item;
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,28 +76,27 @@ class ServerProvider extends BusyProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void addServer(ServerPrivateInfo info) {
|
void addServer(ServerPrivateInfo info) {
|
||||||
_servers.add(info);
|
_servers.add(genInfo(info));
|
||||||
locator<ServerStore>().put(info);
|
locator<ServerStore>().put(info);
|
||||||
initStatusList();
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void delServer(ServerPrivateInfo info) {
|
void delServer(ServerPrivateInfo info) {
|
||||||
_servers.remove(info);
|
_servers.removeWhere((e) => e.info == info);
|
||||||
locator<ServerStore>().delete(info);
|
locator<ServerStore>().delete(info);
|
||||||
initStatusList();
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateServer(ServerPrivateInfo old, ServerPrivateInfo newInfo) {
|
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);
|
locator<ServerStore>().update(old, newInfo);
|
||||||
initStatusList();
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<ServerStatus> _getData(ServerPrivateInfo info, int idx) async {
|
Future<ServerStatus> _getData(ServerPrivateInfo info, int idx) async {
|
||||||
final client = _clients[idx];
|
final client = _servers[idx].client;
|
||||||
if (!(await client.isConnected())) {
|
if (!(await client.isConnected())) {
|
||||||
await client.connect();
|
await client.connect();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,8 @@
|
|||||||
class BuildData {
|
class BuildData {
|
||||||
static const String name = "ToolBox";
|
static const String name = "ToolBox";
|
||||||
static const int build = 10;
|
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 String buildAt = "2021-09-18 17:25:30.528659";
|
||||||
static const int modifications = 9;
|
static const int modifications = 9;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,10 +31,6 @@ class _ServerPageState extends State<ServerPage>
|
|||||||
final usernameController = TextEditingController();
|
final usernameController = TextEditingController();
|
||||||
final passwordController = TextEditingController();
|
final passwordController = TextEditingController();
|
||||||
final keyController = TextEditingController();
|
final keyController = TextEditingController();
|
||||||
final ipFocusNode = FocusNode();
|
|
||||||
final portFocusNode = FocusNode();
|
|
||||||
final usernameFocusNode = FocusNode();
|
|
||||||
final passwordFocusNode = FocusNode();
|
|
||||||
|
|
||||||
late ServerProvider serverProvider;
|
late ServerProvider serverProvider;
|
||||||
final cachedServerStatus = <ServerStatus?>[];
|
final cachedServerStatus = <ServerStatus?>[];
|
||||||
@@ -73,7 +69,7 @@ class _ServerPageState extends State<ServerPage>
|
|||||||
children: [
|
children: [
|
||||||
const SizedBox(height: 13),
|
const SizedBox(height: 13),
|
||||||
...pro.servers.map((e) => _buildEachServerCard(
|
...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,
|
controller: nameController,
|
||||||
keyboardType: TextInputType.text,
|
keyboardType: TextInputType.text,
|
||||||
decoration: _buildDecoration('名称'),
|
decoration: _buildDecoration('名称'),
|
||||||
onSubmitted: (_) =>
|
|
||||||
FocusScope.of(context).requestFocus(ipFocusNode),
|
|
||||||
),
|
),
|
||||||
TextField(
|
TextField(
|
||||||
controller: ipController,
|
controller: ipController,
|
||||||
focusNode: ipFocusNode,
|
|
||||||
keyboardType: TextInputType.text,
|
keyboardType: TextInputType.text,
|
||||||
decoration: _buildDecoration('IP'),
|
decoration: _buildDecoration('IP'),
|
||||||
onSubmitted: (_) =>
|
|
||||||
FocusScope.of(context).requestFocus(usernameFocusNode),
|
|
||||||
),
|
),
|
||||||
TextField(
|
TextField(
|
||||||
controller: portController,
|
controller: portController,
|
||||||
focusNode: portFocusNode,
|
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.number,
|
||||||
decoration: _buildDecoration('Port'),
|
decoration: _buildDecoration('Port'),
|
||||||
onSubmitted: (_) =>
|
|
||||||
FocusScope.of(context).requestFocus(usernameFocusNode),
|
|
||||||
),
|
),
|
||||||
TextField(
|
TextField(
|
||||||
controller: usernameController,
|
controller: usernameController,
|
||||||
focusNode: usernameFocusNode,
|
|
||||||
keyboardType: TextInputType.text,
|
keyboardType: TextInputType.text,
|
||||||
decoration: _buildDecoration('用户名'),
|
decoration: _buildDecoration('用户名'),
|
||||||
onSubmitted: (_) =>
|
|
||||||
FocusScope.of(context).requestFocus(passwordFocusNode),
|
|
||||||
),
|
),
|
||||||
TextField(
|
TextField(
|
||||||
controller: keyController,
|
controller: keyController,
|
||||||
@@ -169,7 +154,6 @@ class _ServerPageState extends State<ServerPage>
|
|||||||
),
|
),
|
||||||
TextField(
|
TextField(
|
||||||
controller: passwordController,
|
controller: passwordController,
|
||||||
focusNode: passwordFocusNode,
|
|
||||||
obscureText: true,
|
obscureText: true,
|
||||||
keyboardType: TextInputType.text,
|
keyboardType: TextInputType.text,
|
||||||
decoration: _buildDecoration('密码'),
|
decoration: _buildDecoration('密码'),
|
||||||
@@ -216,12 +200,13 @@ class _ServerPageState extends State<ServerPage>
|
|||||||
for (var e in ss.memList!) {
|
for (var e in ss.memList!) {
|
||||||
memData.add(IndexPercent(ss.memList!.indexOf(e), e!.toInt()));
|
memData.add(IndexPercent(ss.memList!.indexOf(e), e!.toInt()));
|
||||||
}
|
}
|
||||||
|
|
||||||
final mem1 = memData[1];
|
final mem1 = memData[1];
|
||||||
memData[1] = memData.last;
|
memData[1] = memData.last;
|
||||||
memData.last = mem1;
|
memData.last = mem1;
|
||||||
|
|
||||||
final rootDisk = ss.disk!.firstWhere((element) => element!.mountLocation == '/');
|
final rootDisk =
|
||||||
|
ss.disk!.firstWhere((element) => element!.mountLocation == '/');
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
@@ -261,8 +246,10 @@ class _ServerPageState extends State<ServerPage>
|
|||||||
data: memData,
|
data: memData,
|
||||||
)
|
)
|
||||||
]),
|
]),
|
||||||
_buildIOData('Net', 'Conn:\n' + ss.tcp!.maxConn!.toString(), 'Fail:\n' + ss.tcp!.fail.toString()),
|
_buildIOData('Net', 'Conn:\n' + ss.tcp!.maxConn!.toString(),
|
||||||
_buildIOData('Disk', 'Total:\n' + rootDisk!.size!, 'Used:\n' + rootDisk.usedPercent.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) {
|
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(
|
return SizedBox(
|
||||||
width: _media.size.width * 0.2,
|
width: _media.size.width * 0.2,
|
||||||
height: _media.size.height * 0.1,
|
height: _media.size.height * 0.1,
|
||||||
@@ -280,20 +268,20 @@ class _ServerPageState extends State<ServerPage>
|
|||||||
child: Center(
|
child: Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
up,
|
up,
|
||||||
style: statusTextStyle,
|
style: statusTextStyle,
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 3),
|
const SizedBox(height: 3),
|
||||||
Text(
|
Text(
|
||||||
down + '\n',
|
down + '\n',
|
||||||
style: statusTextStyle,
|
style: statusTextStyle,
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
@@ -315,10 +303,12 @@ class _ServerPageState extends State<ServerPage>
|
|||||||
children: [
|
children: [
|
||||||
DonutPieChart(series),
|
DonutPieChart(series),
|
||||||
Positioned.fill(
|
Positioned.fill(
|
||||||
child: Center(child: Text(
|
child: Center(
|
||||||
'${percent.toStringAsFixed(1)}%\n',
|
child: Text(
|
||||||
textAlign: TextAlign.center,
|
'${percent.toStringAsFixed(1)}%\n',
|
||||||
),),
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
child: Text(title, textAlign: TextAlign.center),
|
child: Text(title, textAlign: TextAlign.center),
|
||||||
|
|||||||
Reference in New Issue
Block a user