M24: LAN-Scanner Aktionen-Button, Traffic-Monitor+Sparkline, ARP-Fix

- Rechtsklick-Kontextmenü ersetzt durch sichtbaren "Aktionen"-Button
  je Geräte-Zeile (bisher nicht diskoverbar)
- Live-Traffic pro Port (↓/↑, wiederverwendet InterfaceTrafficMonitor)
  plus kleines Sparkline-Liniendiagramm der letzten 10 Sekunden
  (Swift Charts, neuer TrafficSample-Typ)
- Bug 34: ARP-Tabelle kann mehrere Zeilen für dieselbe MAC halten
  (reachable + stale/failed) — Auflösung bevorzugte bisher blind die
  zuletzt gesehene Zeile statt die erreichbare. Zwei Regressionstests.
- Nebenbefund: dedizierte SSH-Dienste (Backup/NetworkTools/Traffic/
  Update/FactoryReset) haben keinen eigenen Bestätigungspfad für einen
  neuen SSH-Host-Key, scheitern still solange REST verbindet — als
  offener Punkt dokumentiert.

Alles live bestätigt.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Kay
2026-09-16 11:46:01 +02:00
co-authored by Claude Sonnet 5
parent 567feed93a
commit 4cc30a2016
7 changed files with 310 additions and 6 deletions
@@ -835,6 +835,7 @@ enum L10n {
"Keine Geräte": "No Devices",
"Führe Netzwerk-Test aus…": "Running network test…",
"Neu scannen": "Rescan",
"Aktionen": "Actions",
"Feste Zuweisung entfernen": "Remove Static Assignment",
"Feste IP zuweisen": "Assign Static IP",
"Kein DHCP-Lease — feste Zuweisung hier nicht möglich": "No DHCP lease — static assignment not possible here",
@@ -10,3 +10,14 @@ struct InterfaceTraffic: Equatable {
var isActive: Bool { rxBitsPerSecond > 0 || txBitsPerSecond > 0 }
}
/// One timestamped throughput reading, kept in a short rolling per-port history so the
/// LAN-Scanner's port headers can show a small sparkline of the last few seconds, not just the
/// current instantaneous value (Nutzerwunsch: "ein kleines Liniendiagramm der letzten 10 Sekunden
/// ... pro port"). Timestamped (not just appended in order) so trimming to "last 10 seconds" stays
/// correct even if a poll tick is ever delayed or skipped, rather than assuming a fixed cadence.
struct TrafficSample: Identifiable {
let id = UUID()
let timestamp: Date
let totalBitsPerSecond: Int
}
@@ -1,3 +1,4 @@
import Charts
import SwiftUI
/// "LAN-Scanner" tab: one table per physical Ethernet/WLAN port, each listing the
@@ -56,14 +57,40 @@ struct DevicesView: View {
} else {
DeviceColumnHeader(appLanguage: appLanguage)
ForEach(group.devices) { device in
DeviceRow(device: device, appLanguage: appLanguage)
.contextMenu {
HStack(spacing: 4) {
DeviceRow(device: device, appLanguage: appLanguage)
Spacer()
// Per explicit request: right-click alone wasn't
// discoverable ("nicht eindeutig erkennbar oder
// intuitiv") every action that used to live only in
// `.contextMenu` now lives in this visible button
// instead, same `deviceMenu(for:)` content reused.
Menu {
deviceMenu(for: device)
} label: {
Label(L10n.t("Aktionen", appLanguage), systemImage: "ellipsis.circle")
.labelStyle(.iconOnly)
}
.menuStyle(.borderlessButton)
.fixedSize()
.help(L10n.t("Aktionen", appLanguage))
}
}
}
} header: {
Text("\(group.title) (\(group.devices.count))")
HStack(spacing: 8) {
Text("\(group.title) (\(group.devices.count))")
if let traffic = viewModel.portTraffic[group.id] {
Spacer()
TrafficSparkline(history: viewModel.portTrafficHistory[group.id] ?? [])
Label(
Self.formatTraffic(traffic),
systemImage: traffic.isActive ? "arrow.up.arrow.down.circle.fill" : "arrow.up.arrow.down.circle"
)
.font(.caption2)
.foregroundStyle(traffic.isActive ? .green : .secondary)
}
}
}
}
}
@@ -107,6 +134,18 @@ struct DevicesView: View {
await viewModel.load()
}
}
.onAppear {
if let credentials = connectionService.credentials {
viewModel.startTrafficPolling(credentials: credentials)
}
}
.onChange(of: connectionService.state) { _, newState in
if case .connected = newState, let credentials = connectionService.credentials {
viewModel.startTrafficPolling(credentials: credentials)
} else {
viewModel.stopTrafficPolling()
}
}
.withStaticAssignmentDialogs(
viewModel: viewModel,
appLanguage: appLanguage,
@@ -117,6 +156,23 @@ struct DevicesView: View {
}
}
/// Same unit-suffix style RouterOS itself reports live traffic in (`SSHTransport.
/// parseBitsPerSecond`'s doc comment: "50.7kbps", "34.0kbps") kept consistent instead of
/// inventing a different display format for the same underlying number.
private static func formatTraffic(_ traffic: InterfaceTraffic) -> String {
"\(formatBitsPerSecond(traffic.rxBitsPerSecond))\(formatBitsPerSecond(traffic.txBitsPerSecond))"
}
private static func formatBitsPerSecond(_ bitsPerSecond: Int) -> String {
let units: [(suffix: String, divisor: Double)] = [
("Gbps", 1_000_000_000), ("Mbps", 1_000_000), ("kbps", 1_000)
]
for unit in units where Double(bitsPerSecond) >= unit.divisor {
return String(format: "%.1f%@", Double(bitsPerSecond) / unit.divisor, unit.suffix)
}
return "\(bitsPerSecond)bps"
}
@ViewBuilder
private func deviceMenu(for device: LanDevice) -> some View {
if device.hasLease {
@@ -286,6 +342,37 @@ private extension View {
}
}
/// Small sparkline next to each port header last-10-seconds rolling window from
/// `DevicesViewModel.portTrafficHistory` (Nutzerwunsch: "ein kleines Liniendiagramm der letzten
/// 10 Sekunden ... pro Port"). No axes/labels by design a trend glance, not a readable chart;
/// the exact current numbers are already shown right next to it via the / text. Stays an empty
/// fixed-size placeholder (not collapsing/disappearing) with fewer than two points, so the row
/// layout doesn't jump around during the first couple of poll ticks after opening the tab.
private struct TrafficSparkline: View {
let history: [TrafficSample]
var body: some View {
Group {
if history.count >= 2 {
Chart(history) { sample in
LineMark(
x: .value("Zeit", sample.timestamp),
y: .value("Traffic", sample.totalBitsPerSecond)
)
.interpolationMethod(.linear)
.foregroundStyle(Color.accentColor)
}
.chartXAxis(.hidden)
.chartYAxis(.hidden)
.chartYScale(domain: 0...max(history.map(\.totalBitsPerSecond).max() ?? 1, 1))
} else {
Color.clear
}
}
.frame(width: 50, height: 16)
}
}
/// 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
@@ -32,6 +32,21 @@ final class DevicesViewModel: ObservableObject {
/// `PortScanner`, not through the router see its own doc comment for why.
@Published var portScanResult: PortScanResult?
/// Live per-port throughput (Nutzerwunsch: "ein Traffic-Monitor im LAN-Scanner zu den
/// einzelnen Geräten" per-device counters aren't something RouterOS exposes natively
/// without setting up queue trees per MAC, so this reuses the same per-*port* live monitor
/// already built for the Verbinden-Tab instead; keyed by `DevicePortGroup.id` (the physical
/// interface name), not by device). "unbekannt" (devices RouterOS couldn't resolve onto a
/// real port) is never polled it isn't a real interface.
@Published private(set) var portTraffic: [String: InterfaceTraffic] = [:]
/// Rolling last-10-seconds history per port, for the small sparkline next to each port
/// header (Nutzerwunsch: "ein kleines Liniendiagramm der letzten 10 Sekunden ... pro Port").
/// Trimmed by actual elapsed time each tick, not by a fixed sample count, so it stays a true
/// "last 10 seconds" window even if a poll tick is ever late.
@Published private(set) var portTrafficHistory: [String: [TrafficSample]] = [:]
private let trafficMonitor = InterfaceTrafficMonitor()
private var trafficPollingTask: Task<Void, Never>?
private let connectionService: ConnectionService
private let backupService: BackupService
private let networkToolsService: NetworkToolsService
@@ -46,6 +61,44 @@ final class DevicesViewModel: ObservableObject {
self.networkToolsService = networkToolsService
}
/// Same 3s cadence/reasoning as `ConnectViewModel.startTrafficPolling` re-reads
/// `portGroups` every tick so a newly-appearing port (e.g. a VLAN interface added via the
/// Setup wizard while this tab is open) gets picked up without restarting the poll.
func startTrafficPolling(credentials: RouterOSCredentials) {
stopTrafficPolling()
trafficPollingTask = Task {
while !Task.isCancelled {
let names = portGroups.map(\.id).filter { $0 != "unbekannt" }
if !names.isEmpty {
let traffic = await trafficMonitor.fetchTraffic(interfaceNames: names, for: credentials)
if !Task.isCancelled {
portTraffic = traffic
appendTrafficHistory(traffic, at: Date())
}
}
try? await Task.sleep(for: .seconds(3))
}
}
}
private func appendTrafficHistory(_ traffic: [String: InterfaceTraffic], at timestamp: Date) {
let cutoff = timestamp.addingTimeInterval(-10)
for (name, sample) in traffic {
var history = portTrafficHistory[name] ?? []
history.append(TrafficSample(timestamp: timestamp, totalBitsPerSecond: sample.rxBitsPerSecond + sample.txBitsPerSecond))
history.removeAll { $0.timestamp < cutoff }
portTrafficHistory[name] = history
}
}
func stopTrafficPolling() {
trafficPollingTask?.cancel()
trafficPollingTask = nil
portTraffic = [:]
portTrafficHistory = [:]
Task { await trafficMonitor.disconnect() }
}
func runPing(for device: LanDevice) {
runNetworkTool(title: "Ping: \(device.ipAddress)") { [networkToolsService] credentials in
try await networkToolsService.ping(address: device.ipAddress, for: credentials)
@@ -277,11 +330,36 @@ final class DevicesViewModel: ObservableObject {
// Fallback: ARP's "interface" field exact if that interface isn't a bridge, otherwise
// only "somewhere on this bridge" (resolved further by the bridge host table above when
// available).
// available). RouterOS can hold MULTIPLE ARP rows for the same MAC at once (live-
// confirmed: a device on a standalone port like "ether4" still had a second, stale
// "status=failed" row for an old address on "interface=bridge", left over from before it
// moved networks) blindly keeping the last row seen made the resolution depend on
// table order, not correctness, and could silently overwrite a working resolution with
// a dead one (this device fell into "Unbekannter Port" despite a perfectly good
// "reachable" ether4 row existing). Now scored: a "reachable" row always wins over any
// other status, tie-broken by preferring a non-bridge (more specific) interface, so a
// genuinely ambiguous case still prefers whatever's most precise.
var arpInterfaceByMAC: [String: String] = [:]
var arpStatusByMAC: [String: String] = [:]
for item in arpEntries {
guard let mac = item.fields["mac-address"]?.lowercased(), let iface = item.fields["interface"] else { continue }
arpInterfaceByMAC[mac] = iface
let status = item.fields["status"] ?? ""
guard let existingStatus = arpStatusByMAC[mac] else {
arpInterfaceByMAC[mac] = iface
arpStatusByMAC[mac] = status
continue
}
let isNewReachable = status == "reachable"
let isExistingReachable = existingStatus == "reachable"
if isNewReachable && !isExistingReachable {
arpInterfaceByMAC[mac] = iface
arpStatusByMAC[mac] = status
} else if isNewReachable == isExistingReachable,
let existingIface = arpInterfaceByMAC[mac],
bridgeNames.contains(existingIface), !bridgeNames.contains(iface) {
arpInterfaceByMAC[mac] = iface
arpStatusByMAC[mac] = status
}
}
// Last resort: the DHCP server's own configured interface network-level only, since a