|
- using LiveCharts;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
-
- namespace BPASmartClient.Academy._50L
- {
- public class SqliteOperate
- {
- private volatile static SqliteOperate _Instance;
- public static SqliteOperate GetInstance => _Instance ?? (_Instance = new SqliteOperate());
- public DataFeedBack_50 DataFeedBacks { get; set; } = new DataFeedBack_50();
- private SqliteOperate() { }
-
- static string directoryPath = $"AccessFile\\DB\\{Json<DevicePar>.Data.ProjectTypeName.ToString()}";
- static string path
- {
- get
- {
- Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory);
- return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{directoryPath}\\data.db");
- }
- }
-
- private SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
- {
- ConnectionString = $"Data Source = {path}",
- DbType = DbType.Sqlite,
- IsAutoCloseConnection = true,
- });
-
- public void Init()
- {
- try
- {
- Db.DbMaintenance.CreateDatabase();
- Db.CodeFirst.SplitTables().InitTables<SamplingData>();
- Db.CodeFirst.InitTables<SaveNameData>();
-
- //CreateTestData();
-
- }
- catch (Exception ex)
- { }
- }
- public void RealtimeChart(SamplingData sampling)
- {
- DateTime time = DateTime.Now;
- DataView(DataFeedBacks.Temperature, new DataValue() { DateTime = time, Value = sampling.Temperature });
- DataView(DataFeedBacks.SteamPressure, new DataValue() { DateTime = time, Value = sampling.SteamPressure });
- DataView(DataFeedBacks.SteamFlowRate, new DataValue() { DateTime = time, Value = sampling.SteamFlowRate });
- DataView(DataFeedBacks.CondensateWaterTemperature, new DataValue() { DateTime = time, Value = sampling.CondensateWaterTemperature });
- DataView(DataFeedBacks.CondensateWaterHumidity, new DataValue() { DateTime = time, Value = sampling.CondensateWaterHumidity });
- DataView(DataFeedBacks.NegativePressureFlowRate, new DataValue() { DateTime = time, Value = sampling.NegativePressureFlowRate });
- DataView(DataFeedBacks.WeighingWaterTankWeight, new DataValue() { DateTime = time, Value = sampling.WeighingWaterTankWeight });
- DataView(DataFeedBacks.ReactEncoderValue, new DataValue() { DateTime = time, Value = sampling.ReactEncoderValue });
- DataView(DataFeedBacks.ProportionalValveOpening, new DataValue() { DateTime = time, Value = sampling.ProportionalValveOpening });
- DataView(DataFeedBacks.BrineTankWeight, new DataValue() { DateTime = time, Value = sampling.BrineTankWeight });
- DataView(DataFeedBacks.ReactPressure, new DataValue() { DateTime = time, Value = sampling.ReactPressure });
- }
- private void DataView<T>(ChartValues<T> values, T value)
- {
- if (values.Count >= 20)
- {
- values.RemoveAt(0);
- values.Add(value);
- }
- else
- {
- values.Add(value);
- }
- }
- ///// <summary>
- ///// 生成测试数据
- ///// </summary>
- //private void CreateTestData()
- //{
- // for (int i = 0; i < 5; i++)
- // {
- // var d1 = new SaveNameData($"test{DateTime.Now.Ticks}");
- // Add(d1);
- // Random rd = new Random();
- // List<SamplingData> sd = new List<SamplingData>();
- // for (int m = 0; m < 10000; m++)
- // {
- // sd.Add(new SamplingData(d1.Id));
- // }
- // for (int x = 0; x < 10; x++)
- // {
- // Db.Insertable<SamplingData>(sd.GetRange(0 * 1000, 1000)).SplitTable().ExecuteCommand();
- // }
- // }
- // MessageNotify.GetInstance.OpenMsg("测试数据生成成功");
- //}
-
- /// <summary>
- /// 数据库存储曲线数据
- /// </summary>
- /// <param name="sd">曲线数据/组</param>
- public void Add(SamplingData sd)
- {
- try
- {
- Db.Insertable<SamplingData>(sd).SplitTable().ExecuteCommand();
- }
- catch (Exception ex) { }
- }
-
- public void Add(SaveNameData snd)
- {
- try
- {
- Db.Insertable<SaveNameData>(snd).ExecuteCommand();
- }
- catch (Exception ex) { }
- }
-
- public List<SamplingData> QueryableById(string id)
- {
- try
- {
- return Db.Queryable<SamplingData>().SplitTable().Where(p => p.ProductNumberId == id).ToList();
- }
- catch (Exception ex) { return new List<SamplingData>(); }
- }
-
- public List<SamplingData> QueryableByNum(string num)
- {
- try
- {
- var snd = Db.Queryable<SaveNameData>().First(p => p.ProductNumber == num);
- if (snd != null)
- return Db.Queryable<SamplingData>().SplitTable().Where(p => p.ProductNumberId == snd.Id).ToList();
- else
- return new List<SamplingData>();
- }
- catch (Exception ex) { return new List<SamplingData>(); }
- }
-
- public List<SaveNameData> FindNames(DateTime dt)
- {
- try
- {
- DateTime startOfDay = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
- DateTime endOfDay = startOfDay.AddDays(1).AddTicks(-1);
- return Db.Queryable<SaveNameData>().Where(p => p.Createtime >= startOfDay && p.Createtime < endOfDay).ToList();
- }
- catch (Exception ex) { return new List<SaveNameData>(); }
- }
-
- public List<SaveNameData> FindNames(string name)
- {
- try
- {
- if (string.IsNullOrEmpty(name))
- {
- MessageNotify.GetInstance.OpenMsg("请输入有效的产品编码!", EnumPromptType.Error, "错误");
- return new List<SaveNameData>();
- }
- return Db.Queryable<SaveNameData>().Where(p => p.ProductNumber == name).ToList();
- }
- catch (Exception ex) { return new List<SaveNameData>(); }
- }
- }
- }
|