BalancingControlViewModel.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using IModel;
  2. using Shaker.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ShakerService.ViewModel
  9. {
  10. internal sealed class BalancingControlViewModel:ViewModelBase<BalancingControlModel>
  11. {
  12. /// <summary>
  13. /// 启用静支撑积分
  14. /// </summary>
  15. public bool EnabledIntegration =>CurrentModel.EnabledIntegration;
  16. /// <summary>
  17. /// 静支撑积分
  18. /// </summary>
  19. public double BalancingIntegration =>CurrentModel.BalancingIntegration;
  20. /// <summary>
  21. /// 静支撑增益
  22. /// </summary>
  23. public double StaticSupportGain =>CurrentModel.StaticSupportGain;
  24. /// <summary>
  25. /// 静支撑闭环
  26. /// </summary>
  27. public bool StaticSupportCloseLoop =>CurrentModel.StaticSupportCloseLoop;
  28. /// <summary>
  29. /// 静支撑开环驱动(V)
  30. /// </summary>
  31. public double[] StaticSupportOpenLoopDrvier =>CurrentModel.StaticSupportOpenLoopDrvier;
  32. /// <summary>
  33. /// 预加载质量(kg)
  34. /// </summary>
  35. public double[] PreloadingQuality =>CurrentModel.PreloadingQuality;
  36. /// <summary>
  37. /// 支撑缸面积(mm^2)
  38. /// </summary>
  39. public double Area =>CurrentModel.Area;
  40. private BalancingControlViewModel():base()
  41. {
  42. }
  43. static BalancingControlViewModel()
  44. {
  45. }
  46. public override void SetFpga()
  47. {
  48. base.SetFpga();
  49. ShakerFpga.ShakerFpga.Instance.静支撑增益.Value = CurrentModel.StaticSupportGain;
  50. ShakerFpga.ShakerFpga.Instance.空台面压力.Value = [.. PreloadingQuality.Select(x => x * ShakerConfigViewModel.Instance.G / Area)];
  51. ShakerFpga.ShakerFpga.Instance.静支撑开环驱动.Value = StaticSupportOpenLoopDrvier;
  52. ShakerFpga.ShakerFpga.Instance.静支撑积分.Value = EnabledIntegration;
  53. ShakerFpga.ShakerFpga.Instance.静支撑闭环.Value = StaticSupportCloseLoop;
  54. ShakerFpga.ShakerFpga.Instance.支撑缸积分.Value = BalancingIntegration;
  55. }
  56. private protected override void ReadModel()
  57. {
  58. Communication.Instance.DbConnection.CreateTable<BalancingControlModel>(tableName: nameof(BalancingControlModel));
  59. var model = Communication.Instance.DbConnection.Query<BalancingControlModel>($"select * from {nameof(BalancingControlModel)} limit 1")?.FirstOrDefault() ?? new BalancingControlModel();
  60. Communication.Instance.DbConnection.Execute($"CREATE TABLE IF NOT EXISTS {nameof(BalancingControlModel.PreloadingQuality)} ({nameof(BaseModel.Id)} INTEGER PRIMARY KEY AUTOINCREMENT, Value REAL);");
  61. model.PreloadingQuality = Communication.Instance.DbConnection.Query<double>($"select Value from {nameof(BalancingControlModel.PreloadingQuality)} limit 4")?.ToArray() ?? new double[4];
  62. if(model.PreloadingQuality == null|| model.PreloadingQuality.Length!=4)
  63. {
  64. model.PreloadingQuality = new double[4];
  65. }
  66. Communication.Instance.DbConnection.Execute($"CREATE TABLE IF NOT EXISTS {nameof(BalancingControlModel.StaticSupportOpenLoopDrvier)} ({nameof(BaseModel.Id)} INTEGER PRIMARY KEY AUTOINCREMENT, Value REAL);");
  67. model.StaticSupportOpenLoopDrvier = Communication.Instance.DbConnection.Query<double>($"select Value from {nameof(BalancingControlModel.StaticSupportOpenLoopDrvier)} limit 4")?.ToArray() ?? new double[4];
  68. if(model.StaticSupportOpenLoopDrvier ==null || model.StaticSupportOpenLoopDrvier.Length!=4)
  69. {
  70. model.StaticSupportOpenLoopDrvier = new double[4];
  71. }
  72. CurrentModel = model;
  73. SaveModel();
  74. base.ReadModel();
  75. }
  76. private protected override void UpModel(BalancingControlModel model)
  77. {
  78. if(model.PreloadingQuality==null || model.PreloadingQuality.Length!=4)
  79. {
  80. model.PreloadingQuality = new double[4];
  81. }
  82. if(model.StaticSupportOpenLoopDrvier==null || model.StaticSupportOpenLoopDrvier.Length!=4)
  83. {
  84. model.StaticSupportOpenLoopDrvier = new double[4];
  85. }
  86. base.UpModel(model);
  87. }
  88. private protected override void SaveModel()
  89. {
  90. base.SaveModel();
  91. Communication.Instance.DbConnection.DeleteAll<BalancingControlModel>(tableName: nameof(BalancingControlModel));
  92. Communication.Instance.DbConnection.Insert(CurrentModel, nameof(BalancingControlModel));
  93. Communication.Instance.DbConnection.DeleteAll<double>(tableName: nameof(BalancingControlModel.PreloadingQuality));
  94. for(int i=0;i<CurrentModel.PreloadingQuality.Length;i++)
  95. {
  96. Communication.Instance.DbConnection.Execute($"INSERT INTO {nameof(BalancingControlModel.PreloadingQuality)} (Value) VALUES ({CurrentModel.PreloadingQuality[i]});");
  97. }
  98. Communication.Instance.DbConnection.DeleteAll<double>(tableName: nameof(BalancingControlModel.StaticSupportOpenLoopDrvier));
  99. for (int i = 0; i < CurrentModel.StaticSupportOpenLoopDrvier.Length; i++)
  100. {
  101. Communication.Instance.DbConnection.Execute($"INSERT INTO {nameof(BalancingControlModel.StaticSupportOpenLoopDrvier)} (Value) VALUES ({CurrentModel.StaticSupportOpenLoopDrvier[i]});");
  102. }
  103. }
  104. public static BalancingControlViewModel Instance { get; } = new BalancingControlViewModel();
  105. }
  106. }