PmIoViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 int MenuPermission;
  41. private bool _TwoColumnMode;
  42. public bool TwoColumnMode
  43. {
  44. get { return _TwoColumnMode; }
  45. set { _TwoColumnMode = value; NotifyOfPropertyChange("TwoColumnMode"); }
  46. }
  47. public string SystemName { get; set; }
  48. public ObservableCollection<IOItem<float>> AIs { get; private set; }
  49. public ObservableCollection<AOItem32> AOs { get; private set; }
  50. public ObservableCollection<IOItem<bool>> DIs { get; private set; }
  51. public ObservableCollection<IOItem<bool>> DOs { get; private set; }
  52. private Dictionary<string, IOItem<bool>> _diMap = new Dictionary<string, IOItem<bool>>();
  53. private Dictionary<string, IOItem<bool>> _doMap = new Dictionary<string, IOItem<bool>>();
  54. private Dictionary<string, IOItem<float>> _aiMap = new Dictionary<string, IOItem<float>>();
  55. private Dictionary<string, AOItem32> _aoMap = new Dictionary<string, AOItem32>();
  56. protected override void OnInitialize()
  57. {
  58. base.OnInitialize();
  59. TwoColumnMode = true;
  60. MenuPermission = ClientApp.Instance.GetPermission($"IO{SystemName}");
  61. BuildIoSchema();
  62. }
  63. protected override void OnActivate()
  64. {
  65. base.OnActivate();
  66. BaseApp.Instance.UIHandler.Register(SystemName + "IOView", GetIOs);
  67. }
  68. protected override void OnDeactivate(bool close)
  69. {
  70. base.OnDeactivate(close);
  71. BaseApp.Instance.UIHandler.UnRegister(SystemName + "IOView");
  72. }
  73. public void SetAO(AOItem32 aoItem)
  74. {
  75. if (MenuPermission != 3) return;
  76. InvokeClient.Instance.Service.DoOperation("System.SetAoValue32", aoItem.Name, aoItem.NewValue);
  77. }
  78. public void SetDO(IOItem<bool> doItem)
  79. {
  80. InvokeClient.Instance.Service.DoOperation("System.SetDoValue", doItem.Name, !doItem.Value);
  81. }
  82. public void BuildIoSchema()
  83. {
  84. if (DIs == null)
  85. {
  86. var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.DIItemList");
  87. if (itemList != null)
  88. {
  89. DIs = new ObservableCollection<IOItem<bool>>();
  90. foreach (var item in (List<NotifiableIoItem>)itemList)
  91. {
  92. var io = new IOItem<bool>()
  93. {
  94. Index = item.Index,
  95. Name = item.Name,
  96. Value = item.BoolValue,
  97. Address = item.Address
  98. };
  99. DIs.Add(io);
  100. _diMap.Add($"IO.{item.Name}", io);
  101. }
  102. }
  103. NotifyOfPropertyChange(nameof(DIs));
  104. }
  105. if (DOs == null)
  106. {
  107. var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.DOItemList");
  108. if (itemList != null)
  109. {
  110. DOs = new ObservableCollection<IOItem<bool>>();
  111. foreach (var item in (List<NotifiableIoItem>)itemList)
  112. {
  113. var io = new IOItem<bool>()
  114. {
  115. Index = item.Index,
  116. Name = item.Name,
  117. Value = item.BoolValue,
  118. Address = item.Address
  119. };
  120. DOs.Add(io);
  121. _doMap.Add($"IO.{item.Name}", io);
  122. }
  123. }
  124. NotifyOfPropertyChange(nameof(DOs));
  125. }
  126. if (AIs == null)
  127. {
  128. var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.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. NotifyOfPropertyChange(nameof(AIs));
  146. }
  147. if (AOs == null)
  148. {
  149. var itemList = QueryDataClient.Instance.Service.GetData($"{SystemName}.PLC.AOItemList");
  150. if (itemList != null)
  151. {
  152. AOs = new ObservableCollection<AOItem32>();
  153. foreach (var item in (List<NotifiableIoItem>)itemList)
  154. {
  155. var io = new AOItem32()
  156. {
  157. Index = item.Index,
  158. Name = item.Name,
  159. Value = item.ShortValue,
  160. Address = item.Address
  161. };
  162. AOs.Add(io);
  163. _aoMap.Add($"IO32.{item.Name}", io);
  164. }
  165. }
  166. NotifyOfPropertyChange(nameof(AOs));
  167. }
  168. }
  169. public void GetIOs()
  170. {
  171. var diValues = QueryDataClient.Instance.Service.PollData(_diMap.Keys);
  172. foreach (var item in _diMap)
  173. {
  174. if (diValues.ContainsKey(item.Key))
  175. _diMap[item.Key].Value = (bool)diValues[item.Key];
  176. }
  177. var doValues = QueryDataClient.Instance.Service.PollData(_doMap.Keys);
  178. foreach (var item in _doMap)
  179. {
  180. if (doValues.ContainsKey(item.Key))
  181. _doMap[item.Key].Value = (bool)doValues[item.Key];
  182. }
  183. var aiValues = QueryDataClient.Instance.Service.PollData(_aiMap.Keys);
  184. foreach (var item in _aiMap)
  185. {
  186. if (aiValues.ContainsKey(item.Key))
  187. _aiMap[item.Key].Value = (float)aiValues[item.Key];
  188. }
  189. var aoValues = QueryDataClient.Instance.Service.PollData(_aoMap.Keys);
  190. foreach (var item in _aoMap)
  191. {
  192. if (aoValues.ContainsKey(item.Key))
  193. _aoMap[item.Key].Value = (float)aoValues[item.Key];
  194. }
  195. }
  196. }
  197. }