using PLCSharp.Core.Prism; using PLCSharp.Models; using PLCSharp.VVMs.MotionController; using Prism.Dialogs; using Prism.Events; using Prism.Ioc; using System.Collections.ObjectModel; using System.ComponentModel; using System.Threading; namespace PLCSharp.VVMs.Cylinders { /// /// 气缸模型 /// [Model] public class CylinderModel : ModelBase { /// /// 气缸模型 /// public CylinderModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService) { bkgWorker = WorkerGuard.Watch(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(); foreach (var cylinder in list) { if (cylinder != null) RefreshFeedback(cylinder, GlobalModel?.ControllersModel?.Controller); } } catch (Exception ex) { SendErr(ex.Message); } } } internal static void RefreshFeedback(Cylinder cylinder, Controller controller) { var live = cylinder.Enabled && controller?.Connected == true; var hasSensorA = cylinder.SensorType is SensorType.Two or SensorType.OnlyA; var hasSensorB = cylinder.SensorType is SensorType.Two or SensorType.OnlyB; var inputA = live && hasSensorA && !string.IsNullOrEmpty(cylinder.InA) ? controller.DI.FirstOrDefault(p => p.Name == cylinder.InA) : null; var inputB = live && hasSensorB && !string.IsNullOrEmpty(cylinder.InB) ? controller.DI.FirstOrDefault(p => p.Name == cylinder.InB) : null; var outputA = live && !string.IsNullOrEmpty(cylinder.OutA) ? controller.DQ.FirstOrDefault(p => p.Name == cylinder.OutA) : null; var outputB = live && !string.IsNullOrEmpty(cylinder.OutB) ? controller.DQ.FirstOrDefault(p => p.Name == cylinder.OutB) : null; var now = DateTime.Now; // 更换反馈来源或首次看到有效信号时,重新计算连续到位时间。 var activeA = hasSensorA ? inputA?.Status == true : outputA?.Status == true; var activeB = hasSensorB ? inputB?.Status == true : outputB?.Status == true; if (!activeA || (hasSensorA ? inputA != cylinder.DIA || !cylinder.StatusAIn : outputA != cylinder.DQA || !cylinder.StatusAOut || cylinder.DIA != null)) cylinder.ADelayTimer = now; if (!activeB || (hasSensorB ? inputB != cylinder.DIB || !cylinder.StatusBIn : outputB != cylinder.DQB || !cylinder.StatusBOut || cylinder.DIB != null)) cylinder.BDelayTimer = now; cylinder.DIA = inputA; cylinder.DIB = inputB; cylinder.DQA = outputA; cylinder.DQB = outputB; cylinder.StatusAIn = inputA?.Status == true; cylinder.StatusBIn = inputB?.Status == true; cylinder.StatusAOut = outputA?.Status == true; cylinder.StatusBOut = outputB?.Status == true; cylinder.AComplit = activeA && (now - cylinder.ADelayTimer).TotalSeconds >= cylinder.ADelay; cylinder.BComplit = activeB && (now - cylinder.BDelayTimer).TotalSeconds >= cylinder.BDelay; } /// /// 全局模型 /// public GlobalModel GlobalModel { get; set; } private ObservableCollection _Cylinders = []; /// /// 气缸列表 /// public ObservableCollection Cylinders { get { return _Cylinders; } set { SetProperty(ref _Cylinders, value); } } /// /// 初始化 /// /// 全局模型 public void Init(GlobalModel globalModel) { GlobalModel = globalModel; foreach (var item in _DatasContext.Cylinders) { Cylinders.Add(item); } } } }