DamagesPage.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Panuon.WPF.UI;
  2. using SWRIS.Core;
  3. using SWRIS.Dtos;
  4. using SWRIS.Extensions;
  5. using SWRIS.Models;
  6. using SWRIS.Models.ViewModel;
  7. using SWRIS.Repository;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Controls.Primitives;
  13. using System.Windows.Input;
  14. namespace SWRIS.Pages
  15. {
  16. /// <summary>
  17. /// DamagesPage.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class DamagesPage : Page
  20. {
  21. private readonly string[] _colors = new string[] { "#70a1d7", "#a1de93", "#f7f48b", "#f47c7c", "#c264fe", "#00fff5", "#f8b195", "#7a08fa" };
  22. private readonly IRecordRepository recordRepository;
  23. public DamagesViewModel DamagesView { get; set; }
  24. public DamagesPage()
  25. {
  26. InitializeComponent();
  27. DamagesView = new DamagesViewModel
  28. {
  29. Record = new RecordDto(),
  30. Sensors = new ObservableCollection<SensorModel>()
  31. };
  32. recordRepository = new RecordRepository();
  33. }
  34. private void Page_Loaded(object sender, RoutedEventArgs e)
  35. {
  36. if (NavigationService != null)
  37. {
  38. var query = System.Web.HttpUtility.ParseQueryString(NavigationService.CurrentSource.OriginalString.Split('?')[1]);
  39. var record = recordRepository.GetRecord(int.Parse(query["id"]));
  40. if (record != null && record.DataFilePath != null)
  41. {
  42. DamagesView.Record = record;
  43. var inUseSensors = record.InUseSensors?.Split(',').Select(s => int.Parse(s.Trim())).ToArray();
  44. foreach (var sensorNo in inUseSensors)
  45. {
  46. var sensor = new SensorModel
  47. {
  48. Id = sensorNo,
  49. Name = $"传感器{sensorNo}",
  50. Color = _colors[sensorNo - 1],
  51. IsActive = false
  52. };
  53. sensor.PropertyChanged += (s, arg) =>
  54. {
  55. if (arg.PropertyName == nameof(SensorModel.IsActive))
  56. {
  57. if (s is SensorModel sensor1 && sensor1 != null && sensor1.IsActive)
  58. {
  59. // 如果有传感器被选中,取消合值的选中状态
  60. DamagesView.IsSummaryActive = false;
  61. }
  62. }
  63. };
  64. DamagesView.Sensors.Add(sensor);
  65. }
  66. var damageData = DatFileHandler.ReadDatFile(record.DataFilePath);
  67. if (damageData?.Length > 0)
  68. {
  69. var parsedData = ParseSensorData(damageData, record.SensorCount, record.StartPoint, record.EndPoint);
  70. chartLine.AddDataPoints(parsedData, inUseSensors, record.Damages.ToList());
  71. }
  72. }
  73. }
  74. DataContext = DamagesView;
  75. }
  76. private void Delete_MouseDown(object sender, MouseButtonEventArgs e)
  77. {
  78. if ((sender as Image)?.Tag is int id)
  79. {
  80. var result = MessageBoxX.Show(Application.Current.MainWindow, "此操作不可恢复,是否确认删除损伤记录?", "警告", MessageBoxButton.YesNo, MessageBoxIcon.Warning, DefaultButton.YesOK);
  81. if (result == MessageBoxResult.Yes)
  82. {
  83. if (recordRepository.DeleteDamage(id))
  84. {
  85. DamagesView.Record.Damages.Remove(DamagesView.Record.Damages.FirstOrDefault(d => d.Id == id));
  86. recordRepository.UpdateRecordDamageCount(DamagesView.Record.Id.Value, DamagesView.Record.Damages.Count());
  87. NoticeBox.Show("损伤数据删除成功", "通知", MessageBoxIcon.Success, false, 3000);
  88. }
  89. }
  90. }
  91. }
  92. private void GoBack_Click(object sender, RoutedEventArgs e)
  93. {
  94. NavigationService?.GoBack();
  95. }
  96. public (double[] Positions, ushort[,] Damages) ParseSensorData(byte[] data, int sensorCount, double startPosition, double endPosition)
  97. {
  98. // 检查数据长度是否能被传感器数量整除
  99. if (data.Length % sensorCount != 0)
  100. {
  101. LogHelper.Error($"数据长度不匹配。数据长度 {data.Length} 字节不能被传感器数量 {sensorCount} 整除。");
  102. return (null, null);
  103. }
  104. // 计算样本数量
  105. int sampleCount = data.Length / sensorCount;
  106. // 计算采样步长
  107. double samplingStep = (endPosition - startPosition) / (sampleCount - 1);
  108. // 初始化结果列表
  109. (double[] Positions, ushort[,] Damages) sensorData = (new double[sampleCount], new ushort[sensorCount, sampleCount]);
  110. // 解析数据
  111. for (int sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++)
  112. {
  113. sensorData.Positions[sampleIndex] = startPosition;
  114. for (int sensorIndex = 0; sensorIndex < sensorCount; sensorIndex++)
  115. {
  116. // 计算数据在字节数组中的位置
  117. int dataIndex = (sampleIndex * sensorCount) + sensorIndex;
  118. // 将 byte 转换为 ushort(直接赋值,因为 byte 可以隐式转换为 ushort)
  119. sensorData.Damages[sensorIndex, sampleIndex] = data[dataIndex];
  120. }
  121. startPosition += samplingStep;
  122. }
  123. return sensorData;
  124. }
  125. private void Sensor_Checked(object sender, RoutedEventArgs e)
  126. {
  127. if (sender is ToggleButton toggleButton && int.TryParse(toggleButton.Tag?.ToString(), out int id))
  128. {
  129. chartLine?.SignalPlotVisible(id, true);
  130. }
  131. }
  132. private void Sensor_UnChecked(object sender, RoutedEventArgs e)
  133. {
  134. if (sender is ToggleButton toggleButton && int.TryParse(toggleButton.Tag?.ToString(), out int id))
  135. {
  136. chartLine?.SignalPlotVisible(id, false);
  137. }
  138. }
  139. }
  140. }