junction/src/Junction.App/ViewModels/MachineRowViewModel.cs
dtrentin 507753f82e init: scaffold Junction multi-protocol machine monitor (M1 walking skeleton)
Greenfield C# app reading industrial machine data over pluggable protocols.
M1 walking skeleton: MTConnect plugin polls a machine, Avalonia dashboard
shows live last datum. Build 0 warn/0 err on net48 + net8.0 (Linux),
115 unit tests + 2 docker-gated integration tests.

Projects:
- Junction.Domain (netstandard2.0): Result<T>, models, IProtocolDriver,
  IMachineRepository, plugin manifest. Zero package deps.
- Junction.Core (netstandard2.0): PollingEngine, PluginLoader
  (Assembly.LoadFrom), MachineMonitor, DI extensions.
- Junction.Persistence (netstandard2.0): SqliteMachineRepository (Dapper),
  schema, connection factory. Provider-swap seam to SQL Server.
- Junction.Protocols.MTConnect (netstandard2.0): HTTP driver + namespace-
  version-agnostic parser (MTConnect 1.7 + 2.0). Runtime plugin.
- Junction.App (net48;net8.0): Avalonia MVVM, live dashboard, NLog, DI root.
- Junction.Tests (net8.0): xUnit + Moq, 117 tests.
- mock/: docker-compose MTConnect agent (ladder99/agent).

Target net48 for Windows 7/8 fleet compatibility. Avalonia pinned 11.3.x.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-21 23:32:56 +02:00

83 lines
2.8 KiB
C#

using System;
using CommunityToolkit.Mvvm.ComponentModel;
using Junction.Domain.Models;
namespace Junction.App.ViewModels
{
/// <summary>
/// One dashboard row = one configured machine + its latest snapshot projection.
/// Mutated only on the UI thread (see <see cref="DashboardViewModel"/> marshalling).
/// </summary>
public sealed partial class MachineRowViewModel : ViewModelBase
{
public Guid MachineId { get; }
[ObservableProperty] private string _name;
[ObservableProperty] private string _protocolId;
[ObservableProperty] private ConnectionState _connectionState;
[ObservableProperty] private string _lastDatum;
[ObservableProperty] private string _lastUpdated;
public MachineRowViewModel(Machine machine)
{
MachineId = machine.Id;
_name = machine.Name;
_protocolId = machine.ProtocolId;
_connectionState = ConnectionState.Unknown;
_lastDatum = "—";
_lastUpdated = "—";
}
/// <summary>Projects a snapshot onto this row. Call on the UI thread.</summary>
public void Apply(MachineSnapshot snapshot)
{
if (snapshot == null)
{
return;
}
ConnectionState = snapshot.ConnectionState;
LastUpdated = snapshot.CapturedAt.LocalDateTime.ToString("HH:mm:ss");
LastDatum = Representative(snapshot);
}
/// <summary>
/// Picks a human-meaningful datum to show: prefer an availability/execution item,
/// else the first item's value, else an em-dash placeholder.
/// </summary>
private static string Representative(MachineSnapshot snapshot)
{
if (snapshot.Items.Count == 0)
{
return "—";
}
DataItem? preferred = null;
for (int i = 0; i < snapshot.Items.Count; i++)
{
var item = snapshot.Items[i];
if (Matches(item.Id) || Matches(item.Name))
{
preferred = item;
break;
}
}
var chosen = preferred ?? snapshot.Items[0];
var value = string.IsNullOrWhiteSpace(chosen.Value) ? "—" : chosen.Value;
var label = string.IsNullOrWhiteSpace(chosen.Name) ? chosen.Id : chosen.Name;
return string.IsNullOrWhiteSpace(label) ? value : label + " = " + value;
}
private static bool Matches(string s)
{
if (string.IsNullOrEmpty(s))
{
return false;
}
return s.IndexOf("avail", StringComparison.OrdinalIgnoreCase) >= 0
|| s.IndexOf("execution", StringComparison.OrdinalIgnoreCase) >= 0;
}
}
}