- 工作台成员扫码登入:workstation_login_records 记录谁在什么时候登入/下线了哪台工作台, 超时(团购 1h / 散单 30min)由 schedule/workstation_idle_offline 自动下线; 运单查询的 packers 取当时的登入成员 - 面单摄像头识别:workstation_cameras.is_label_camera + service/label_scan 下发本机该跑的识别配置, 设备端(桌面端 LabelScan 工作线程)按配置做面单识别并上报 - 虚拟通道 CAM-<工作台id>:工作台没接扫码枪时,面单摄像头那一路顶替扫码枪上报,records 规则按 ws_type 区分 - 桌面端:新增 LabelScanMonitor/LabelScanWorker/ScanDeviceKind 与面单预览页,扫描通道支持改名,静默窗口 5s Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
253 lines
10 KiB
C#
253 lines
10 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.Linq;
|
||
using System.Runtime.CompilerServices;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Threading;
|
||
using YuncangTrace.LocalService.Services;
|
||
using YuncangTrace.LocalService.Services.Scan;
|
||
|
||
namespace YuncangTrace.LocalService.Views;
|
||
|
||
/// <summary>
|
||
/// 「面单摄像头」页签:每路识别任务的状态 + **最近识别到的条码**。
|
||
///
|
||
/// 数据来源:
|
||
/// - 服务模式(本机跑的就是后台服务):读后台服务写的 <c>agent-status.json</c> 的 <c>LabelScan</c>
|
||
/// —— 真正拉流识别的是服务进程,界面只展示;
|
||
/// - 普通模式:直接读本进程 <see cref="ScanService.LabelScan"/> 的运行时状态。
|
||
///
|
||
/// 识别本身在 <see cref="LabelScanWorker"/>:ffmpeg 拉 RTSP → 灰度帧 → ZXing → 上报。
|
||
/// </summary>
|
||
public partial class LabelCameraView : UserControl
|
||
{
|
||
private static readonly TimeSpan AgentPollInterval = TimeSpan.FromMilliseconds(300);
|
||
|
||
private readonly ScanService _scan;
|
||
private readonly ObservableCollection<LabelRow> _rows = new();
|
||
private readonly DispatcherTimer _agentTimer = new() { Interval = AgentPollInterval };
|
||
private DateTime _agentStatusAt;
|
||
private bool _refreshing;
|
||
|
||
private static bool IsAgentMode => AgentPaths.MachineMode && !AgentPaths.IsServiceProcess;
|
||
|
||
public LabelCameraView(ScanService scan)
|
||
{
|
||
InitializeComponent();
|
||
|
||
_scan = scan;
|
||
CameraList.ItemsSource = _rows;
|
||
|
||
// 事件一直挂着(切页签/隐藏到托盘都会触发 Unloaded,退订就再也不刷新了)
|
||
_scan.LabelScan.Changed += OnLabelScanChanged;
|
||
|
||
Loaded += (_, _) => Refresh();
|
||
|
||
if (IsAgentMode)
|
||
{
|
||
_agentTimer.Tick += (_, _) => ApplyAgentStatus();
|
||
_agentTimer.Start();
|
||
}
|
||
|
||
Refresh();
|
||
}
|
||
|
||
/// <summary>列表行(界面显示用;按 <see cref="Key"/> 原地更新,避免每次命中都清空重画)</summary>
|
||
public sealed class LabelRow : INotifyPropertyChanged
|
||
{
|
||
private string _channelText = string.Empty;
|
||
private string _stateText = string.Empty;
|
||
private string _codeText = "—";
|
||
private string _firstSeenText = "—";
|
||
private string _lastHitText = "—";
|
||
private string _streamText = "—";
|
||
private string _decodeText = "—";
|
||
private string _hitsText = "—";
|
||
private string _errorText = string.Empty;
|
||
|
||
public LabelRow(string key)
|
||
{
|
||
Key = key;
|
||
}
|
||
|
||
/// <summary>工作台|摄像头|通道:配置变了就是新的一行</summary>
|
||
public string Key { get; }
|
||
|
||
public string WorkstationName { get; init; } = string.Empty;
|
||
public string CameraName { get; init; } = string.Empty;
|
||
public string Summary { get; set; } = string.Empty;
|
||
|
||
public string ChannelText { get => _channelText; set => Set(ref _channelText, value); }
|
||
public string StateText { get => _stateText; set => Set(ref _stateText, value); }
|
||
|
||
/// <summary>最近识别到的条码(条码本身,不带时间)</summary>
|
||
public string CodeText { get => _codeText; set => Set(ref _codeText, value); }
|
||
|
||
/// <summary>首次识别时间:也就是这条记录真正用的时间,面单不拿走就不变</summary>
|
||
public string FirstSeenText { get => _firstSeenText; set => Set(ref _firstSeenText, value); }
|
||
|
||
/// <summary>最近一次命中时间:每秒都在变,只用来看摄像头有没有在扫</summary>
|
||
public string LastHitText { get => _lastHitText; set => Set(ref _lastHitText, value); }
|
||
|
||
/// <summary>当前码流(主/子 + 分辨率)</summary>
|
||
public string StreamText { get => _streamText; set => Set(ref _streamText, value); }
|
||
|
||
/// <summary>最近一帧解码耗时</summary>
|
||
public string DecodeText { get => _decodeText; set => Set(ref _decodeText, value); }
|
||
|
||
public string HitsText { get => _hitsText; set => Set(ref _hitsText, value); }
|
||
public string ErrorText { get => _errorText; set => Set(ref _errorText, value); }
|
||
|
||
public event PropertyChangedEventHandler? PropertyChanged;
|
||
|
||
private void Set(ref string field, string value, [CallerMemberName] string? name = null)
|
||
{
|
||
if (string.Equals(field, value, StringComparison.Ordinal)) return;
|
||
field = value;
|
||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
||
}
|
||
}
|
||
|
||
/// <summary>一行要显示的内容(与行对象解耦,方便按 key 比对增量更新)</summary>
|
||
private sealed record RowData(
|
||
string Key,
|
||
string WorkstationName,
|
||
string CameraName,
|
||
string ChannelText,
|
||
string StateText,
|
||
string CodeText,
|
||
string FirstSeenText,
|
||
string LastHitText,
|
||
string StreamText,
|
||
string DecodeText,
|
||
string HitsText,
|
||
string ErrorText,
|
||
string Summary);
|
||
|
||
/// <summary>按 key 增量更新列表(新增/删除/原地改),不整体重画</summary>
|
||
private void Render(IReadOnlyList<RowData> data)
|
||
{
|
||
for (var index = _rows.Count - 1; index >= 0; index--)
|
||
{
|
||
if (data.Any(item => item.Key == _rows[index].Key)) continue;
|
||
_rows.RemoveAt(index);
|
||
}
|
||
|
||
for (var index = 0; index < data.Count; index++)
|
||
{
|
||
var item = data[index];
|
||
var row = _rows.FirstOrDefault(existing => existing.Key == item.Key);
|
||
if (row is null)
|
||
{
|
||
row = new LabelRow(item.Key) { WorkstationName = item.WorkstationName, CameraName = item.CameraName };
|
||
_rows.Insert(Math.Min(index, _rows.Count), row);
|
||
}
|
||
|
||
row.ChannelText = item.ChannelText;
|
||
row.StateText = item.StateText;
|
||
row.CodeText = item.CodeText;
|
||
row.FirstSeenText = item.FirstSeenText;
|
||
row.LastHitText = item.LastHitText;
|
||
row.StreamText = item.StreamText;
|
||
row.DecodeText = item.DecodeText;
|
||
row.HitsText = item.HitsText;
|
||
row.ErrorText = item.ErrorText;
|
||
row.Summary = item.Summary;
|
||
}
|
||
}
|
||
|
||
private void OnLabelScanChanged()
|
||
{
|
||
if (Dispatcher.CheckAccess()) Refresh();
|
||
else Dispatcher.Invoke(Refresh);
|
||
}
|
||
|
||
/// <summary>服务模式:读后台服务写的运行状态(有变化才重画)</summary>
|
||
private void ApplyAgentStatus()
|
||
{
|
||
var status = AgentFiles.LoadStatus();
|
||
if (status is null || status.UpdatedAt == _agentStatusAt) return;
|
||
|
||
_agentStatusAt = status.UpdatedAt;
|
||
Refresh();
|
||
}
|
||
|
||
/// <summary>重新渲染(保持选中项不需要:这里只展示)</summary>
|
||
public void Refresh()
|
||
{
|
||
var label = IsAgentMode ? AgentFiles.LoadStatus()?.LabelScan : null;
|
||
|
||
if (IsAgentMode && label is not null)
|
||
{
|
||
StatusText.Text = string.IsNullOrWhiteSpace(label.StatusText) ? "还没拉取" : label.StatusText;
|
||
SetWarnings(label.Warnings);
|
||
RefreshButton.Content = "由后台服务拉取";
|
||
RefreshButton.IsEnabled = false;
|
||
|
||
Render(label.Items.Select(item => new RowData(
|
||
$"{item.WorkstationId}|{item.CameraId}|{item.ChannelId}",
|
||
string.IsNullOrWhiteSpace(item.WorkstationName) ? "—" : item.WorkstationName,
|
||
item.CameraName,
|
||
string.IsNullOrWhiteSpace(item.ChannelName) ? item.ChannelId : $"{item.ChannelId}「{item.ChannelName}」",
|
||
item.StateText,
|
||
string.IsNullOrEmpty(item.LastCode) ? "—" : item.LastCode,
|
||
item.LastCodeAt?.ToString("MM-dd HH:mm:ss") ?? "—",
|
||
item.LastHitAt?.ToString("HH:mm:ss.fff") ?? "—",
|
||
string.IsNullOrWhiteSpace(item.StreamText) ? "—" : item.StreamText,
|
||
item.LastDecodeMs > 0 ? $"{item.LastDecodeMs:0}ms" : "—",
|
||
item.Hits > 0 ? item.Hits.ToString() : "—",
|
||
item.LastError ?? string.Empty,
|
||
item.Summary)).ToList());
|
||
return;
|
||
}
|
||
|
||
var snapshot = _scan.LabelScan.Snapshot;
|
||
StatusText.Text = snapshot.StatusText;
|
||
SetWarnings(snapshot.Warnings.Select(item => item.Display).ToList());
|
||
RefreshButton.Content = "刷新配置";
|
||
RefreshButton.IsEnabled = !_refreshing && !_scan.LabelScan.Refreshing;
|
||
|
||
Render(_scan.LabelScan.Runtimes.Select(runtime => new RowData(
|
||
$"{runtime.WorkstationId}|{runtime.CameraId}|{runtime.ChannelId}",
|
||
string.IsNullOrWhiteSpace(runtime.WorkstationName) ? "—" : runtime.WorkstationName,
|
||
runtime.CameraName,
|
||
string.IsNullOrWhiteSpace(runtime.ChannelName) ? runtime.ChannelId : $"{runtime.ChannelId}「{runtime.ChannelName}」",
|
||
runtime.StateText,
|
||
string.IsNullOrEmpty(runtime.LastCode) ? "—" : runtime.LastCode,
|
||
runtime.LastCodeAt?.ToString("MM-dd HH:mm:ss") ?? "—",
|
||
runtime.LastHitAt?.ToString("HH:mm:ss.fff") ?? "—",
|
||
string.IsNullOrWhiteSpace(runtime.StreamText) ? "—" : runtime.StreamText,
|
||
runtime.LastDecodeMs > 0 ? $"{runtime.LastDecodeMs:0}ms" : "—",
|
||
runtime.Hits > 0 ? runtime.Hits.ToString() : "—",
|
||
runtime.LastError ?? string.Empty,
|
||
runtime.Summary)).ToList());
|
||
}
|
||
|
||
private void SetWarnings(System.Collections.Generic.IReadOnlyList<string> warnings)
|
||
{
|
||
WarnText.Text = warnings.Count == 0
|
||
? string.Empty
|
||
: string.Join(Environment.NewLine, warnings.Select(item => $"⚠ {item}"));
|
||
}
|
||
|
||
/// <summary>手动刷新配置(服务模式下由后台服务负责,这里不可用)</summary>
|
||
private async void Refresh_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
_refreshing = true;
|
||
RefreshButton.IsEnabled = false;
|
||
try
|
||
{
|
||
await _scan.LabelScan.RefreshAsync("手动刷新");
|
||
}
|
||
finally
|
||
{
|
||
_refreshing = false;
|
||
Refresh();
|
||
}
|
||
}
|
||
}
|