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;
using SamplePre.Models.Models;
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();
///
/// u错误队列
///
ConcurrentQueueExt errQueue = new SamplePre.Common.ConcurrentQueueExt();
///
/// 所有动作
///
List sys_Actions = new List();
public MonitorViewModel()
{
//所有动作
sys_Actions = actionMangerBll.QuerySysAction();
//获取所有样本信息
GetSampleData();
//获取当前SOP所有动作列表
GetCurrentSopByID(CurrentSop.id);
//计算进度百分比
CalculationProgress(CurrentSop.id);
//初始化功能单元
InitWorkUnits();
///启动数据监控线程
UpdateMonitorData();
//错误信息处理
ErrInfoDo();
}
#region PLC 通信状态监控
[ObservableProperty]
public string txtState = "PLC通信异常!";
#endregion
#region 样品列表
[ObservableProperty]
public List sampleExecList = new List();
///
/// 获取样本信息
///
///
private void GetSampleData()
{
List samples = sampleSequenceBll.GetSampleExecDetails();
SampleExecList = samples;
//设置当前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;
}
}
}
#endregion
#region 当前sop的动作列表
///
/// sop动作执行信息
///
private List sopExecInfo = new List();
public List SopExecInfo
{
get { return sopExecInfo; }
set { sopExecInfo = value; }
}
///
/// 当前SOP动作
///
public List CurrentstandardActionList = new List();
///
/// 获取当前SOP所有动作列表
///
///
public void GetCurrentSopByID(string sopID)
{
CurrentstandardActionList = sopManagerBll.QueryStandardActionByStandardId(sopID)?.OrderBy(p => p.process_no).ToList(); ;
SopExecInfo.Clear();
int index = 1;
foreach (var item in CurrentstandardActionList)
{
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
});
}
index++;
}
}
#endregion
#region sop执行进度
///
/// 当前SOP
///
[ObservableProperty]
public sys_standard currentSop = new sys_standard();
///
/// 当前SOP进度
///
[ObservableProperty]
public CurrentSopProgress sopProgress = new CurrentSopProgress();
///
/// 计算SOP进度百分比
///
///
public void CalculationProgress(string SopId)
{
SopProgress.sopName = this.CurrentSop.standard_name;
SopProgress.sampleName = "xxxxx";
SopProgress.actionName = CurrentstandardActionList.FirstOrDefault()?.action_name;
SopProgress.CurrentPercent = 40;
}
#endregion
#region 功能单元
private List _workUnits = new List();
public List WorkUnits
{
get { return _workUnits; }
set
{
SetProperty(ref _workUnits, value);
}
}
///
/// 初始化功能岛
///
public void InitWorkUnits()
{
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);
WorkUnits.Add(new WorkUnit() { actionName = "无", state = 0, StateName = "未连通", unitCode = item.id, unitName = item.data_type, imgPath = imgPath, packIconKind = icon });
}
}
public PackIconKind GetIconKind(string code)
{
switch (code)
{
case "300":
return PackIconKind.RobotIndustrialOutline;//Resources.机器人1;
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;
}
}
public string GetIcon(string code)
{
switch (code)
{
case "300":
return "/Assets/机器人1.png";//Resources.机器人1;
case "301":
return "/Assets/分液1.png";//Resources.分液1;
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;
}
}
///
/// 更新监控数据
///
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);
//刷新样品列表
GetSampleData();
//跟新plc通信状态
if(SystemConst.MasterPLC.IsConnected)
{
TxtState = "PLC通信正常";
}
else
{
TxtState = "PLC通信异常!";
this.errQueue.Enqueue(new FaultInfo() { FaultName = "PLC通信异常!", FaultDesc = "请检查PLC是否正常!", FaultDate = DateTime.Now });
}
await Task.Delay(3000);
}
}
catch (Exception ex)
{
LoggerHelper.Logger.Error("更新监控数据异常退出:" + ex.Message + ex.StackTrace);
}
});
}
///
/// 更新功能岛信息
///
///
public void UpdateWorkUnit(ushort[] data1)
{
//WorkUnits
if (data1 != null && data1.Length > 0)
{
foreach (var workUnit in WorkUnits)
{
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];//动作的状态码
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;
}
}
//如果动作一样,状态发生变化,就要修改
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;
}
}
//添加故障信息
if (workUnit.state == 0)
{
//Application.Current?.Dispatcher.BeginInvoke(new Action(() => {
// FaultInfos.Add(new FaultInfo { FaultName = workUnit.unitName + " - 未连通", FaultType = FaultType.Warning });
//}), DispatcherPriority.Normal);
errQueue.Enqueue(new FaultInfo { FaultName = workUnit.unitName + " - 未连通", FaultDate = DateTime.Now, FaultType = FaultType.Warning });
}
}
}
}
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 "未连接";
}
}
#endregion
#region 处理故障信息
///
/// 故障信息
///
private ObservableCollection faultInfos = new ObservableCollection();
public ObservableCollection FaultInfos
{
get { return faultInfos; }
set
{
SetProperty(ref faultInfos, value);
}
}
public void ErrInfoDo()
{
Task.Run(() => {
while (true)
{
var errInfo = this.errQueue.GetOne();
if(errInfo != null)
{
Application.Current?.Dispatcher.Invoke(() =>
{
FaultInfos.Add(errInfo);
});
}
Task.Delay(1000).Wait();
}
});
}
#endregion
///
/// 打开详情窗口
///
[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();
}
}
///
/// 打开明细进度窗口
///
[RelayCommand]
public void MouseDoubleDetailWindow()
{
SampleDetailWindow frm = new SampleDetailWindow();
frm.ShowDialog();
}
}
}