IO2ViewModel.cs 11 KB

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