IO3ViewModel.cs 11 KB

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