You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
6.1 KiB
C#


using CommunityToolkit.Mvvm.ComponentModel;
using LiveCharts;
using LiveCharts.Wpf;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Business;
using SlnMesnac.Model.domain;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.0.30319.42000
* T14-GEN3-7895
* SlnMesnac.WPF.ViewModel
* 9e7a55b1-ef98-4957-9a44-16dd251c4b5c
*
* WenJY
*
* 2024-09-18 10:46:52
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.WPF.ViewModel
{
public class IndexViewModel : ObservableObject
{
private TagScanBusiness tagScanBusiness;
public IndexViewModel()
{
tagScanBusiness = App.ServiceProvider.GetService<TagScanBusiness>();
this.Init();
this.RefreshScanInfo();
}
private void Init()
{
PassRate = 30;
// 初始化数据
RecogEfficiency = new SeriesCollection
{
new LineSeries
{
Title = "Achievement",
Values = new ChartValues<double> { 3, 5, 7, 2 } // 示例数据
}
};
AxisX = new ObservableCollection<string> { "8", "12", "16", "20" };
ScanItems = new ObservableCollection<ScanLogModel>();
}
#region 参数定义
/// <summary>
/// 条码信息
/// </summary>
public string _tagCode;
public string TagCode
{
get => _tagCode;
set => SetProperty(ref _tagCode, value);
}
/// <summary>
/// 条码总数
/// </summary>
public int _tagAmount;
public int TagAmount
{
get => _tagAmount;
set => SetProperty(ref _tagAmount, value);
}
/// <summary>
/// 合格率
/// </summary>
public int _passRate;
public int PassRate
{
get => _passRate;
set => SetProperty(ref _passRate, value);
}
/// <summary>
/// 效率数值
/// </summary>
private SeriesCollection _recogEfficiency;
public SeriesCollection RecogEfficiency
{
get => _recogEfficiency;
set => SetProperty(ref _recogEfficiency, value);
}
/// <summary>
/// 效率曲线X坐标
/// </summary>
private ObservableCollection<string> _axisX;
public ObservableCollection<string> AxisX
{
get => _axisX;
set => SetProperty(ref _axisX, value);
}
/// <summary>
/// 图像路径
/// </summary>
private string _imagePath;
public string ImagePath
{
get => _imagePath;
set => SetProperty(ref _imagePath, value);
}
/// <summary>
/// 日志信息
/// </summary>
private ObservableCollection<string> _logMessages = new ObservableCollection<string>();
public ObservableCollection<string> LogMessages
{
get => _logMessages;
set => SetProperty(ref _logMessages, value);
}
/// <summary>
/// 扫描记录
/// </summary>
private ObservableCollection<ScanLogModel> _scanItems = new ObservableCollection<ScanLogModel>();
public ObservableCollection<ScanLogModel> ScanItems
{
get => _scanItems;
set => SetProperty(ref _scanItems, value);
}
#endregion
private void RefreshScanInfo()
{
//数据刷新
tagScanBusiness.RefreshTagScanInfoEvent += info =>
{
App.Current.Dispatcher.Invoke(() =>
{
TagCode = info.Rfid;
ImagePath = info.Url;
string fileName = Path.GetFileName(info.Url);
info.Url = fileName;
ScanItems.Add(info);
TagAmount++;
});
};
//报警信息刷新
tagScanBusiness.RefreshDeviceAlarmMessageEvent += msg =>
{
RefreshLogMessage(msg);
};
}
private ObservableCollection<string> listItems = new ObservableCollection<string>();
/// <summary>
/// 系统运行日志输出
/// </summary>
/// <param name="message"></param>
private async void RefreshLogMessage(string message)
{
await Task.Run(() =>
{
try
{
string formattedMessage = $"{DateTime.Now.ToString("HH:mm:ss.ss")} ==> {message}";
lock (listItems)
{
listItems.Add(formattedMessage);
while (listItems.Count > 120)
{
listItems.RemoveAt(0);
}
var orderedList = listItems.OrderByDescending(x => x).ToList(); // 排序后转为 List
App.Current.Dispatcher.Invoke(() =>
{
LogMessages = new ObservableCollection<string>(orderedList); // 更新 UI
});
}
}
catch (Exception ex)
{
//_logger.LogError($"日志数据绑定异常:{ex.Message}");
}
});
}
}
}