PmIoViewModel.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using MECF.Framework.Common.DataCenter;
  5. using MECF.Framework.Common.IOCore;
  6. using MECF.Framework.Common.OperationCenter;
  7. using OpenSEMI.ClientBase;
  8. using OpenSEMI.ClientBase.IO;
  9. namespace VirgoUI.Client.Models.PMs
  10. {
  11. public class AOItem32 : IOItem<float>
  12. {
  13. public static readonly DependencyProperty NewValueProperty = DependencyProperty.Register(nameof(NewValue), typeof(float), typeof(AOItem32), new PropertyMetadata((object)(float)0));
  14. public static readonly DependencyProperty TextSavedProperty = DependencyProperty.Register(nameof(TextSaved), typeof(bool), typeof(AOItem32), new PropertyMetadata((object)true));
  15. public float NewValue
  16. {
  17. get
  18. {
  19. return (float)this.GetValue( NewValueProperty);
  20. }
  21. set
  22. {
  23. this.SetValue( NewValueProperty, (object)value);
  24. }
  25. }
  26. public bool TextSaved
  27. {
  28. get
  29. {
  30. return (bool)this.GetValue( TextSavedProperty);
  31. }
  32. set
  33. {
  34. this.SetValue( TextSavedProperty, (object)value);
  35. }
  36. }
  37. }
  38. public class PmIoViewModel : BaseModel, ISupportMultipleSystem
  39. {
  40. private bool _TwoColumnMode;
  41. public bool TwoColumnMode
  42. {
  43. get { return _TwoColumnMode; }
  44. set { _TwoColumnMode = value; NotifyOfPropertyChange("TwoColumnMode"); }
  45. }
  46. public string SystemName { get; set; }
  47. public ObservableCollection<IOItem<float>> AIs { get; private set; }
  48. public ObservableCollection<AOItem32> AOs { get; private set; }
  49. public ObservableCollection<IOItem<bool>> DIs { get; private set; }
  50. public ObservableCollection<IOItem<bool>> DOs { get; private set; }
  51. private Dictionary<string, IOItem<bool>> _diMap = new Dictionary<string, IOItem<bool>>();
  52. private Dictionary<string, IOItem<bool>> _doMap = new Dictionary<string, IOItem<bool>>();
  53. private Dictionary<string, IOItem<float>> _aiMap = new Dictionary<string, IOItem<float>>();
  54. private Dictionary<string, AOItem32> _aoMap = new Dictionary<string, AOItem32>();
  55. protected override void OnInitialize()
  56. {
  57. base.OnInitialize();
  58. TwoColumnMode = true;
  59. BuildIoSchema();
  60. }
  61. protected override void OnActivate()
  62. {
  63. base.OnActivate();
  64. BaseApp.Instance.UIHandler.Register(SystemName + "IOView", GetIOs);
  65. }
  66. protected override void OnDeactivate(bool close)
  67. {
  68. base.OnDeactivate(close);
  69. BaseApp.Instance.UIHandler.UnRegister(SystemName + "IOView");
  70. }
  71. public void SetAO(AOItem32 aoItem)
  72. {
  73. InvokeClient.Instance.Service.DoOperation("System.SetAoValue32", aoItem.Name, aoItem.NewValue);
  74. }
  75. public void SetDO(IOItem<bool> doItem)
  76. {
  77. InvokeClient.Instance.Service.DoOperation("System.SetDoValue", doItem.Name, !doItem.Value);
  78. }
  79. public void BuildIoSchema()
  80. {
  81. if (DIs == null)
  82. {
  83. var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.DIItemList");
  84. if (itemList != null)
  85. {
  86. DIs = new ObservableCollection<IOItem<bool>>();
  87. foreach (var item in (List<NotifiableIoItem>)itemList)
  88. {
  89. var io = new IOItem<bool>()
  90. {
  91. Index = item.Index,
  92. Name = item.Name,
  93. Value = item.BoolValue,
  94. Address = item.Address
  95. };
  96. DIs.Add(io);
  97. _diMap.Add($"IO.{item.Name}", io);
  98. }
  99. }
  100. NotifyOfPropertyChange(nameof(DIs));
  101. }
  102. if (DOs == null)
  103. {
  104. var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.DOItemList");
  105. if (itemList != null)
  106. {
  107. DOs = new ObservableCollection<IOItem<bool>>();
  108. foreach (var item in (List<NotifiableIoItem>)itemList)
  109. {
  110. var io = new IOItem<bool>()
  111. {
  112. Index = item.Index,
  113. Name = item.Name,
  114. Value = item.BoolValue,
  115. Address = item.Address
  116. };
  117. DOs.Add(io);
  118. _doMap.Add($"IO.{item.Name}", io);
  119. }
  120. }
  121. NotifyOfPropertyChange(nameof(DOs));
  122. }
  123. if (AIs == null)
  124. {
  125. var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.AIItemList");
  126. if (itemList != null)
  127. {
  128. AIs = new ObservableCollection<IOItem<float>>();
  129. foreach (var item in (List<NotifiableIoItem>)itemList)
  130. {
  131. var io = new IOItem<float>()
  132. {
  133. Index = item.Index,
  134. Name = item.Name,
  135. Value = item.ShortValue,
  136. Address = item.Address
  137. };
  138. AIs.Add(io);
  139. _aiMap.Add($"IO32.{item.Name}", io);
  140. }
  141. }
  142. NotifyOfPropertyChange(nameof(AIs));
  143. }
  144. if (AOs == null)
  145. {
  146. var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.AOItemList");
  147. if (itemList != null)
  148. {
  149. AOs = new ObservableCollection<AOItem32>();
  150. foreach (var item in (List<NotifiableIoItem>)itemList)
  151. {
  152. var io = new AOItem32()
  153. {
  154. Index = item.Index,
  155. Name = item.Name,
  156. Value = item.ShortValue,
  157. Address = item.Address
  158. };
  159. AOs.Add(io);
  160. _aoMap.Add($"IO32.{item.Name}", io);
  161. }
  162. }
  163. NotifyOfPropertyChange(nameof(AOs));
  164. }
  165. }
  166. public void GetIOs()
  167. {
  168. var diValues = QueryDataClient.Instance.Service.PollData(_diMap.Keys);
  169. foreach (var item in _diMap)
  170. {
  171. if (diValues.ContainsKey(item.Key))
  172. _diMap[item.Key].Value = (bool)diValues[item.Key];
  173. }
  174. var doValues = QueryDataClient.Instance.Service.PollData(_doMap.Keys);
  175. foreach (var item in _doMap)
  176. {
  177. if (doValues.ContainsKey(item.Key))
  178. _doMap[item.Key].Value = (bool)doValues[item.Key];
  179. }
  180. var aiValues = QueryDataClient.Instance.Service.PollData(_aiMap.Keys);
  181. foreach (var item in _aiMap)
  182. {
  183. if (aiValues.ContainsKey(item.Key))
  184. _aiMap[item.Key].Value = (float)aiValues[item.Key];
  185. }
  186. var aoValues = QueryDataClient.Instance.Service.PollData(_aoMap.Keys);
  187. foreach (var item in _aoMap)
  188. {
  189. if (aoValues.ContainsKey(item.Key))
  190. _aoMap[item.Key].Value = (float)aoValues[item.Key];
  191. }
  192. }
  193. }
  194. }