using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.IOCore; using Aitex.Core.UI.MVVM; using MECF.Framework.Common.IOCore; namespace EFEM.RT.Backends { /// /// IOView.xaml 的交互逻辑 /// public partial class IOView : UserControl { public IOView() { InitializeComponent(); this.Loaded += IOView_Loaded; this.IsVisibleChanged += IOView_IsVisibleChanged; } private void IOView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (this.DataContext == null) { DataContext = new IOMonitorViewModel(); } (DataContext as TimerViewModelBase).EnableTimer(IsVisible); } private void IOView_Loaded(object sender, RoutedEventArgs e) { if (this.DataContext == null) { DataContext = new IOMonitorViewModel(); } } } public class IOMonitorViewModel : TimerViewModelBase { public static readonly int IoCardQuantity = int.Parse(DATA.Poll("DeviceDefine.IoCardQuantity").ToString()); public static readonly bool MotionAxisInstalled = bool.Parse(DATA.Poll("DeviceDefine.MotionAxisInstalled").ToString()); private static readonly string[] Source = new [] { "System.dio000", "System.dio001", "System.dio002", "System.dio003", "System.dio004", "System.dio005", "System.dio006", }; public const string MotionAxis = "System.MotionAxis"; public ObservableCollection DiItemList { get; set; } public ObservableCollection DoItemList { get; set; } public ObservableCollection AiItemList { get; set; } public ObservableCollection AoItemList { get; set; } //public ICommand SetScCommand { get; set; } public IOMonitorViewModel():base(nameof(IOMonitorViewModel)) { Init(); //SetScCommand = new DelegateCommand(SetSc); } void Init() { if (DiItemList != null) return; { DiItemList = new ObservableCollection(); for (int i = 0; i < IoCardQuantity; i++) { List diItems = IO.GetDiList(Source[i]); if (diItems==null) continue; foreach (var diItem in diItems) { NotifiableIoItem item = new NotifiableIoItem() { Name = diItem.Name, Index = diItem.Index, Description = diItem.Description, BoolValue = diItem.Value, Address = diItem.Addr, }; DiItemList.Add(item); } } InvokePropertyChanged(nameof(DiItemList)); } if (DoItemList != null) return; { DoItemList = new ObservableCollection(); for (int i = 0; i < IoCardQuantity; i++) { List doItems = IO.GetDoList(Source[i]); if (doItems==null) continue; foreach (var doItem in doItems) { NotifiableIoItem item = new NotifiableIoItem() { Name = doItem.Name, Index = doItem.Index, Description = doItem.Description, BoolValue = doItem.Value, Address = doItem.Addr, }; DoItemList.Add(item); } } InvokePropertyChanged(nameof(DoItemList)); } if (MotionAxisInstalled) { if (AiItemList != null) return; { AiItemList = new ObservableCollection(); List aiItems = IO.GetAiList(MotionAxis); if (aiItems!=null) foreach (var aiItem in aiItems) { NotifiableIoItem item = new NotifiableIoItem() { Name = aiItem.Name, Index = aiItem.Index, Description = aiItem.Description, ShortValue = aiItem.Value, Address = aiItem.Addr, }; AiItemList.Add(item); } InvokePropertyChanged(nameof(AiItemList)); } if (AoItemList != null) return; { AoItemList = new ObservableCollection(); List aoItems = IO.GetAoList(MotionAxis); if (aoItems!=null) foreach (var aoItem in aoItems) { NotifiableIoItem item = new NotifiableIoItem() { Name = aoItem.Name, Index = aoItem.Index, Description = aoItem.Description, ShortValue = aoItem.Value, Address = aoItem.Addr, }; AoItemList.Add(item); } InvokePropertyChanged(nameof(AoItemList)); } } } protected override void Poll() { string[] values = new string[128]; int i = 0; foreach (var notifiableIoItem in DiItemList) { notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value; notifiableIoItem.InvokePropertyChanged(nameof(notifiableIoItem.BoolValue)); values[i++] = notifiableIoItem.BoolValue ? "1" : "0"; } foreach (var notifiableIoItem in DoItemList) { notifiableIoItem.BoolValue = IO.DO[notifiableIoItem.Name].Value; notifiableIoItem.InvokePropertyChanged(nameof(notifiableIoItem.BoolValue)); values[i++] = notifiableIoItem.BoolValue ? "1" : "0"; } //System.Diagnostics.Trace.WriteLine(string.Join(",", values)); } } }