IOViewModel.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using MECF.Framework.Common.DataCenter;
  2. using MECF.Framework.Common.IOCore;
  3. using MECF.Framework.Common.OperationCenter;
  4. using OpenSEMI.ClientBase.IO;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Threading;
  15. namespace CyberX8_MainPages.ViewModels
  16. {
  17. public class IOViewModel : BindableBase
  18. {
  19. #region 私有属性
  20. public string ModuleName;
  21. private Dictionary<string, IOItem<bool>> _diMap = new Dictionary<string, IOItem<bool>>();
  22. private Dictionary<string, IOItem<bool>> _doMap = new Dictionary<string, IOItem<bool>>();
  23. private Dictionary<string, IOItem<float>> _aiMap = new Dictionary<string, IOItem<float>>();
  24. private Dictionary<string, AOItem32> _aoMap = new Dictionary<string, AOItem32>();
  25. private ObservableCollection<IOItem<float>> m_AIs = new ObservableCollection<IOItem<float>>();
  26. private ObservableCollection<AOItem32> m_AOs = new ObservableCollection<AOItem32>();
  27. private ObservableCollection<IOItem<bool>> m_DIs = new ObservableCollection<IOItem<bool>>();
  28. private ObservableCollection<IOItem<bool>> m_DOs = new ObservableCollection<IOItem<bool>>();
  29. #endregion
  30. #region 属性
  31. public ObservableCollection<IOItem<float>> AIs
  32. {
  33. get { return m_AIs; }
  34. set { SetProperty(ref m_AIs, value); }
  35. }
  36. public ObservableCollection<AOItem32> AOs
  37. {
  38. get { return m_AOs; }
  39. set { SetProperty(ref m_AOs, value); }
  40. }
  41. public ObservableCollection<IOItem<bool>> DIs
  42. {
  43. get { return m_DIs; }
  44. set { SetProperty(ref m_DIs, value); }
  45. }
  46. public ObservableCollection<IOItem<bool>> DOs
  47. {
  48. get { return m_DOs; }
  49. set { SetProperty(ref m_DOs, value); }
  50. }
  51. #endregion
  52. #region 命令
  53. private DelegateCommand<IOItem<bool>> _SetDOCommand;
  54. public DelegateCommand<IOItem<bool>> SetDOCommand =>
  55. _SetDOCommand ?? (_SetDOCommand = new DelegateCommand<IOItem<bool>>(SetDO));
  56. private DelegateCommand<AOItem32> _SetAOCommand;
  57. public DelegateCommand<AOItem32> SetAOCommand =>
  58. _SetAOCommand ?? (_SetAOCommand = new DelegateCommand<AOItem32>(SetAO));
  59. //private DelegateCommand _SetDOCommand;
  60. //public DelegateCommand SetDOCommand =>
  61. // _SetDOCommand ?? (_SetDOCommand = new DelegateCommand(SetDO));
  62. #endregion
  63. public IOViewModel()
  64. {
  65. DispatcherTimer timer = new DispatcherTimer();
  66. timer.Interval = TimeSpan.FromSeconds(2);
  67. timer.Tick += timer_Tick;
  68. timer.Start();
  69. BuildIoSchema();
  70. }
  71. void timer_Tick(object sender, EventArgs e)
  72. {
  73. GetIOs();
  74. }
  75. #region 私有方法
  76. public void SetAO(AOItem32 aoItem)
  77. {
  78. InvokeClient.Instance.Service.DoOperation("System.SetAoValue32", aoItem.Name, aoItem.NewValue);
  79. }
  80. private void SetDO(IOItem<bool> doItem)
  81. {
  82. InvokeClient.Instance.Service.DoOperation("System.SetDoValue", doItem.Name, !doItem.Value);
  83. }
  84. public void BuildIoSchema()
  85. {
  86. if (DIs.Count == 0)
  87. {
  88. var itemList = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PLC.DIItemList");
  89. if (itemList != null)
  90. {
  91. DIs = new ObservableCollection<IOItem<bool>>();
  92. foreach (var item in (List<NotifiableIoItem>)itemList)
  93. {
  94. var io = new IOItem<bool>()
  95. {
  96. Index = item.Index,
  97. Name = item.Name,
  98. Value = item.BoolValue,
  99. Address = item.Address
  100. };
  101. DIs.Add(io);
  102. _diMap.Add($"IO.{item.Name}", io);
  103. }
  104. }
  105. }
  106. if (DOs.Count == 0)
  107. {
  108. var itemList = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PLC.DOItemList");
  109. if (itemList != null)
  110. {
  111. DOs = new ObservableCollection<IOItem<bool>>();
  112. foreach (var item in (List<NotifiableIoItem>)itemList)
  113. {
  114. var io = new IOItem<bool>()
  115. {
  116. Index = item.Index,
  117. Name = item.Name,
  118. Value = item.BoolValue,
  119. Address = item.Address
  120. };
  121. DOs.Add(io);
  122. _doMap.Add($"IO.{item.Name}", io);
  123. }
  124. }
  125. }
  126. if (AIs.Count == 0)
  127. {
  128. var itemList = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PLC.AIItemList");
  129. if (itemList != null)
  130. {
  131. AIs = new ObservableCollection<IOItem<float>>();
  132. foreach (var item in (List<NotifiableIoItem>)itemList)
  133. {
  134. var io = new IOItem<float>()
  135. {
  136. Index = item.Index,
  137. Name = item.Name,
  138. Value = item.ShortValue,
  139. Address = item.Address
  140. };
  141. AIs.Add(io);
  142. _aiMap.Add($"IO32.{item.Name}", io);
  143. }
  144. }
  145. }
  146. if (AOs.Count == 0)
  147. {
  148. var itemList = QueryDataClient.Instance.Service.GetData($"{ModuleName}.PLC.AOItemList");
  149. if (itemList != null)
  150. {
  151. AOs = new ObservableCollection<AOItem32>();
  152. foreach (var item in (List<NotifiableIoItem>)itemList)
  153. {
  154. var io = new AOItem32()
  155. {
  156. Index = item.Index,
  157. Name = item.Name,
  158. Value = item.ShortValue,
  159. Address = item.Address
  160. };
  161. AOs.Add(io);
  162. _aoMap.Add($"IO32.{item.Name}", io);
  163. }
  164. }
  165. }
  166. }
  167. public void GetIOs()
  168. {
  169. var diValues = QueryDataClient.Instance.Service.PollData(_diMap.Keys);
  170. foreach (var item in _diMap)
  171. {
  172. if (diValues.ContainsKey(item.Key))
  173. _diMap[item.Key].Value = (bool)diValues[item.Key];
  174. }
  175. var doValues = QueryDataClient.Instance.Service.PollData(_doMap.Keys);
  176. foreach (var item in _doMap)
  177. {
  178. if (doValues.ContainsKey(item.Key))
  179. _doMap[item.Key].Value = (bool)doValues[item.Key];
  180. }
  181. var aiValues = QueryDataClient.Instance.Service.PollData(_aiMap.Keys);
  182. foreach (var item in _aiMap)
  183. {
  184. if (aiValues.ContainsKey(item.Key))
  185. _aiMap[item.Key].Value = (float)aiValues[item.Key];
  186. }
  187. var aoValues = QueryDataClient.Instance.Service.PollData(_aoMap.Keys);
  188. foreach (var item in _aoMap)
  189. {
  190. if (aoValues.ContainsKey(item.Key))
  191. _aoMap[item.Key].Value = (float)aoValues[item.Key];
  192. }
  193. }
  194. #endregion
  195. }
  196. public class AOItem32 : IOItem<double>
  197. {
  198. public static readonly DependencyProperty NewValueProperty = DependencyProperty.Register(nameof(NewValue), typeof(double), typeof(AOItem32), new PropertyMetadata((double)0));
  199. public static readonly DependencyProperty TextSavedProperty = DependencyProperty.Register(nameof(TextSaved), typeof(bool), typeof(AOItem32), new PropertyMetadata(true));
  200. public double NewValue
  201. {
  202. get
  203. {
  204. return (double)this.GetValue(NewValueProperty);
  205. }
  206. set
  207. {
  208. this.SetValue(NewValueProperty, (object)value);
  209. }
  210. }
  211. public bool TextSaved
  212. {
  213. get
  214. {
  215. return (bool)this.GetValue(TextSavedProperty);
  216. }
  217. set
  218. {
  219. this.SetValue(TextSavedProperty, (object)value);
  220. }
  221. }
  222. }
  223. }