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.

530 lines
16 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Aucma.Core.CodeBinding.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows;
using System;
using log4net;
using System.Diagnostics;
using System.Threading;
using NPOI.HSSF.Record;
using System.Threading.Tasks;
using System.Linq;
using Admin.Core.Common;
using Aucma.Core.Scanner;
using Admin.Core.Repository;
using Admin.Core.IRepository;
using Admin.Core.IService;
using Admin.Core.Service;
using Microsoft.Extensions.DependencyInjection;
using Admin.Core.Model;
using Admin.Core.Tasks;
using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData;
using System.Windows.Threading;
using Aucma.Core.CodeBinding.Business;
using MvCodeHelper = Aucma.Core.CodeBinding.Business.MvCodeHelper;
using Aucma.Core.HwPLc;
using Admin.Core.Socket;
using System.Collections.Generic;
namespace Aucma.Core.CodeBinding.ViewModels
{
public partial class MainWindowViewModel : ObservableObject
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(MainWindowViewModel));
private IndexPageView firstPage = new IndexPageView();//首页
private StatisticsPageView recordPage = new StatisticsPageView();
private static List<ScannerModel> allScanners = Appsettings.app<ScannerModel>("ScannerServer").ToList();
// mes扫码器ip
private static string MesScannerIp = allScanners.First(x => x.Name == "mes扫码器").Ip;
// sn扫码器ip
private static string SnScannerIp = allScanners.First(x => x.Name == "sn扫码器").Ip;
/// <summary>
/// 查询信息更新
/// </summary>
/// <param name="Code1"></param>
public delegate Task queryList(object obj);
public static event queryList? queryListEvent;
public MainWindowViewModel()
{
UserContent = firstPage;
// 创建一个DispatcherTimer对象
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
timer.Start();
Scanner1State(false);
Scanner2State(false);
// Job_AllState_Quartz.RefreshStateEvent += RefreshStatus;
// MvCodeHelper.RefreshStateEvent += RefreshScanner;
TouchSocketService.RefreshStateEvent += RefreshScanner;
GunBusiness.InstanceSerialPort3();
// InitHikRobot();
}
#region 开启海康扫码器
public void InitHikRobot()
{
try
{
Task.Run(() =>
{
Thread.Sleep(2000);
Business.MvCodeHelper.Shell();
});
Task.Run(() =>
{
Thread.Sleep(4000);
try
{
var obj = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("条码绑定Plc"));
if (obj != null)
{
PlcState(true);
}
else
{
PlcState(false);
}
}catch (Exception ex)
{
PlcState(false);
}
Business.MvCodeHelper.Liner();
});
}
catch (Exception ex)
{
log.Error("InitHikRobot()开启海康扫码器和扫码枪方法异常" + ex.Message.ToString());
//MvCodeHelper.CloseAllDevice();
//InitHikRobot();
}
}
#endregion
#region 设备状态刷新
public void RefreshScanner(string ip, bool flag)
{
if (ip == SnScannerIp)
{
Scanner1State(flag);
}
else if (ip == MesScannerIp)
{
Scanner2State(flag);
}
}
/// <summary>
/// 设备状态刷新
/// </summary>
public Task RefreshStatus(bool mesState,bool plcState,bool scanner1,bool scanner2)
{
MesDbState(mesState);
PlcState(plcState);
// Scanner1State(scanner1);
// Scanner2State(scanner2);
return Task.CompletedTask;
}
/// <summary>
/// 扫码器状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshScanner()
{
string ip1 = Appsettings.app("Middleware", "Scanner1", "Ip");
string ip2 = Appsettings.app("Middleware", "Scanner2", "Ip");
// bool flag1 = MvCodeHelper.ConnectionStatus(ip1);
// bool flag2 = MvCodeHelper.ConnectionStatus(ip2);
Scanner1State(true);
Scanner2State(true);
}
#endregion
#region 更换界面
public System.Windows.Controls.UserControl _content;
public System.Windows.Controls.UserControl UserContent
{
get => _content;
set => SetProperty(ref _content, value);
}
#endregion
#region 窗口操作
/// <summary>
/// 窗口操作
/// </summary>
[RelayCommand]
private void FormControl(object obj)
{
try
{
string controlType = obj as string;
switch (controlType)
{
// 关闭当前窗口
case "Exit":
if (System.Windows.MessageBox.Show("确定要退出系统吗?", "系统提醒", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
Environment.Exit(0);
}
break;
// 打开软盘
case "TabTip":
OpenOsk();
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)
{
log.Error("窗体控制逻辑异常", ex);
}
}
#endregion
#region 打开软盘
/// <summary>
/// 打开软盘
/// </summary>
public static void OpenOsk()
{
Process proc = new Process();
proc.StartInfo.FileName = @"C:\Windows\System32\osk.exe";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
proc.Start();
}
#endregion
#region 界面切换
/// <summary>
/// 界面切换
/// </summary>
[RelayCommand]
private void SwitchPages(string page)
{
switch (page)
{
case "FirstPage":
UserContent = firstPage;
break;
case "RecordPage":
UserContent = recordPage;
// 刷新查询框
queryListEvent?.Invoke(null);
break;
default:
break;
}
}
#endregion
#region MES数据库状态
/// <summary>
/// MES数据库-文字
/// </summary>
public string _mesDbUIStatusWb;
public string MesDbUIStatusWb
{
get => _mesDbUIStatusWb;
set => SetProperty(ref _mesDbUIStatusWb, value);
}
/// <summary>
/// MES数据库-颜色
/// </summary>
public string _mesDbUIColor;
public string MesDbUIColor
{
get => _mesDbUIColor;
set => SetProperty(ref _mesDbUIColor, value);
}
/// <summary>
/// MES数据库-图标
/// </summary>
public string _mesUIIcon;
public string MesUIIcon
{
get => _mesUIIcon;
set => SetProperty(ref _mesUIIcon, value);
}
/// <summary>
/// MES数据库连接状态
/// </summary>
/// <param name="type"></param>
public void MesDbState(bool type)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (type)
{
MesDbUIStatusWb = "MES数据库";
MesDbUIColor = "Green";
MesUIIcon = "Assets/Images/Green.png";
}
else
{
MesDbUIStatusWb = "MES数据库";
MesDbUIColor = "Red";
MesUIIcon = "Assets/Images/Red.png";
}
});
}
#endregion
#region plc 状态
/// <summary>
/// UI plc 展示状态-文字
/// </summary>
public string _plcUIStatusWb;
public string PlcUIStatusWb
{
get => _plcUIStatusWb;
set => SetProperty(ref _plcUIStatusWb, value);
}
/// <summary>
/// UI plc 展示状态-颜色
/// </summary>
public string _plcUIColor;
public string PlcUIColor
{
get => _plcUIColor;
set => SetProperty(ref _plcUIColor, value);
}
/// <summary>
/// UI plc 展示状态-图标
/// </summary>
public string _plcUIIcon;
public string PlcUIIcon
{
get => _plcUIIcon;
set => SetProperty(ref _plcUIIcon, value);
}
/// <summary>
/// PLC连接状态-true:连接成功false:失败
/// </summary>
/// <param name="type"></param>
public void PlcState(bool type)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (type)
{
PlcUIStatusWb = "PLC状态";
PlcUIColor = "Green";
PlcUIIcon = "Assets/Images/Green.png";
}
else
{
PlcUIStatusWb = "PLC状态";
PlcUIColor = "Red";
PlcUIIcon = "Assets/Images/Red.png";
}
});
}
#endregion
#region 扫码器1状态
/// <summary>
/// UI 展示状态-文字
/// </summary>
public string _scanner1UIStatusWb;
public string Scanner1UIStatusWb
{
//get { return plcUIStatusWb; }
//set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); }
get => _scanner1UIStatusWb;
set => SetProperty(ref _scanner1UIStatusWb, value);
}
/// <summary>
/// UI 展示状态-颜色
/// </summary>
public string _scanner1UIColor;
public string Scanner1UIColor
{
//get { return plcUIColor; }
//set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); }
get => _scanner1UIColor;
set => SetProperty(ref _scanner1UIColor, value);
}
/// <summary>
/// UI 展示状态-图标
/// </summary>
public string _scanner1UIIcon;
public string Scanner1UIIcon
{
//get { return plcUIIcon; }
//set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); }
get => _scanner1UIIcon;
set => SetProperty(ref _scanner1UIIcon, value);
}
/// <summary>
/// 扫码器1连接状态-true:连接成功false:失败
/// </summary>
/// <param name="type"></param>
public void Scanner1State(bool type)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (type)
{
Scanner1UIStatusWb = "SN扫码器";
Scanner1UIColor = "Green";
Scanner1UIIcon = "Assets/Images/Green.png";
}
else
{
Scanner1UIStatusWb = "SN扫码器";
Scanner1UIColor = "Red";
Scanner1UIIcon = "Assets/Images/Red.png";
}
});
}
#endregion
#region 扫码器2状态
/// <summary>
/// UI 展示状态-文字
/// </summary>
public string _scanner2UIStatusWb;
public string Scanner2UIStatusWb
{
//get { return plcUIStatusWb; }
//set { plcUIStatusWb = value; RaisePropertyChanged("PlcUIStatusWb"); }
get => _scanner2UIStatusWb;
set => SetProperty(ref _scanner2UIStatusWb, value);
}
/// <summary>
/// UI 展示状态-颜色
/// </summary>
public string _scanner2UIColor;
public string Scanner2UIColor
{
//get { return plcUIColor; }
//set { plcUIColor = value; RaisePropertyChanged("PlcUIColor"); }
get => _scanner2UIColor;
set => SetProperty(ref _scanner2UIColor, value);
}
/// <summary>
/// UI 展示状态-图标
/// </summary>
public string _scanner2UIIcon;
public string Scanner2UIIcon
{
//get { return plcUIIcon; }
//set { plcUIIcon = value; RaisePropertyChanged("plcUIIcon"); }
get => _scanner2UIIcon;
set => SetProperty(ref _scanner2UIIcon, value);
}
/// <summary>
/// 扫码器2连接状态-true:连接成功false:失败
/// </summary>
/// <param name="type"></param>
public void Scanner2State(bool type)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (type)
{
Scanner2UIStatusWb = "Mes扫码器";
Scanner2UIColor = "Green";
Scanner2UIIcon = "Assets/Images/Green.png";
}
else
{
Scanner2UIStatusWb = "Mes扫码器";
Scanner2UIColor = "Red";
Scanner2UIIcon = "Assets/Images/Red.png";
}
});
}
#endregion
public string _currentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
public string CurrentDateTime
{
get => _currentDateTime;
set => SetProperty(ref _currentDateTime, value);
}
public string _shiftStr = string.Empty;
public string ShiftStr
{
get => _shiftStr;
set => SetProperty(ref _shiftStr, value);
}
private void Timer_Tick(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
// 判断当前是否是白班时间段
if (now.Hour >= 8 && now.Hour < 20)
{
ShiftStr = $"白班 08点-20点";
}
else
{
ShiftStr = $"夜班 20点-08点";
}
CurrentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}