ChartPageViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using ivf_tl_Operate.Converts;
  3. using OxyPlot;
  4. using OxyPlot.Axes;
  5. using OxyPlot.Legends;
  6. using OxyPlot.Series;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace ivf_tl_Operate.ViewModel
  15. {
  16. public partial class ChartPageViewModel : ObservableObject
  17. {
  18. public MainPageViewModel mainPageViewModel { get; set; }
  19. public bool isClose = false;
  20. public ChartPageViewModel(int newHouseId, MainPageViewModel _vm)
  21. {
  22. CurrentHouseId = newHouseId;
  23. mainPageViewModel = _vm;
  24. Init();
  25. Task.Run(() =>
  26. {
  27. decimal temprature = 0;
  28. decimal pressure = 0;
  29. double minT = 0;
  30. double minP = 0;
  31. double maxT = 0;
  32. double maxP = 0;
  33. bool dataAny = false;
  34. while (true)
  35. {
  36. if (isClose) return;
  37. temprature = 0;
  38. pressure = 0;
  39. switch (CurrentHouseId)
  40. {
  41. case 1:
  42. temprature = mainPageViewModel.ExHouse1.temperature;
  43. pressure = mainPageViewModel.ExHouse1.pressure;
  44. break;
  45. case 2:
  46. temprature = mainPageViewModel.ExHouse2.temperature;
  47. pressure = mainPageViewModel.ExHouse2.pressure;
  48. break;
  49. case 3:
  50. temprature = mainPageViewModel.ExHouse3.temperature;
  51. pressure = mainPageViewModel.ExHouse3.pressure;
  52. break;
  53. case 4:
  54. temprature = mainPageViewModel.ExHouse4.temperature;
  55. pressure = mainPageViewModel.ExHouse4.pressure;
  56. break;
  57. case 5:
  58. temprature = mainPageViewModel.ExHouse5.temperature;
  59. pressure = mainPageViewModel.ExHouse5.pressure;
  60. break;
  61. case 6:
  62. temprature = mainPageViewModel.ExHouse6.temperature;
  63. pressure = mainPageViewModel.ExHouse6.pressure;
  64. break;
  65. case 7:
  66. temprature = mainPageViewModel.ExHouse7.temperature;
  67. pressure = mainPageViewModel.ExHouse7.pressure;
  68. break;
  69. case 8:
  70. temprature = mainPageViewModel.ExHouse8.temperature;
  71. pressure = mainPageViewModel.ExHouse8.pressure;
  72. break;
  73. case 9:
  74. temprature = mainPageViewModel.ExHouse9.temperature;
  75. pressure = mainPageViewModel.ExHouse9.pressure;
  76. break;
  77. case 10:
  78. temprature = mainPageViewModel.ExHouse10.temperature;
  79. pressure = mainPageViewModel.ExHouse10.pressure;
  80. break;
  81. default:
  82. break;
  83. }
  84. if (temprature >= 35 && temprature <= 38)
  85. {
  86. TemperatureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(DateTime.Now, (double)temprature));
  87. }
  88. PressureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(DateTime.Now, (double)pressure));
  89. //if (pressure >= 35 && pressure <= 55)
  90. //{
  91. // PressureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(DateTime.Now, (double)pressure));
  92. //}
  93. if (TemperatureRealDataPoints.Count > 3000)
  94. {
  95. TemperatureRealDataPoints.RemoveAt(0);
  96. PressureRealDataPoints.RemoveAt(0);
  97. }
  98. if (RealModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis)
  99. {
  100. if (TemperatureRealDataPoints.Any())
  101. {
  102. minT = TemperatureRealDataPoints.First().X;
  103. maxT = TemperatureRealDataPoints.Last().X;
  104. }
  105. if (PressureRealDataPoints.Any())
  106. {
  107. minP = PressureRealDataPoints.First().X;
  108. maxP = PressureRealDataPoints.Last().X;
  109. }
  110. if (minT != 0 && minP != 0)
  111. {
  112. temperatureDateTimeAxis.Minimum = minT < minP ? minT : minP;
  113. }
  114. else if (minT != 0 && minP == 0)
  115. {
  116. temperatureDateTimeAxis.Minimum = minT;
  117. }
  118. else if (minT == 0 && minP != 0)
  119. {
  120. temperatureDateTimeAxis.Minimum = minP;
  121. }
  122. if (maxT != 0 && maxP != 0)
  123. {
  124. temperatureDateTimeAxis.Maximum = maxT > maxP ? maxT : maxP;
  125. }
  126. else if (maxT != 0 && maxP == 0)
  127. {
  128. temperatureDateTimeAxis.Maximum = maxT;
  129. }
  130. else if (maxT == 0 && maxP != 0)
  131. {
  132. temperatureDateTimeAxis.Maximum = maxP;
  133. }
  134. }
  135. RealModel.ResetAllAxes();
  136. RealModel.InvalidatePlot(true);
  137. Thread.Sleep(1000);
  138. }
  139. });
  140. }
  141. public int CurrentHouseId { get; set; }
  142. public string tlSn { get; set; }
  143. [ObservableProperty]
  144. private string leftDateString;
  145. [ObservableProperty]
  146. private string rightDateString;
  147. private DateTime leftDate;
  148. public DateTime LeftDate
  149. {
  150. get { return leftDate; }
  151. set { leftDate = value; LeftDateString = $"{leftDate.Year}-{leftDate.Month.ToString().PadLeft(2, '0')}-{leftDate.Day.ToString().PadLeft(2, '0')}"; }
  152. }
  153. private DateTime rightDate;
  154. public DateTime RightDate
  155. {
  156. get { return rightDate; }
  157. set { rightDate = value; RightDateString = $"{rightDate.Year}-{rightDate.Month.ToString().PadLeft(2, '0')}-{rightDate.Day.ToString().PadLeft(2, '0')}"; }
  158. }
  159. [ObservableProperty]
  160. private string historyTitle = "";//1号舱室温压历史曲线
  161. [ObservableProperty]
  162. private string realTitle = "";//1号舱室温压实时曲线
  163. /// <summary>
  164. /// 历史曲线模型
  165. /// </summary>
  166. [ObservableProperty]
  167. private PlotModel historyModel;
  168. /// <summary>
  169. /// 气压历史曲线数据
  170. /// </summary>
  171. [ObservableProperty]
  172. private ObservableCollection<DataPoint> pressureHistoryDataPoints = new ObservableCollection<DataPoint>();
  173. /// <summary>
  174. /// 温度历史曲线数据
  175. /// </summary>
  176. [ObservableProperty]
  177. private ObservableCollection<DataPoint> temperatureHistoryDataPoints = new ObservableCollection<DataPoint>();
  178. /// <summary>
  179. /// 实时曲线模型
  180. /// </summary>
  181. [ObservableProperty]
  182. private PlotModel realModel;
  183. /// <summary>
  184. /// 气压实时曲线数据
  185. /// </summary>
  186. [ObservableProperty]
  187. private ObservableCollection<DataPoint> pressureRealDataPoints = new ObservableCollection<DataPoint>();
  188. /// <summary>
  189. /// 温度实时曲线数据
  190. /// </summary>
  191. [ObservableProperty]
  192. private ObservableCollection<DataPoint> temperatureRealDataPoints = new ObservableCollection<DataPoint>();
  193. public void RefHistroy()
  194. {
  195. var a = AppData.Instance.HttpHelper.GetHouseEnvironmentListApi(tlSn, CurrentHouseId, LeftDate.ToString("yyyy-MM-dd HH:mm:ss"), RightDate.ToString("yyyy-MM-dd HH:mm:ss"));
  196. TemperatureHistoryDataPoints.Clear();
  197. if (a.temperatureList != null) foreach (var item in a.temperatureList) TemperatureHistoryDataPoints.Add(OxyPlot.Axes.DateTimeAxis.CreateDataPoint(item.createTime, (double)item.temperature));
  198. PressureHistoryDataPoints.Clear();
  199. if (a.pressureList != null) foreach (var item in a.pressureList) PressureHistoryDataPoints.Add(DateTimeAxis.CreateDataPoint(item.createTime, item.pressure));
  200. if (HistoryModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis)
  201. {
  202. temperatureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(LeftDate);
  203. temperatureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(RightDate);
  204. }
  205. HistoryModel.ResetAllAxes();
  206. HistoryModel.InvalidatePlot(true);
  207. }
  208. private void Init()
  209. {
  210. string wendu = KeyToStringConvert.GetLanguageStringByKey("C0073");
  211. string shij = KeyToStringConvert.GetLanguageStringByKey("C0284");
  212. string qiya = KeyToStringConvert.GetLanguageStringByKey("C0285");
  213. string yali= KeyToStringConvert.GetLanguageStringByKey("C0074");
  214. double StrokeThicknessD = 3;
  215. HistoryModel = new PlotModel();
  216. RealModel = new PlotModel();
  217. string TrackerFormatString = shij + ":{2:MM-dd HH:mm}\n" + wendu + ":{4:0.00}℃";
  218. string TrackerFormatString1 = shij + ":{2:MM-dd HH:mm}\n" + yali + ":{4:0.00}mbar";
  219. HistoryModel.Series.Add(new LineSeries { Title = wendu, ItemsSource = TemperatureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = TrackerFormatString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(169, 201, 238), StrokeThickness = StrokeThicknessD });
  220. RealModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, Minimum = 0, Maximum = 60 });
  221. HistoryModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, Minimum = 0, Maximum = 60, IsPanEnabled = true, IsZoomEnabled = true });
  222. RealModel.Series.Add(new LineSeries { Title = wendu, ItemsSource = TemperatureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = TrackerFormatString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(106, 170, 245), StrokeThickness = StrokeThicknessD });
  223. HistoryModel.Axes.Add(new DateTimeAxis() { Position = AxisPosition.Bottom, StringFormat = "MM-dd\nHH:mm" });
  224. RealModel.Series.Add(new LineSeries { Title = qiya, ItemsSource = PressureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = TrackerFormatString1, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(157, 119, 238), StrokeThickness = StrokeThicknessD });
  225. HistoryModel.Series.Add(new LineSeries { Title = qiya, ItemsSource = PressureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = TrackerFormatString1, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(197, 175, 244), StrokeThickness = StrokeThicknessD });
  226. RealModel.Axes.Add(new DateTimeAxis() { Position = AxisPosition.Bottom, StringFormat = "HH:mm", MinorIntervalType = DateTimeIntervalType.Seconds, IntervalType = DateTimeIntervalType.Seconds });//, MinimumMajorStep = 0.01, MajorStep = 0.5
  227. RealModel.Legends.Add(new Legend() { LegendPosition = LegendPosition.BottomCenter, LegendOrientation = LegendOrientation.Horizontal, LegendFontSize = 25, LegendPlacement = LegendPlacement.Outside, LegendSymbolLength = 60 });
  228. HistoryModel.Legends.Add(new Legend() { LegendPosition = LegendPosition.BottomCenter, LegendOrientation = LegendOrientation.Horizontal, LegendFontSize = 25, LegendPlacement = LegendPlacement.Outside, LegendSymbolLength = 60 });
  229. //HistoryModel.LegendPosition = LegendPosition.BottomCenter;
  230. //HistoryModel.LegendOrientation = LegendOrientation.Horizontal;
  231. //HistoryModel.LegendFontSize = 25;
  232. //HistoryModel.LegendPlacement = LegendPlacement.Outside;
  233. ////RealModel.LegendItemSpacing = 100;
  234. //HistoryModel.LegendSymbolLength = 60;
  235. }
  236. }
  237. }