Env.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using DIL;
  2. using SCADA.CommonLib;
  3. using SCADA.CommonLib.Data.DIL;
  4. using SCADA.CommonLib.Helper;
  5. using SCADA.Instrument.AIP;
  6. using SysManage.User;
  7. using System;
  8. using System.Collections.ObjectModel;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. namespace SCADA_DAQ
  13. {
  14. class Env
  15. {
  16. public static DILDB DAL = null;
  17. public static User NewUser;
  18. public static Schedual Schedual { get; internal set; } = Schedual.Instance;
  19. public static AutoSaveParameterItem<bool> DataBaseNeedRest { get; set; } = AutoSaveParameterItem.Create("App.DatabaseNeedRest", false);
  20. public static DataBaseConfig CurrentDbConfig { get; set; }
  21. public static AutoSaveParameterItem<DatabaseSelector> CurrentDataBase { get; set; }
  22. public static DataBaseConfig SysDBConfig { get; set; }
  23. public static AutoSaveParameterItem<DatabaseSelector> SysDataBase { get; set; }
  24. public static DataBaseConfig AlarmDBConfig { get; set; }
  25. public static AutoSaveParameterItem<DatabaseSelector> AlarmDataBase { get; set; }
  26. public static AutoSaveParameterItem<ObservableCollection<DataBaseConfig>> DataBaseResource { get; set; }
  27. /// <summary>
  28. /// 设备配置文件
  29. /// </summary>
  30. public static AutoSaveParameterItem<string> DeviceConfigFilePath;
  31. public static void ParameterInit(IApp app)
  32. {
  33. CurrentDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig", new DatabaseSelector()
  34. { CurrentDataBaseConfig = "Default" });
  35. SysDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig.System", new DatabaseSelector()
  36. { CurrentDataBaseConfig = "SysDB" });
  37. AlarmDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig.Alarm", new DatabaseSelector()
  38. { CurrentDataBaseConfig = "AlarmDB" });
  39. DataBaseResource = AutoSaveParameterItem.Create("App.DataBaseResource",
  40. new ObservableCollection<DataBaseConfig>()
  41. {
  42. new DataBaseConfig()
  43. {
  44. ResourceName="Default",
  45. DBType = DatabaseType.Sqlite,
  46. FilePath = @"Data\QwPlatform.db"
  47. },
  48. new DataBaseConfig()
  49. {
  50. ResourceName="AlarmDB",
  51. DBType = DatabaseType.Sqlite,
  52. FilePath = @"Data\QwPlatform.db"
  53. },
  54. new DataBaseConfig()
  55. {
  56. ResourceName="SysDB",
  57. DBType = DatabaseType.Sqlite,
  58. FilePath = @"Data\LocalApp.db"
  59. },
  60. }, true);
  61. dataBaseConfigInit(DataBaseResource, CurrentDataBase.Value, out var currentconfig);
  62. dataBaseConfigInit(DataBaseResource, SysDataBase.Value, out var sysConfig);
  63. dataBaseConfigInit(DataBaseResource, AlarmDataBase.Value, out var alarmConfig);
  64. CurrentDbConfig = currentconfig;
  65. AlarmDBConfig = alarmConfig;
  66. SysDBConfig = sysConfig;
  67. void dataBaseConfigInit(ObservableCollection<DataBaseConfig> srouce, DatabaseSelector selector, out DataBaseConfig config)
  68. {
  69. config = DataBaseResource.Value.FirstOrDefault(t => t.ResourceName == selector?.CurrentDataBaseConfig);
  70. selector.DataBaseConfigResource = DataBaseResource.Value;
  71. if (config == null)
  72. {
  73. config = DataBaseResource.Value.FirstOrDefault();
  74. selector.CurrentDataBaseConfig = config.ResourceName;
  75. }
  76. selector.DataBaseConfig = config;
  77. }
  78. bool neeRestDatabase = DataBaseNeedRest.Value;
  79. void dbInit(DataBaseConfig dataBaseConfig, Type dbType, out BaseDB dilDb)
  80. {
  81. if (dataBaseConfig.DBType == DatabaseType.Sqlite) //如果是sqlite数据库就检查数据库的状态
  82. {
  83. string dbInfo = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dataBaseConfig.FilePath);
  84. dilDb = (BaseDB)Activator.CreateInstance(dbType, Path.GetDirectoryName(dbInfo), Path.GetFileName(dataBaseConfig.FilePath),
  85. dataBaseConfig.UserName,
  86. dataBaseConfig.Password);
  87. CheckDataBase(dilDb, Path.Combine(dilDb.ServerName, dilDb.DatabaseInfo), dataBaseConfig.NeedReset || DataBaseNeedRest.Value);
  88. dataBaseConfig.NeedReset = false;
  89. }
  90. else
  91. {
  92. dilDb = (BaseDB)Activator.CreateInstance(dbType, dataBaseConfig.DatabaseServer, dataBaseConfig.DataBaseName, dataBaseConfig.UserName, dataBaseConfig.Password);
  93. }
  94. }
  95. dbInit(CurrentDbConfig, typeof(DILDB), out BaseDB currentDB);
  96. dbInit(SysDBConfig, typeof(LocalDB.DIL.DILDB), out BaseDB sysDB);
  97. dbInit(AlarmDBConfig, typeof(DILDB), out BaseDB alarmDB);
  98. DAL = (DILDB)currentDB;
  99. app.RuntimeInfo.GlobalDB = currentDB;
  100. app.RuntimeInfo.AlarmDB = alarmDB;
  101. app.RuntimeInfo.LocalAppDB = sysDB;
  102. NewUser = new User(DAL)
  103. {
  104. UserName = "Admin",
  105. };
  106. }
  107. static Env()
  108. {
  109. }
  110. private static void CheckDataBase(BaseDB db, string databasepath, bool dataBaseNeedRest)
  111. {
  112. if (dataBaseNeedRest) //用户设置了重置数据库
  113. {
  114. System.Diagnostics.Trace.TraceInformation($"用户请求重置数据库,数据库开始重置");
  115. if (ReleseDatabase())
  116. {
  117. DataBaseNeedRest.Value = false; //数据库重置成功
  118. }
  119. }
  120. else
  121. {
  122. if (File.Exists(databasepath))
  123. {
  124. //object dt = db.ExecuteScalar("PRAGMA integrity_check");
  125. //if ($"{dt}".ToUpper() == "OK")
  126. //{
  127. // System.Diagnostics.Debug.WriteLine($"{databasepath} 数据库检测正常");
  128. //}
  129. //else
  130. //{
  131. // System.Diagnostics.Trace.TraceWarning($"检测到本地数据库已损坏,开始重置数据库");
  132. // _ = ReleseDatabase();
  133. //}
  134. }
  135. else
  136. {
  137. ReleseDatabase();
  138. }
  139. }
  140. bool ReleseDatabase()
  141. {
  142. try
  143. {
  144. if (File.Exists(databasepath))
  145. {
  146. File.Move(databasepath, $"{databasepath}.{DateTime.Now:yyyyMMddHHmmss}");
  147. }
  148. var assembly = Assembly.GetEntryAssembly();
  149. Stream res = null;
  150. if (Path.GetFileName(databasepath).ToUpper() == "LOCALAPP.DB")
  151. {
  152. res = new MemoryStream(LocalResource.LocalApp);
  153. }
  154. else if (Path.GetFileName(databasepath).ToUpper() == "PLATFORM.DB")
  155. {
  156. res = new MemoryStream(LocalResource.Platform);
  157. }
  158. if (res == null) return true;
  159. using (res)
  160. {
  161. FileHelper.EnsureDirectoryExist(databasepath);
  162. byte[] save = ZipHelper.UnZip(res)[0];
  163. using (FileStream fsObj = new FileStream(databasepath, FileMode.Create))
  164. {
  165. fsObj.Write(save, 0, save.Length);
  166. fsObj.Close();
  167. }
  168. }
  169. System.Diagnostics.Debug.WriteLine($"{databasepath} 数据库修复成功");
  170. return true;
  171. }
  172. catch (Exception ex)
  173. {
  174. System.Diagnostics.Trace.Fail($"{databasepath} 数据库修复失败,{ex}");
  175. //_ = MessageBox.Show(Application.Current?.MainWindow, ex.ToString(), LanguageHelper.GetStringByKey("SysCfg_DatabaseRepairFail"));
  176. }
  177. return false;
  178. }
  179. }
  180. }
  181. }