FFUConfigViewModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. RTName = deviceName,
  71. DisplayName = deviceName,
  72. });
  73. }
  74. ffuDictionary = FFUDataList.ToDictionary(a => a.RTName);
  75. }
  76. protected override void InvokeAfterUpdateProperty(Dictionary<string, object> data)
  77. {
  78. base.InvokeAfterUpdateProperty(data);
  79. var values = QueryDataClient.Instance.Service.PollData(FFUKeyDict).Values.Select(a => (a as AITFFUData)).ToList();
  80. if (ffuDictionary != null)
  81. {
  82. foreach (var item in values)
  83. {
  84. if (item==null || !ffuDictionary.TryGetValue(item.RTName, out var ffu))
  85. continue;
  86. ffu.DisplayName = item.DisplayName;
  87. ffu.MaxValue = item.Max.ToString();
  88. ffu.MinValue = item.Min.ToString();
  89. ffu.ActualValue = item.Feedback;
  90. ffu.RTName = item.RTName;
  91. ffu.IsSwitch = item.IsSwitchOn;
  92. ffu.Value = item.SetPoint.ToString();
  93. ffu.LastSetValue = item.SetPoint;
  94. }
  95. }
  96. }
  97. async void DelayData(string type, object sender, object item)
  98. {
  99. await WaitForResultsAsync();
  100. if (!string.IsNullOrEmpty(type) && item != null && sender != null)
  101. {
  102. var dataItem = item as FFUData;
  103. string value = ((TextBox)sender).Text;
  104. var setValue = double.Parse(value);
  105. var max = double.Parse(dataItem.MaxValue);
  106. var min = double.Parse(dataItem.MinValue);
  107. if (setValue != dataItem.LastSetValue)
  108. {
  109. //if (setValue < min || setValue > max)
  110. //{
  111. // DialogBox.ShowWarning($"{dataItem.DisplayName} setValue={setValue}, limit is ({min}, {max})");
  112. // return;
  113. //}
  114. InvokeClient.Instance.Service.DoOperation($"PM1.{dataItem.RTName}.SetCurrectSpeed", value);
  115. }
  116. }
  117. }
  118. private async Task WaitForResultsAsync()
  119. {
  120. // Simulate waiting for results using a delay
  121. // In a real-world scenario, you might wait for an event or a specific condition
  122. await Task.Delay(10);
  123. // Here you can add logic to check if the results are ready
  124. // For example, polling or using a completion source
  125. }
  126. public void AllFFUPower(string value)
  127. {
  128. var setValue = bool.Parse(value);
  129. if (setValue)
  130. {
  131. PowerOnVisibility = false;
  132. PowerOffVisibility = true;
  133. }
  134. else
  135. {
  136. PowerOnVisibility = true;
  137. PowerOffVisibility = false;
  138. }
  139. InvokeClient.Instance.Service.DoOperation($"PM1.SetAllFFUPower", setValue);
  140. }
  141. public void SetValueTextChanged(string type, object sender, object item)
  142. {
  143. try
  144. {
  145. DelayData(type,sender,item);
  146. }
  147. catch (Exception ex)
  148. {
  149. LOG.Write(ex);
  150. }
  151. }
  152. }
  153. public class FFUData : PropertyChangedBase
  154. {
  155. private int _no;
  156. public int No
  157. {
  158. get => _no;
  159. set
  160. {
  161. _no = value;
  162. NotifyOfPropertyChange(nameof(No));
  163. }
  164. }
  165. private string _rtName;
  166. public string RTName
  167. {
  168. get => _rtName;
  169. set
  170. {
  171. _rtName = value;
  172. NotifyOfPropertyChange(nameof(RTName));
  173. }
  174. }
  175. private string _displayName;
  176. public string DisplayName
  177. {
  178. get => _displayName;
  179. set
  180. {
  181. _displayName = value;
  182. NotifyOfPropertyChange(nameof(DisplayName));
  183. }
  184. }
  185. private double _lastSetValue;
  186. public double LastSetValue
  187. {
  188. get => _lastSetValue;
  189. set
  190. {
  191. _lastSetValue = value;
  192. NotifyOfPropertyChange(nameof(LastSetValue));
  193. }
  194. }
  195. private double _actualValue;
  196. public double ActualValue
  197. {
  198. get => _actualValue;
  199. set
  200. {
  201. _actualValue = value;
  202. NotifyOfPropertyChange(nameof(ActualValue));
  203. }
  204. }
  205. private string _value;
  206. public string Value
  207. {
  208. get => _value;
  209. set
  210. {
  211. _value = value;
  212. NotifyOfPropertyChange(nameof(Value));
  213. }
  214. }
  215. private bool _isSwitch;
  216. public bool IsSwitch
  217. {
  218. get => _isSwitch;
  219. set
  220. {
  221. _isSwitch = value;
  222. NotifyOfPropertyChange(nameof(IsSwitch));
  223. }
  224. }
  225. private string _maxValue;
  226. public string MaxValue
  227. {
  228. get => _maxValue; set
  229. {
  230. _maxValue = value;
  231. NotifyOfPropertyChange(nameof(MaxValue));
  232. }
  233. }
  234. private string _minValue;
  235. public string MinValue
  236. {
  237. get => _minValue; set
  238. {
  239. _minValue = value;
  240. NotifyOfPropertyChange(nameof(MinValue));
  241. }
  242. }
  243. private bool _isSetChanged = true;
  244. public bool IsSetChanged
  245. {
  246. get => _isSetChanged;
  247. set
  248. {
  249. _isSetChanged = value;
  250. NotifyOfPropertyChange(nameof(IsSetChanged));
  251. }
  252. }
  253. }
  254. }