using IModel; using Shaker.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShakerService.ViewModel { internal sealed class BalancingControlViewModel:ViewModelBase { /// /// 启用静支撑积分 /// public bool EnabledIntegration =>CurrentModel.EnabledIntegration; /// /// 静支撑积分 /// public double BalancingIntegration =>CurrentModel.BalancingIntegration; /// /// 静支撑增益 /// public double StaticSupportGain =>CurrentModel.StaticSupportGain; /// /// 静支撑闭环 /// public bool StaticSupportCloseLoop =>CurrentModel.StaticSupportCloseLoop; /// /// 静支撑开环驱动(V) /// public double[] StaticSupportOpenLoopDrvier =>CurrentModel.StaticSupportOpenLoopDrvier; /// /// 预加载质量(kg) /// public double[] PreloadingQuality =>CurrentModel.PreloadingQuality; /// /// 支撑缸面积(mm^2) /// public double Area =>CurrentModel.Area; private BalancingControlViewModel():base() { } static BalancingControlViewModel() { } public override void SetFpga() { base.SetFpga(); ShakerFpga.ShakerFpga.Instance.静支撑增益.Value = CurrentModel.StaticSupportGain; ShakerFpga.ShakerFpga.Instance.空台面压力.Value = [.. PreloadingQuality.Select(x => x * ShakerConfigViewModel.Instance.G / Area)]; ShakerFpga.ShakerFpga.Instance.静支撑开环驱动.Value = StaticSupportOpenLoopDrvier; ShakerFpga.ShakerFpga.Instance.静支撑积分.Value = EnabledIntegration; ShakerFpga.ShakerFpga.Instance.静支撑闭环.Value = StaticSupportCloseLoop; ShakerFpga.ShakerFpga.Instance.支撑缸积分.Value = BalancingIntegration; } private protected override void ReadModel() { Communication.Instance.DbConnection.CreateTable(tableName: nameof(BalancingControlModel)); var model = Communication.Instance.DbConnection.Query($"select * from {nameof(BalancingControlModel)} limit 1")?.FirstOrDefault() ?? new BalancingControlModel(); Communication.Instance.DbConnection.Execute($"CREATE TABLE IF NOT EXISTS {nameof(BalancingControlModel.PreloadingQuality)} ({nameof(BaseModel.Id)} INTEGER PRIMARY KEY AUTOINCREMENT, Value REAL);"); model.PreloadingQuality = Communication.Instance.DbConnection.Query($"select Value from {nameof(BalancingControlModel.PreloadingQuality)} limit 4")?.ToArray() ?? new double[4]; if(model.PreloadingQuality == null|| model.PreloadingQuality.Length!=4) { model.PreloadingQuality = new double[4]; } Communication.Instance.DbConnection.Execute($"CREATE TABLE IF NOT EXISTS {nameof(BalancingControlModel.StaticSupportOpenLoopDrvier)} ({nameof(BaseModel.Id)} INTEGER PRIMARY KEY AUTOINCREMENT, Value REAL);"); model.StaticSupportOpenLoopDrvier = Communication.Instance.DbConnection.Query($"select Value from {nameof(BalancingControlModel.StaticSupportOpenLoopDrvier)} limit 4")?.ToArray() ?? new double[4]; if(model.StaticSupportOpenLoopDrvier ==null || model.StaticSupportOpenLoopDrvier.Length!=4) { model.StaticSupportOpenLoopDrvier = new double[4]; } CurrentModel = model; SaveModel(); base.ReadModel(); } private protected override void UpModel(BalancingControlModel model) { if(model.PreloadingQuality==null || model.PreloadingQuality.Length!=4) { model.PreloadingQuality = new double[4]; } if(model.StaticSupportOpenLoopDrvier==null || model.StaticSupportOpenLoopDrvier.Length!=4) { model.StaticSupportOpenLoopDrvier = new double[4]; } base.UpModel(model); } private protected override void SaveModel() { base.SaveModel(); Communication.Instance.DbConnection.DeleteAll(tableName: nameof(BalancingControlModel)); Communication.Instance.DbConnection.Insert(CurrentModel, nameof(BalancingControlModel)); Communication.Instance.DbConnection.DeleteAll(tableName: nameof(BalancingControlModel.PreloadingQuality)); for(int i=0;i(tableName: nameof(BalancingControlModel.StaticSupportOpenLoopDrvier)); for (int i = 0; i < CurrentModel.StaticSupportOpenLoopDrvier.Length; i++) { Communication.Instance.DbConnection.Execute($"INSERT INTO {nameof(BalancingControlModel.StaticSupportOpenLoopDrvier)} (Value) VALUES ({CurrentModel.StaticSupportOpenLoopDrvier[i]});"); } } public static BalancingControlViewModel Instance { get; } = new BalancingControlViewModel(); } }