new: custom cmds (#313)

This commit is contained in:
lollipopkit
2024-03-24 23:03:39 -06:00
parent 96866565c4
commit b6a797c993
32 changed files with 260 additions and 146 deletions

View File

@@ -4,40 +4,49 @@ part 'custom.g.dart';
@HiveType(typeId: 7)
final class ServerCustom {
@HiveField(0)
final String? temperature;
// @HiveField(0)
// final String? temperature;
@HiveField(1)
final String? pveAddr;
@HiveField(2)
final bool? pveIgnoreCert;
@HiveField(2, defaultValue: false)
final bool pveIgnoreCert;
/// {"title": "cmd"}
@HiveField(3)
final Map<String, String>? cmds;
const ServerCustom({
this.temperature,
//this.temperature,
this.pveAddr,
this.pveIgnoreCert,
this.pveIgnoreCert = false,
this.cmds,
});
static ServerCustom fromJson(Map<String, dynamic> json) {
final temperature = json["temperature"] as String?;
//final temperature = json["temperature"] as String?;
final pveAddr = json["pveAddr"] as String?;
final pveIgnoreCert = json["pveIgnoreCert"] as bool?;
final pveIgnoreCert = json["pveIgnoreCert"] as bool;
final cmds = json["cmds"] as Map<String, dynamic>?;
return ServerCustom(
temperature: temperature,
//temperature: temperature,
pveAddr: pveAddr,
pveIgnoreCert: pveIgnoreCert,
cmds: cmds?.cast<String, String>(),
);
}
Map<String, dynamic> toJson() {
final json = <String, dynamic>{};
if (temperature != null) {
json["temperature"] = temperature;
}
// if (temperature != null) {
// json["temperature"] = temperature;
// }
if (pveAddr != null) {
json["pveAddr"] = pveAddr;
}
if (pveIgnoreCert != null) {
json["pveIgnoreCert"] = pveIgnoreCert;
json["pveIgnoreCert"] = pveIgnoreCert;
if (cmds != null) {
json["cmds"] = cmds;
}
return json;
}

View File

@@ -17,9 +17,9 @@ class ServerCustomAdapter extends TypeAdapter<ServerCustom> {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return ServerCustom(
temperature: fields[0] as String?,
pveAddr: fields[1] as String?,
pveIgnoreCert: fields[2] as bool?,
pveIgnoreCert: fields[2] == null ? false : fields[2] as bool,
cmds: (fields[3] as Map?)?.cast<String, String>(),
);
}
@@ -27,12 +27,12 @@ class ServerCustomAdapter extends TypeAdapter<ServerCustom> {
void write(BinaryWriter writer, ServerCustom obj) {
writer
..writeByte(3)
..writeByte(0)
..write(obj.temperature)
..writeByte(1)
..write(obj.pveAddr)
..writeByte(2)
..write(obj.pveIgnoreCert);
..write(obj.pveIgnoreCert)
..writeByte(3)
..write(obj.cmds);
}
@override

View File

@@ -414,11 +414,11 @@ final class PveRes {
static Future<PveRes> parse((List list, PveRes? old) val) async {
final (list, old) = val;
final items = list.map((e) => PveResIface.fromJson(e)).toList();
final Order<PveQemu> qemus = [];
final Order<PveLxc> lxcs = [];
final Order<PveNode> nodes = [];
final Order<PveStorage> storages = [];
final Order<PveSdn> sdns = [];
final List<PveQemu> qemus = [];
final List<PveLxc> lxcs = [];
final List<PveNode> nodes = [];
final List<PveStorage> storages = [];
final List<PveSdn> sdns = [];
for (final item in items) {
switch (item.type) {
case PveResType.lxc:

View File

@@ -1,4 +1,5 @@
import 'package:dartssh2/dartssh2.dart';
import 'package:toolbox/core/extension/context/locale.dart';
import 'package:toolbox/data/model/app/shell_func.dart';
import 'package:toolbox/data/model/server/battery.dart';
import 'package:toolbox/data/model/server/conn.dart';
@@ -14,6 +15,8 @@ import 'package:toolbox/data/model/server/temp.dart';
import '../app/tag_pickable.dart';
part 'server.ext.dart';
class Server implements TagPickable {
ServerPrivateInfo spi;
ServerStatus status;
@@ -58,6 +61,7 @@ class ServerStatus {
final Map<StatusCmdType, String> more = {};
final List<SensorItem> sensors = [];
DiskUsage? diskUsage;
final Map<String, String> customCmds = {};
ServerStatus({
required this.cpu,

View File

@@ -0,0 +1,39 @@
part of 'server.dart';
extension ServerX on Server {
String get topRightStr {
switch (state) {
case ServerState.disconnected:
return l10n.disconnected;
case ServerState.finished:
final cmdTemp = status.customCmds['temperature'];
final temp = status.temps.first;
final sensorTemp = SensorItem.findPreferTempVal(status.sensors);
final temperatureVal = () {
if (temp != null) {
return temp;
}
if (sensorTemp != null) {
return sensorTemp;
}
return null;
}();
final tempVal = temperatureVal != null
? '${temperatureVal.toStringAsFixed(1)}°C'
: null;
final upTime = status.more[StatusCmdType.uptime];
final items = [tempVal ?? cmdTemp, upTime];
final str = items.where((e) => e != null && e.isNotEmpty).join(' | ');
if (str.isEmpty) return l10n.noResult;
return str;
case ServerState.loading:
return l10n.serverTabLoading;
case ServerState.connected:
return l10n.connected;
case ServerState.connecting:
return l10n.serverTabConnecting;
case ServerState.failed:
return status.err ?? l10n.serverTabFailed;
}
}
}

View File

@@ -122,7 +122,8 @@ class ServerPrivateInfo {
pwd != old.pwd ||
keyId != old.keyId ||
alterUrl != old.alterUrl ||
jumpId != old.jumpId;
jumpId != old.jumpId ||
custom?.cmds != old.custom?.cmds;
}
_IpPort fromStringUrl() {

View File

@@ -16,11 +16,13 @@ class ServerStatusUpdateReq {
final ServerStatus ss;
final List<String> segments;
final SystemType system;
final Map<String, String> customCmds;
const ServerStatusUpdateReq({
required this.system,
required this.ss,
required this.segments,
required this.customCmds,
});
}
@@ -155,6 +157,16 @@ Future<ServerStatus> _getLinuxStatus(ServerStatusUpdateReq req) async {
Loggers.parse.warning(e, s);
}
try {
for (int idx = 0; idx < req.customCmds.length; idx++) {
final key = req.customCmds.keys.elementAt(idx);
final value = req.segments[idx + req.system.segmentsLen];
req.ss.customCmds[key] = value;
}
} catch (e, s) {
Loggers.parse.warning(e, s);
}
return req.ss;
}