plcsharp/PLCSharpWpf/VVMs/Cylinders/CylinderModel.cs
2026-08-12 11:49:54 +08:00

189 lines
6.5 KiB
C#

using PLCSharp.Core.Prism;
using PLCSharp.Models;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
namespace PLCSharp.VVMs.Cylinders
{
/// <summary>
/// 气缸模型
/// </summary>
[Model]
public class CylinderModel : ModelBase
{
/// <summary>
/// 气缸模型
/// </summary>
public CylinderModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
{
bkgWorker = new BackgroundWorker { WorkerSupportsCancellation = true };
bkgWorker.DoWork += BackgroundWork;
if (!bkgWorker.IsBusy)
bkgWorker.RunWorkerAsync();
}
private readonly BackgroundWorker bkgWorker;
private void BackgroundWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
while (!worker.CancellationPending)
{
Thread.Sleep(50);
try
{
// 快照遍历,避免与 UI 线程增删气缸产生集合并发问题
var list = Cylinders.ToList();
for (int i = 0; i < list.Count; i++)
{
if (list[i] == null
|| list[i].Enabled == false)//未启的气缸不进行处理
continue;
var controller = GlobalModel.ControllersModel.Controllers.FirstOrDefault(c => c.ControllerNo == list[i].IOController);
if (controller == null) continue;
// 更新 DI DQ
if (list[i].SensorType == SensorType.Two || list[i].SensorType == SensorType.OnlyA)
{
if (string.IsNullOrEmpty(list[i].InA) == false)
{
if (list[i].DIA == null || list[i].DIA.Name != list[i].InA)
list[i].DIA = controller.DI.FirstOrDefault(d => d.Name == list[i].InA);
}
}
if (string.IsNullOrEmpty(list[i].OutA) == false)
{
if (list[i].DQA == null || list[i].DQA.Name != list[i].OutA)
list[i].DQA = controller.DQ.FirstOrDefault(d => d.Name == list[i].OutA);
}
if (list[i].SensorType == SensorType.Two || list[i].SensorType == SensorType.OnlyB)
{
if (string.IsNullOrEmpty(list[i].InB) == false)
{
if (list[i].DIB == null || list[i].DIB.Name != list[i].InB)
list[i].DIB = controller.DI.FirstOrDefault(d => d.Name == list[i].InB);
}
}
if (string.IsNullOrEmpty(list[i].OutB) == false)
{
if (list[i].DQB == null || list[i].DQB.Name != list[i].OutB)
list[i].DQB = controller.DQ.FirstOrDefault(d => d.Name == list[i].OutB);
}
//气缸功能块
FB(list[i]);
}
}
catch (Exception ex)
{
SendErr(ex.Message);
}
}
}
private void FB(Cylinder cylinder)
{
//如果气缸没有传感器 则判断气缸输出延时是否完成
if (cylinder.SensorType == SensorType.None || cylinder.SensorType == SensorType.OnlyB)
{
if (cylinder.DQA != null && cylinder.DQA.Status)
{
cylinder.AComplit = cylinder.ADelayTimer.AddSeconds(cylinder.ADelay) < DateTime.Now;
}
else
{
cylinder.AComplit = false;
cylinder.ADelayTimer = DateTime.Now;
}
}
if (cylinder.SensorType == SensorType.None || cylinder.SensorType == SensorType.OnlyA)
{
if (cylinder.DQB != null && cylinder.DQB.Status)
{
cylinder.BComplit = cylinder.BDelayTimer.AddSeconds(cylinder.BDelay) < DateTime.Now;
}
else
{
cylinder.BComplit = false;
cylinder.BDelayTimer = DateTime.Now;
}
}
//如果气缸有传感器 则判断传感器倒位延时是否完成
if (cylinder.SensorType == SensorType.Two || cylinder.SensorType == SensorType.OnlyA)
{
if (cylinder.DIA != null && cylinder.DIA.Status)
{
cylinder.AComplit = cylinder.ADelayTimer.AddSeconds(cylinder.ADelay) < DateTime.Now;
}
else
{
cylinder.AComplit = false;
cylinder.ADelayTimer = DateTime.Now;
}
}
if (cylinder.SensorType == SensorType.Two || cylinder.SensorType == SensorType.OnlyB)
{
if (cylinder.DIB != null && cylinder.DIB.Status)
{
cylinder.BComplit = cylinder.BDelayTimer.AddSeconds(cylinder.BDelay) < DateTime.Now;
}
else
{
cylinder.BComplit = false;
cylinder.BDelayTimer = DateTime.Now;
}
}
}
/// <summary>
/// 全局模型
/// </summary>
public GlobalModel GlobalModel { get; set; }
private ObservableCollection<Cylinder> _Cylinders = [];
/// <summary>
/// 气缸列表
/// </summary>
public ObservableCollection<Cylinder> Cylinders
{
get { return _Cylinders; }
set { SetProperty(ref _Cylinders, value); }
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="globalModel">全局模型</param>
public void Init(GlobalModel globalModel)
{
GlobalModel = globalModel;
foreach (var item in _DatasContext.Cylinders)
{
Cylinders.Add(item);
}
}
}
}