Env.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using DIL;
  2. using SCADA.CommonLib;
  3. using SCADA.CommonLib.Data.DIL;
  4. using SCADA.CommonLib.Helper;
  5. using SysManage.User;
  6. using System;
  7. using System.Collections.ObjectModel;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. namespace SCADA_DAQ
  12. {
  13. class Env
  14. {
  15. public static DILDB DAL = null;
  16. public static User NewUser;
  17. public static Schedual Schedual { get; internal set; } = Schedual.Instance;
  18. //public static readonly ConcurrentDictionary<string, BaseComm> CommResource = new ConcurrentDictionary<string, BaseComm>();
  19. public static DateTime StartTime { get; set; }
  20. public static int StartTick { get; internal set; }
  21. public static AutoSaveParameterItem<string> ProductName;
  22. public static AutoSaveParameterItem<string> CompanyName;
  23. public static AutoSaveParameterItem<string> MainDllName;
  24. public static AutoSaveParameterItem<string> DefaultUserName;
  25. public static AutoSaveParameterItem<string> WebServerAddress = AutoSaveParameterItem.Create("App.WebServer", "www.skdscada.com");
  26. public static AutoSaveParameterItem<bool> DataBaseNeedRest { get; set; } = AutoSaveParameterItem.Create("App.DatabaseNeedRest", false);
  27. public static AutoSaveParameterItem<string> ComputerTag;
  28. public static AutoSaveParameterItem<int> RESTfulPort;
  29. public static AutoSaveParameterItem<int> RPCPort;
  30. public static AutoSaveParameterItem<bool> SingleModel;
  31. public static AutoSaveParameterItem<SCADA.CommonLib.LoggerHelper.LogLevel> LogLevel;
  32. public static DataBaseConfig CurrentDbConfig { get; set; }
  33. public static AutoSaveParameterItem<DatabaseSelector> CurrentDataBase { get; set; }
  34. public static DataBaseConfig SysDBConfig { get; set; }
  35. public static AutoSaveParameterItem<DatabaseSelector> SysDataBase { get; set; }
  36. public static DataBaseConfig AlarmDBConfig { get; set; }
  37. public static AutoSaveParameterItem<DatabaseSelector> AlarmDataBase { get; set; }
  38. public static AutoSaveParameterItem<ObservableCollection<DataBaseConfig>> DataBaseResource { get; set; }
  39. /// <summary>
  40. /// 设备配置文件
  41. /// </summary>
  42. public static AutoSaveParameterItem<string> DeviceConfigFilePath;
  43. public static void ParameterInit()
  44. {
  45. ProductName = AutoSaveParameterItem.Create("App.ProductName", "", true);
  46. ProductName.DisplayName = "产品名称";
  47. CompanyName = AutoSaveParameterItem.Create("App.ProductName", "", true);
  48. CompanyName.DisplayName = "公司名称";
  49. MainDllName = AutoSaveParameterItem.Create("App.MainDllname", "", true);
  50. {
  51. MainDllName.DisplayName = "主dll名称";
  52. };
  53. DefaultUserName = AutoSaveParameterItem.Create("App.DefualtUserName", "", true);
  54. DefaultUserName.DisplayName = "默认用户";
  55. ComputerTag = AutoSaveParameterItem.Create("App.ComputerTag", "", true);
  56. ComputerTag.DisplayName = "电脑标签";
  57. ComputerTag.IsReadOnly = true;
  58. RESTfulPort = AutoSaveParameterItem.Create("App.RESTfulPort", 7888);
  59. RESTfulPort.DisplayName = "网页端口";
  60. RPCPort = AutoSaveParameterItem.Create("App.RPCPort", 7880);
  61. RPCPort.DisplayName = "本地RPC端口";
  62. SingleModel = AutoSaveParameterItem.Create("App.SingleModel", true);
  63. SingleModel.DisplayName = "单例模式";
  64. LogLevel = AutoSaveParameterItem.Create("App.LogLevel", SCADA.CommonLib.LoggerHelper.LogLevel.All);
  65. LogLevel.DisplayName = "日志级别";
  66. DeviceConfigFilePath = AutoSaveParameterItem.Create("DeviceConfigFilePath", "DeviceCfg.Json");
  67. DeviceConfigFilePath.DisplayName = "设备配置文件";
  68. CurrentDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig", new DatabaseSelector()
  69. { CurrentDataBaseConfig = "Default" });
  70. SysDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig.System", new DatabaseSelector()
  71. { CurrentDataBaseConfig = "SysDB" });
  72. AlarmDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig.Alarm", new DatabaseSelector()
  73. { CurrentDataBaseConfig = "AlarmDB" });
  74. DataBaseResource = AutoSaveParameterItem.Create("App.DataBaseResource",
  75. new ObservableCollection<DataBaseConfig>()
  76. {
  77. new DataBaseConfig()
  78. {
  79. ResourceName="Default",
  80. DBType = DatabaseType.Sqlite,
  81. FilePath = @"Data\QwPlatform.db"
  82. },
  83. new DataBaseConfig()
  84. {
  85. ResourceName="AlarmDB",
  86. DBType = DatabaseType.Sqlite,
  87. FilePath = @"Data\QwPlatform.db"
  88. },
  89. new DataBaseConfig()
  90. {
  91. ResourceName="SysDB",
  92. DBType = DatabaseType.Sqlite,
  93. FilePath = @"Data\LocalApp.db"
  94. },
  95. }, true);
  96. dataBaseConfigInit(DataBaseResource, CurrentDataBase.Value, out var currentconfig);
  97. dataBaseConfigInit(DataBaseResource, SysDataBase.Value, out var sysConfig);
  98. dataBaseConfigInit(DataBaseResource, AlarmDataBase.Value, out var alarmConfig);
  99. CurrentDbConfig = currentconfig;
  100. AlarmDBConfig = alarmConfig;
  101. SysDBConfig = sysConfig;
  102. void dataBaseConfigInit(ObservableCollection<DataBaseConfig> srouce, DatabaseSelector selector, out DataBaseConfig config)
  103. {
  104. config = DataBaseResource.Value.FirstOrDefault(t => t.ResourceName == selector?.CurrentDataBaseConfig);
  105. selector.DataBaseConfigResource = DataBaseResource.Value;
  106. if (config == null)
  107. {
  108. config = DataBaseResource.Value.FirstOrDefault();
  109. selector.CurrentDataBaseConfig = config.ResourceName;
  110. }
  111. selector.DataBaseConfig = config;
  112. }
  113. bool neeRestDatabase = DataBaseNeedRest.Value;
  114. void dbInit(DataBaseConfig dataBaseConfig, Type dbType, out BaseDB dilDb)
  115. {
  116. if (dataBaseConfig.DBType == DatabaseType.Sqlite) //如果是sqlite数据库就检查数据库的状态
  117. {
  118. string dbInfo = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dataBaseConfig.FilePath);
  119. //string dbInfo = Path.GetFullPath(dataBaseConfig.FilePath);
  120. dilDb = (BaseDB)Activator.CreateInstance(dbType, Path.GetDirectoryName(dbInfo), Path.GetFileName(dataBaseConfig.FilePath),
  121. dataBaseConfig.UserName,
  122. dataBaseConfig.Password);
  123. CheckDataBase(dilDb, Path.Combine(dilDb.ServerName, dilDb.DatabaseInfo), dataBaseConfig.NeedReset || DataBaseNeedRest.Value);
  124. dataBaseConfig.NeedReset = false;
  125. }
  126. else
  127. {
  128. dilDb = (BaseDB)Activator.CreateInstance(dbType, dataBaseConfig.DatabaseServer, dataBaseConfig.DataBaseName, dataBaseConfig.UserName, dataBaseConfig.Password);
  129. }
  130. }
  131. dbInit(CurrentDbConfig, typeof(DILDB), out BaseDB currentDB);
  132. dbInit(SysDBConfig, typeof(LocalDB.DIL.DILDB), out BaseDB sysDB);
  133. dbInit(AlarmDBConfig, typeof(DILDB), out BaseDB alarmDB);
  134. DAL = (DILDB)currentDB;
  135. GlobalEnv.Instance.GlobalDB = currentDB;
  136. GlobalEnv.Instance.AlarmDB = alarmDB;
  137. GlobalEnv.Instance.LocalAppDB = sysDB;
  138. NewUser = new User(DAL)
  139. {
  140. UserName = "Admin",
  141. };
  142. }
  143. static Env()
  144. {
  145. //XmlUserConfig.Create();
  146. //ParameterInit();
  147. }
  148. private static void CheckDataBase(BaseDB db, string databasepath, bool dataBaseNeedRest)
  149. {
  150. if (dataBaseNeedRest) //用户设置了重置数据库
  151. {
  152. System.Diagnostics.Trace.TraceInformation($"用户请求重置数据库,数据库开始重置");
  153. if (ReleseDatabase())
  154. {
  155. DataBaseNeedRest.Value = false; //数据库重置成功
  156. }
  157. }
  158. else
  159. {
  160. if (File.Exists(databasepath))
  161. {
  162. object dt = db.ExecuteScalar("PRAGMA integrity_check");
  163. if ($"{dt}".ToUpper() == "OK")
  164. {
  165. System.Diagnostics.Debug.WriteLine($"{databasepath} 数据库检测正常");
  166. }
  167. else
  168. {
  169. System.Diagnostics.Trace.TraceWarning($"检测到本地数据库已损坏,开始重置数据库");
  170. _ = ReleseDatabase();
  171. }
  172. }
  173. else
  174. {
  175. ReleseDatabase();
  176. }
  177. }
  178. bool ReleseDatabase()
  179. {
  180. try
  181. {
  182. if (File.Exists(databasepath))
  183. {
  184. File.Move(databasepath, $"{databasepath}.{DateTime.Now:yyyyMMddHHmmss}");
  185. }
  186. var assembly = Assembly.GetEntryAssembly();
  187. Stream res = null;
  188. if (Path.GetFileName(databasepath).ToUpper() == "LOCALAPP.DB")
  189. {
  190. res = new MemoryStream(LocalResource.LocalApp);
  191. }
  192. else if (Path.GetFileName(databasepath).ToUpper() == "QWPLATFORM.DB")
  193. {
  194. res = new MemoryStream(LocalResource.QwPlatform);
  195. }
  196. if (res == null) return true;
  197. using (res)
  198. {
  199. FileHelper.EnsureDirectoryExist(databasepath);
  200. byte[] save = ZipHelper.UnZip(res)[0];
  201. using (FileStream fsObj = new FileStream(databasepath, FileMode.Create))
  202. {
  203. fsObj.Write(save, 0, save.Length);
  204. fsObj.Close();
  205. }
  206. }
  207. System.Diagnostics.Debug.WriteLine($"{databasepath} 数据库修复成功");
  208. return true;
  209. }
  210. catch (Exception ex)
  211. {
  212. System.Diagnostics.Trace.Fail($"{databasepath} 数据库修复失败,{ex}");
  213. //_ = MessageBox.Show(Application.Current?.MainWindow, ex.ToString(), LanguageHelper.GetStringByKey("SysCfg_DatabaseRepairFail"));
  214. }
  215. return false;
  216. }
  217. }
  218. }
  219. }