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