CalibrationTableViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using MECF.Framework.Common.CommonData;
  2. using MECF.Framework.Common.DataCenter;
  3. using MECF.Framework.Common.OperationCenter;
  4. using OpenSEMI.ClientBase;
  5. using SciChart.Core.Extensions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using VirgoUI.Client.Models.Sys;
  14. namespace VirgoUI.Client.Models.PMs
  15. {
  16. public interface ICalibrationTableViewModelParameter
  17. {
  18. List<CalibrationTableItem> Items { get; set; }
  19. }
  20. public class CalibrationTableItem
  21. {
  22. public string DisplayName { get; set; }
  23. public string ItemTableScName { get; set; }
  24. public string ItemEnableScName { get; set; }
  25. }
  26. public class CalibrationTableViewModel : UiViewModelBase, ISupportMultipleSystem
  27. {
  28. public ICalibrationTableViewModelParameter CustomParameter { get; set; }
  29. public string SystemName { get; set; }
  30. public class NotifiableCalibrationTableItem : NotifiableItem
  31. {
  32. public string DisplayName { get; set; }
  33. public float FeedbackValue { get; set; }
  34. public float CalibrationValue { get; set; }
  35. }
  36. public ObservableCollection<CalibrationTableItem> CalibrationItems { get; set; }
  37. public ObservableCollection<NotifiableCalibrationTableItem> TableData { get; set; }
  38. public string FeedbackValue { get; set; }
  39. public string CalibrationValue { get; set; }
  40. private CalibrationTableItem _currentSelection;
  41. public CalibrationTableItem CurrentSelection
  42. {
  43. get { return _currentSelection; }
  44. set
  45. {
  46. _currentSelection = value;
  47. ChangeSelection(_currentSelection);
  48. NotifyOfPropertyChange(nameof(CurrentSelection));
  49. }
  50. }
  51. public CalibrationTableViewModel()
  52. {
  53. DisplayName = "Calibration Table ";
  54. CalibrationItems = new ObservableCollection<CalibrationTableItem>();
  55. TableData = new ObservableCollection<NotifiableCalibrationTableItem>();
  56. }
  57. protected override void OnInitialize()
  58. {
  59. base.OnInitialize();
  60. }
  61. protected override void OnActivate()
  62. {
  63. base.OnActivate();
  64. if (CustomParameter != null && CustomParameter.Items != null && CalibrationItems.IsEmpty())
  65. {
  66. foreach (var item in CustomParameter.Items)
  67. {
  68. CalibrationItems.Add(new CalibrationTableItem()
  69. {
  70. DisplayName = item.DisplayName,
  71. ItemEnableScName = item.ItemEnableScName,
  72. ItemTableScName = item.ItemTableScName,
  73. });
  74. }
  75. }
  76. }
  77. protected void ChangeSelection(CalibrationTableItem item)
  78. {
  79. if (item == null)
  80. {
  81. TableData.Clear();
  82. return;
  83. }
  84. var tableValues = QueryDataClient.Instance.Service.GetConfig(item.ItemTableScName);
  85. if (tableValues == null)
  86. return;
  87. var scValue = (string)tableValues;
  88. string[] items = scValue.Split(';');
  89. var tableData = new ObservableCollection<NotifiableCalibrationTableItem>();
  90. for (int i = 0; i < items.Length; i++)
  91. {
  92. if (items.Length > i)
  93. {
  94. string itemValue = items[i];
  95. if (!string.IsNullOrEmpty(itemValue))
  96. {
  97. string[] pairValue = itemValue.Split('#');
  98. if (pairValue.Length == 2)
  99. {
  100. if (float.TryParse(pairValue[0], out float rangeItem1)
  101. && float.TryParse(pairValue[1], out float rangeItem2))
  102. {
  103. tableData.Add(new NotifiableCalibrationTableItem()
  104. {
  105. FeedbackValue = rangeItem1,
  106. CalibrationValue = rangeItem2,
  107. });
  108. }
  109. }
  110. }
  111. }
  112. }
  113. TableData = new ObservableCollection<NotifiableCalibrationTableItem>(tableData.OrderBy(x => x.FeedbackValue).ToList());
  114. NotifyOfPropertyChange(nameof(TableData));
  115. }
  116. public void Save()
  117. {
  118. if (CurrentSelection == null)
  119. return;
  120. string data = "";
  121. foreach (var item in TableData)
  122. {
  123. data += $"{item.FeedbackValue}#{item.CalibrationValue};";
  124. }
  125. InvokeClient.Instance.Service.DoOperation("System.SetConfig", CurrentSelection.ItemTableScName, data);
  126. Reload();
  127. }
  128. public void Cancel()
  129. {
  130. Reload();
  131. }
  132. private void Reload()
  133. {
  134. ChangeSelection(CurrentSelection);
  135. }
  136. public void Add()
  137. {
  138. if (string.IsNullOrEmpty(FeedbackValue) || string.IsNullOrEmpty(CalibrationValue))
  139. {
  140. MessageBox.Show("Input value is empty");
  141. return;
  142. }
  143. if (!float.TryParse(FeedbackValue, out float feedback) ||
  144. !float.TryParse(CalibrationValue, out float calibrationValue))
  145. {
  146. MessageBox.Show("Input value not valid");
  147. return;
  148. }
  149. FeedbackValue = "";
  150. CalibrationValue = "";
  151. NotifyOfPropertyChange(nameof(FeedbackValue));
  152. NotifyOfPropertyChange(nameof(CalibrationValue));
  153. TableData.Add(new NotifiableCalibrationTableItem()
  154. {
  155. DisplayName = CurrentSelection.DisplayName,
  156. FeedbackValue = feedback,
  157. CalibrationValue = calibrationValue,
  158. });
  159. }
  160. public void DeleteItem(NotifiableCalibrationTableItem item)
  161. {
  162. TableData.Remove(item);
  163. }
  164. }
  165. }