123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using DIL;
- using SCADA.CommonLib;
- using SCADA.CommonLib.Helper;
- using SysManage.User;
- using System;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Linq;
- using System.Collections.Generic;
- 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 readonly ConcurrentDictionary<string, BaseComm> CommResource = new ConcurrentDictionary<string, BaseComm>();
- public static DateTime StartTime { get; set; }
- public static int StartTick { get; internal set; }
- public static AutoSaveParameterItem<string> ProductName;
- public static AutoSaveParameterItem<string> CompanyName;
- public static AutoSaveParameterItem<string> MainDllName;
- public static AutoSaveParameterItem<string> DefaultUserName;
- public static AutoSaveParameterItem<string> WebServerAddress = AutoSaveParameterItem.Create("App.WebServer", "www.imaodou.com.cn");
- public static AutoSaveParameterItem<bool> DataBaseNeedRest { get; set; } = AutoSaveParameterItem.Create("App.DatabaseNeedRest", false);
- public static AutoSaveParameterItem<string> ComputerTag;
- public static AutoSaveParameterItem<int> RESTfulPort;
- public static AutoSaveParameterItem<int> RPCPort;
- public static AutoSaveParameterItem<bool> SingleModel;
- public static AutoSaveParameterItem<SCADA.CommonLib.LoggerHelper.LogLevel> LogLevel;
- public static DataBaseConfig LocalDbConfig { get; set; }
- public static AutoSaveParameterItem<DatabaseSelector> CurrentDataBase { get; set; }
- public static AutoSaveParameterItem<ObservableCollection<DataBaseConfig>> DataBaseResource { get; set; }
- /// <summary>
- /// 设备配置文件
- /// </summary>
- public static AutoSaveParameterItem<string> DeviceConfigFilePath;
- static void ParameterInit()
- {
- ProductName = AutoSaveParameterItem.Create("App.ProductName", "", true);
- ProductName.DisplayName = "产品名称";
- CompanyName = AutoSaveParameterItem.Create("App.ProductName", "", true);
- CompanyName.DisplayName = "公司名称";
- MainDllName = AutoSaveParameterItem.Create("App.MainDllname", "", true);
- {
- MainDllName.DisplayName = "主dll名称";
- };
- DefaultUserName = AutoSaveParameterItem.Create("App.DefualtUserName", "", true);
- DefaultUserName.DisplayName = "默认用户";
- ComputerTag = AutoSaveParameterItem.Create("App.ComputerTag", "", true);
- ComputerTag.DisplayName = "电脑标签";
- ComputerTag.IsReadOnly = true;
- RESTfulPort = AutoSaveParameterItem.Create("App.RESTfulPort", 7888);
- RESTfulPort.DisplayName = "网页端口";
- RPCPort = AutoSaveParameterItem.Create("App.RPCPort", 7880);
- RPCPort.DisplayName = "本地RPC端口";
- SingleModel = AutoSaveParameterItem.Create("App.SingleModel", true);
- SingleModel.DisplayName = "单例模式";
- LogLevel = AutoSaveParameterItem.Create("App.LogLevel", SCADA.CommonLib.LoggerHelper.LogLevel.All);
- LogLevel.DisplayName = "日志级别";
- DeviceConfigFilePath = AutoSaveParameterItem.Create("DeviceConfigFilePath", "DeviceCfg.xml");
- DeviceConfigFilePath.DisplayName = "设备配置文件";
- CurrentDataBase = AutoSaveParameterItem.Create("App.DataBaseConfig", new DatabaseSelector()
- {
- CurrentDataBaseConfig = "Default"
- });
- DataBaseResource = AutoSaveParameterItem.Create("App.DataBaseResource",
- new ObservableCollection<DataBaseConfig>()
- {
- new DataBaseConfig()
- {
- ResourceName="Default",
- DBType = DatabaseType.Sqlite,
- FilePath = @"Data\QwPlatform.db"
- }
- }, true);
- LocalDbConfig = DataBaseResource.Value.Where(t =>
- t.ResourceName == CurrentDataBase.Value.CurrentDataBaseConfig).FirstOrDefault();
- CurrentDataBase.Value.DataBaseConfigResource = DataBaseResource;
- if (LocalDbConfig == null)
- {
- LocalDbConfig = DataBaseResource.Value.FirstOrDefault();
- CurrentDataBase.Value.CurrentDataBaseConfig = LocalDbConfig.ResourceName;
- }
- CurrentDataBase.Value.DataBaseType = $"{LocalDbConfig.DBType}";
- }
- static Env()
- {
- XmlUserConfig.Create();
- ParameterInit();
- Task.Factory.StartNew(() =>
- {
- if (LocalDbConfig.DBType == DatabaseType.Sqlite)
- {
- string dbInfo = Path.GetFullPath(LocalDbConfig.FilePath);
- DAL = new DIL.DILDB(Path.GetDirectoryName(dbInfo), Path.GetFileName(LocalDbConfig.FilePath));
- CheckDataBase(Path.Combine(DAL.ServerName, DAL.DatabaseInfo), DataBaseNeedRest.Value);
- }
- else
- {
- DAL = new DIL.DILDB(LocalDbConfig.DatabaseServer, LocalDbConfig.DataBaseName, LocalDbConfig.UserName, LocalDbConfig.Password);
- }
- GlobalEnv.Instance.GlobalDB = DAL;
- LocalDB.DIL.DILDB localdb = new LocalDB.DIL.DILDB($"{AppDomain.CurrentDomain.BaseDirectory}Data", "LocalApp.db");
- GlobalEnv.Instance.LocalAppDB = localdb;
- NewUser = new User(DAL)
- {
- UserName = "Admin",
- };
- });
- }
- private static void CheckDataBase(string databasepath, bool dataBaseNeedRest)
- {
- //var dataBaseNeedRest = AutoSaveParameterItem.Create("App.DatabaseNeedRest", false); //是否需要重置数据库
- //var databasepath = Path.Combine(DAL.ServerName, DAL.DatabaseInfo);
- if (dataBaseNeedRest) //用户设置了重置数据库
- {
- System.Diagnostics.Trace.TraceInformation($"用户请求重置数据库,数据库开始重置");
- if (ReleseDatabase())
- {
- DataBaseNeedRest.Value = false; //数据库重置成功
- }
- }
- else
- {
- if (File.Exists(databasepath))
- {
- object dt = DAL.ExecuteScalar("PRAGMA integrity_check");
- if ($"{dt}".ToUpper() == "OK")
- {
- System.Diagnostics.Debug.WriteLine($"数据库检测正常");
- }
- 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() == "QWPLATFORM.DB")
- {
- res = new MemoryStream(LocalResource.QwPlatform);
- }
- 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;
- }
- }
- }
- }
|