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.

532 lines
16 KiB
C#

using Aucma.Core.DoorFoam.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows;
using System;
using log4net;
using System.Diagnostics;
using System.Threading;
using System.Linq;
using Aucma.Core.HwPLc;
using System.Windows.Threading;
using System.Threading.Tasks;
using Aucma.Core.DoorFoam.Business;
using Admin.Core.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections;
using Admin.Core.Socket;
using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData;
10 months ago
using Admin.Core.Common;
namespace Aucma.Core.DoorFoam.ViewModels
{
public partial class MainWindowViewModel : ObservableObject
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(MainWindowViewModel));
private IndexPageView firstPage = new IndexPageView();//首页
1 year ago
//private StatisticsPageView recordPage = new StatisticsPageView();
RealTimePageView realTimePageView = new RealTimePageView();
FoamMachinesPageView foamMachinesPageView = new FoamMachinesPageView();
private DoorCollection doorCollection = new DoorCollection();
private CollectionFoamMachine collectionFoam = new CollectionFoamMachine();
public MainWindowViewModel()
{
UserContent = firstPage;
TouchSocketService.RefreshStateEvent += RefreshScanner;
10 months ago
string address = Appsettings.app("Middleware", "TouchSocket", "Address").ObjToString();
RefreshScanner(address, false);
init();
}
#region 初始化
public void init()
{
// 设备状态刷新定时器
1 year ago
System.Timers.Timer timer = new System.Timers.Timer(1000 * 5);
timer.Elapsed += new System.Timers.ElapsedEventHandler(RefreshStatus);
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
// 创建一个DispatcherTimer对象
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Tick += Timer_Tick;
dispatcherTimer.Start();
}
#endregion
#region 设备状态刷新
/// <summary>
/// 设备状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshStatus(object sender, System.Timers.ElapsedEventArgs e)
{
RefreshMesDb();
RefreshPlc();
}
/// <summary>
/// 数据库状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshMesDb()
{
MesDbState(true);
}
/// <summary>
/// plc状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshPlc()
{
var obj = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("FoamPlc"));
var obj2 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("DoorPlc"));
// 发泡机plc
if (obj != null)
{
if (obj.plc.IsConnected)
{
PlcState(true);
}
else
{
PlcState(false);
}
}
else
{
PlcState(false);
}
if (obj2 != null)
{
if (obj2.plc.IsConnected)
{
Plc2State(true);
}
else
{
Plc2State(false);
}
}
else
{
Plc2State(false);
}
}
/// <summary>
/// 扫码器状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshScanner(string ip, bool flag)
{
Scanner1State(flag); ;
}
#endregion
1 year ago
#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)
{
1 year ago
case "FoamLinePage":
UserContent = firstPage;
break;
1 year ago
case "RealTimePage":
UserContent = realTimePageView;
break;
case "FoamMachinesPage":
UserContent = foamMachinesPageView;
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>
/// 发泡机 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 展示状态-文字
/// </summary>
public string _plc2UIStatusWb;
public string Plc2UIStatusWb
{
get => _plc2UIStatusWb;
set => SetProperty(ref _plc2UIStatusWb, value);
}
/// <summary>
/// UI plc 展示状态-颜色
/// </summary>
public string _plc2UIColor;
public string Plc2UIColor
{
get => _plc2UIColor;
set => SetProperty(ref _plc2UIColor, value);
}
/// <summary>
/// UI plc 展示状态-图标
/// </summary>
public string _plc2UIIcon;
public string Plc2UIIcon
{
get => _plc2UIIcon;
set => SetProperty(ref _plc2UIIcon, value);
}
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";
}
});
}
public void Plc2State(bool type)
{
Application.Current.Dispatcher.Invoke(() =>
{
if (type)
{
Plc2UIStatusWb = "发泡线PLC";
Plc2UIColor = "Green";
Plc2UIIcon = "Assets/Images/Green.png";
}
else
{
Plc2UIStatusWb = "发泡线PLC";
Plc2UIColor = "Red";
Plc2UIIcon = "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 = "扫码器连接成功";
Scanner1UIColor = "Green";
Scanner1UIIcon = "Assets/Images/Green.png";
}
else
{
Scanner1UIStatusWb = "扫码器状态异常";
Scanner1UIColor = "Red";
Scanner1UIIcon = "Assets/Images/Red.png";
}
});
}
#endregion
10 months ago
private ObservableCollection<DoorFoamMachine> dataGridItemLeft = new ObservableCollection<DoorFoamMachine>();
private IEnumerable doorFoamMachineDataGrid_Left;
public IEnumerable DoorFoamMachineDataGrid_Left
{
get { return doorFoamMachineDataGrid_Left; }
set { doorFoamMachineDataGrid_Left = value; OnPropertyChanged(); }
}
private ObservableCollection<DoorFoamMachine> dataGridItemRight = new ObservableCollection<DoorFoamMachine>();
private IEnumerable doorFoamMachineDataGrid_Right;
public IEnumerable DoorFoamMachineDataGrid_Right
{
get { return doorFoamMachineDataGrid_Right; }
set { doorFoamMachineDataGrid_Right = value; OnPropertyChanged(); }
}
private void RefreshDoorFoamMachineDataGrid(List<DoorFoamMachine> data)
{
dataGridItemLeft = new ObservableCollection<DoorFoamMachine>();
int totalItems = data.Count;
int half = totalItems / 2;
List<DoorFoamMachine> doorFoamMachineLeft = data.Take(half).ToList();
List<DoorFoamMachine> doorFoamMachineRight = data.Skip(half).ToList();
doorFoamMachineLeft.ForEach(x => dataGridItemLeft.Add(x));
DoorFoamMachineDataGrid_Left = dataGridItemLeft;
doorFoamMachineRight.ForEach(x => dataGridItemRight.Add(x));
DoorFoamMachineDataGrid_Right = doorFoamMachineRight;
}
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");
}
}
}