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 保留所有权利。 * CLR版本:4.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(); this.Init(); this.RefreshScanInfo(); } private void Init() { PassRate = tagScanBusiness.TagScanPassRate(); // 初始化数据 RecogEfficiency = new SeriesCollection { new LineSeries { Title = "Achievement", Values = new ChartValues { 3, 5, 7, 2 } // 示例数据 } }; AxisX = new ObservableCollection { "8", "12", "16", "20" }; ScanItems = new ObservableCollection(); } #region 参数定义 /// /// 条码信息 /// public string _tagCode; public string TagCode { get => _tagCode; set => SetProperty(ref _tagCode, value); } /// /// 条码总数 /// public int _tagAmount; public int TagAmount { get => _tagAmount; set => SetProperty(ref _tagAmount, value); } /// /// 合格率 /// public double _passRate; public double PassRate { get => _passRate; set => SetProperty(ref _passRate, value); } /// /// 效率数值 /// private SeriesCollection _recogEfficiency; public SeriesCollection RecogEfficiency { get => _recogEfficiency; set => SetProperty(ref _recogEfficiency, value); } /// /// 效率曲线X坐标 /// private ObservableCollection _axisX; public ObservableCollection AxisX { get => _axisX; set => SetProperty(ref _axisX, value); } /// /// 图像路径 /// private string _imagePath; public string ImagePath { get => _imagePath; set => SetProperty(ref _imagePath, value); } /// /// 日志信息 /// private ObservableCollection _logMessages = new ObservableCollection(); public ObservableCollection LogMessages { get => _logMessages; set => SetProperty(ref _logMessages, value); } /// /// 扫描记录 /// private List scanLogModelsItems = new List(); private ObservableCollection _scanItems = new ObservableCollection(); public ObservableCollection 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; scanLogModelsItems.Add(info); ScanItems = new ObservableCollection(scanLogModelsItems.OrderByDescending(x=>x.CreateTime).ToList()); TagAmount++; PassRate = tagScanBusiness.TagScanPassRate(); }); }; //报警信息刷新 tagScanBusiness.RefreshDeviceAlarmMessageEvent += msg => { RefreshLogMessage(msg); }; } private ObservableCollection listItems = new ObservableCollection(); /// /// 系统运行日志输出 /// /// 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(orderedList); // 更新 UI }); } } catch (Exception ex) { //_logger.LogError($"日志数据绑定异常:{ex.Message}"); } }); } } }