终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

171 satır
6.5 KiB

  1. using LiveCharts;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml.Linq;
  10. namespace BPASmartClient.Academy._50L
  11. {
  12. public class SqliteOperate
  13. {
  14. private volatile static SqliteOperate _Instance;
  15. public static SqliteOperate GetInstance => _Instance ?? (_Instance = new SqliteOperate());
  16. public DataFeedBack_50 DataFeedBacks { get; set; } = new DataFeedBack_50();
  17. private SqliteOperate() { }
  18. static string directoryPath = $"AccessFile\\DB\\{Json<DevicePar>.Data.ProjectTypeName.ToString()}";
  19. static string path
  20. {
  21. get
  22. {
  23. Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory);
  24. return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{directoryPath}\\data.db");
  25. }
  26. }
  27. private SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
  28. {
  29. ConnectionString = $"Data Source = {path}",
  30. DbType = DbType.Sqlite,
  31. IsAutoCloseConnection = true,
  32. });
  33. public void Init()
  34. {
  35. try
  36. {
  37. Db.DbMaintenance.CreateDatabase();
  38. Db.CodeFirst.SplitTables().InitTables<SamplingData>();
  39. Db.CodeFirst.InitTables<SaveNameData>();
  40. //CreateTestData();
  41. }
  42. catch (Exception ex)
  43. { }
  44. }
  45. public void RealtimeChart(SamplingData sampling)
  46. {
  47. DateTime time = DateTime.Now;
  48. DataView(DataFeedBacks.Temperature, new DataValue() { DateTime = time, Value = sampling.Temperature });
  49. DataView(DataFeedBacks.SteamPressure, new DataValue() { DateTime = time, Value = sampling.SteamPressure });
  50. DataView(DataFeedBacks.SteamFlowRate, new DataValue() { DateTime = time, Value = sampling.SteamFlowRate });
  51. DataView(DataFeedBacks.CondensateWaterTemperature, new DataValue() { DateTime = time, Value = sampling.CondensateWaterTemperature });
  52. DataView(DataFeedBacks.CondensateWaterHumidity, new DataValue() { DateTime = time, Value = sampling.CondensateWaterHumidity });
  53. DataView(DataFeedBacks.NegativePressureFlowRate, new DataValue() { DateTime = time, Value = sampling.NegativePressureFlowRate });
  54. DataView(DataFeedBacks.WeighingWaterTankWeight, new DataValue() { DateTime = time, Value = sampling.WeighingWaterTankWeight });
  55. DataView(DataFeedBacks.ReactEncoderValue, new DataValue() { DateTime = time, Value = sampling.ReactEncoderValue });
  56. DataView(DataFeedBacks.ProportionalValveOpening, new DataValue() { DateTime = time, Value = sampling.ProportionalValveOpening });
  57. DataView(DataFeedBacks.BrineTankWeight, new DataValue() { DateTime = time, Value = sampling.BrineTankWeight });
  58. DataView(DataFeedBacks.ReactPressure, new DataValue() { DateTime = time, Value = sampling.ReactPressure });
  59. }
  60. private void DataView<T>(ChartValues<T> values, T value)
  61. {
  62. if (values.Count >= 20)
  63. {
  64. values.RemoveAt(0);
  65. values.Add(value);
  66. }
  67. else
  68. {
  69. values.Add(value);
  70. }
  71. }
  72. ///// <summary>
  73. ///// 生成测试数据
  74. ///// </summary>
  75. //private void CreateTestData()
  76. //{
  77. // for (int i = 0; i < 5; i++)
  78. // {
  79. // var d1 = new SaveNameData($"test{DateTime.Now.Ticks}");
  80. // Add(d1);
  81. // Random rd = new Random();
  82. // List<SamplingData> sd = new List<SamplingData>();
  83. // for (int m = 0; m < 10000; m++)
  84. // {
  85. // sd.Add(new SamplingData(d1.Id));
  86. // }
  87. // for (int x = 0; x < 10; x++)
  88. // {
  89. // Db.Insertable<SamplingData>(sd.GetRange(0 * 1000, 1000)).SplitTable().ExecuteCommand();
  90. // }
  91. // }
  92. // MessageNotify.GetInstance.OpenMsg("测试数据生成成功");
  93. //}
  94. /// <summary>
  95. /// 数据库存储曲线数据
  96. /// </summary>
  97. /// <param name="sd">曲线数据/组</param>
  98. public void Add(SamplingData sd)
  99. {
  100. try
  101. {
  102. Db.Insertable<SamplingData>(sd).SplitTable().ExecuteCommand();
  103. }
  104. catch (Exception ex) { }
  105. }
  106. public void Add(SaveNameData snd)
  107. {
  108. try
  109. {
  110. Db.Insertable<SaveNameData>(snd).ExecuteCommand();
  111. }
  112. catch (Exception ex) { }
  113. }
  114. public List<SamplingData> QueryableById(string id)
  115. {
  116. try
  117. {
  118. return Db.Queryable<SamplingData>().SplitTable().Where(p => p.ProductNumberId == id).ToList();
  119. }
  120. catch (Exception ex) { return new List<SamplingData>(); }
  121. }
  122. public List<SamplingData> QueryableByNum(string num)
  123. {
  124. try
  125. {
  126. var snd = Db.Queryable<SaveNameData>().First(p => p.ProductNumber == num);
  127. if (snd != null)
  128. return Db.Queryable<SamplingData>().SplitTable().Where(p => p.ProductNumberId == snd.Id).ToList();
  129. else
  130. return new List<SamplingData>();
  131. }
  132. catch (Exception ex) { return new List<SamplingData>(); }
  133. }
  134. public List<SaveNameData> FindNames(DateTime dt)
  135. {
  136. try
  137. {
  138. DateTime startOfDay = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
  139. DateTime endOfDay = startOfDay.AddDays(1).AddTicks(-1);
  140. return Db.Queryable<SaveNameData>().Where(p => p.Createtime >= startOfDay && p.Createtime < endOfDay).ToList();
  141. }
  142. catch (Exception ex) { return new List<SaveNameData>(); }
  143. }
  144. public List<SaveNameData> FindNames(string name)
  145. {
  146. try
  147. {
  148. if (string.IsNullOrEmpty(name))
  149. {
  150. MessageNotify.GetInstance.OpenMsg("请输入有效的产品编码!", EnumPromptType.Error, "错误");
  151. return new List<SaveNameData>();
  152. }
  153. return Db.Queryable<SaveNameData>().Where(p => p.ProductNumber == name).ToList();
  154. }
  155. catch (Exception ex) { return new List<SaveNameData>(); }
  156. }
  157. }
  158. }