IOMonitorView.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using Aitex.Core.RT.IOCore;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.UI.MVVM;
  11. using MECF.Framework.Common.IOCore;
  12. namespace CyberX8_RT.Backends
  13. {
  14. /// <summary>
  15. /// IOView.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class IOView : UserControl
  18. {
  19. public IOView()
  20. {
  21. InitializeComponent();
  22. DataContext = new IOMonitorViewModel();
  23. this.IsVisibleChanged += IOView_IsVisibleChanged;
  24. }
  25. private void IOView_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  26. {
  27. (DataContext as TimerViewModelBase).EnableTimer(IsVisible);
  28. }
  29. }
  30. public class IoValues
  31. {
  32. public ObservableCollection<NotifiableIoItem> DiItemList { get; set; }
  33. public ObservableCollection<NotifiableIoItem> DoItemList { get; set; }
  34. public ObservableCollection<NotifiableIoItem> AiItemList { get; set; }
  35. public ObservableCollection<NotifiableIoItem> AoItemList { get; set; }
  36. #region InitDefine
  37. public IoValues(string source)
  38. {
  39. DiItemList = new ObservableCollection<NotifiableIoItem>();
  40. List<DIAccessor> diItems = IO.GetDiList(source);
  41. if (diItems != null)
  42. {
  43. foreach (var diItem in diItems)
  44. {
  45. NotifiableIoItem item = new NotifiableIoItem()
  46. {
  47. Name = diItem.Name,
  48. Index = diItem.Index,
  49. Description = diItem.Description,
  50. BoolValue = diItem.Value,
  51. Address = diItem.Addr,
  52. };
  53. DiItemList.Add(item);
  54. }
  55. }
  56. DoItemList = new ObservableCollection<NotifiableIoItem>();
  57. List<DOAccessor> doItems = IO.GetDoList(source);
  58. if (doItems != null)
  59. {
  60. foreach (var doItem in doItems)
  61. {
  62. NotifiableIoItem item = new NotifiableIoItem()
  63. {
  64. Name = doItem.Name,
  65. Index = doItem.Index,
  66. Description = doItem.Description,
  67. BoolValue = doItem.Value,
  68. Address = doItem.Addr,
  69. };
  70. DoItemList.Add(item);
  71. }
  72. }
  73. AiItemList = new ObservableCollection<NotifiableIoItem>();
  74. List<AIAccessor> aiItems = IO.GetAiList(source);
  75. if (aiItems != null)
  76. {
  77. foreach (var aiItem in aiItems)
  78. {
  79. NotifiableIoItem item = new NotifiableIoItem()
  80. {
  81. Name = aiItem.Name,
  82. Index = aiItem.Index,
  83. Description = aiItem.Description,
  84. ShortValue = aiItem.Value,
  85. Address = aiItem.Addr,
  86. };
  87. AiItemList.Add(item);
  88. }
  89. }
  90. AoItemList = new ObservableCollection<NotifiableIoItem>();
  91. List<AOAccessor> aoItems = IO.GetAoList(source);
  92. if (aoItems != null)
  93. {
  94. foreach (var aoItem in aoItems)
  95. {
  96. NotifiableIoItem item = new NotifiableIoItem()
  97. {
  98. Name = aoItem.Name,
  99. Index = aoItem.Index,
  100. Description = aoItem.Description,
  101. ShortValue = aoItem.Value,
  102. Address = aoItem.Addr,
  103. CanEdit = SC.GetValue<bool>("System.IsOpenIOEdit"),
  104. };
  105. AoItemList.Add(item);
  106. }
  107. }
  108. }
  109. #endregion
  110. public void UpdateData()
  111. {
  112. bool canEdit = SC.GetValue<bool>("System.IsOpenIOEdit");
  113. foreach (var item in DiItemList)
  114. {
  115. item.BoolValue = IO.DI[item.Name].Value;
  116. item.InvokePropertyChanged("BoolValue");
  117. }
  118. foreach (var item in DoItemList)
  119. {
  120. item.BoolValue = IO.DO[item.Name].Value;
  121. item.InvokePropertyChanged("BoolValue");
  122. }
  123. foreach (var item in AiItemList)
  124. {
  125. item.ShortValue = IO.AI[item.Name].Value;
  126. item.InvokePropertyChanged("ShortValue");
  127. }
  128. foreach (var item in AoItemList)
  129. {
  130. item.ShortValue = IO.AO[item.Name].Value;
  131. item.InvokePropertyChanged("ShortValue");
  132. item.CanEdit = canEdit;
  133. item.InvokePropertyChanged("CanEdit");
  134. }
  135. }
  136. }
  137. public class IOMonitorViewModel : TimerViewModelBase
  138. {
  139. public IoValues[] IoList { get; set; }
  140. public IOMonitorViewModel():base(nameof(IOMonitorViewModel))
  141. {
  142. //IoList = new IoValues[2];
  143. //for (int i = 0; i < IoList.Length; i++)
  144. //{
  145. // IoList[i] = new IoValues("System.io"+(i+1));
  146. //}
  147. IoList = new IoValues[2];
  148. List<string> strs = new List<string>();
  149. strs.Add("PMA");
  150. strs.Add("PMB");
  151. for (int i = 0; i < IoList.Length; i++)
  152. {
  153. //IoList[i] = new IoValues("System.io"+(i+1));
  154. var t = new IoValues($"{strs[i]}.PLC");
  155. IoList[i] = t;
  156. }
  157. }
  158. protected override void Poll()
  159. {
  160. for (int i = 0; i < IoList.Length; i++)
  161. {
  162. IoList[i].UpdateData();
  163. }
  164. //string[] values = new string[128];
  165. //int i = 0;
  166. //foreach (var notifiableIoItem in Card1.DiItemList)
  167. //{
  168. // notifiableIoItem.BoolValue = IO.DI[notifiableIoItem.Name].Value;
  169. // notifiableIoItem.InvokePropertyChanged(nameof(notifiableIoItem.BoolValue));
  170. // values[i++] = notifiableIoItem.BoolValue ? "1" : "0";
  171. //}
  172. //System.Diagnostics.Trace.WriteLine(string.Join(",", values));
  173. }
  174. }
  175. }