FFUConfigViewModel.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Controls;
  10. using Aitex.Core.Common.DeviceData;
  11. using Aitex.Core.RT.IOCore;
  12. using Aitex.Core.RT.Log;
  13. using Aitex.Core.UI.DeviceControl;
  14. using Aitex.Core.Util;
  15. using Caliburn.Micro.Core;
  16. using FurnaceUI.Models;
  17. using FurnaceUI.Views.Recipes;
  18. using MECF.Framework.Common.CommonData.DeviceData;
  19. using MECF.Framework.Common.DataCenter;
  20. using MECF.Framework.Common.Device;
  21. using MECF.Framework.Common.OperationCenter;
  22. using OpenSEMI.ClientBase;
  23. namespace FurnaceUI.Views.Maintenances
  24. {
  25. public class FFUConfigViewModel : FurnaceUIViewModelBase
  26. {
  27. public bool SetField<T>(ref T field, T value, string propertyName)
  28. {
  29. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  30. field = value;
  31. NotifyOfPropertyChange(propertyName);
  32. return true;
  33. }
  34. [Subscription("System.FFUKeyDict")]
  35. public List<string> FFUKeyDict { get; set; }
  36. public bool IsPermission { get => this.Permission == 3; }
  37. private ObservableCollection<FFUData> _ffuDatas = new ObservableCollection<FFUData>();
  38. public ObservableCollection<FFUData> FFUDataList
  39. {
  40. get => _ffuDatas;
  41. set
  42. {
  43. _ffuDatas = value;
  44. NotifyOfPropertyChange(nameof(FFUDataList));
  45. }
  46. }
  47. private bool _powerOffVisibility = false;
  48. public bool PowerOffVisibility
  49. {
  50. get => _powerOffVisibility;
  51. set => SetField(ref _powerOffVisibility, value, nameof(PowerOffVisibility));
  52. }
  53. private bool _powerOnVisibility = true;
  54. public bool PowerOnVisibility
  55. {
  56. get => _powerOnVisibility;
  57. set => SetField(ref _powerOnVisibility, value, nameof(PowerOnVisibility));
  58. }
  59. Dictionary<string, FFUData> ffuDictionary = new Dictionary<string, FFUData>();
  60. protected override void OnActivate()
  61. {
  62. base.OnActivate();
  63. FFUKeyDict = QueryDataClient.Instance.Service.GetData("System.FFUKeyDict") as List<string>;
  64. FFUDataList.Clear();
  65. foreach (var item in FFUKeyDict)
  66. {
  67. var deviceName = item.Split('.').ToList()[1];
  68. FFUDataList.Add(new FFUData()
  69. {
  70. DisplayName = deviceName,
  71. });
  72. }
  73. ffuDictionary = FFUDataList.ToDictionary(a => a.DisplayName);
  74. }
  75. protected override void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  76. {
  77. base.InvokeAfterUpdateProperty(data);
  78. var values = QueryDataClient.Instance.Service.PollData(FFUKeyDict).Values.Select(a => (a as AITFFUData)).ToList();
  79. if (ffuDictionary != null)
  80. {
  81. foreach (var item in values)
  82. {
  83. if (item==null || !ffuDictionary.TryGetValue(item.DisplayName, out var ffu))
  84. continue;
  85. ffu.MaxValue = item.Max.ToString();
  86. ffu.MinValue = item.Min.ToString();
  87. ffu.ActualValue = item.Feedback;
  88. ffu.IsSwitch = item.IsSwitchOn;
  89. ffu.Value = item.SetPoint.ToString();
  90. ffu.LastSetValue = item.SetPoint;
  91. }
  92. }
  93. }
  94. async void DelayData(string type, object sender, object item)
  95. {
  96. await WaitForResultsAsync();
  97. if (!string.IsNullOrEmpty(type) && item != null && sender != null)
  98. {
  99. var dataItem = item as FFUData;
  100. string value = ((TextBox)sender).Text;
  101. var setValue = double.Parse(value);
  102. var max = double.Parse(dataItem.MaxValue);
  103. var min = double.Parse(dataItem.MinValue);
  104. if (setValue != dataItem.LastSetValue)
  105. {
  106. //if (setValue < min || setValue > max)
  107. //{
  108. // DialogBox.ShowWarning($"{dataItem.DisplayName} setValue={setValue}, limit is ({min}, {max})");
  109. // return;
  110. //}
  111. InvokeClient.Instance.Service.DoOperation($"PM1.{dataItem.DisplayName}.SetCurrectSpeed", value);
  112. }
  113. }
  114. }
  115. private async Task WaitForResultsAsync()
  116. {
  117. // Simulate waiting for results using a delay
  118. // In a real-world scenario, you might wait for an event or a specific condition
  119. await Task.Delay(10);
  120. // Here you can add logic to check if the results are ready
  121. // For example, polling or using a completion source
  122. }
  123. public void AllFFUPower(string value)
  124. {
  125. var setValue = bool.Parse(value);
  126. if (setValue)
  127. {
  128. PowerOnVisibility = false;
  129. PowerOffVisibility = true;
  130. }
  131. else
  132. {
  133. PowerOnVisibility = true;
  134. PowerOffVisibility = false;
  135. }
  136. InvokeClient.Instance.Service.DoOperation($"PM1.SetAllFFUPower", setValue);
  137. }
  138. public void SetValueTextChanged(string type, object sender, object item)
  139. {
  140. try
  141. {
  142. DelayData(type,sender,item);
  143. }
  144. catch (Exception ex)
  145. {
  146. LOG.Write(ex);
  147. }
  148. }
  149. }
  150. public class FFUData : PropertyChangedBase
  151. {
  152. private int _no;
  153. public int No
  154. {
  155. get => _no;
  156. set
  157. {
  158. _no = value;
  159. NotifyOfPropertyChange(nameof(No));
  160. }
  161. }
  162. private string _displayName;
  163. public string DisplayName
  164. {
  165. get => _displayName;
  166. set
  167. {
  168. _displayName = value;
  169. NotifyOfPropertyChange(nameof(DisplayName));
  170. }
  171. }
  172. private double _lastSetValue;
  173. public double LastSetValue
  174. {
  175. get => _lastSetValue;
  176. set
  177. {
  178. _lastSetValue = value;
  179. NotifyOfPropertyChange(nameof(LastSetValue));
  180. }
  181. }
  182. private double _actualValue;
  183. public double ActualValue
  184. {
  185. get => _actualValue;
  186. set
  187. {
  188. _actualValue = value;
  189. NotifyOfPropertyChange(nameof(ActualValue));
  190. }
  191. }
  192. private string _value;
  193. public string Value
  194. {
  195. get => _value;
  196. set
  197. {
  198. _value = value;
  199. NotifyOfPropertyChange(nameof(Value));
  200. }
  201. }
  202. private bool _isSwitch;
  203. public bool IsSwitch
  204. {
  205. get => _isSwitch;
  206. set
  207. {
  208. _isSwitch = value;
  209. NotifyOfPropertyChange(nameof(IsSwitch));
  210. }
  211. }
  212. private string _maxValue;
  213. public string MaxValue
  214. {
  215. get => _maxValue; set
  216. {
  217. _maxValue = value;
  218. NotifyOfPropertyChange(nameof(MaxValue));
  219. }
  220. }
  221. private string _minValue;
  222. public string MinValue
  223. {
  224. get => _minValue; set
  225. {
  226. _minValue = value;
  227. NotifyOfPropertyChange(nameof(MinValue));
  228. }
  229. }
  230. private bool _isSetChanged = true;
  231. public bool IsSetChanged
  232. {
  233. get => _isSetChanged;
  234. set
  235. {
  236. _isSetChanged = value;
  237. NotifyOfPropertyChange(nameof(IsSetChanged));
  238. }
  239. }
  240. }
  241. }