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.

605 lines
21 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 Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Socket;
using Aucma.Core.BoxFoam.Business;
using Aucma.Core.BoxFoam.Views;
using Aucma.Core.HwPLc;
using Aucma.Core.Scanner;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using log4net;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData;
namespace Aucma.Core.BoxFoam.ViewModels
{
public partial class MainWindowViewModel : ObservableObject
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(MainWindowViewModel));
private ISysUserInfoServices _sysUserInfoServices;
private IndexPageView firstPage = new IndexPageView();//首页
CollectionFoamLine line = new CollectionFoamLine();
CollectionFoamMachine machine = new CollectionFoamMachine();
RealTimeInventoryPageView realTimeInventoryPage = new RealTimeInventoryPageView();
EnterWarehouseStatisticsPageView enterWarehouseStatisticsPage = new EnterWarehouseStatisticsPageView();
FoamPlanPageView foamPlanPageView = new FoamPlanPageView();
FoamMonitorPageView foamMonitorPageView = new FoamMonitorPageView();
MonitorPageView monitorPage = new MonitorPageView();//任务监控
FoamMachinesPageView foamMachinesPageView = new FoamMachinesPageView();
bool startflag = true;
bool flag2 = true;
public MainWindowViewModel()
{
UserContent = firstPage;
_sysUserInfoServices = App.ServiceProvider.GetService<ISysUserInfoServices>();
TouchSocketService.RefreshStateEvent += RefreshScanner;
string address = Appsettings.app("Middleware", "TouchSocket", "Address").ObjToString();
RefreshScanner(address, false);
Task.Run(async () =>
{
await init();
await RefreshTeamTime();//班组时间
//await ClearProduct();
});
TeamSwitchBusiness teamSwitchBusiness = new TeamSwitchBusiness();
teamSwitchBusiness.Init();//切换班组清空发泡产量
//实时更新界面时间
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1); //间隔1秒
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
public Task init()
{
// 设备状态刷新定时器
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();
return Task.CompletedTask;
}
#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 info = PlcHelper.siemensList.ToList();
if (info != null)
{
if (info.Count > 0)
{
PlcUIColor.Clear();
info = info.OrderBy(x => x.Id).ToList();
PlcUIColor.Clear();
PlcUIIcon.Clear();
foreach (var item in info)
{
if (item.plc.IsConnected)
{
PlcState(true);
}
else
{
PlcState(false);
}
}
}
}
else
{
PlcUIColor.Clear();
PlcState(false);
PlcState(false);
PlcState(false);
PlcState(false);
}
}
/// <summary>
/// 扫码器状态刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void RefreshScanner(string address,bool type)
{
Scanner1State(type);
}
#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)
{
log.Warn($"系统退出,当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
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 "RealTimeInventoryPage":
UserContent = realTimeInventoryPage;
break;
case "StatisticsPage":
UserContent = enterWarehouseStatisticsPage;
break;
case "MonitorPage":
UserContent = monitorPage;
break;
case "FoamPlanPage":
UserContent = foamPlanPageView;
break;
case "FoamMonitorPage":
UserContent = foamMonitorPageView;
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>
/// UI plc 展示状态-文字
/// </summary>
public string _plcUIStatusWb;
public string PlcUIStatusWb
{
get => _plcUIStatusWb;
set => SetProperty(ref _plcUIStatusWb, value);
}
/// <summary>
/// UI plc 展示状态-颜色
/// </summary>
private ObservableCollection<string> _plcUIColor = new ObservableCollection<string>();
public ObservableCollection<string> PlcUIColor
{
get => _plcUIColor;
set => SetProperty(ref _plcUIColor, value);
}
/// <summary>
/// UI plc 展示状态-图标
/// </summary>
private ObservableCollection<string> _plcUIIcon = new ObservableCollection<string>();
public ObservableCollection<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.Add("Green");
PlcUIIcon.Add("Assets/Images/Green.png");
}
else
{
PlcUIStatusWb = "PLC";
PlcUIColor.Add("Red");
PlcUIIcon.Add("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
#region 班组信息
/// <summary>
/// 班组信息
/// </summary>
public string _teamName;
public string TeamName
{
get => _teamName;
set => SetProperty(ref _teamName, value);
}
#endregion
#region MyRegion
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);
}
#endregion
#region 切换班组
public async Task RefreshTeamTime()
{
while (true)
{
var list = await _sysUserInfoServices.GetTeamData();
if (list != null && list.Count > 0)
{
var sysUserInfo = list.First();
TeamName = $"{sysUserInfo.TeamName}({list.Min(d => d.StartTime).ToString("HH:mm")}~{list.Max(d => d.EndTime).ToString("HH:mm")})";
//TeamName = $"{sysUserInfo.TeamName}";
}
Thread.Sleep(5000);
}
}
#endregion
#region 定时刷新时间
void timer_Tick(object sender, EventArgs e)
{
System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
{
if (startflag)
{
startflag = false;
try
{
DateTime now = DateTime.Now;
CurrentDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
startflag = true;
}
}
}));
}
#endregion
#region 定时清除产量
private Task ClearProduct()
{
while (true)
{
if (flag2)
{
flag2 = false;
try
{
DateTime now = DateTime.Now;
string date = now.ToString("yyyy-MM-dd HH:mm:ss");
DateTime scheduledTime = new DateTime(now.Year, now.Month, now.Day, 20, 00, 00);
string sTime = scheduledTime.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine($"定时班组切换清空产量时间夜班:{sTime.Equals(date)}");
if (sTime.Equals(date))
{
var obj1 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine1Plc"));
if (obj1 != null)
{
if (obj1.plc.IsConnected)
{
bool flag = obj1.plc.WriteBool("M6.0", true);//换班 数据清空
Thread.Sleep(500);
bool flag1 = obj1.plc.WriteBool("M6.0", false);//换班 数据清空
Console.WriteLine($"班组切换清空1~6区产量数据{flag}:{flag1}");
log.Warn($"定时班组切换清空1~6区产量数据{(flag == true ? "" : "")}");
}
}
var obj2 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine2Plc"));
if (obj2 != null)
{
if (obj2.plc.IsConnected)
{
bool flag = obj2.plc.WriteBool("M6.0", true);//换班 数据清空
Thread.Sleep(500);
bool flag1 = obj2.plc.WriteBool("M6.0", false);//换班 数据清空
Console.WriteLine($"班组切换清空7~12区产量数据{flag}:{flag1}");
log.Warn($"定时班组切换清空7~12区产量数据{(flag == true ? "" : "")}");
}
}
}
DateTime scheduledTime2 = new DateTime(now.Year, now.Month, now.Day, 8, 00, 00);
string date2 = scheduledTime2.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine($"定时班组切换清空产量时间白班:{sTime.Equals(date2)}");
if (sTime.Equals(date2))
{
var obj1 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine1Plc"));
if (obj1 != null)
{
if (obj1.plc.IsConnected)
{
var flag = obj1.plc.WriteBool("M6.0", true);//换班 数据清空
Thread.Sleep(500);
var flag1 = obj1.plc.WriteBool("M6.0", false);//换班 数据清空
Console.WriteLine($"班组切换清空产量时间夜班:{flag}:{flag1}");
log.Warn($"定时班组切换清空1~6区产量数据{(flag1 == true ? "" : "")}");
}
}
var obj2 = PlcHelper.siemensList.FirstOrDefault(d => d.EquipName.Equals("foamLine2Plc"));
if (obj2 != null)
{
if (obj2.plc.IsConnected)
{
bool flag = obj2.plc.WriteBool("M6.0", true);//换班 数据清空
Thread.Sleep(500);
bool flag2 = obj2.plc.WriteBool("M6.0", false);//换班 数据清空
Console.WriteLine($"班组切换清空产量时间夜班:{flag}:{flag2}");
log.Warn($"定时班组切换清空7~12区产量数据{(flag == true ? "" : "")}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
startflag = true;
}
}
}
}
#endregion
}
}