Files
SamplePreSystem-CS/SamplePreSystem.UI/ViewModel/Monitor/MonitorViewModel.cs

555 lines
15 KiB
C#
Raw Normal View History

2026-04-30 11:34:41 +08:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MaterialDesignThemes.Wpf;
using Models.Const;
using Models.Models;
using SamplePre.Common;
using SamplePre.Communication.Communication;
using SamplePre.Models;
using SamplePre.Models.Ext;
2026-05-09 17:49:46 +08:00
using SamplePre.Models.Models;
2026-04-30 11:34:41 +08:00
using SamplePre.ProcessBll.BLL;
using SamplePre.ProcessBll.SampleSequence;
using SamplePre.UIWpf.MonitorManager.chirld;
using SamplePre.UIWpf.SampleManager.chirld;
using SamplePreSystem.UI.Views.MonitorManager;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using static MaterialDesignThemes.Wpf.Theme.ToolBar;
namespace SamplePreSystem.UI.ViewModel.Monitor
{
public partial class MonitorViewModel: ObservableObject
{
SampleSequenceBll sampleSequenceBll = new SampleSequenceBll();
ActionMangerBll actionMangerBll = new ActionMangerBll();
MonitorBll monitorBll = new MonitorBll();
CommunicationBll communicationBll = new CommunicationBll();
SOPManagerBll sopManagerBll = new SOPManagerBll();
2026-05-09 17:49:46 +08:00
/// <summary>
/// u错误队列
/// </summary>
ConcurrentQueueExt<FaultInfo> errQueue = new SamplePre.Common.ConcurrentQueueExt<FaultInfo>();
2026-04-30 11:34:41 +08:00
/// <summary>
/// 所有动作
/// </summary>
List<sys_action_ext> sys_Actions = new List<sys_action_ext>();
public MonitorViewModel()
{
//所有动作
sys_Actions = actionMangerBll.QuerySysAction();
2026-05-09 17:49:46 +08:00
//获取所有样本信息
GetSampleData();
//获取当前SOP所有动作列表
GetCurrentSopByID(CurrentSop.id);
//计算进度百分比
CalculationProgress(CurrentSop.id);
2026-04-30 11:34:41 +08:00
//初始化功能单元
2026-05-09 17:49:46 +08:00
InitWorkUnits();
2026-04-30 11:34:41 +08:00
///启动数据监控线程
UpdateMonitorData();
2026-05-09 17:49:46 +08:00
//错误信息处理
ErrInfoDo();
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
}
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
#region PLC
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
[ObservableProperty]
public string txtState = "PLC通信异常";
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
#endregion
#region
[ObservableProperty]
public List<sample_input> sampleExecList = new List<sample_input>();
2026-04-30 11:34:41 +08:00
/// <summary>
2026-05-09 17:49:46 +08:00
/// 获取样本信息
2026-04-30 11:34:41 +08:00
/// </summary>
2026-05-09 17:49:46 +08:00
/// <param name="standID"></param>
private void GetSampleData()
2026-04-30 11:34:41 +08:00
{
2026-05-09 17:49:46 +08:00
List<sample_input> samples = sampleSequenceBll.GetSampleExecDetails();
SampleExecList = samples;
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
//设置当前SOP——ID
string standrad_id = samples.FirstOrDefault()?.standrad_id;
string standrad_name = samples.FirstOrDefault()?.standrad;
if (!string.IsNullOrEmpty(standrad_id))
{
if (standrad_id.Contains(","))
{
CurrentSop.id = standrad_id.Split(",")[0];
CurrentSop.standard_name = standrad_name.Split(",")[0];
}
else
{
CurrentSop.id = standrad_id;
CurrentSop.standard_name = standrad_name;
}
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
}
2026-04-30 11:34:41 +08:00
}
2026-05-09 17:49:46 +08:00
#endregion
#region sop的动作列表
2026-04-30 11:34:41 +08:00
/// <summary>
2026-05-09 17:49:46 +08:00
/// sop动作执行信息
2026-04-30 11:34:41 +08:00
/// </summary>
2026-05-09 17:49:46 +08:00
private List<SopExecInfo> sopExecInfo = new List<SopExecInfo>();
public List<SopExecInfo> SopExecInfo
{
get { return sopExecInfo; }
set { sopExecInfo = value; }
}
2026-04-30 11:34:41 +08:00
/// <summary>
/// 当前SOP动作
/// </summary>
public List<sys_standard_action_ext> CurrentstandardActionList = new List<sys_standard_action_ext>();
2026-05-09 17:49:46 +08:00
/// <summary>
/// 获取当前SOP所有动作列表
/// </summary>
/// <param name="sopID"></param>
public void GetCurrentSopByID(string sopID)
2026-04-30 11:34:41 +08:00
{
2026-05-09 17:49:46 +08:00
CurrentstandardActionList = sopManagerBll.QueryStandardActionByStandardId(sopID)?.OrderBy(p => p.process_no).ToList(); ;
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
SopExecInfo.Clear();
int index = 1;
foreach (var item in CurrentstandardActionList)
2026-04-30 11:34:41 +08:00
{
2026-05-09 17:49:46 +08:00
if (index == CurrentstandardActionList.Count)
{
SopExecInfo.Add(new SopExecInfo()
{
sapmleName = this.currentSop.standard_name,
actionName = item.action_name,
starDate = DateTime.Now,
endDate = DateTime.Now,
state = "进行中",
doIngShow = true
});
}
else
{
SopExecInfo.Add(new SopExecInfo()
{
sapmleName = this.currentSop.standard_name,
actionName = item.action_name,
starDate = DateTime.Now,
endDate = DateTime.Now,
state = "完成",
doIngShow = false
});
}
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
index++;
}
2026-04-30 11:34:41 +08:00
}
2026-05-09 17:49:46 +08:00
#endregion
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
#region sop执行进度
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
/// <summary>
/// 当前SOP
/// </summary>
[ObservableProperty]
public sys_standard currentSop = new sys_standard();
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
/// <summary>
/// 当前SOP进度
/// </summary>
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
[ObservableProperty]
public CurrentSopProgress sopProgress = new CurrentSopProgress();
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
/// <summary>
/// 计算SOP进度百分比
/// </summary>
/// <param name="actionID"></param>
public void CalculationProgress(string SopId)
2026-04-30 11:34:41 +08:00
{
2026-05-09 17:49:46 +08:00
SopProgress.sopName = this.CurrentSop.standard_name;
SopProgress.sampleName = "xxxxx";
SopProgress.actionName = CurrentstandardActionList.FirstOrDefault()?.action_name;
SopProgress.CurrentPercent = 40;
2026-04-30 11:34:41 +08:00
}
2026-05-09 17:49:46 +08:00
#endregion
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
#region
2026-04-30 11:34:41 +08:00
private List<WorkUnit> _workUnits = new List<WorkUnit>();
public List<WorkUnit> WorkUnits
{
get { return _workUnits; }
2026-05-09 17:49:46 +08:00
set
{
2026-04-30 11:34:41 +08:00
SetProperty(ref _workUnits, value);
2026-05-09 17:49:46 +08:00
2026-04-30 11:34:41 +08:00
}
}
/// <summary>
/// 初始化功能岛
/// </summary>
public void InitWorkUnits()
{
2026-05-09 17:49:46 +08:00
2026-04-30 11:34:41 +08:00
var list = SystemConst.dictDatas.Where(p => p.class_id == 2).ToList();
foreach (var item in list)
{
string imgPath = GetIcon(item.id);
var icon = GetIconKind(item.id);
2026-05-09 17:49:46 +08:00
WorkUnits.Add(new WorkUnit() { actionName = "无", state = 0, StateName = "未连通", unitCode = item.id, unitName = item.data_type, imgPath = imgPath, packIconKind = icon });
2026-04-30 11:34:41 +08:00
}
}
2026-05-09 17:49:46 +08:00
public PackIconKind GetIconKind(string code)
2026-04-30 11:34:41 +08:00
{
2026-05-09 17:49:46 +08:00
switch (code)
{
case "300":
return PackIconKind.RobotIndustrialOutline;//Resources.机器人1;
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
case "301":
return PackIconKind.TestTube;//Resources.分液1;
case "302":
return PackIconKind.GoogleCirclesCommunities;//Resources.离心机1;
case "303":
return PackIconKind.GoogleCirclesGroup;
case "304":
return PackIconKind.InboxMultiple;//Resources.固相萃取1;
default:
return PackIconKind.ThermometerWater; //Resources.机器人1;
2026-04-30 11:34:41 +08:00
}
}
2026-05-09 17:49:46 +08:00
public string GetIcon(string code)
{
switch (code)
{
case "300":
return "/Assets/机器人1.png";//Resources.机器人1;
case "301":
return "/Assets/分液1.png";//Resources.分液1;
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
case "302":
return "/Assets/离心机1.png";//Resources.离心机1;
case "303":
return "/Assets/氮吹1.png";
case "304":
return "/Assets/固相萃取1.png";//Resources.固相萃取1;
default:
return "/Assets/机器人1.png";//Resources.机器人1;
}
}
2026-04-30 11:34:41 +08:00
/// <summary>
/// 更新监控数据
/// </summary>
public void UpdateMonitorData()
{
//if (tileGroup2.Items.Count <= 0) return;
Task.Factory.StartNew(async () => {
try
{
while (true)
{
//ushort[] data1 = SystemConst.MasterPLC.ReadArraysShort(100, 1000, 1000);
ushort[] data1 = communicationBll.ReadSateData(100, 1000, 1000);
//跨线程
Application.Current?.Dispatcher.Invoke(() =>
{
FaultInfos.Clear();
});
//刷新功能岛
UpdateWorkUnit(data1);
2026-05-09 17:49:46 +08:00
2026-04-30 11:34:41 +08:00
//刷新样品列表
GetSampleData();
2026-05-09 17:49:46 +08:00
//跟新plc通信状态
if(SystemConst.MasterPLC.IsConnected)
{
TxtState = "PLC通信正常";
}
else
{
TxtState = "PLC通信异常";
this.errQueue.Enqueue(new FaultInfo() { FaultName = "PLC通信异常", FaultDesc = "请检查PLC是否正常", FaultDate = DateTime.Now });
}
2026-04-30 11:34:41 +08:00
await Task.Delay(3000);
}
}
catch (Exception ex)
{
LoggerHelper.Logger.Error("更新监控数据异常退出:" + ex.Message + ex.StackTrace);
}
});
}
/// <summary>
/// 更新功能岛信息
/// </summary>
/// <param name="data1"></param>
public void UpdateWorkUnit(ushort[] data1)
{
//WorkUnits
if (data1 != null && data1.Length > 0)
{
2026-05-09 17:49:46 +08:00
foreach (var workUnit in WorkUnits)
2026-04-30 11:34:41 +08:00
{
var unit_actions = sys_Actions.Where(p => p.action_unit == workUnit.unitCode).ToList();
foreach (var action in unit_actions)
{
int plcCode = int.Parse(action.plc_type);
ushort state = data1[plcCode];//动作的状态码
2026-05-09 17:49:46 +08:00
2026-04-30 11:34:41 +08:00
if (state >= workUnit.state || state == 2)
{ //发生改变再修改
if (workUnit.state != state || workUnit.actionName != action.action_name)
{
workUnit.state = state;
workUnit.StateName = SetStateData(state);
workUnit.RunColor = WorkUnit.GetStateColor(state);
workUnit.actionName = state < 2 ? "无" : action.action_name;
2026-05-09 17:49:46 +08:00
2026-04-30 11:34:41 +08:00
}
}
//如果动作一样,状态发生变化,就要修改
if (workUnit.state != state && workUnit.actionName == action.action_name)
{
workUnit.state = state;
workUnit.StateName = SetStateData(state);
workUnit.RunColor = WorkUnit.GetStateColor(state);
workUnit.actionName = state < 2 ? "无" : action.action_name;
2026-05-09 17:49:46 +08:00
2026-04-30 11:34:41 +08:00
}
}
//添加故障信息
if (workUnit.state == 0)
{
2026-05-09 17:49:46 +08:00
//Application.Current?.Dispatcher.BeginInvoke(new Action(() => {
// FaultInfos.Add(new FaultInfo { FaultName = workUnit.unitName + " - 未连通", FaultType = FaultType.Warning });
//}), DispatcherPriority.Normal);
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
errQueue.Enqueue(new FaultInfo { FaultName = workUnit.unitName + " - 未连通", FaultDate = DateTime.Now, FaultType = FaultType.Warning });
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
}
}
2026-04-30 11:34:41 +08:00
}
}
public string SetStateData(int state)
{
try
{
if (state == 0)
{
return "未连接";
}
if (state == 1)
{
return "待机";
}
else if (state == 2)
{
return "运行";
}
else if (state == 3)
{
return "完成";
}
return "未连接";
}
catch (Exception ex)
{
LoggerHelper.Logger.Error(ex.Message + ex.StackTrace);
return "未连接";
}
2026-05-09 17:49:46 +08:00
2026-04-30 11:34:41 +08:00
}
2026-05-09 17:49:46 +08:00
#endregion
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
#region
2026-04-30 11:34:41 +08:00
/// <summary>
2026-05-09 17:49:46 +08:00
/// 故障信息
2026-04-30 11:34:41 +08:00
/// </summary>
2026-05-09 17:49:46 +08:00
private ObservableCollection<FaultInfo> faultInfos = new ObservableCollection<FaultInfo>();
public ObservableCollection<FaultInfo> FaultInfos
2026-04-30 11:34:41 +08:00
{
2026-05-09 17:49:46 +08:00
get { return faultInfos; }
set
{
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
SetProperty(ref faultInfos, value);
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
}
2026-04-30 11:34:41 +08:00
}
2026-05-09 17:49:46 +08:00
public void ErrInfoDo()
2026-04-30 11:34:41 +08:00
{
2026-05-09 17:49:46 +08:00
Task.Run(() => {
while (true)
{
var errInfo = this.errQueue.GetOne();
if(errInfo != null)
{
Application.Current?.Dispatcher.Invoke(() =>
{
FaultInfos.Add(errInfo);
});
}
2026-04-30 11:34:41 +08:00
2026-05-09 17:49:46 +08:00
Task.Delay(1000).Wait();
}
});
2026-04-30 11:34:41 +08:00
}
2026-05-09 17:49:46 +08:00
#endregion
2026-04-30 11:34:41 +08:00
/// <summary>
/// 打开详情窗口
/// </summary>
[RelayCommand]
public void HLinkDetailClick(string unitName)
{
var param = unitName;
var item = SystemConst.dictDatas.Where(p => p.data_type == param.ToString() && p.class_id == 2).FirstOrDefault();
if (item != null)
{
UnitDetailWindow frm = new UnitDetailWindow(item.id);
frm.ShowDialog();
}
}
/// <summary>
/// 打开明细进度窗口
/// </summary>
[RelayCommand]
public void MouseDoubleDetailWindow()
{
SampleDetailWindow frm = new SampleDetailWindow();
frm.ShowDialog();
}
}
}