Env.cs 9.1 KB

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