using CommunityToolkit.Mvvm.ComponentModel; using ivf_tl_Entity.Entity; using ivf_tl_Entity.Entity.Alarm; using ivf_tl_Entity.Entity.balance; using ivf_tl_Manage.Converts; using ivf_tl_Manage.Views; using ivf_tl_Service.HttpProvider; using OxyPlot; using OxyPlot.Axes; using OxyPlot.Series; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; namespace ivf_tl_Manage.ViewModels { public partial class BinSettingViewModel : BaseViewModel { public ObservableCollection TlInfoList { get; set; } = new ObservableCollection(); TLInfoProvider _tLInfoProvider = null; public AppData Appdata { get; set; } System.Threading.Timer _timer; public List HouseList { get; set; } = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; [ObservableProperty] private TLInfo selectTL; [ObservableProperty] private string selectHouse; public BinSettingViewModel(string tlsn) { base.ViewModelName = nameof(BinSettingViewModel); Init(); Appdata = AppData.Instance; _tLInfoProvider = Appdata.GetTLInfoProvider(); TlInfoList = new ObservableCollection(AppData.Instance.MainWindowViewModel.devManageViewModel.TlInfoList); if (!string.IsNullOrEmpty(tlsn)) SelectTL = TlInfoList.FirstOrDefault(x => x.tlSn == tlsn); //StartTimer(); } public void StartTimer() { if (_timer != null) _timer.Dispose(); TemperatureRealDataPoints.Clear(); PressureRealDataPoints.Clear(); DateTime nowTime1 = DateTime.Now; DateTime nowTime = nowTime1.AddMinutes(-1); if (TemperatureRealModel.Axes[1] is DateTimeAxis dateTimeAxis) { dateTimeAxis.Minimum = DateTimeAxis.ToDouble(nowTime); dateTimeAxis.Maximum = DateTimeAxis.ToDouble(nowTime1); } if (PressureRealModel.Axes[1] is DateTimeAxis dateTimeAxis1) { dateTimeAxis1.Minimum = DateTimeAxis.ToDouble(nowTime); dateTimeAxis1.Maximum = DateTimeAxis.ToDouble(nowTime1); } TemperatureRealModel.ResetAllAxes(); TemperatureRealModel.InvalidatePlot(true); PressureRealModel.ResetAllAxes(); PressureRealModel.InvalidatePlot(true); if (SelectTL == null || string.IsNullOrEmpty(selectHouse)) return; if (SelectTL.online != 1) return; _timer = new System.Threading.Timer(new TimerCallback(TimerProc)); _timer.Change(2000, 2000); } private void TimerProc(object? state) // 入参对象为 Timer 对象 { if (SelectTL == null || string.IsNullOrEmpty(selectHouse)) return; TLInfo currentTLInfo = TlInfoList.First(a => a.id == SelectTL.id); if (currentTLInfo == null) return; House currentHouse = currentTLInfo.houses.First(a => a.houseSn.ToString() == selectHouse); if (currentHouse == null) return; UpdateTemperatureRealModel(DateTime.Now, (double)currentHouse.temperature); UpdatePressureRealModel(DateTime.Now, (double)currentHouse.pressure); //System.Threading.Timer t = (System.Threading.Timer)state; //t.Dispose(); // 调用一次就释放掉,或者添加条件释放 //Console.WriteLine("The timer callback executes."); } public void SetTlSn(string tlSn, string HouseSn, string startTime, string endTime) { string houseSn = HouseSn; Dictionary TemperatureHistoryData = new Dictionary(); Dictionary PreHistoryData = new Dictionary(); var startTime1 = DateTime.Parse(startTime); var endTime1 = DateTime.Parse(endTime); var binSettings = _tLInfoProvider.GetHouseEnvironmentListApi(tlSn, houseSn, startTime, endTime); if(binSettings.temperatureList!=null && binSettings.temperatureList.Any()) { foreach (var item in binSettings.temperatureList) { TemperatureHistoryData.Add(item.createTime, (double)item.temperature); } UpdateTemperatureHistoryModel(TemperatureHistoryData, startTime1, endTime1); } else { UpdateTemperatureHistoryModelX(DateTime.Parse(startTime), DateTime.Parse(endTime)); } if (binSettings.pressureList != null && binSettings.pressureList.Any()) { foreach (var item in binSettings.pressureList) { PreHistoryData.Add(item.createTime, (double)item.pressure); } UpdatePressureHistoryModel(PreHistoryData, startTime1, endTime1); } else { UpdatePressureHistoryModelX(DateTime.Parse(startTime), DateTime.Parse(endTime)); } } public List GetSettingEntities(string tlSn, string HouseSn, string startTime, string endTime) { return _tLInfoProvider.GetBinSetting(tlSn, HouseSn, startTime, endTime); } public void DisposeTimer() { if (_timer != null) { _timer.Dispose(); } } /// /// 气压历史曲线模型 /// [ObservableProperty] private PlotModel pressureHistoryModel; /// /// 气压历史曲线数据 /// [ObservableProperty] private ObservableCollection pressureHistoryDataPoints = new ObservableCollection(); /// /// 温度历史曲线模型 /// [ObservableProperty] private PlotModel temperatureHistoryModel; /// /// 温度历史曲线模型 /// [ObservableProperty] private ObservableCollection temperatureHistoryDataPoints = new ObservableCollection(); /// /// 气压实时曲线模型 /// [ObservableProperty] private PlotModel pressureRealModel; /// /// 气压实时曲线数据 /// [ObservableProperty] private ObservableCollection pressureRealDataPoints = new ObservableCollection(); /// /// 温度实时曲线模型 /// [ObservableProperty] private PlotModel temperatureRealModel; /// /// 温度实时曲线数据 /// [ObservableProperty] private ObservableCollection temperatureRealDataPoints = new ObservableCollection(); public void UpdateHistoryModelX(DateTime minTime, DateTime maxTime) { if (!(TemperatureHistoryModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis)) return; temperatureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(minTime); temperatureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(maxTime); TemperatureHistoryModel.ResetAllAxes(); TemperatureHistoryModel.InvalidatePlot(true); if (!(PressureHistoryModel.Axes[1] is DateTimeAxis pressureDateTimeAxis)) return; pressureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(minTime); pressureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(maxTime); PressureHistoryModel.ResetAllAxes(); PressureHistoryModel.InvalidatePlot(true); } public void UpdateTemperatureHistoryModelX(DateTime minTime, DateTime maxTime) { TemperatureHistoryDataPoints.Clear(); if (!(TemperatureHistoryModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis)) return; temperatureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(minTime); temperatureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(maxTime); TemperatureHistoryModel.ResetAllAxes(); TemperatureHistoryModel.InvalidatePlot(true); } public void UpdatePressureHistoryModelX(DateTime minTime, DateTime maxTime) { PressureHistoryDataPoints.Clear(); if (!(PressureHistoryModel.Axes[1] is DateTimeAxis pressureDateTimeAxis)) return; pressureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(minTime); pressureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(maxTime); PressureHistoryModel.ResetAllAxes(); PressureHistoryModel.InvalidatePlot(true); } /// /// 刷新温度历史曲线模型 /// public void UpdateTemperatureHistoryModel(Dictionary TemperatureHistoryData,DateTime startTime,DateTime endTime) { TemperatureHistoryDataPoints.Clear(); foreach (var item in TemperatureHistoryData) TemperatureHistoryDataPoints.Add(OxyPlot.Axes.DateTimeAxis.CreateDataPoint(item.Key, item.Value)); if (TemperatureHistoryModel.Axes[1] is DateTimeAxis temperatureDateTimeAxis) { temperatureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(startTime); temperatureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(endTime); } TemperatureHistoryModel.ResetAllAxes(); TemperatureHistoryModel.InvalidatePlot(true); } /// /// 刷新气压历史曲线模型 /// public void UpdatePressureHistoryModel(Dictionary PreHistoryData, DateTime startTime, DateTime endTime) { PressureHistoryDataPoints.Clear(); foreach (var item in PreHistoryData) PressureHistoryDataPoints.Add(DateTimeAxis.CreateDataPoint(item.Key, item.Value)); if (PressureHistoryModel.Axes[1] is DateTimeAxis pressureDateTimeAxis) { pressureDateTimeAxis.Minimum = DateTimeAxis.ToDouble(startTime); pressureDateTimeAxis.Maximum = DateTimeAxis.ToDouble(endTime); } PressureHistoryModel.ResetAllAxes(); PressureHistoryModel.InvalidatePlot(true); } /// /// 刷新气压实时数据 /// /// /// public void UpdatePressureRealModel(DateTime dateTime, double newValue) { PressureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(dateTime, newValue)); int dataCount = PressureRealDataPoints.Count; if (dataCount > 3000) PressureRealDataPoints.RemoveAt(0); if (PressureRealModel.Axes[1] is DateTimeAxis dateTimeAxis) { dateTimeAxis.Minimum = PressureRealDataPoints.First().X; dateTimeAxis.Maximum = PressureRealDataPoints.Last().X; } if (dataCount > 1) { PressureRealModel.ResetAllAxes(); PressureRealModel.InvalidatePlot(true); } //if (newValue >= 35 && newValue <= 55) //{ //} } /// /// 刷新温度实时数据 /// /// /// public void UpdateTemperatureRealModel(DateTime dateTime, double newValue) { if (newValue >= 35 && newValue <= 38) { TemperatureRealDataPoints.Add(DateTimeAxis.CreateDataPoint(dateTime, newValue)); int dataCount = TemperatureRealDataPoints.Count; if (dataCount > 3000) TemperatureRealDataPoints.RemoveAt(0); if (TemperatureRealModel.Axes[1] is DateTimeAxis dateTimeAxis) { dateTimeAxis.Minimum = TemperatureRealDataPoints.First().X; dateTimeAxis.Maximum = TemperatureRealDataPoints.Last().X; } if(dataCount > 1) { TemperatureRealModel.ResetAllAxes(); TemperatureRealModel.InvalidatePlot(true); } } } /// /// 设置曲线样式 /// private void Init() { double preMaximum = 60.00; string trackString = "{2:MM-dd HH:mm}"; DateTime nowTime1 = DateTime.Now; DateTime nowTime = nowTime1.AddHours(-1); TemperatureHistoryModel = new PlotModel() { PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 1), PlotAreaBorderColor = OxyColor.FromRgb(210, 219, 234) }; TemperatureRealModel = new PlotModel() { PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 1), PlotAreaBorderColor = OxyColor.FromRgb(210, 219, 234) }; PressureHistoryModel = new PlotModel() { PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 1), PlotAreaBorderColor = OxyColor.FromRgb(210, 219, 234) }; PressureRealModel = new PlotModel() { PlotAreaBorderThickness = new OxyThickness(0, 0, 0, 1), PlotAreaBorderColor = OxyColor.FromRgb(210, 219, 234) }; //TemperatureHistoryModel.Series.Add(new LineSeries { Title = "温度", ItemsSource = TemperatureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(169, 201, 238), StrokeThickness = 5 }); 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 }); 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 }); 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 }); 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 }); //PressureRealModel.Series.Add(new LineSeries { Title = "气压", ItemsSource = PressureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(157, 119, 238), StrokeThickness = 5 }); 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 }); 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 }); 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 }); //TemperatureRealModel.Series.Add(new LineSeries { Title = "温度", ItemsSource = TemperatureRealDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(106, 170, 245), StrokeThickness = 5 }); 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 }); 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 }); //PressureHistoryModel.Series.Add(new LineSeries { Title = "气压", ItemsSource = PressureHistoryDataPoints, InterpolationAlgorithm = InterpolationAlgorithms.CatmullRomSpline, TrackerFormatString = trackString, MarkerType = MarkerType.None, Color = OxyColor.FromRgb(197, 175, 244), StrokeThickness = 5 }); 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 }); 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) }); 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) }); } } }