123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using DIL;
- using SCADA.CommonLib;
- using SCADA.CommonLib.Data.DIL;
- using SCADA.CommonLib.Helper;
- using SCADA.Instrument.AIP;
- using SysManage.User;
- using System;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- namespace SCADA_DAQ
- {
- class Env
- {
- public static DILDB DAL = null;
- public static User NewUser;
- public static Schedual Schedual { get; internal set; } = Schedual.Instance;
- public static AutoSaveParameterItem<bool> DataBaseNeedRest { get; set; } = AutoSaveParameterItem.Create("App.DatabaseNeedRest", false);
- public static DataBaseConfig CurrentDbConfig { get; set; }
- public static AutoSaveParameterItem<DatabaseSelector> CurrentDataBase { get; set; }
- public static DataBaseConfig SysDBConfig { get; set; }
- public static AutoSaveParameterItem<DatabaseSelector> SysDataBase { get; set; }
- public static DataBaseConfig AlarmDBConfig { get; set; }
- public static AutoSaveParameterItem<DatabaseSelector> AlarmDataBase { get; set; }
- public static AutoSaveParameterItem<ObservableCollection<DataBaseConfig>> DataBaseResource { get; set; }
- /// <summary>
- /// 设备配置文件
- /// </summary>
- public static AutoSaveParameterItem<string> DeviceConfigFilePath;
- public static void ParameterInit(IApp app)
- {
- CurrentDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig", new DatabaseSelector()
- { CurrentDataBaseConfig = "Default" });
- SysDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig.System", new DatabaseSelector()
- { CurrentDataBaseConfig = "SysDB" });
- AlarmDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig.Alarm", new DatabaseSelector()
- { CurrentDataBaseConfig = "AlarmDB" });
- DataBaseResource = AutoSaveParameterItem.Create("App.DataBaseResource",
- new ObservableCollection<DataBaseConfig>()
- {
- new DataBaseConfig()
- {
- ResourceName="Default",
- DBType = DatabaseType.Sqlite,
- FilePath = @"Data\QwPlatform.db"
- },
- new DataBaseConfig()
- {
- ResourceName="AlarmDB",
- DBType = DatabaseType.Sqlite,
- FilePath = @"Data\QwPlatform.db"
- },
- new DataBaseConfig()
- {
- ResourceName="SysDB",
- DBType = DatabaseType.Sqlite,
- FilePath = @"Data\LocalApp.db"
- },
- }, true);
- dataBaseConfigInit(DataBaseResource, CurrentDataBase.Value, out var currentconfig);
- dataBaseConfigInit(DataBaseResource, SysDataBase.Value, out var sysConfig);
- dataBaseConfigInit(DataBaseResource, AlarmDataBase.Value, out var alarmConfig);
- CurrentDbConfig = currentconfig;
- AlarmDBConfig = alarmConfig;
- SysDBConfig = sysConfig;
- void dataBaseConfigInit(ObservableCollection<DataBaseConfig> srouce, DatabaseSelector selector, out DataBaseConfig config)
- {
- config = DataBaseResource.Value.FirstOrDefault(t => t.ResourceName == selector?.CurrentDataBaseConfig);
- selector.DataBaseConfigResource = DataBaseResource.Value;
- if (config == null)
- {
- config = DataBaseResource.Value.FirstOrDefault();
- selector.CurrentDataBaseConfig = config.ResourceName;
- }
- selector.DataBaseConfig = config;
- }
- bool neeRestDatabase = DataBaseNeedRest.Value;
- void dbInit(DataBaseConfig dataBaseConfig, Type dbType, out BaseDB dilDb)
- {
- if (dataBaseConfig.DBType == DatabaseType.Sqlite) //如果是sqlite数据库就检查数据库的状态
- {
- string dbInfo = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dataBaseConfig.FilePath);
- dilDb = (BaseDB)Activator.CreateInstance(dbType, Path.GetDirectoryName(dbInfo), Path.GetFileName(dataBaseConfig.FilePath),
- dataBaseConfig.UserName,
- dataBaseConfig.Password);
- CheckDataBase(dilDb, Path.Combine(dilDb.ServerName, dilDb.DatabaseInfo), dataBaseConfig.NeedReset || DataBaseNeedRest.Value);
- dataBaseConfig.NeedReset = false;
- }
- else
- {
- dilDb = (BaseDB)Activator.CreateInstance(dbType, dataBaseConfig.DatabaseServer, dataBaseConfig.DataBaseName, dataBaseConfig.UserName, dataBaseConfig.Password);
- }
- }
- dbInit(CurrentDbConfig, typeof(DILDB), out BaseDB currentDB);
- dbInit(SysDBConfig, typeof(LocalDB.DIL.DILDB), out BaseDB sysDB);
- dbInit(AlarmDBConfig, typeof(DILDB), out BaseDB alarmDB);
- DAL = (DILDB)currentDB;
- app.RuntimeInfo.GlobalDB = currentDB;
- app.RuntimeInfo.AlarmDB = alarmDB;
- app.RuntimeInfo.LocalAppDB = sysDB;
- NewUser = new User(DAL)
- {
- UserName = "Admin",
- };
- }
- static Env()
- {
- }
- private static void CheckDataBase(BaseDB db, string databasepath, bool dataBaseNeedRest)
- {
- if (dataBaseNeedRest) //用户设置了重置数据库
- {
- System.Diagnostics.Trace.TraceInformation($"用户请求重置数据库,数据库开始重置");
- if (ReleseDatabase())
- {
- DataBaseNeedRest.Value = false; //数据库重置成功
- }
- }
- else
- {
- if (File.Exists(databasepath))
- {
- //object dt = db.ExecuteScalar("PRAGMA integrity_check");
- //if ($"{dt}".ToUpper() == "OK")
- //{
- // System.Diagnostics.Debug.WriteLine($"{databasepath} 数据库检测正常");
- //}
- //else
- //{
- // System.Diagnostics.Trace.TraceWarning($"检测到本地数据库已损坏,开始重置数据库");
- // _ = ReleseDatabase();
- //}
- }
- else
- {
- ReleseDatabase();
- }
- }
- bool ReleseDatabase()
- {
- try
- {
- if (File.Exists(databasepath))
- {
- File.Move(databasepath, $"{databasepath}.{DateTime.Now:yyyyMMddHHmmss}");
- }
- var assembly = Assembly.GetEntryAssembly();
- Stream res = null;
- if (Path.GetFileName(databasepath).ToUpper() == "LOCALAPP.DB")
- {
- res = new MemoryStream(LocalResource.LocalApp);
- }
- else if (Path.GetFileName(databasepath).ToUpper() == "PLATFORM.DB")
- {
- res = new MemoryStream(LocalResource.Platform);
- }
- if (res == null) return true;
- using (res)
- {
- FileHelper.EnsureDirectoryExist(databasepath);
- byte[] save = ZipHelper.UnZip(res)[0];
- using (FileStream fsObj = new FileStream(databasepath, FileMode.Create))
- {
- fsObj.Write(save, 0, save.Length);
- fsObj.Close();
- }
- }
- System.Diagnostics.Debug.WriteLine($"{databasepath} 数据库修复成功");
- return true;
- }
- catch (Exception ex)
- {
- System.Diagnostics.Trace.Fail($"{databasepath} 数据库修复失败,{ex}");
- //_ = MessageBox.Show(Application.Current?.MainWindow, ex.ToString(), LanguageHelper.GetStringByKey("SysCfg_DatabaseRepairFail"));
- }
- return false;
- }
- }
- }
- }
|