BinSettingViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using ivf_tl_Entity.Entity;
  3. using ivf_tl_Entity.Entity.Alarm;
  4. using ivf_tl_Entity.Entity.balance;
  5. using ivf_tl_Manage.Converts;
  6. using ivf_tl_Manage.Views;
  7. using ivf_tl_Service.HttpProvider;
  8. using OxyPlot;
  9. using OxyPlot.Axes;
  10. using OxyPlot.Series;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Timers;
  19. namespace ivf_tl_Manage.ViewModels
  20. {
  21. public partial class BinSettingViewModel : BaseViewModel
  22. {
  23. public ObservableCollection<TLInfo> TlInfoList { get; set; } = new ObservableCollection<TLInfo>();
  24. TLInfoProvider _tLInfoProvider = null;
  25. public AppData Appdata { get; set; }
  26. System.Threading.Timer _timer;
  27. public List<int> HouseList { get; set; } = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  28. [ObservableProperty]
  29. private TLInfo selectTL;
  30. [ObservableProperty]
  31. private string selectHouse;
  32. public BinSettingViewModel(string tlsn)
  33. {
  34. base.ViewModelName = nameof(BinSettingViewModel);
  35. Init();
  36. Appdata = AppData.Instance;
  37. _tLInfoProvider = Appdata.GetTLInfoProvider();
  38. TlInfoList = new ObservableCollection<TLInfo>(AppData.Instance.MainWindowViewModel.devManageViewModel.TlInfoList);
  39. if (!string.IsNullOrEmpty(tlsn)) SelectTL = TlInfoList.FirstOrDefault(x => x.tlSn == tlsn);
  40. //StartTimer();
  41. }
  42. public void StartTimer()
  43. {
  44. if (_timer != null) _timer.Dispose();
  45. TemperatureRealDataPoints.Clear();
  46. PressureRealDataPoints.Clear();
  47. DateTime nowTime1 = DateTime.Now;
  48. DateTime nowTime = nowTime1.AddMinutes(-1);
  49. if (TemperatureRealModel.Axes[1] is DateTimeAxis dateTimeAxis)
  50. {
  51. dateTimeAxis.Minimum = DateTimeAxis.ToDouble(nowTime);
  52. dateTimeAxis.Maximum = DateTimeAxis.ToDouble(nowTime1);
  53. }
  54. if (PressureRealModel.Axes[1] is DateTimeAxis dateTimeAxis1)
  55. {
  56. dateTimeAxis1.Minimum = DateTimeAxis.ToDouble(nowTime);
  57. dateTimeAxis1.Maximum = DateTimeAxis.ToDouble(nowTime1);
  58. }
  59. TemperatureRealModel.ResetAllAxes();
  60. TemperatureRealModel.InvalidatePlot(true);
  61. PressureRealModel.ResetAllAxes();
  62. PressureRealModel.InvalidatePlot(true);
  63. if (SelectTL == null || string.IsNullOrEmpty(selectHouse)) return;
  64. if (SelectTL.online != 1) return;
  65. _timer = new System.Threading.Timer(new TimerCallback(TimerProc));
  66. _timer.Change(2000, 2000);
  67. }
  68. private void TimerProc(object? state) // 入参对象为 Timer 对象
  69. {
  70. if (SelectTL == null || string.IsNullOrEmpty(selectHouse)) return;
  71. TLInfo currentTLInfo = TlInfoList.First(a => a.id == SelectTL.id);
  72. if (currentTLInfo == null) return;
  73. House currentHouse = currentTLInfo.houses.First(a => a.houseSn.ToString() == selectHouse);
  74. if (currentHouse == null) return;
  75. UpdateTemperatureRealModel(DateTime.Now, (double)currentHouse.temperature);
  76. UpdatePressureRealModel(DateTime.Now, (double)currentHouse.pressure);
  77. //System.Threading.Timer t = (System.Threading.Timer)state;
  78. //t.Dispose(); // 调用一次就释放掉,或者添加条件释放
  79. //Console.WriteLine("The timer callback executes.");
  80. }
  81. public void SetTlSn(string tlSn, string HouseSn, string startTime, string endTime)
  82. {
  83. string houseSn = HouseSn;
  84. Dictionary<DateTime, double> TemperatureHistoryData = new Dictionary<DateTime, double>();
  85. Dictionary<DateTime, double> PreHistoryData = new Dictionary<DateTime, double>();
  86. var startTime1 = DateTime.Parse(startTime);
  87. var endTime1 = DateTime.Parse(endTime);
  88. var binSettings = _tLInfoProvider.GetHouseEnvironmentListApi(tlSn, houseSn, startTime, endTime);
  89. if(binSettings.temperatureList!=null && binSettings.temperatureList.Any())
  90. {
  91. foreach (var item in binSettings.temperatureList)
  92. {
  93. TemperatureHistoryData.Add(item.createTime, (double)item.temperature);
  94. }
  95. UpdateTemperatureHistoryModel(TemperatureHistoryData, startTime1, endTime1);
  96. }
  97. else
  98. {
  99. UpdateTemperatureHistoryModelX(DateTime.Parse(startTime), DateTime.Parse(endTime));
  100. }
  101. if (binSettings.pressureList != null && binSettings.pressureList.Any())
  102. {
  103. foreach (var item in binSettings.pressureList)
  104. {
  105. PreHistoryData.Add(item.createTime, (double)item.pressure);
  106. }
  107. UpdatePressureHistoryModel(PreHistoryData, startTime1, endTime1);
  108. }
  109. else
  110. {
  111. UpdatePressureHistoryModelX(DateTime.Parse(startTime), DateTime.Parse(endTime));
  112. }
  113. }
  114. public List<SettingEntity> GetSettingEntities(string tlSn, string HouseSn, string startTime, string endTime)
  115. {
  116. return _tLInfoProvider.GetBinSetting(tlSn, HouseSn, startTime, endTime);
  117. }
  118. public void DisposeTimer()
  119. {
  120. if (_timer != null)
  121. {
  122. _timer.Dispose();
  123. }
  124. }
  125. /// <summary>
  126. /// 气压历史曲线模型
  127. /// </summary>
  128. [ObservableProperty]
  129. private PlotModel pressureHistoryModel;
  130. /// <summary>
  131. /// 气压历史曲线数据
  132. /// </summary>
  133. [ObservableProperty]
  134. private ObservableCollection<DataPoint> pressureHistoryDataPoints = new ObservableCollection<DataPoint>();
  135. /// <summary>
  136. /// 温度历史曲线模型
  137. /// </summary>
  138. [ObservableProperty]
  139. private PlotModel temperatureHistoryModel;
  140. /// <summary>
  141. /// 温度历史曲线模型
  142. /// </summary>
  143. [ObservableProperty]
  144. private ObservableCollection<DataPoint> temperatureHistoryDataPoints = new ObservableCollection<DataPoint>();
  145. /// <summary>
  146. /// 气压实时曲线模型
  147. /// </summary>
  148. [ObservableProperty]
  149. private PlotModel pressureRealModel;
  150. /// <summary>
  151. /// 气压实时曲线数据
  152. /// </summary>
  153. [ObservableProperty]
  154. private ObservableCollection<DataPoint> pressureRealDataPoints = new ObservableCollection<DataPoint>();
  155. /// <summary>
  156. /// 温度实时曲线模型
  157. /// </summary>
  158. [ObservableProperty]
  159. private PlotModel temperatureRealModel;
  160. /// <summary>
  161. /// 温度实时曲线数据
  162. /// </summary>
  163. [ObservableProperty]
  164. private ObservableCollection<DataPoint> temperatureRealDataPoints = new ObservableCollection<DataPoint>();
  165. public void UpdateHistoryModelX(DateTime minTime, DateTime maxTime)
  166. {
  167. if (!(TemperatureHistoryModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis)) return;
  168. temperatureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(minTime);
  169. temperatureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(maxTime);
  170. TemperatureHistoryModel.ResetAllAxes();
  171. TemperatureHistoryModel.InvalidatePlot(true);
  172. if (!(PressureHistoryModel.Axes[1] is DateTimeAxis pressureDateTimeAxis)) return;
  173. pressureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(minTime);
  174. pressureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(maxTime);
  175. PressureHistoryModel.ResetAllAxes();
  176. PressureHistoryModel.InvalidatePlot(true);
  177. }
  178. public void UpdateTemperatureHistoryModelX(DateTime minTime, DateTime maxTime)
  179. {
  180. TemperatureHistoryDataPoints.Clear();
  181. if (!(TemperatureHistoryModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis)) return;
  182. temperatureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(minTime);
  183. temperatureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(maxTime);
  184. TemperatureHistoryModel.ResetAllAxes();
  185. TemperatureHistoryModel.InvalidatePlot(true);
  186. }
  187. public void UpdatePressureHistoryModelX(DateTime minTime, DateTime maxTime)
  188. {
  189. PressureHistoryDataPoints.Clear();
  190. if (!(PressureHistoryModel.Axes[1] is DateTimeAxis pressureDateTimeAxis)) return;
  191. pressureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(minTime);
  192. pressureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(maxTime);
  193. PressureHistoryModel.ResetAllAxes();
  194. PressureHistoryModel.InvalidatePlot(true);
  195. }
  196. /// <summary>
  197. /// 刷新温度历史曲线模型
  198. /// </summary>
  199. public void UpdateTemperatureHistoryModel(Dictionary<DateTime, double> TemperatureHistoryData,DateTime startTime,DateTime endTime)
  200. {
  201. TemperatureHistoryDataPoints.Clear();
  202. foreach (var item in TemperatureHistoryData) TemperatureHistoryDataPoints.Add(OxyPlot.Axes.DateTimeAxis.CreateDataPoint(item.Key, item.Value));
  203. if (TemperatureHistoryModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis)
  204. {
  205. temperatureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(startTime);
  206. temperatureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(endTime);
  207. }
  208. TemperatureHistoryModel.ResetAllAxes();
  209. TemperatureHistoryModel.InvalidatePlot(true);
  210. }
  211. /// <summary>
  212. /// 刷新气压历史曲线模型
  213. /// </summary>
  214. public void UpdatePressureHistoryModel(Dictionary<DateTime, double> PreHistoryData, DateTime startTime, DateTime endTime)
  215. {
  216. PressureHistoryDataPoints.Clear();
  217. foreach (var item in PreHistoryData) PressureHistoryDataPoints.Add(DateTimeAxis.CreateDataPoint(item.Key, item.Value));
  218. if (PressureHistoryModel.Axes[1] is DateTimeAxis pressureDateTimeAxis)
  219. {
  220. pressureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(startTime);
  221. pressureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(endTime);
  222. }
  223. PressureHistoryModel.ResetAllAxes();
  224. PressureHistoryModel.InvalidatePlot(true);
  225. }
  226. /// <summary>
  227. /// 刷新气压实时数据
  228. /// </summary>
  229. /// <param name="dateTime"></param>
  230. /// <param name="newValue"></param>
  231. public void UpdatePressureRealModel(DateTime dateTime, double newValue)
  232. {
  233. PressureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(dateTime, newValue));
  234. int dataCount = PressureRealDataPoints.Count;
  235. if (dataCount > 3000) PressureRealDataPoints.RemoveAt(0);
  236. if (PressureRealModel.Axes[1] is DateTimeAxis dateTimeAxis)
  237. {
  238. dateTimeAxis.Minimum = PressureRealDataPoints.First().X;
  239. dateTimeAxis.Maximum = PressureRealDataPoints.Last().X;
  240. }
  241. if (dataCount > 1)
  242. {
  243. PressureRealModel.ResetAllAxes();
  244. PressureRealModel.InvalidatePlot(true);
  245. }
  246. //if (newValue >= 35 && newValue <= 55)
  247. //{
  248. //}
  249. }
  250. /// <summary>
  251. /// 刷新温度实时数据
  252. /// </summary>
  253. /// <param name="dateTime"></param>
  254. /// <param name="newValue"></param>
  255. public void UpdateTemperatureRealModel(DateTime dateTime, double newValue)
  256. {
  257. if (newValue >= 35 && newValue <= 38)
  258. {
  259. TemperatureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(dateTime, newValue));
  260. int dataCount = TemperatureRealDataPoints.Count;
  261. if (dataCount > 3000) TemperatureRealDataPoints.RemoveAt(0);
  262. if (TemperatureRealModel.Axes[1] is DateTimeAxis dateTimeAxis)
  263. {
  264. dateTimeAxis.Minimum = TemperatureRealDataPoints.First().X;
  265. dateTimeAxis.Maximum = TemperatureRealDataPoints.Last().X;
  266. }
  267. if(dataCount > 1)
  268. {
  269. TemperatureRealModel.ResetAllAxes();
  270. TemperatureRealModel.InvalidatePlot(true);
  271. }
  272. }
  273. }
  274. /// <summary>
  275. /// 设置曲线样式
  276. /// </summary>
  277. private void Init()
  278. {
  279. double preMaximum = 60.00;
  280. string trackString = "{2:MM-dd HH:mm}";
  281. DateTime nowTime1 = DateTime.Now;
  282. DateTime nowTime = nowTime1.AddHours(-1);
  283. TemperatureHistoryModel = new PlotModel() { PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 1), PlotAreaBorderColor = OxyColor.FromRgb(210, 219, 234) };
  284. TemperatureRealModel = new PlotModel() { PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 1), PlotAreaBorderColor = OxyColor.FromRgb(210, 219, 234) };
  285. PressureHistoryModel = new PlotModel() { PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 1), PlotAreaBorderColor = OxyColor.FromRgb(210, 219, 234) };
  286. PressureRealModel = new PlotModel() { PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 1), PlotAreaBorderColor = OxyColor.FromRgb(210, 219, 234) };
  287. //TemperatureHistoryModel.Series.Add(new LineSeries { Title = "温度", ItemsSource = TemperatureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(169, 201, 238), StrokeThickness = 5 });
  288. TemperatureHistoryModel.Series.Add(new LineSeries { Title = KeyToStringConvert.GetLanguageStringByKey("0348"), ItemsSource = TemperatureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(169, 201, 238), StrokeThickness = 5 });
  289. PressureRealModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(210, 219, 234), TickStyle = TickStyle.None, Minimum = 0, Maximum = preMaximum, MinimumMajorStep = 1, MajorStep = 5 });
  290. PressureHistoryModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(210, 219, 234), TickStyle = TickStyle.None, Minimum = 0, Maximum = preMaximum, MinimumMajorStep = 1, MajorStep = 5 });
  291. TemperatureHistoryModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(210, 219, 234), TickStyle = TickStyle.None, Minimum = 35, Maximum = 38, MinimumMajorStep = 0.01, MajorStep = 0.5 });
  292. //PressureRealModel.Series.Add(new LineSeries { Title = "气压", ItemsSource = PressureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(157, 119, 238), StrokeThickness = 5 });
  293. PressureRealModel.Series.Add(new LineSeries { Title = KeyToStringConvert.GetLanguageStringByKey("0349"), ItemsSource = PressureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(157, 119, 238), StrokeThickness = 5 });
  294. TemperatureRealModel.Axes.Add(new LinearAxis() { Position = AxisPosition.Left, MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(210, 219, 234), TickStyle = TickStyle.None, Minimum = 35, Maximum = 38, MinimumMajorStep = 0.01, MajorStep = 0.5 });
  295. PressureHistoryModel.Axes.Add(new DateTimeAxis() { Position = AxisPosition.Bottom, StringFormat = "MM-dd\nHH:mm", MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(210, 219, 234), MinorGridlineStyle = LineStyle.None, TickStyle = TickStyle.None, MinorIntervalType = DateTimeIntervalType.Hours, IntervalType = DateTimeIntervalType.Hours });
  296. //TemperatureRealModel.Series.Add(new LineSeries { Title = "温度", ItemsSource = TemperatureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(106, 170, 245), StrokeThickness = 5 });
  297. TemperatureRealModel.Series.Add(new LineSeries { Title = KeyToStringConvert.GetLanguageStringByKey("0348"), ItemsSource = TemperatureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(106, 170, 245), StrokeThickness = 5 });
  298. TemperatureHistoryModel.Axes.Add(new DateTimeAxis() { Position = AxisPosition.Bottom, StringFormat = "MM-dd\nHH:mm", MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(210, 219, 234), MinorGridlineStyle = LineStyle.None, TickStyle = TickStyle.None, MinorIntervalType = DateTimeIntervalType.Hours, IntervalType = DateTimeIntervalType.Hours });
  299. //PressureHistoryModel.Series.Add(new LineSeries { Title = "气压", ItemsSource = PressureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(197, 175, 244), StrokeThickness = 5 });
  300. PressureHistoryModel.Series.Add(new LineSeries { Title = KeyToStringConvert.GetLanguageStringByKey("0349"), ItemsSource = PressureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(197, 175, 244), StrokeThickness = 5 });
  301. TemperatureRealModel.Axes.Add(new DateTimeAxis() { Position = AxisPosition.Bottom, StringFormat = "HH:mm", MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(210, 219, 234), MinorGridlineStyle = LineStyle.None, TickStyle = TickStyle.None, MinorIntervalType = DateTimeIntervalType.Hours, IntervalType = DateTimeIntervalType.Hours, Minimum = DateTimeAxis.ToDouble(nowTime), Maximum = DateTimeAxis.ToDouble(nowTime1) });
  302. PressureRealModel.Axes.Add(new DateTimeAxis() { Position = AxisPosition.Bottom, StringFormat = "HH:mm", MajorGridlineStyle = LineStyle.Solid, MajorGridlineColor = OxyColor.FromRgb(210, 219, 234), MinorGridlineStyle = LineStyle.None, TickStyle = TickStyle.None, MinorIntervalType = DateTimeIntervalType.Hours, IntervalType = DateTimeIntervalType.Hours,Minimum= DateTimeAxis.ToDouble(nowTime), Maximum = DateTimeAxis.ToDouble(nowTime1) });
  303. }
  304. }
  305. }