mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 23:34:24 +01:00
#76 new: switch net view
This commit is contained in:
39
lib/data/model/app/net_view.dart
Normal file
39
lib/data/model/app/net_view.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:toolbox/data/model/server/server_status.dart';
|
||||
|
||||
@HiveType(typeId: 5)
|
||||
enum NetViewType {
|
||||
@HiveField(0)
|
||||
count,
|
||||
@HiveField(1)
|
||||
speed,
|
||||
@HiveField(2)
|
||||
size;
|
||||
|
||||
NetViewData build(ServerStatus ss) {
|
||||
switch (this) {
|
||||
case NetViewType.count:
|
||||
return NetViewData(
|
||||
'Conn:\n${ss.tcp.maxConn}',
|
||||
'Fail:\n${ss.tcp.fail}',
|
||||
);
|
||||
case NetViewType.speed:
|
||||
return NetViewData(
|
||||
'In:\n${ss.netSpeed.speedIn(all: true)}',
|
||||
'Out:\n${ss.netSpeed.speedOut(all: true)}',
|
||||
);
|
||||
case NetViewType.size:
|
||||
return NetViewData(
|
||||
'In:\n${ss.netSpeed.sizeIn(all: true)}',
|
||||
'Out:\n${ss.netSpeed.sizeOut(all: true)}',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class NetViewData {
|
||||
final String up;
|
||||
final String down;
|
||||
|
||||
NetViewData(this.up, this.down);
|
||||
}
|
||||
@@ -28,34 +28,61 @@ class NetSpeed {
|
||||
|
||||
BigInt get timeDiff => _now[0].time - _old[0].time;
|
||||
|
||||
String speedIn({String? device}) {
|
||||
double _speedIn(int i) => (_now[i].bytesIn - _old[i].bytesIn) / timeDiff;
|
||||
double _speedOut(int i) => (_now[i].bytesOut - _old[i].bytesOut) / timeDiff;
|
||||
BigInt _sizeIn(int i) => _now[i].bytesIn;
|
||||
BigInt _sizeOut(int i) => _now[i].bytesOut;
|
||||
|
||||
String speedIn({String? device, bool all = false}) {
|
||||
if (_old[0].device == '' || _now[0].device == '') return '0kb/s';
|
||||
if (all) {
|
||||
var speed = 0.0;
|
||||
for (var i = 0; i < _now.length; i++) {
|
||||
speed += _speedIn(i);
|
||||
}
|
||||
return buildStandardOutput(speed);
|
||||
}
|
||||
final idx = deviceIdx(device);
|
||||
final speedInBytesPerSecond =
|
||||
(_now[idx].bytesIn - _old[idx].bytesIn) / timeDiff;
|
||||
return buildStandardOutput(speedInBytesPerSecond);
|
||||
return buildStandardOutput(_speedIn(idx));
|
||||
}
|
||||
|
||||
String totalIn({String? device}) {
|
||||
String sizeIn({String? device, bool all = false}) {
|
||||
if (_old[0].device == '' || _now[0].device == '') return '0kb';
|
||||
if (all) {
|
||||
var size = BigInt.from(0);
|
||||
for (var i = 0; i < _now.length; i++) {
|
||||
size += _sizeIn(i);
|
||||
}
|
||||
return size.convertBytes;
|
||||
}
|
||||
final idx = deviceIdx(device);
|
||||
final totalInBytes = _now[idx].bytesIn;
|
||||
return totalInBytes.toInt().convertBytes;
|
||||
return _sizeIn(idx).convertBytes;
|
||||
}
|
||||
|
||||
String speedOut({String? device}) {
|
||||
String speedOut({String? device, bool all = false}) {
|
||||
if (_old[0].device == '' || _now[0].device == '') return '0kb/s';
|
||||
if (all) {
|
||||
var speed = 0.0;
|
||||
for (var i = 0; i < _now.length; i++) {
|
||||
speed += _speedOut(i);
|
||||
}
|
||||
return buildStandardOutput(speed);
|
||||
}
|
||||
final idx = deviceIdx(device);
|
||||
final speedOutBytesPerSecond =
|
||||
(_now[idx].bytesOut - _old[idx].bytesOut) / timeDiff;
|
||||
return buildStandardOutput(speedOutBytesPerSecond);
|
||||
return buildStandardOutput(_speedOut(idx));
|
||||
}
|
||||
|
||||
String totalOut({String? device}) {
|
||||
String sizeOut({String? device, bool all = false}) {
|
||||
if (_old[0].device == '' || _now[0].device == '') return '0kb';
|
||||
if (all) {
|
||||
var size = BigInt.from(0);
|
||||
for (var i = 0; i < _now.length; i++) {
|
||||
size += _sizeOut(i);
|
||||
}
|
||||
return size.convertBytes;
|
||||
}
|
||||
final idx = deviceIdx(device);
|
||||
final totalOutBytes = _now[idx].bytesOut;
|
||||
return totalOutBytes.toInt().convertBytes;
|
||||
return _sizeOut(idx).convertBytes;
|
||||
}
|
||||
|
||||
int deviceIdx(String? device) {
|
||||
|
||||
@@ -57,4 +57,9 @@ class ServerPrivateInfo {
|
||||
bool shouldReconnect(ServerPrivateInfo old) {
|
||||
return id != old.id || pwd != old.pwd || pubKeyId != old.pubKeyId;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user