IO1ViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using Aitex.Core.RT.IOCore;
  5. using MECF.Framework.Common.DataCenter;
  6. using MECF.Framework.Common.IOCore;
  7. using MECF.Framework.Common.OperationCenter;
  8. using MECF.Framework.RT.Core.IoProviders;
  9. using MECF.Framework.UI.Client.ClientBase;
  10. using OpenSEMI.ClientBase;
  11. using OpenSEMI.ClientBase.IO;
  12. namespace MECF.Framework.UI.Client.CenterViews.Maitenances.IO1
  13. {
  14. public class IO1ViewModel : UiViewModelBase, ISupportMultipleSystem
  15. {
  16. public bool IsPermission { get => this.Permission == 3; }
  17. public string SystemName { get; set; }
  18. public Visibility DIVisibility
  19. {
  20. get { return DIs.Count > 0 ? Visibility.Visible : Visibility.Collapsed; }
  21. }
  22. public Visibility DOVisibility
  23. {
  24. get { return DOs.Count > 0 ? Visibility.Visible : Visibility.Collapsed; }
  25. }
  26. public Visibility AIVisibility
  27. {
  28. get { return AIs.Count > 0 ? Visibility.Visible : Visibility.Collapsed; }
  29. }
  30. public Visibility AOVisibility
  31. {
  32. get { return AOs.Count > 0 ? Visibility.Visible : Visibility.Collapsed; }
  33. }
  34. public int DIWidth
  35. {
  36. get { return (DIs.Count / 31 + 1) * 355; }
  37. }
  38. public int DOWidth
  39. {
  40. get { return (DOs.Count / 31 + 1) * 405; }
  41. }
  42. public int AIWidth
  43. {
  44. get { return (AIs.Count / 31 + 1) * 370; }
  45. }
  46. public int AOWidth
  47. {
  48. get { return (AOs.Count / 31 + 1) * 370; }
  49. }
  50. public ObservableCollection<IOItem<short>> AIs { get; private set; }
  51. public ObservableCollection<AOItem> AOs { get; private set; }
  52. public ObservableCollection<IOItem<bool>> DIs { get; private set; }
  53. public ObservableCollection<IOItem<bool>> DOs { get; private set; }
  54. private string _diKey;
  55. private string _doKey;
  56. private string _aiKey;
  57. private string _aoKey;
  58. protected override void OnInitialize()
  59. {
  60. base.OnInitialize();
  61. _diKey = $"{SystemName}.DIItemList";
  62. _doKey = $"{SystemName}.DOItemList";
  63. _aiKey = $"{SystemName}.AIItemList";
  64. _aoKey = $"{SystemName}.AOItemList";
  65. this.AIs = InitIOData<short>(IOType.AI, _aiKey);
  66. this.AOs = InitIOData(IOType.AO, _aoKey);
  67. this.DIs = InitIOData<bool>(IOType.DI, _diKey);
  68. this.DOs = InitIOData<bool>(IOType.DO, _doKey);
  69. _diKey = $"{SystemName}.DIList";
  70. _doKey = $"{SystemName}.DOList";
  71. _aiKey = $"{SystemName}.AIList";
  72. _aoKey = $"{SystemName}.AOList";
  73. Subscribe(_aiKey);
  74. Subscribe(_aoKey);
  75. Subscribe(_diKey);
  76. Subscribe(_doKey);
  77. }
  78. protected override void OnActivate()
  79. {
  80. base.OnActivate();
  81. }
  82. protected override void OnDeactivate(bool close)
  83. {
  84. base.OnDeactivate(close);
  85. }
  86. protected override void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  87. {
  88. base.InvokeAfterUpdateProperty(data);
  89. if (data[_aiKey] != null)
  90. {
  91. List<NotifiableIoItem> lstData = (List<NotifiableIoItem>)data[_aiKey];
  92. Dictionary<string, short> dicValues = new Dictionary<string, short>();
  93. for (int i = 0; i < lstData.Count; i++)
  94. {
  95. dicValues[lstData[i].Name] = lstData[i].ShortValue;
  96. }
  97. foreach (IOItem<short> item in AIs)
  98. {
  99. if (dicValues.ContainsKey(item.Name))
  100. item.Value = dicValues[item.Name];
  101. }
  102. }
  103. if (data[_aoKey] != null)
  104. {
  105. List<NotifiableIoItem> lstData = (List<NotifiableIoItem>)data[_aoKey];
  106. Dictionary<string, short> dicValues = new Dictionary<string, short>();
  107. for (int i = 0; i < lstData.Count; i++)
  108. {
  109. dicValues[lstData[i].Name] = lstData[i].ShortValue;
  110. }
  111. foreach (IOItem<short> item in AOs)
  112. {
  113. if (dicValues.ContainsKey(item.Name))
  114. item.Value = dicValues[item.Name];
  115. }
  116. }
  117. if (data[_diKey] != null)
  118. {
  119. List<NotifiableIoItem> lstData = (List<NotifiableIoItem>)data[_diKey];
  120. Dictionary<string, bool> dicValues = new Dictionary<string, bool>();
  121. for (int i = 0; i < lstData.Count; i++)
  122. {
  123. dicValues[lstData[i].Name] = lstData[i].BoolValue;
  124. }
  125. foreach (IOItem<bool> item in DIs)
  126. {
  127. if (dicValues.ContainsKey(item.Name))
  128. item.Value = dicValues[item.Name];
  129. }
  130. }
  131. if (data[_doKey] != null)
  132. {
  133. List<NotifiableIoItem> lstData = (List<NotifiableIoItem>)data[_doKey];
  134. Dictionary<string, bool> dicValues = new Dictionary<string, bool>();
  135. for (int i = 0; i < lstData.Count; i++)
  136. {
  137. dicValues[lstData[i].Name] = lstData[i].BoolValue;
  138. }
  139. foreach (IOItem<bool> item in DOs)
  140. {
  141. if (dicValues.ContainsKey(item.Name))
  142. item.Value = dicValues[item.Name];
  143. }
  144. }
  145. }
  146. public void SetDO(IOItem<bool> doItem)
  147. {
  148. if (MessageBox.Show(
  149. $"Please be attention, direct control DO is generally forbidden, Are you sure you want to do the operation?\r\n {doItem.Name} = {!doItem.Value}",
  150. "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes)
  151. return;
  152. InvokeClient.Instance.Service.DoOperation("System.SetDoValue", doItem.Name, !doItem.Value);
  153. }
  154. public void SetAO(AOItem aoItem)
  155. {
  156. if (MessageBox.Show(
  157. $"Please be attention, direct control AO is generally forbidden, Are you sure you want to do the operation?\r\n {aoItem.Name} = {aoItem.NewValue}",
  158. "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes)
  159. return;
  160. InvokeClient.Instance.Service.DoOperation("System.SetAoValue", aoItem.Name, aoItem.NewValue);
  161. aoItem.TextSaved = true;
  162. }
  163. public ObservableCollection<IOItem<T>> InitIOData<T>(IOType type, string dataName)
  164. {
  165. //get the whole informations
  166. ObservableCollection<IOItem<T>> da = new ObservableCollection<IOItem<T>>();
  167. if (type == IOType.DI)
  168. {
  169. var diList = QueryDataClient.Instance.Service.GetData(dataName);
  170. if (diList != null)
  171. {
  172. List<NotifiableIoItem> di = (List<NotifiableIoItem>)diList;
  173. for (int i = 0; i < di.Count; i++)
  174. {
  175. bool value = true;
  176. if (value is T)
  177. {
  178. da.Add(new IOItem<T>()
  179. {
  180. Index = di[i].Index,
  181. Name = di[i].Name ,
  182. DisplayName = di[i].Name.Substring(di[i].Name.IndexOf('.') + 1),
  183. Value = (T)(object)di[i].BoolValue,
  184. Address = di[i].Address
  185. });
  186. }
  187. }
  188. }
  189. }
  190. if (type == IOType.DO)
  191. {
  192. var diList = QueryDataClient.Instance.Service.GetData(dataName);
  193. if (diList != null)
  194. {
  195. List<NotifiableIoItem> item = (List<NotifiableIoItem>)diList;
  196. for (int i = 0; i < item.Count; i++)
  197. {
  198. bool value = true;
  199. if (value is T)
  200. {
  201. da.Add(new IOItem<T>()
  202. {
  203. Index = item[i].Index,
  204. Name = item[i].Name ,
  205. DisplayName = item[i].Name.Substring(item[i].Name.IndexOf('.') + 1),
  206. Value = (T)(object)item[i].BoolValue,
  207. Address = item[i].Address
  208. });
  209. }
  210. }
  211. }
  212. }
  213. if (type == IOType.AI)
  214. {
  215. var diList = QueryDataClient.Instance.Service.GetData(dataName);
  216. if (diList != null)
  217. {
  218. List<NotifiableIoItem> item = (List<NotifiableIoItem>)diList;
  219. for (int i = 0; i < item.Count; i++)
  220. {
  221. da.Add(new IOItem<T>()
  222. {
  223. Index = item[i].Index,
  224. Name = item[i].Name ,
  225. DisplayName = item[i].Name.Substring(item[i].Name.IndexOf('.') + 1),
  226. Value = (T)(object)item[i].ShortValue,
  227. Address = item[i].Address
  228. });
  229. }
  230. }
  231. }
  232. return da;
  233. }
  234. public ObservableCollection<AOItem> InitIOData(IOType type, string dataName)
  235. {
  236. //get the whole informations
  237. ObservableCollection<AOItem> da = new ObservableCollection<AOItem>();
  238. if (type == IOType.AO)
  239. {
  240. var diList = QueryDataClient.Instance.Service.GetData(dataName);
  241. if (diList != null)
  242. {
  243. List<NotifiableIoItem> item = (List<NotifiableIoItem>)diList;
  244. for (int i = 0; i < item.Count; i++)
  245. {
  246. {
  247. da.Add(new AOItem()
  248. {
  249. Index = item[i].Index,
  250. Name = item[i].Name ,
  251. DisplayName = item[i].Name.Substring(item[i].Name.IndexOf('.') + 1),
  252. Value = item[i].ShortValue,
  253. Address = item[i].Address
  254. });
  255. }
  256. }
  257. }
  258. }
  259. return da;
  260. }
  261. }
  262. }