BeckhoffIOViewModel.cs 8.8 KB

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