M2 "Machine Management UX": full N-machine management from UI plus runtime resilience. All 3 screens now live (dashboard, detail, config). Core: - PollingEngine offline detection: consecutive-fail threshold emits a synthetic Disconnected snapshot (no-spam, resets on recovery). - MachineMonitor dynamic API: AddOrUpdateMachineAsync / RemoveMachineAsync start/restart/stop a machine's polling live (no app restart); idempotent. - MachineMonitor.AvailableProtocols exposes loaded protocol ids. App: - Machine detail screen: full current snapshot, reachable from dashboard row, live-refreshing, Back nav. - Config screen: add / edit / delete machines with validation; Save upserts + reloads monitor live; Delete two-state confirm + stops polling. - Dashboard: "Add machine" button, per-row Details, reload after mutation. Repo hygiene: - Untrack stray src/Junction.App/plugins/ build artifact (real output goes to bin/*/plugins via build target); add to .gitignore. PAUL: initialized .paul/ (PROJECT/ROADMAP/STATE + paul.json) as cross-session system-of-record. v0.1 shipped, v0.2 complete, v0.3 OPC UA next. Build 0 warn/0 err (net48 + net8.0). Tests: 125 unit + 2 docker integration. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
136 lines
4.7 KiB
C#
136 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Avalonia.Threading;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Junction.Core.Monitoring;
|
|
using Junction.Domain.Models;
|
|
using Junction.Domain.Persistence;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Junction.App.ViewModels
|
|
{
|
|
/// <summary>
|
|
/// Live machine dashboard. Loads configured machines from the repository, seeds each row from
|
|
/// the monitor's latest-snapshot cache, and refreshes rows as <see cref="IMachineMonitor.SnapshotUpdated"/>
|
|
/// fires. Snapshot events arrive on poll-loop threads and are marshalled onto the UI thread.
|
|
/// </summary>
|
|
public sealed partial class DashboardViewModel : ViewModelBase, IDisposable
|
|
{
|
|
private readonly IMachineRepository _repository;
|
|
private readonly IMachineMonitor _monitor;
|
|
private readonly ILogger<DashboardViewModel> _logger;
|
|
private readonly Dictionary<Guid, MachineRowViewModel> _rowsById = new Dictionary<Guid, MachineRowViewModel>();
|
|
private bool _subscribed;
|
|
private bool _disposed;
|
|
|
|
public string Title => "Junction — Machines";
|
|
|
|
/// <summary>
|
|
/// Shell reference, set by <see cref="MainWindowViewModel"/> after construction (breaks the
|
|
/// otherwise-circular DI graph). Used to open a machine's detail screen on row click.
|
|
/// </summary>
|
|
public MainWindowViewModel? Navigator { get; set; }
|
|
|
|
public ObservableCollection<MachineRowViewModel> Machines { get; } = new ObservableCollection<MachineRowViewModel>();
|
|
|
|
public DashboardViewModel(IMachineRepository repository, IMachineMonitor monitor, ILogger<DashboardViewModel> logger)
|
|
{
|
|
_repository = repository;
|
|
_monitor = monitor;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>Loads machines, seeds latest snapshots, and subscribes to live updates.</summary>
|
|
public async Task LoadAsync()
|
|
{
|
|
// Subscribe first so no update slips through between load and subscribe;
|
|
// rows are looked up by id, unknown ids are ignored.
|
|
if (!_subscribed)
|
|
{
|
|
_monitor.SnapshotUpdated += OnSnapshotUpdated;
|
|
_subscribed = true;
|
|
}
|
|
|
|
var result = await _repository.GetAllAsync(CancellationToken.None).ConfigureAwait(true);
|
|
if (!result.IsSuccess)
|
|
{
|
|
var detail = result.Errors.Count > 0 ? result.Errors[0].Message : "unknown error";
|
|
_logger.LogError("Dashboard load failed: {Detail}", detail);
|
|
return;
|
|
}
|
|
|
|
Machines.Clear();
|
|
_rowsById.Clear();
|
|
|
|
var latest = _monitor.LatestSnapshots;
|
|
foreach (var machine in result.Value)
|
|
{
|
|
var row = new MachineRowViewModel(machine, OpenDetail);
|
|
if (latest.TryGetValue(machine.Id, out var snapshot))
|
|
{
|
|
row.Apply(snapshot);
|
|
}
|
|
|
|
_rowsById[machine.Id] = row;
|
|
Machines.Add(row);
|
|
}
|
|
|
|
_logger.LogInformation("Dashboard loaded {Count} machine(s).", Machines.Count);
|
|
}
|
|
|
|
/// <summary>Header action: opens the add-machine config screen via the shell.</summary>
|
|
[RelayCommand]
|
|
private void AddMachine() => Navigator?.ShowConfig(null);
|
|
|
|
/// <summary>Row-click handler: routes to the machine-detail screen via the shell.</summary>
|
|
private void OpenDetail(MachineRowViewModel row)
|
|
{
|
|
if (row == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Navigator?.ShowDetail(row.MachineId, row.Name);
|
|
}
|
|
|
|
private void OnSnapshotUpdated(object? sender, MachineSnapshot snapshot)
|
|
{
|
|
if (snapshot == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Event fires on a poll-loop thread → marshal all observable mutations to the UI thread.
|
|
Dispatcher.UIThread.Post(() =>
|
|
{
|
|
if (_disposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_rowsById.TryGetValue(snapshot.MachineId, out var row))
|
|
{
|
|
row.Apply(snapshot);
|
|
}
|
|
});
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_disposed = true;
|
|
if (_subscribed)
|
|
{
|
|
_monitor.SnapshotUpdated -= OnSnapshotUpdated;
|
|
_subscribed = false;
|
|
}
|
|
}
|
|
}
|
|
}
|