using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using HslCommunication.Enthernet; using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Logging; using SlnMesnac.Mqtt; using SlnMesnac.TouchSocket; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using SlnMesnac.Rfid; using System.IO; using SqlSugar; using Microsoft.Win32; using System.Drawing; using IronOcr; using Microsoft.AspNetCore.Components.Web.Virtualization; using PaddleOCRSharp; using SlnMesnac.Repository.service; using SlnMesnac.Common; using HslCommunication; using SlnMesnac.Model.domain; using SlnMesnac.WPF.Views; using SlnMesnac.Business; using SlnMesnac.Plc; using static SlnMesnac.TouchSocket.TcpServer; using SlnMesnac.Config; using System.Timers; using System.Windows.Threading; namespace SlnMesnac.WPF.ViewModel { public class MainWindowViewModel : ViewModelBase { private readonly ILogger _logger; private readonly ILogger _logger2; private IBaseMaterialService? baseMaterialService; private ILogoIdentifyService? ocrVerfiyService; private ILogoConfigService? logoConfigService; private TcpServer? tcpServer; private PlcPool plcPool = null; private IndexPage indexPage = new IndexPage(); private StatisticsPageView statisticsPageView = new StatisticsPageView(); private ConfigPage configPage = new ConfigPage(); private DebugConfig config = DebugConfig.Instance; private GunHelper gunHelper = GunHelper.Instance; public delegate void RefreDataGrid(); public static event RefreDataGrid RefreDataGridEvent; public MainWindowViewModel() { UserContent = indexPage; baseMaterialService = App.ServiceProvider.GetService(); ocrVerfiyService = App.ServiceProvider.GetService(); logoConfigService = App.ServiceProvider.GetService(); plcPool = App.ServiceProvider.GetService(); tcpServer = App.ServiceProvider.GetService(); _logger = App.ServiceProvider.GetService>(); _logger2 = App.ServiceProvider.GetService>(); LogoBusiness.GetInstance(_logger2, logoConfigService,baseMaterialService, ocrVerfiyService, plcPool, tcpServer); ControlOnClickCommand = new RelayCommand(obj => ControlOnClick(obj)); FormControlCommand = new RelayCommand(x => FormControl(x)); TcpServer.RefreshStateEvent += RefreshScanner; //PLC状态刷新 StartState(); RefreshTime(); } private void RefreshTime() { DispatcherTimer _timer = new DispatcherTimer(); _timer.Interval = TimeSpan.FromSeconds(1); _timer.Tick += Timer_Tick; _timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { Date = DateTime.Now.ToString("yyyy年MM月dd日"); Time = DateTime.Now.ToString("HH:mm:ss"); } #region 参数定义 /// /// 数据库状态 /// private int _DataBaseStatus = 1; public int DataBaseStatus { get { return _DataBaseStatus; } set { _DataBaseStatus = value; RaisePropertyChanged(nameof(DataBaseStatus)); } } /// /// PLC设备状态 /// private int _PlcStatus = 2; public int PlcStatus { get { return _PlcStatus; } set { _PlcStatus = value; RaisePropertyChanged(nameof(PlcStatus)); } } /// /// 扫码器状态 /// private int _ScannerStatus = 2; public int ScannerStatus { get { return _ScannerStatus; } set { _ScannerStatus = value; RaisePropertyChanged(nameof(ScannerStatus)); } } /// /// 能耗标签相机状态 /// private int _EnergyCameraStatus = 2; public int EnergyCameraStatus { get { return _EnergyCameraStatus; } set { _EnergyCameraStatus = value; RaisePropertyChanged(nameof(EnergyCameraStatus)); } } private string _time; public string Time { get { return _time; } set { _time = value; RaisePropertyChanged(nameof(Time)); } } private string _Date; public string Date { get { return _Date; } set { _Date = value; RaisePropertyChanged(nameof(Date)); } } public System.Windows.Controls.UserControl _content; public System.Windows.Controls.UserControl UserContent { get { return _content; } set { _content = value; RaisePropertyChanged(nameof(UserContent)); } } #endregion #region 事件定义 /// /// 界面跳转按钮事件 /// public RelayCommand ControlOnClickCommand { get; set; } /// /// 打开系统键盘 /// public RelayCommand OpenSystemKeyboardCommand { get; set; } /// /// 窗体控制 /// public RelayCommand FormControlCommand { get; set; } #endregion public void StartState() { // 设备状态刷新定时器 System.Timers.Timer timer = new System.Timers.Timer(1000 * 3); timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshStatus); timer.AutoReset = true; timer.Enabled = true; timer.Start(); } /// /// PLC状态刷新 /// public void RefreshStatus(object sender, System.Timers.ElapsedEventArgs e) { PlcAbsractFactory plc = plcPool.GetPlcByKey("plc"); if (plc != null && plc.IsConnected) { PlcStatus = 1; } else { PlcStatus = 2; } } /// /// 扫码器状态刷新 /// /// /// public void RefreshScanner(string ip, bool flag) { if (config.CameraIP == ip) { EnergyCameraStatus = flag ? 1 : 2; }else if(config.ScannerIP == ip) { ScannerStatus = flag ? 1 : 2; } } /// /// 窗体控制 /// /// private void FormControl(object obj) { try { string controlType = obj as string; switch (controlType) { // 关闭当前窗口 case "Exit": { gunHelper.SendData("Exit"); Environment.Exit(0); } // Application.Current.Shutdown(); break; // 还原 或者 最大化当前窗口 case "Normal": if (Application.Current.MainWindow.WindowState == WindowState.Normal) { Application.Current.MainWindow.WindowState = WindowState.Maximized; break; } if (Application.Current.MainWindow.WindowState == WindowState.Maximized) { Application.Current.MainWindow.WindowState = WindowState.Normal; break; } break; // 最小化当前窗口 case "Minimized": Application.Current.MainWindow.WindowState = WindowState.Minimized; break; default: break; } } catch (Exception ex) { _logger.LogError("窗体控制逻辑异常", ex); } } /// /// 界面跳转 /// private void ControlOnClick(object obj) { try { string info = obj as string; switch (info) { case "Index": UserContent = indexPage; break; case "List": UserContent = statisticsPageView; RefreDataGridEvent?.Invoke(); break; case "Config": UserContent = configPage; break; default : break; } } catch (Exception ex) { _logger.LogError("界面跳转逻辑异常", ex); } } #region 暂未使用-- 照片与byte[]转换 private List ConvertImagesToByteArrays(string[] fileNames) { List imageBytesList = new List(); for(int i = 0; i < 10; i++) { foreach (string fileName in fileNames) { // 读取图像文件并转换为字节数组 byte[] imageBytes = File.ReadAllBytes(fileName); // 将字节数组添加到列表中 imageBytesList.Add(imageBytes); } } return imageBytesList; } private byte[] ConvertImagesToSingleByteArray(List byteArrays) { // 计算所有字节数组的总长度 int totalLength = byteArrays.Sum(arr => arr.Length); // 创建一个新的字节数组来存储所有数据 byte[] combinedBytes = new byte[totalLength]; // 将每个字节数组拷贝到新数组中 int offset = 0; foreach (byte[] byteArray in byteArrays) { Buffer.BlockCopy(byteArray, 0, combinedBytes, offset, byteArray.Length); offset += byteArray.Length; } return combinedBytes; } #endregion } }