TraceDataListViewModel.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Dm.util;
  2. using Microsoft.Win32;
  3. using ProximaAnalizer.Helpers;
  4. using ScottPlot;
  5. using SqlSugar;
  6. using System.Collections.ObjectModel;
  7. using System.IO;
  8. using System.Reflection.Metadata.Ecma335;
  9. using System.Security.Claims;
  10. using System.Text;
  11. using System.Windows;
  12. using System.Windows.Controls.Ribbon;
  13. namespace ProximaAnalizer.ViewModels.Dialog;
  14. internal partial class TraceDataListViewModel : ObservableObject, IDialogAware
  15. {
  16. public DialogCloseListener RequestClose { get; set; }
  17. public string Title { get; set; } = "DataLog";
  18. [ObservableProperty]
  19. private ObservableDictionary<DateTime, DynamicDisplay> _DynamicDisplay = [];
  20. [ObservableProperty]
  21. private ObservableCollection<string> _Headers = [];
  22. public bool CanCloseDialog()
  23. {
  24. return true;
  25. }
  26. public void OnDialogClosed()
  27. {
  28. }
  29. public void OnDialogOpened(IDialogParameters parameters)
  30. {
  31. if (!parameters.TryGetValue("Data", out object? trace))
  32. return;
  33. if (trace is not DisplayDataHelper traceData)
  34. return;
  35. Dictionary<string, List<float>> cache = [];
  36. cache = cache.Concat(traceData.DataLeft).ToDictionary();
  37. if (traceData.DataRight is not null)
  38. cache = cache.Concat(traceData.DataRight).ToDictionary();
  39. this.Headers.Clear();
  40. HashSet<string> headers = new();
  41. for (int i = 0; i < traceData.Time.Count; i++)
  42. {
  43. DynamicDisplay dynamicDisplay = new();
  44. long ticks = traceData.Time[i].Ticks;
  45. ticks -= ticks % 10000000;
  46. dynamicDisplay.Time = new(ticks);
  47. this.DynamicDisplay[dynamicDisplay.Time] = dynamicDisplay;
  48. foreach (var item in cache)
  49. {
  50. PairData pairData = new()
  51. {
  52. Name = item.Key,
  53. Value = item.Value[i].ToString("0.00"),
  54. };
  55. headers.Add(item.Key);
  56. dynamicDisplay.Data.Add(pairData);
  57. }
  58. }
  59. Headers.AddRange(headers);
  60. if (!parameters.TryGetValue("Alarm", out object? rawAlarm))
  61. return;
  62. if (rawAlarm is not IEnumerable<EventData> alarms)
  63. return;
  64. foreach (EventData eventData in alarms)
  65. {
  66. if (string.IsNullOrEmpty(eventData.Description))
  67. continue;
  68. long ticks = eventData.Occur_Time.Ticks;
  69. ticks -= ticks % 10000000;
  70. if (!this.DynamicDisplay.TryGetValue(new(ticks), out DynamicDisplay? display) || display is null)
  71. continue;
  72. display.Alarm.Add(eventData.Description);
  73. }
  74. }
  75. [RelayCommand]
  76. private void Export()
  77. {
  78. SaveFileDialog saveFileDialog = new()
  79. {
  80. Filter = "History files |*.csv;",
  81. FileName = "DataLog.csv",
  82. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
  83. };
  84. if (saveFileDialog.ShowDialog() != true)
  85. return;
  86. try
  87. {
  88. using StreamWriter sw = new(saveFileDialog.FileName);
  89. StringBuilder header = new();
  90. header.append("Time");
  91. header.Append(',');
  92. foreach (var item in Headers)
  93. {
  94. header.append(item);
  95. header.Append(',');
  96. }
  97. header.append("Alarm");
  98. sw.WriteLine(header);
  99. foreach (var item in this.DynamicDisplay)
  100. {
  101. StringBuilder sb = new();
  102. sb.Append(item.Value.Time.ToString("yyyy-MM-dd HH:mm:ss.f"));
  103. sb.Append(',');
  104. foreach (var data in item.Value.Data)
  105. {
  106. sb.append(data.Value);
  107. sb.Append(',');
  108. }
  109. StringBuilder sbAlarm = new();
  110. for (int i = 0; i < item.Value.Alarm.Count; i++)
  111. {
  112. sbAlarm.Append($"{item.Value.Alarm[i].Replace("°C", string.Empty)}");
  113. if (i != item.Value.Alarm.Count - 1)
  114. sbAlarm.Append(Environment.NewLine);
  115. }
  116. if (item.Value.Alarm.Count != 0)
  117. sb.append($"\"{sbAlarm}\"");
  118. sw.WriteLine(sb);
  119. }
  120. MessageBoxResult result = MessageBox.Show("文件保存成功,是否查看文件?", "Save History File", MessageBoxButton.YesNo, MessageBoxImage.Question);
  121. FileInfo fileInfo = new(saveFileDialog.FileName);
  122. if (result == MessageBoxResult.Yes && fileInfo.Directory is not null)
  123. System.Diagnostics.Process.Start("Explorer.exe", fileInfo.Directory.FullName);
  124. }
  125. catch
  126. {
  127. MessageBox.Show("文件保存失败!", "Save History File", MessageBoxButton.OK, MessageBoxImage.Error);
  128. }
  129. }
  130. }
  131. public class DynamicDisplay
  132. {
  133. public DateTime Time { get; set; }
  134. public List<PairData> Data { get; } = [];
  135. public List<string> Alarm { get; } = [];
  136. }
  137. public class PairData
  138. {
  139. public string? Name { get; set; }
  140. public string? Value { get; set; }
  141. }