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.

346 lines
10 KiB
C#

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<MainWindowViewModel> _logger;
private IBaseMaterialService? baseMaterialService;
private ILogoIdentifyService? ocrVerfiyService;
private PlcPool plcPool = null;
private IndexPage indexPage = new IndexPage();
private StatisticsPageView statisticsPageView = new StatisticsPageView();
private ConfigPage configPage = new ConfigPage();
private DebugConfig config = DebugConfig.Instance;
public delegate void RefreDataGrid();
public static event RefreDataGrid RefreDataGridEvent;
public MainWindowViewModel()
{
UserContent = indexPage;
baseMaterialService = App.ServiceProvider.GetService<IBaseMaterialService>();
ocrVerfiyService = App.ServiceProvider.GetService<ILogoIdentifyService>();
plcPool = App.ServiceProvider.GetService<PlcPool>();
_logger = App.ServiceProvider.GetService<ILogger<MainWindowViewModel>>();
LogoBusiness.GetInstance(baseMaterialService, ocrVerfiyService, plcPool);
ControlOnClickCommand = new RelayCommand<object>(obj => ControlOnClick(obj));
FormControlCommand = new RelayCommand<object>(x => FormControl(x));
TcpServer.RefreshStateEvent += RefreshScanner;
CCameraHik.RefreshCameraEvent += RefreshCamare;
hikHelper.DeviceListAndStart();
//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)
{
Time = DateTime.Now.ToString("HH:mm:ss");
}
#region 参数定义
/// <summary>
/// PLC设备状态
/// </summary>
private int _PlcStatus = 2;
public int PlcStatus
{
get { return _PlcStatus; }
set { _PlcStatus = value; RaisePropertyChanged(nameof(PlcStatus)); }
}
/// <summary>
/// 扫码器状态
/// </summary>
private int _ScannerStatus = 2;
public int ScannerStatus
{
get { return _ScannerStatus; }
set { _ScannerStatus = value; RaisePropertyChanged(nameof(ScannerStatus)); }
}
/// <summary>
/// 能耗标签相机状态
/// </summary>
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));
}
}
public System.Windows.Controls.UserControl _content;
public System.Windows.Controls.UserControl UserContent
{
get { return _content; }
set
{
_content = value;
RaisePropertyChanged(nameof(UserContent));
}
}
#endregion
#region 事件定义
/// <summary>
/// 界面跳转按钮事件
/// </summary>
public RelayCommand<object> ControlOnClickCommand { get; set; }
/// <summary>
/// 打开系统键盘
/// </summary>
public RelayCommand OpenSystemKeyboardCommand { get; set; }
/// <summary>
/// 窗体控制
/// </summary>
public RelayCommand<object> FormControlCommand { get; set; }
#endregion
private CCameraHik hikHelper = CCameraHik.Instance;
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();
}
/// <summary>
/// PLC状态刷新
/// </summary>
public void RefreshStatus(object sender, System.Timers.ElapsedEventArgs e)
{
PlcAbsractFactory plc = plcPool.GetPlcByKey("plc");
if (plc != null && plc.IsConnected)
{
PlcStatus = 1;
}
else
{
PlcStatus = 2;
}
}
/// <summary>
/// 扫码器状态刷新
/// </summary>
/// <param name="ip"></param>
/// <param name="flag"></param>
public void RefreshScanner(string ip, bool flag)
{
if (flag)
{
ScannerStatus = 1;
}
else
{
ScannerStatus = 2;
}
}
/// <summary>
/// 相机状态刷新
/// </summary>
/// <param name="ip"></param>
/// <param name="flag"></param>
public void RefreshCamare(string ip,bool flag)
{
if (config.Scanner1IP==ip)
{
EnergyCameraStatus = flag ? 1 : 2;
}
}
/// <summary>
/// 窗体控制
/// </summary>
/// <param name="obj"></param>
private void FormControl(object obj)
{
try
{
string controlType = obj as string;
switch (controlType)
{
// 关闭当前窗口
case "Exit":
{
hikHelper.StopGrab();
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);
}
}
/// <summary>
/// 界面跳转
/// </summary>
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<byte[]> ConvertImagesToByteArrays(string[] fileNames)
{
List<byte[]> imageBytesList = new List<byte[]>();
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<byte[]> 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
}
}