| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- 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<TLInfo> TlInfoList { get; set; } = new ObservableCollection<TLInfo>();
- TLInfoProvider _tLInfoProvider = null;
- public AppData Appdata { get; set; }
- System.Threading.Timer _timer;
- public List<int> HouseList { get; set; } = new List<int> { 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<TLInfo>(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<DateTime, double> TemperatureHistoryData = new Dictionary<DateTime, double>();
- Dictionary<DateTime, double> PreHistoryData = new Dictionary<DateTime, double>();
- 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<SettingEntity> GetSettingEntities(string tlSn, string HouseSn, string startTime, string endTime)
- {
- return _tLInfoProvider.GetBinSetting(tlSn, HouseSn, startTime, endTime);
- }
- public void DisposeTimer()
- {
- if (_timer != null)
- {
- _timer.Dispose();
- }
- }
- /// <summary>
- /// 气压历史曲线模型
- /// </summary>
- [ObservableProperty]
- private PlotModel pressureHistoryModel;
- /// <summary>
- /// 气压历史曲线数据
- /// </summary>
- [ObservableProperty]
- private ObservableCollection<DataPoint> pressureHistoryDataPoints = new ObservableCollection<DataPoint>();
- /// <summary>
- /// 温度历史曲线模型
- /// </summary>
- [ObservableProperty]
- private PlotModel temperatureHistoryModel;
- /// <summary>
- /// 温度历史曲线模型
- /// </summary>
- [ObservableProperty]
- private ObservableCollection<DataPoint> temperatureHistoryDataPoints = new ObservableCollection<DataPoint>();
- /// <summary>
- /// 气压实时曲线模型
- /// </summary>
- [ObservableProperty]
- private PlotModel pressureRealModel;
- /// <summary>
- /// 气压实时曲线数据
- /// </summary>
- [ObservableProperty]
- private ObservableCollection<DataPoint> pressureRealDataPoints = new ObservableCollection<DataPoint>();
- /// <summary>
- /// 温度实时曲线模型
- /// </summary>
- [ObservableProperty]
- private PlotModel temperatureRealModel;
- /// <summary>
- /// 温度实时曲线数据
- /// </summary>
- [ObservableProperty]
- private ObservableCollection<DataPoint> temperatureRealDataPoints = new ObservableCollection<DataPoint>();
- 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);
- }
- /// <summary>
- /// 刷新温度历史曲线模型
- /// </summary>
- public void UpdateTemperatureHistoryModel(Dictionary<DateTime, double> 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);
- }
- /// <summary>
- /// 刷新气压历史曲线模型
- /// </summary>
- public void UpdatePressureHistoryModel(Dictionary<DateTime, double> 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);
- }
- /// <summary>
- /// 刷新气压实时数据
- /// </summary>
- /// <param name="dateTime"></param>
- /// <param name="newValue"></param>
- 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)
- //{
-
- //}
- }
- /// <summary>
- /// 刷新温度实时数据
- /// </summary>
- /// <param name="dateTime"></param>
- /// <param name="newValue"></param>
- 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);
- }
- }
- }
- /// <summary>
- /// 设置曲线样式
- /// </summary>
- 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) });
- }
- }
- }
|