CalibrationTableViewModel.cs 6.4 KB

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