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.ProcessBll.BLL;
using SamplePre.ProcessBll.SampleSequence;
using SamplePre.UIWpf.MonitorManager.chirld;
using SamplePre.UIWpf.SampleManager.chirld;
using SamplePreSystem.UI.Views.MonitorManager;
//using SamplePre.UIWpf.Properties;
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();
///
/// 所有动作
///
List sys_Actions = new List();
public MonitorViewModel()
{
//所有动作
sys_Actions = actionMangerBll.QuerySysAction();
GetSampleData();
//初始化功能单元
InitWorkUnits();
///启动数据监控线程
UpdateMonitorData();
Test();
//获取当前SOP所有动作列表
GetCurrentSopByID(this.CurrentSopID);
}
///
/// 获取当前SOP所有动作列表
///
///
public void GetCurrentSopByID(string sopID)
{
CurrentstandardActionList = sopManagerBll.QueryStandardActionByStandardId(sopID)?.OrderBy(p => p.process_no).ToList(); ;
}
///
/// 计算进度百分比
///
///
public void CalculationProgress(string actionID)
{
if (CurrentstandardActionList.Count <= 0) return;
//var item = CurrentstandardActionList.Where(p => p.process_id == actionID && p.ExecFinishFlag == false).FirstOrDefault();
//if (item != null)
//{
// item.ExecFinishFlag = true;
// int finishcount = CurrentstandardActionList.Count(p => p.ExecFinishFlag == true);
// int totalcount = CurrentstandardActionList.Count;
// double percent = (float)finishcount / totalcount;
// CurrentPercent = Math.Round(percent * 100,0);
//}
}
///
/// 当前SOP_ID
///
[ObservableProperty]
public string currentSopID = "";
///
/// 当前百分比
///
[ObservableProperty]
public double currentPercent = 40;
///
/// 当前SOP动作
///
public List CurrentstandardActionList = new List();
public void Test()
{
SopExecInfo.Add(new SopExecInfo()
{
sapmleName = "DCM固相萃取",
actionName = "加液",
starDate = DateTime.Now,
endDate = DateTime.Now,
state = "完成",
doIngShow = false
});
SopExecInfo.Add(new SopExecInfo()
{
sapmleName = "DCM固相萃取",
actionName = "加液",
starDate = DateTime.Now,
endDate = DateTime.Now,
state = "完成",
doIngShow = false
});
SopExecInfo.Add(new SopExecInfo()
{
sapmleName = "DCM固相萃取",
actionName = "加液",
starDate = DateTime.Now,
endDate = DateTime.Now,
state = "完成",
doIngShow = false
});
SopExecInfo.Add(new SopExecInfo()
{
sapmleName = "DCM固相萃取",
actionName = "离心",
starDate = DateTime.Now,
endDate = DateTime.Now,
state = "执行",
doIngShow = true
});
}
private List _sampleExecList = new List();
public List SampleExecList
{
get { return _sampleExecList; }
set {
SetProperty(ref _sampleExecList, value);
}
}
private string _sopExecData;
public string SopExecData
{
get { return _sopExecData; }
set {
SetProperty(ref _sopExecData, value);
}
}
///
/// 获取样本信息
///
///
private void GetSampleData()
{
List samples = sampleSequenceBll.GetSampleExecDetails();
SampleExecList = samples;
//设置当前SOP——ID
string standrad_id = samples.FirstOrDefault()?.standrad_id;
if (!string.IsNullOrEmpty(standrad_id))
{
if(standrad_id.Contains(","))
{
CurrentSopID = standrad_id.Split(",")[0];
}
else
{
CurrentSopID = standrad_id;
}
}
}
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 });
}
}
///
/// 故障信息
///
private ObservableCollection faultInfos = new ObservableCollection();
public ObservableCollection FaultInfos
{
get { return faultInfos; }
set {
SetProperty(ref faultInfos, value);
}
}
///
/// 更新监控数据
///
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();
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];//动作的状态码
//UCFunUnit uCFunUnit = this.dictUnits[unitCode];
if(state >= 2)
{
//处理sop执行状态,计算执行百分比
CalculationProgress(action.id);
}
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);
}
}
}
}
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 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 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 "未连接";
}
}
SocketClient client;
///
/// 启动socket
///
///
public async Task StarClientAsync()
{
client = new SocketClient();
//回调函数
client.ReceiveMsgAction = UpdateSopExecInfo;
await client.StartClientAsync();
}
public void SendSocketTest(string msg)
{
client.SendMsgAsync(msg);
}
///
/// 更新sop执行信息
///
///
public void UpdateSopExecInfo(string receiveData)
{
SopExecData += receiveData + "\r\n";
}
///
/// sop执行信息
///
private List sopExecInfo = new List();
public List SopExecInfo
{
get { return sopExecInfo; }
set { sopExecInfo = value; }
}
///
/// 打开详情窗口
///
[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();
}
}
}