forked from kay/RouterOS
Neuer Tab: eine Tabelle pro physischem Ethernet/WLAN-Port mit den dort gefundenen Geräten (Name, IP, MAC, Fest/Dynamisch/Kein-DHCP), gebaut aus DHCP-Leases + ARP + Bridge-Host-Tabelle. Rechtsklick auf ein dynamisches Gerät -> "Feste IP zuweisen" (RouterOS' "Make Static", per /ip dhcp-server lease make-static), mit Bestätigungsdialog und Session-Backup vor dem ersten Schreibvorgang (geteilter Mechanismus mit dem Experte-Tab). Vier reale Bugs live gefunden und gefixt (siehe HANDOFF.md Bug 14-17): - "print terse" gibt das "dynamic"-Feld von /ip dhcp-server lease nie aus, in keinem Zustand -> Status kommt jetzt über RouterOS' find/get gegen die interne Eigenschaft, nicht aus gelesenen Feldern. - fetchMenuItems' .id-Positionsüberlagerung ordnete für dieses Menü die falsche .id der falschen Zeile zu -> Erkennung und make-static-Ziel laufen jetzt über die MAC-Adresse statt .id. - Ein SwiftUI-.confirmationDialog löschte sein eigenes Ziel-Objekt vor der Ausführung der bestätigten Aktion (Setter feuert bei jedem Knopfdruck, nicht nur Abbrechen) -> Dialog-Sichtbarkeit und Nutzlast entkoppelt, wie in BackupListView. - Die eigene Verifikations-Abfrage (get [find ...] feld als ein kombinierter Befehl) war selbst eine nie verifizierte Annahme und lieferte falsche Negative -> ersetzt durch :foreach aus zwei einzeln bestätigten Bausteinen (find, get <id> feld). RouterOSCommand bekommt einen neuen .action-Operationstyp für RouterOS-"Menü-spezifische Befehle" jenseits von add/set/remove (aktuell nur make-static). HANDOFF.md/CHATLOG.md mit allen vier Bugs, neuen Milestones M11/M12 und offenen Punkten aktualisiert. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CTgRxJTzaQwaRkngbaE1GJ
260 lines
10 KiB
Swift
260 lines
10 KiB
Swift
import SwiftUI
|
|
|
|
/// "Geräte" tab: LAN scanner — one table per physical Ethernet/WLAN port, each listing the
|
|
/// devices resolved onto it (name/IP/MAC/status), plus a right-click action to give a device a
|
|
/// permanent static IP the same way Winbox's "Make Static" does.
|
|
struct DevicesView: View {
|
|
@ObservedObject var connectionService: ConnectionService
|
|
@StateObject private var viewModel: DevicesViewModel
|
|
@State private var rawFieldsDevice: LanDevice?
|
|
/// Plain, independent Bool — deliberately NOT derived from `pendingStaticAssignment`.
|
|
/// SwiftUI's `.confirmationDialog` calls the `isPresented` binding's setter with `false` on
|
|
/// EVERY dismissal, including tapping "Zuweisen" itself — a computed binding that reacted to
|
|
/// that by nil-ing `pendingStaticAssignment` was found to clear it before
|
|
/// `confirmStaticAssignment()` could read it, so the confirmed action silently ran on `nil`
|
|
/// and did nothing. Matches this app's own established, working pattern for confirmation
|
|
/// dialogs elsewhere (BackupListView's factory-reset dialog): the dialog's own visibility and
|
|
/// its payload are two separate pieces of state.
|
|
@State private var showStaticConfirmation = false
|
|
|
|
init(connectionService: ConnectionService) {
|
|
self.connectionService = connectionService
|
|
_viewModel = StateObject(wrappedValue: DevicesViewModel(connectionService: connectionService))
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Group {
|
|
if connectionService.credentials == nil {
|
|
ContentUnavailableView(
|
|
"Nicht verbunden",
|
|
systemImage: "network.slash",
|
|
description: Text("Verbinde dich zuerst im Tab \"Verbinden\" mit deinem Router.")
|
|
)
|
|
} else if viewModel.isLoading && viewModel.devices.isEmpty {
|
|
ProgressView("Suche Geräte im Netz…")
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
} else if let error = viewModel.loadError, viewModel.devices.isEmpty {
|
|
ContentUnavailableView(
|
|
"Konnte Geräte nicht laden", systemImage: "exclamationmark.triangle",
|
|
description: Text(error)
|
|
)
|
|
} else if viewModel.portGroups.isEmpty {
|
|
ContentUnavailableView(
|
|
"Keine Ports gefunden", systemImage: "wifi.slash",
|
|
description: Text("Der Router meldet aktuell keine physischen Interfaces.")
|
|
)
|
|
} else {
|
|
List {
|
|
ForEach(viewModel.portGroups) { group in
|
|
Section {
|
|
if group.devices.isEmpty {
|
|
Text("Keine Geräte").font(.caption).foregroundStyle(.secondary)
|
|
} else {
|
|
DeviceColumnHeader()
|
|
ForEach(group.devices) { device in
|
|
DeviceRow(device: device)
|
|
.contextMenu {
|
|
deviceMenu(for: device)
|
|
}
|
|
}
|
|
}
|
|
} header: {
|
|
Text("\(group.title) (\(group.devices.count))")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Geräte")
|
|
.toolbar {
|
|
ToolbarItem {
|
|
Button {
|
|
Task { await viewModel.load() }
|
|
} label: {
|
|
if viewModel.isLoading {
|
|
ProgressView().controlSize(.small)
|
|
} else {
|
|
Label("Aktualisieren", systemImage: "arrow.clockwise")
|
|
}
|
|
}
|
|
.disabled(viewModel.isLoading)
|
|
}
|
|
}
|
|
.task {
|
|
if viewModel.devices.isEmpty {
|
|
await viewModel.load()
|
|
}
|
|
}
|
|
.confirmationDialog(
|
|
"Feste IP-Adresse zuweisen?",
|
|
isPresented: $showStaticConfirmation,
|
|
titleVisibility: .visible
|
|
) {
|
|
Button("Zuweisen") {
|
|
Task { await viewModel.confirmStaticAssignment() }
|
|
}
|
|
Button("Abbrechen", role: .cancel) { viewModel.cancelStaticAssignment() }
|
|
} message: {
|
|
if let device = viewModel.pendingStaticAssignment {
|
|
Text(
|
|
"\(device.ipAddress) wird dauerhaft an \(device.macAddress)"
|
|
+ (device.hostName.map { " (\($0))" } ?? "")
|
|
+ " gebunden — die Adresse ändert sich für dieses Gerät nicht mehr automatisch."
|
|
+ "\n\nBefehl: \(viewModel.pendingCommand?.cliLine ?? "")"
|
|
)
|
|
}
|
|
}
|
|
.alert(
|
|
"Fehler",
|
|
isPresented: Binding(
|
|
get: { viewModel.applyError != nil },
|
|
set: { if !$0 { viewModel.dismissApplyError() } }
|
|
),
|
|
presenting: viewModel.applyError
|
|
) { _ in
|
|
Button("OK") {}
|
|
} message: { error in
|
|
Text(error)
|
|
}
|
|
.sheet(item: $rawFieldsDevice) { device in
|
|
RawFieldsSheet(device: device) { rawFieldsDevice = nil }
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func deviceMenu(for device: LanDevice) -> some View {
|
|
if device.hasLease {
|
|
if device.isStatic {
|
|
Text("Bereits fest zugewiesen")
|
|
} else {
|
|
Button {
|
|
viewModel.pendingStaticAssignment = device
|
|
showStaticConfirmation = true
|
|
} label: {
|
|
Label("Feste IP zuweisen", systemImage: "pin.fill")
|
|
}
|
|
}
|
|
} else {
|
|
Text("Kein DHCP-Lease — feste Zuweisung hier nicht möglich")
|
|
}
|
|
Divider()
|
|
Button {
|
|
rawFieldsDevice = device
|
|
} label: {
|
|
Label("Rohdaten anzeigen", systemImage: "list.bullet.rectangle")
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Column widths shared between the header and each row so they line up like a real table.
|
|
private enum DeviceColumn {
|
|
static let icon: CGFloat = 16
|
|
static let name: CGFloat = 130
|
|
static let ip: CGFloat = 110
|
|
static let mac: CGFloat = 140
|
|
static let status: CGFloat = 80
|
|
}
|
|
|
|
private struct DeviceColumnHeader: View {
|
|
var body: some View {
|
|
HStack(spacing: 8) {
|
|
Spacer().frame(width: DeviceColumn.icon)
|
|
Text("Name").frame(width: DeviceColumn.name, alignment: .leading)
|
|
Text("IP-Adresse").frame(width: DeviceColumn.ip, alignment: .leading)
|
|
Text("MAC-Adresse").frame(width: DeviceColumn.mac, alignment: .leading)
|
|
Text("Status").frame(width: DeviceColumn.status, alignment: .leading)
|
|
Spacer()
|
|
}
|
|
.font(.caption2.bold())
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
}
|
|
|
|
private struct DeviceRow: View {
|
|
let device: LanDevice
|
|
|
|
/// Three distinct states, not two — a device with no matching DHCP lease at all is neither
|
|
/// "Fest" (a real static reservation) nor "Dynamisch" (a real active lease); conflating it
|
|
/// with "Fest" previously mislabeled every ARP-only device, DHCP clients included whenever
|
|
/// their lease didn't get matched.
|
|
private var statusIcon: String {
|
|
guard device.hasLease else { return "questionmark.circle" }
|
|
return device.isStatic ? "pin.fill" : "circle.fill"
|
|
}
|
|
|
|
private var statusColor: Color {
|
|
guard device.hasLease else { return .secondary }
|
|
return device.isStatic ? .orange : .green
|
|
}
|
|
|
|
private var statusText: String {
|
|
guard device.hasLease else { return "Kein DHCP" }
|
|
return device.isStatic ? "Fest" : "Dynamisch"
|
|
}
|
|
|
|
var body: some View {
|
|
HStack(spacing: 8) {
|
|
Image(systemName: statusIcon)
|
|
.foregroundStyle(statusColor)
|
|
.font(.system(size: 8))
|
|
.frame(width: DeviceColumn.icon)
|
|
.help(statusText)
|
|
Text(device.hostName?.isEmpty == false ? device.hostName! : "-")
|
|
.font(.system(size: 12))
|
|
.lineLimit(1)
|
|
.frame(width: DeviceColumn.name, alignment: .leading)
|
|
Text(device.ipAddress)
|
|
.font(.system(size: 12))
|
|
.frame(width: DeviceColumn.ip, alignment: .leading)
|
|
Text(device.macAddress)
|
|
.font(.system(size: 11, design: .monospaced))
|
|
.frame(width: DeviceColumn.mac, alignment: .leading)
|
|
Text(statusText)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.frame(width: DeviceColumn.status, alignment: .leading)
|
|
Spacer()
|
|
}
|
|
.padding(.vertical, 1)
|
|
}
|
|
}
|
|
|
|
private struct RawFieldsSheet: View {
|
|
let device: LanDevice
|
|
let onClose: () -> Void
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 12) {
|
|
Text("Rohdaten: \(device.macAddress)")
|
|
.font(.headline)
|
|
Text("Alle Felder, die RouterOS für diesen Eintrag zurückgegeben hat — hilfreich, falls Status/Port hier falsch aussieht.")
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
Divider()
|
|
ScrollView {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
ForEach(device.rawFields, id: \.key) { pair in
|
|
HStack(alignment: .top) {
|
|
Text(pair.key).font(.caption.monospaced()).foregroundStyle(.secondary)
|
|
Spacer()
|
|
Text(pair.value).font(.caption.monospaced()).multilineTextAlignment(.trailing)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
HStack {
|
|
Spacer()
|
|
Button("Schließen") { onClose() }
|
|
}
|
|
}
|
|
.padding()
|
|
.frame(minWidth: 380, minHeight: 300)
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
DevicesView(connectionService: ConnectionService())
|
|
}
|