SweepConfigViewModel.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Utilities;
  4. using CommunityToolkit.Mvvm.Input;
  5. using OxyPlot;
  6. using OxyPlot.Series;
  7. using Shaker.Models;
  8. using Shaker.Models.Tools;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Linq.Expressions;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Input;
  16. namespace ShakerApp.ViewModels
  17. {
  18. internal class SweepConfigViewModel:ViewModelBase<Shaker.Models.SweepConfigModel>
  19. {
  20. private float interval = 0.1f;
  21. List<OxyPlot.Series.LineSeries> lineSeries = new List<OxyPlot.Series.LineSeries>();
  22. List<DataPoint> velocitydata = new List<DataPoint>();
  23. LineSeries velocity = new LineSeries();
  24. LineSeries displacement = new LineSeries();
  25. List<DataPoint> displacementdata = new List<DataPoint>();
  26. List<OxyColor> oxyColors = new List<OxyColor>() { OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  27. List<LineStyle> lineStyles = new List<LineStyle>() { LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  28. List<string> properties = new List<string>() { nameof(SweepData.TargetAcceleration), nameof(SweepData.UpStopAcceleration), nameof(SweepData.DownStopAcceleration), nameof(SweepData.UpWarnAcceleration), nameof(SweepData.DownWarnAcceleration) };
  29. List<SweepData> datas = new List<SweepData>();
  30. private SweepConfigViewModel()
  31. {
  32. OnceSweepTime = Model.OCTToTime(Model.SweepSpeed);
  33. Content = typeof(Views.SweepConfigView);
  34. for(int i=0;i<ShakerConfigViewModel.Instance.AccelerationSensorCount;i++)
  35. {
  36. Channels.Add(new KeyValuePair<int, int>(i+1,i));
  37. }
  38. #region 加速度谱曲线
  39. AccelerationModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  40. {
  41. MaximumPadding = 0,
  42. MinimumPadding = 0,
  43. Title = App.Current?.FindResource(Shaker.Models.ShakerConstant.FrequencyKey) + "",
  44. Unit = "Hz",
  45. MajorGridlineStyle = LineStyle.Solid,
  46. Position = OxyPlot.Axes.AxisPosition.Bottom,
  47. Key = "Freq"
  48. });
  49. AccelerationModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  50. {
  51. Key= "Ampt",
  52. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey)+"",
  53. Unit="g",
  54. MajorGridlineStyle = LineStyle.Solid,
  55. Position = OxyPlot.Axes.AxisPosition.Left,
  56. });
  57. AccelerationModel.Title = App.Current?.FindResource(ShakerConstant.AccelerationSpectrumKey)+ "";
  58. for(int i=0;i<oxyColors.Count;i++)
  59. {
  60. LineSeries line = new LineSeries();
  61. line.Title = App.Current?.FindResource(ShakerConstant.SweepAccelerationSpectrumNames[i])+"";
  62. line.Color = oxyColors[i];
  63. line.StrokeThickness = 1;
  64. line.DataFieldX = nameof(SweepData.Frequency);
  65. line.DataFieldY = properties[i];
  66. line.LineStyle = lineStyles[i];
  67. line.XAxisKey = "Freq";
  68. line.YAxisKey = "Ampt";
  69. lineSeries.Add(line);
  70. AccelerationModel.Series.Add(line);
  71. }
  72. AccelerationModel.Legends.Add(new OxyPlot.Legends.Legend()
  73. {
  74. ShowInvisibleSeries = true,
  75. });
  76. #endregion
  77. #region 速度
  78. VelocityModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  79. {
  80. MaximumPadding = 0,
  81. MinimumPadding = 0,
  82. Title = App.Current?.FindResource(Shaker.Models.ShakerConstant.FrequencyKey) + "",
  83. Unit = "Hz",
  84. MajorGridlineStyle = LineStyle.Solid,
  85. Position = OxyPlot.Axes.AxisPosition.Bottom,
  86. Key = "Freq"
  87. });
  88. VelocityModel.Title = App.Current?.FindResource(ShakerConstant.VelocityKey) + "";
  89. VelocityModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  90. {
  91. Key = "Ampt",
  92. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  93. Unit = "m/s",
  94. MajorGridlineStyle = LineStyle.Solid,
  95. Position = OxyPlot.Axes.AxisPosition.Left,
  96. });
  97. velocity = new LineSeries();
  98. velocity.Title = App.Current?.FindResource(ShakerConstant.VelocityKey) + "";
  99. velocity.Color = oxyColors[0];
  100. velocity.StrokeThickness = 1;
  101. velocity.DataFieldX = nameof(DataPoint.X);
  102. velocity.DataFieldY = nameof(DataPoint.Y);
  103. velocity.LineStyle = lineStyles[0];
  104. velocity.XAxisKey = "Freq";
  105. velocity.YAxisKey = "Ampt";
  106. VelocityModel.Series.Add(velocity);
  107. #endregion
  108. #region 位移
  109. DisplacementModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  110. {
  111. MaximumPadding = 0,
  112. MinimumPadding = 0,
  113. Title = App.Current?.FindResource(Shaker.Models.ShakerConstant.FrequencyKey) + "",
  114. Unit = "Hz",
  115. MajorGridlineStyle = LineStyle.Solid,
  116. Position = OxyPlot.Axes.AxisPosition.Bottom,
  117. Key = "Freq"
  118. });
  119. DisplacementModel.Title = App.Current?.FindResource(ShakerConstant.DisplacementKey) + "";
  120. DisplacementModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  121. {
  122. Key = "Ampt",
  123. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  124. Unit = "mm",
  125. MajorGridlineStyle = LineStyle.Solid,
  126. Position = OxyPlot.Axes.AxisPosition.Left,
  127. });
  128. displacement = new LineSeries();
  129. displacement.Title = App.Current?.FindResource(ShakerConstant.DisplacementKey) + "";
  130. displacement.Color = oxyColors[0];
  131. displacement.StrokeThickness = 1;
  132. displacement.DataFieldX = nameof(DataPoint.X);
  133. displacement.DataFieldY = nameof(DataPoint.Y);
  134. displacement.LineStyle = lineStyles[0];
  135. displacement.XAxisKey = "Freq";
  136. displacement.YAxisKey = "Ampt";
  137. DisplacementModel.Series.Add(displacement);
  138. #endregion
  139. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  140. {
  141. AccelerationModel.InvalidatePlot(false);
  142. AccelerationModel.Axes[0].Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "";
  143. AccelerationModel.Axes[1].Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "";
  144. AccelerationModel.Title = App.Current?.FindResource(ShakerConstant.AccelerationSpectrumKey) + "";
  145. for(int i=0;i<lineSeries.Count;i++)
  146. {
  147. lineSeries[i].Title = App.Current?.FindResource(ShakerConstant.SweepAccelerationSpectrumNames[i]) + "";
  148. }
  149. AccelerationModel.InvalidatePlot(true);
  150. VelocityModel.InvalidatePlot(false);
  151. VelocityModel.Axes[0].Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "";
  152. VelocityModel.Axes[1].Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "";
  153. VelocityModel.Title = App.Current?.FindResource(ShakerConstant.VelocityKey) + "";
  154. velocity.Title = App.Current?.FindResource(ShakerConstant.VelocityKey) + "";
  155. VelocityModel.InvalidatePlot(true);
  156. DisplacementModel.InvalidatePlot(false);
  157. DisplacementModel.Axes[0].Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "";
  158. DisplacementModel.Axes[1].Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "";
  159. DisplacementModel.Title = App.Current?.FindResource(ShakerConstant.DisplacementKey) + "";
  160. displacement.Title = App.Current?.FindResource(ShakerConstant.DisplacementKey) + "";
  161. DisplacementModel.InvalidatePlot(true);
  162. });
  163. GetEvent<AllConfig>().Subscrip((sender, args) =>
  164. {
  165. UpDateModel(args.Data.SweepConfig);
  166. if(CommunicationViewModel.Intance.ServiceIsStart)
  167. {
  168. CommunicationViewModel.Intance.ServiceCommunication.GetEvent<SweepConfigModel>().Publish(this, args.Data.SweepConfig);
  169. }
  170. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<SweepConfigModel>()?.Subscrip((sender, args) =>
  171. {
  172. UpDateModel(args.Data);
  173. Refresh();
  174. SineMainPageViewModel.Instance.SetRefSpectrum(datas);
  175. CommunicationViewModel.Intance.ServiceCommunication?.GetEvent<SweepConfigModel>()?.Publish(this, Model);
  176. });
  177. });
  178. }
  179. static SweepConfigViewModel()
  180. {
  181. }
  182. public AvaloniaList<KeyValuePair<int,int>> Channels { get; } = new AvaloniaList<KeyValuePair<int,int>>();
  183. [PropertyAssociation(nameof(SweepConfigModel.SelectChannel))]
  184. public int SelectChannel
  185. {
  186. get => Model.SelectChannel;
  187. set
  188. {
  189. SetProperty(ref Model.SelectChannel, value);
  190. for(int i=0;i<ShakerConfigViewModel.Instance.AccelerationSensorCount;i++)
  191. {
  192. ShakerConfigViewModel.Instance.Accelerations[i].Value.Weight = value == i ? 1 : 0;
  193. }
  194. }
  195. }
  196. [PropertyAssociation(nameof(SweepConfigModel.MaxSweepItemCount))]
  197. public int MaxSweepItemCount => Model.MaxSweepItemCount;
  198. [PropertyAssociation(nameof(SweepConfigModel.SweepCount))]
  199. public uint SweepCount { get => Model.SweepCount; set => SetProperty(ref Model.SweepCount, value); }
  200. [PropertyAssociation(nameof(SweepConfigModel.SweepType))]
  201. public SweepType SweepType
  202. {
  203. get => Model.SweepType;
  204. set
  205. {
  206. if (Model.SweepType == value) return;
  207. SetProperty(ref Model.SweepType, value);
  208. Model.OnceSweepTime = Model.OCTToTime(Model.SweepSpeed);
  209. OnPropertyChanged(nameof(OnceSweepTime));
  210. }
  211. }
  212. [PropertyAssociation(nameof(SweepConfigModel.SweepDirection))]
  213. public SweepDirection SweepDirection { get => Model.SweepDirection; set => SetProperty(ref Model.SweepDirection, value); }
  214. [PropertyAssociation(nameof(SweepConfigModel.SignalType))]
  215. public SignalType SignalType
  216. {
  217. get => Model.SignalType;
  218. set
  219. {
  220. SetProperty(ref Model.SignalType, value);
  221. if(value == SignalType.Sweep)
  222. {
  223. OnceSweepTime = Model.OCTToTime(Model.SweepSpeed);
  224. }
  225. }
  226. }
  227. [PropertyAssociation(nameof(SweepConfigModel.StartFrequency))]
  228. public double StartFrequency
  229. {
  230. get => Model.StartFrequency;
  231. set
  232. {
  233. if (value == Model.StartFrequency) return;
  234. SetProperty(ref Model.StartFrequency, value);
  235. if (SignalType == SignalType.Fixed) return;
  236. OnceSweepTime = Model.OCTToTime(Model.SweepSpeed);
  237. }
  238. }
  239. public ICommand MaxLoadCommand => new RelayCommand(MaxLoad);
  240. private void MaxLoad()
  241. {
  242. SweepItems.Clear();
  243. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(1,new SweepItemViewModel(new SweepItemModel()
  244. {
  245. Frequency = ShakerConfigViewModel.Instance.MinFrequency,
  246. Value = ShakerConfigViewModel.Instance.MaxDisplacement,
  247. SweepValueType = SweepValueType.FixedDisplacement,
  248. })));
  249. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(2,new SweepItemViewModel( new SweepItemModel()
  250. {
  251. Frequency = ShakerConfigViewModel.Instance.MinFrequency + interval,
  252. Value = ShakerConfigViewModel.Instance.MaxVelocity,
  253. SweepValueType = SweepValueType.FixedVelocity,
  254. })));
  255. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(3,new SweepItemViewModel( new SweepItemModel()
  256. {
  257. Frequency = ShakerConfigViewModel.Instance.MinFrequency + interval * 2,
  258. Value = ShakerConfigViewModel.Instance.MaxAcceleration,
  259. SweepValueType = SweepValueType.FixedAcceleration,
  260. })));
  261. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(4,new SweepItemViewModel( new SweepItemModel()
  262. {
  263. Frequency = ShakerConfigViewModel.Instance.MaxFrequency,
  264. Value = ShakerConfigViewModel.Instance.MaxAcceleration,
  265. SweepValueType = SweepValueType.FixedAcceleration,
  266. })));
  267. Refresh();
  268. }
  269. public double MaxAcceleration => datas.Count ==0 ?0: datas.Max(x => x.TargetAcceleration);
  270. public double MaxDisplacement => displacementdata.Count == 0 ? 0 : displacementdata.Max(x => x.Y);
  271. public double MaxVelocity => velocitydata.Count == 0 ? 0 : velocitydata.Max(x => x.Y);
  272. public bool AccelerationOverLimit => MaxAcceleration >= ShakerConfigViewModel.Instance.MaxAcceleration;
  273. public bool DisplacementOverLimit => MaxDisplacement > ShakerConfigViewModel.Instance.MaxDisplacement;
  274. public bool VelocityOverLimit => MaxVelocity > ShakerConfigViewModel.Instance.MaxVelocity;
  275. public double AccelerationLoad => Math.Round(MaxAcceleration / ShakerConfigViewModel.Instance.MaxAcceleration * 100f, 2);
  276. public double DisplacementLoad => Math.Round((double)MaxDisplacement / ShakerConfigViewModel.Instance.MaxDisplacement * 100f, 2);
  277. public double VelocityLoad => Math.Round((double)MaxVelocity / ShakerConfigViewModel.Instance.MaxVelocity * 100f, 2);
  278. [PropertyAssociation(nameof(SweepConfigModel.EndFrequency))]
  279. public double EndFrequency
  280. {
  281. get => Model.EndFrequency;
  282. set
  283. {
  284. if (value == Model.EndFrequency) return;
  285. SetProperty(ref Model.EndFrequency, value);
  286. if (SignalType == SignalType.Fixed) return;
  287. OnceSweepTime = Model.OCTToTime(Model.SweepSpeed);
  288. }
  289. }
  290. [PropertyAssociation(nameof(SweepConfigModel.OnceSweepTime))]
  291. public double OnceSweepTime
  292. {
  293. get => Model.OnceSweepTime;
  294. set
  295. {
  296. if (Math.Abs(value - Model.OnceSweepTime) <= Increment || double.IsNaN(value)) return;
  297. SetProperty(ref Model.OnceSweepTime, value);
  298. OnPropertyChanged(nameof(TotalSweepTime));
  299. if (SignalType == SignalType.Fixed) return;
  300. SweepSpeed = Model.TimeToOCT(value);
  301. }
  302. }
  303. private bool VerifySweepItem()
  304. {
  305. if (SweepItems.Count < 2)
  306. {
  307. Dialogs.Error(string.Format(App.Current?.FindResource("SweepItemsCountError")+"",2));
  308. return false;
  309. }
  310. Model.SweepItems = SweepItems.Select(x => x.Value.Model).Where(x=>x.Value >0).OrderBy(x => x.Frequency).DistinctBy(x => x.Frequency).ToList();
  311. SweepItems.Clear();
  312. if (Model.SweepItems.Count < 2)
  313. {
  314. for (int i = 0; i < Model.SweepItems.Count; i++)
  315. {
  316. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(i + 1, new SweepItemViewModel(Model.SweepItems[i])));
  317. }
  318. Dialogs.Error(string.Format(App.Current?.FindResource("SweepItemsCountError") + "", 2));
  319. return false;
  320. }
  321. else
  322. {
  323. for (int i = 0; i < Model.SweepItems.Count; i++)
  324. {
  325. if (i == 0)
  326. {
  327. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(i + 1, new SweepItemViewModel(Model.SweepItems[i])));
  328. continue;
  329. }
  330. switch (Model.SweepItems[i].SweepValueType)
  331. {
  332. case SweepValueType.DynamicAcceleration:
  333. case SweepValueType.FixedAcceleration:
  334. {
  335. switch(Model.SweepItems[i-1].SweepValueType)
  336. {
  337. case SweepValueType.DynamicAcceleration:
  338. break;
  339. case SweepValueType.FixedAcceleration:
  340. break;
  341. case SweepValueType.FixedDisplacement:
  342. Model.SweepItems[i].Frequency =Math.Round(Math.Sqrt(Model.SweepItems[i].Value*9800 / Model.SweepItems[i - 1].Value) / (2 * Math.PI),2);
  343. break;
  344. case SweepValueType.FixedVelocity:
  345. Model.SweepItems[i].Frequency = Math.Round(Model.SweepItems[i].Value * 9.8f / (2 * MathF.PI * Model.SweepItems[i - 1].Value), 2);
  346. break;
  347. }
  348. }
  349. break;
  350. case SweepValueType.FixedDisplacement:
  351. {
  352. switch(Model.SweepItems[i-1].SweepValueType)
  353. {
  354. case SweepValueType.DynamicAcceleration:
  355. case SweepValueType.FixedAcceleration:
  356. Model.SweepItems[i].Frequency = Math.Round(Math.Sqrt(Model.SweepItems[i-1].Value * 9800 / Model.SweepItems[i].Value) / (2 * Math.PI), 2);
  357. break;
  358. case SweepValueType.FixedVelocity:
  359. Model.SweepItems[i].Frequency = Math.Round(Model.SweepItems[i - 1].Value * 1000 / (2 * Math.PI * Model.SweepItems[i].Value), 2);
  360. break;
  361. case SweepValueType.FixedDisplacement:
  362. break;
  363. }
  364. }
  365. break;
  366. case SweepValueType.FixedVelocity:
  367. {
  368. switch (Model.SweepItems[i - 1].SweepValueType)
  369. {
  370. case SweepValueType.DynamicAcceleration:
  371. case SweepValueType.FixedAcceleration:
  372. Model.SweepItems[i].Frequency = Math.Round(Model.SweepItems[i-1].Value * 9.8f / (2 * Math.PI * Model.SweepItems[i].Value), 2);
  373. break;
  374. case SweepValueType.FixedVelocity:
  375. break;
  376. case SweepValueType.FixedDisplacement:
  377. Model.SweepItems[i].Frequency = Math.Round(Model.SweepItems[i].Value * 1000 / (2 * Math.PI * Model.SweepItems[i-1].Value), 2);
  378. break;
  379. }
  380. }
  381. break;
  382. }
  383. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(i + 1, new SweepItemViewModel(Model.SweepItems[i])));
  384. }
  385. }
  386. return true;
  387. }
  388. public ICommand RefreshCommand => new RelayCommand(Refresh);
  389. private void Refresh()
  390. {
  391. var result = VerifySweepItem();
  392. if (MaxFrequency <= MinFrequency) return;
  393. if (EndFrequency > MaxFrequency) EndFrequency = MaxFrequency;
  394. if (StartFrequency < MinFrequency) StartFrequency = MinFrequency;
  395. if (!result) return;
  396. datas.Clear();
  397. velocitydata.Clear();
  398. displacementdata.Clear();
  399. int count = (int)Math.Ceiling((MaxFrequency - MinFrequency) / interval)+1;
  400. for(int i=0;i<count;i++)
  401. {
  402. double freq = i * interval + MinFrequency;
  403. double acc = 0;
  404. double upstop = 0;
  405. double upwarn = 0;
  406. double downstop = 0;
  407. double downwarn = 0;
  408. Model.CalcAmpt(freq, ref acc, ref upstop, ref upwarn, ref downstop, ref downwarn);
  409. datas.Add(new SweepData()
  410. {
  411. Frequency =freq,
  412. TargetAcceleration= Math.Round(acc,4),
  413. UpStopAcceleration = upstop,
  414. UpWarnAcceleration = upwarn,
  415. DownStopAcceleration = downstop,
  416. DownWarnAcceleration = downwarn,
  417. });
  418. velocitydata.Add(new DataPoint(freq,Math.Round(Shaker.Models.Tools.Tools.AccelerationToVelocity(acc,freq),4)));
  419. displacementdata.Add(new DataPoint(freq,Math.Round(Shaker.Models.Tools.Tools.AccelerationToDisplacement(acc, freq),4)));
  420. }
  421. AccelerationModel.InvalidatePlot(false);
  422. VelocityModel.InvalidatePlot(false);
  423. DisplacementModel.InvalidatePlot(false);
  424. for(int i=0;i<lineSeries.Count;i++)
  425. {
  426. lineSeries[i].ItemsSource = datas;
  427. }
  428. velocity.ItemsSource = velocitydata;
  429. displacement.ItemsSource = displacementdata;
  430. VelocityModel.InvalidatePlot(true);
  431. DisplacementModel.InvalidatePlot(true);
  432. AccelerationModel.InvalidatePlot(true);
  433. OnPropertyChanged(nameof(MaxAcceleration));
  434. OnPropertyChanged(nameof(MaxVelocity));
  435. OnPropertyChanged(nameof(MaxDisplacement));
  436. OnPropertyChanged(nameof(AccelerationLoad));
  437. OnPropertyChanged(nameof(VelocityLoad));
  438. OnPropertyChanged(nameof(DisplacementLoad));
  439. OnPropertyChanged(nameof(AccelerationOverLimit));
  440. OnPropertyChanged(nameof(VelocityOverLimit));
  441. OnPropertyChanged(nameof(DisplacementOverLimit));
  442. OnPropertyChanged(nameof(MaxFrequency));
  443. OnPropertyChanged(nameof(MinFrequency));
  444. }
  445. [PropertyAssociation(nameof(SweepConfigModel.SweepSpeed))]
  446. public double SweepSpeed
  447. {
  448. get => Model.SweepSpeed;
  449. set
  450. {
  451. if (Math.Abs(value - Model.SweepSpeed)<=Increment || double.IsNaN(value)) return;
  452. SetProperty(ref Model.SweepSpeed, value);
  453. OnceSweepTime = Model.OCTToTime(value);
  454. }
  455. }
  456. [PropertyAssociation(nameof(SweepConfigModel.Increment))]
  457. public double Increment => Model.Increment;
  458. [PropertyAssociation(nameof(SweepConfigModel.OnceSweepTime),nameof(SweepConfigModel.SweepCount))]
  459. public double TotalSweepTime => OnceSweepTime * SweepCount;
  460. [PropertyAssociation(nameof(SweepConfigModel.SweepItems))]
  461. public bool AddEnabled => SweepItems.Count < MaxSweepItemCount;
  462. [PropertyAssociation(nameof(SweepConfigModel.SweepItems))]
  463. public bool DeleteEnabled => SweepItems.Count > 0;
  464. [PropertyAssociation(nameof(SweepConfigModel.SweepItems))]
  465. public bool RefreshEnabled => SweepItems.Count >= 2;
  466. [PropertyAssociation(nameof(SweepConfigModel.SweepStartLevel))]
  467. public double SweepStartLevel { get => Model.SweepStartLevel; set => SetProperty(ref Model.SweepStartLevel, value); }
  468. [PropertyAssociation(nameof(SweepConfigModel.CrossoverPoint))]
  469. public double CrossoverPoint { get => Model.CrossoverPoint; set => SetProperty(ref Model.CrossoverPoint, value); }
  470. [PropertyAssociation(nameof(SweepConfigModel.SweepLevelGain))]
  471. public double SweepLevelGain { get => Model.SweepLevelGain; set => SetProperty(ref Model.SweepLevelGain, value); }
  472. [PropertyAssociation(nameof(SweepConfigModel.LowFrequencyMinCorrect))]
  473. public double LowFrequencyMinCorrect { get => Model.LowFrequencyMinCorrect; set => SetProperty(ref Model.LowFrequencyMinCorrect, value); }
  474. [PropertyAssociation(nameof(SweepConfigModel.LowFrequencyMaxCorrect))]
  475. public double LowFrequencyMaxCorrect { get => Model.LowFrequencyMaxCorrect; set => SetProperty(ref Model.LowFrequencyMaxCorrect, value); }
  476. [PropertyAssociation(nameof(SweepConfigModel.HigthFrequencyMinCorrect))]
  477. public double HigthFrequencyMinCorrect { get => Model.HigthFrequencyMinCorrect; set => SetProperty(ref Model.HigthFrequencyMinCorrect, value); }
  478. [PropertyAssociation(nameof(SweepConfigModel.HigthFrequencyMaxCorrect))]
  479. public double HigthFrequencyMaxCorrect { get => Model.HigthFrequencyMaxCorrect; set => SetProperty(ref Model.HigthFrequencyMaxCorrect, value); }
  480. [PropertyAssociation(nameof(SweepConfigModel.SweepItems))]
  481. public AvaloniaList<IndexValueItemViewModel<SweepItemViewModel>> SweepItems { get; } = new AvaloniaList<IndexValueItemViewModel<SweepItemViewModel>>();
  482. public ICommand AddCommand => new RelayCommand(Add);
  483. [PropertyAssociation(nameof(SweepConfigModel.SweepItems))]
  484. public double MaxFrequency => SweepItems.Count ==0 ? ShakerConfigViewModel.Instance.MaxFrequency: SweepItems.Max(x=>x.Value.Frequency);
  485. [PropertyAssociation(nameof(SweepConfigModel.SweepItems))]
  486. public double MinFrequency => SweepItems.Count ==0 ? ShakerConfigViewModel.Instance.MinFrequency: SweepItems.Min(x => x.Value.Frequency);
  487. private void Add()
  488. {
  489. if (SweepItems.Count >= MaxSweepItemCount) return;
  490. Model.SweepItems.Add(new SweepItemModel());
  491. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(SweepItems.Count + 1,new SweepItemViewModel(Model.SweepItems[^1])));
  492. OnPropertyChanged(nameof(AddEnabled));
  493. OnPropertyChanged(nameof(DeleteEnabled));
  494. OnPropertyChanged(nameof(RefreshEnabled));
  495. OnPropertyChanged(nameof(MaxFrequency));
  496. OnPropertyChanged(nameof(MinFrequency));
  497. if (MaxFrequency <= MinFrequency) return;
  498. if (EndFrequency > MaxFrequency) EndFrequency = MaxFrequency;
  499. if (StartFrequency < MinFrequency) StartFrequency = MinFrequency;
  500. }
  501. public ICommand DeleteCommand => new RelayCommand(Delete);
  502. private void Delete()
  503. {
  504. if (SweepItems.Count == 0) return;
  505. Model.SweepItems.RemoveAt(Model.SweepItems.Count - 1);
  506. SweepItems.RemoveAt(SweepItems.Count - 1);
  507. OnPropertyChanged(nameof(AddEnabled));
  508. OnPropertyChanged(nameof(DeleteEnabled));
  509. OnPropertyChanged(nameof(RefreshEnabled));
  510. OnPropertyChanged(nameof(MaxFrequency));
  511. OnPropertyChanged(nameof(MinFrequency));
  512. if (MaxFrequency <= MinFrequency) return;
  513. if (EndFrequency > MaxFrequency) EndFrequency = MaxFrequency;
  514. if (StartFrequency < MinFrequency) StartFrequency = MinFrequency;
  515. }
  516. public override void UpDateModel(SweepConfigModel model)
  517. {
  518. base.UpDateModel(model);
  519. int count = model.SweepItems.Count - SweepItems.Count;
  520. if(count>0)
  521. {
  522. for(int i=0;i<count;i++)
  523. {
  524. SweepItems.Add(new IndexValueItemViewModel<SweepItemViewModel>(SweepItems.Count + 1, new SweepItemViewModel()));
  525. }
  526. }
  527. else if(count<0)
  528. {
  529. SweepItems.RemoveRange(SweepItems.Count + count, Math.Abs(count));
  530. }
  531. else
  532. {
  533. }
  534. for(int i=0;i<SweepItems.Count;i++)
  535. {
  536. SweepItems[i].Value.UpDateModel(model.SweepItems[i]);
  537. }
  538. }
  539. protected override void Save()
  540. {
  541. base.Save();
  542. SineMainPageViewModel.Instance.SetRefSpectrum(datas);
  543. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<SweepConfigModel>()?.Publish(this, Model);
  544. CommunicationViewModel.Intance.ServiceCommunication?.GetEvent<SweepConfigModel>()?.Publish(this, Model);
  545. }
  546. public List<SweepData> RefSpectrum => datas;
  547. public OxyPlot.PlotModel AccelerationModel { get; } = new OxyPlot.PlotModel();
  548. public PlotModel DisplacementModel { get; } = new PlotModel();
  549. public PlotModel VelocityModel { get; } = new PlotModel();
  550. public static SweepConfigViewModel Instance { get; } = new SweepConfigViewModel();
  551. }
  552. }