/// <summary> /// 解析启动参数 /// </summary> /// <param name="args"></param> static void parseStartupArgs(string[] args) { Parser.Default.ParseArguments <StartupArgs>(args).WithParsed(opt => { Logger.Debug("当前操作系统:-" + YUtil.GetOsVersion()); Logger.Debug("当前版本:-" + YUtil.GetAppVersion(Assembly.GetExecutingAssembly())); bool autoStart = bool.Parse(opt.IsAutoStart); YUtil.SetAppAutoStart("Asylum", autoStart); Logger.Debug("是否开机自启动:-" + autoStart); Logger.Debug("HmiPro.exe 路径:-" + opt.HmiProPath); GlobalConfig.StartupArgs = opt; }).WithNotParsed(err => { Logger.Debug("启动命令解析错误"); throw new Exception("启动命令解析错误"); }); }
/// <summary> /// LoggerHelper 和 Assets Helper 已经在 App.xaml.cs 中初始化了,所以这里不必要初始化了 /// </summary> bool globalConfigLoad() { updateLoadingMessage("正在准备系统资源文件", 0.15); Thread.Sleep(CmdOptions.GlobalOptions.WaitSec * 1000); updateLoadingMessage("正在检查系统启动环境...", 0.17); if (processIsStarted()) { var message = "系统重复启动异常"; App.Store.Dispatch(new SysActions.SetLoadingMessage(message, 0.18)); shutdownAppAfterSec(10, 0.18, "重复启动系统异常"); return(false); } updateLoadingMessage("正在初始化异常配置...", 0.20); ExceptionHelper.Init(); updateLoadingMessage("正在加载系统配置...", 0.23); GlobalConfig.Load(YUtil.GetAbsolutePath(".\\Profiles\\Global.xls")); updateLoadingMessage("正在初始化 ActiveMq...", 0.27); ActiveMqHelper.Init(HmiConfig.MqConn, HmiConfig.MqUserName, HmiConfig.MqUserPwd); updateLoadingMessage("正在初始化 MongoDb...", 0.30); MongoHelper.Init(HmiConfig.MongoConn); updateLoadingMessage("正在初始化 InfluxDb...", 0.33); InfluxDbHelper.Init($"http://{HmiConfig.InfluxDbIp}:8086", HmiConfig.InfluxCpmDbName); updateLoadingMessage("正在同步时间...", 0.35); syncTime(!HmiConfig.IsDevUserEnv); Logger.Debug("当前操作系统:" + YUtil.GetOsVersion()); Logger.Debug("当前版本:" + YUtil.GetAppVersion(Assembly.GetExecutingAssembly())); Logger.Debug("是否为开发环境:" + HmiConfig.IsDevUserEnv); Logger.Debug("浮点精度:" + HmiConfig.MathRound); return(true); }
/// <summary> /// 处理命令 /// </summary> /// <param name="e"></param> void hmiConfigInit(StartupEventArgs e) { StartupLog.StartArgs = string.Join(",", e.Args); updateLoadingMessage("正在解析命令...", 0.01); Parser.Default.ParseArguments <CmdOptions>(e.Args).WithParsed(opt => { //指定Hmi名称(调试的时候可用,方便启用某机台的配置) opt.HmiName = opt.HmiName.ToUpper(); //Profiles 文件夹 opt.ProfilesFolder = YUtil.GetAbsolutePath(opt.ProfilesFolder); //Profiles/Dev(Prod) 文件夹 var configFolder = opt.ProfilesFolder + "\\" + opt.Mode; opt.ConfigFolder = configFolder; //Profiles/Assets 文件夹 var assetsFolder = opt.ProfilesFolder + @"\Assets"; updateLoadingMessage("正在唤醒终端...", 0.01); if (bool.Parse(opt.ShowConsole)) { ConsoleHelper.Show(); } Console.WriteLine("当前系统环境:-" + YUtil.GetOsVersion()); Console.WriteLine("开机自启动: -" + opt.AutoSatrt); Console.WriteLine("配置文件夹:-" + opt.ProfilesFolder); Console.WriteLine("当前运行模式:-" + opt.Mode); Console.WriteLine("当前程序版本:-" + YUtil.GetAppVersion(Assembly.GetExecutingAssembly())); Console.WriteLine("启用mock:-" + opt.Mock); Console.WriteLine("Sqlite数据库:-" + opt.SqlitePath); Console.WriteLine("资源文件夹:-" + assetsFolder); if (!string.IsNullOrEmpty(opt.HmiName)) { Console.WriteLine("指定 Hmi:-" + opt.HmiName); } updateLoadingMessage("配置开机自启...", 0.02); YUtil.SetAppAutoStart(GetType().ToString(), bool.Parse(opt.AutoSatrt)); updateLoadingMessage("初始化Hmi配置...", 0.03); var configFile = configFolder + $@"\Hmi.Config.{opt.Config}.json"; HmiConfig.Load(configFile); Console.WriteLine("指定配置文件:-" + configFile); updateLoadingMessage("初始化工艺字典...", 0.04); HmiConfig.InitCraftBomZhsDict(assetsFolder + @"\Dicts\工艺Bom.xls"); updateLoadingMessage("初始化资源文件...", 0.05); AssetsHelper.Init(YUtil.GetAbsolutePath(assetsFolder)); updateLoadingMessage("初始化日志服务...", 0.06); LoggerHelper.Init(YUtil.GetAbsolutePath(HmiConfig.LogFolder)); updateLoadingMessage("初始化 Sqlite...", 0.08); HmiConfig.SqlitePath = YUtil.GetAbsolutePath(opt.SqlitePath); SqliteHelper.Init(YUtil.GetAbsolutePath(HmiConfig.SqlitePath)); //保留启动参数 CmdOptions.GlobalOptions = opt; CmdOptions.StartupEventArgs = e; }).WithNotParsed(err => { var logger = new LoggerService(HmiConfig.LogFolder) { DefaultLocation = "StartError" }; updateLoadingMessage($"解析命令异常..", 0); string msg = ""; err?.ToList()?.ForEach(r => { msg += r.ToString(); }); logger.Error("解析命令参数异常:\r\n参数: " + string.Join(",", e.Args) + "\r\n内容:" + msg); throw new Exception("启动参数异常" + msg); }); //记录程序崩溃日志 AppDomain.CurrentDomain.UnhandledException += (s, ue) => { var logger = new LoggerService(HmiConfig.LogFolder) { DefaultLocation = "UnhandleExp" }; var message = $"程序崩溃:{ue.ExceptionObject}\r\n当前可用内存:{YUtil.GetAvaliableMemoryByte() / 1000000} M"; //将错误日志写入mongoDb logger.ErrorWithDb(message, MongoHelper.LogsDb, MongoHelper.UnhandleExceptionCollection); //1 秒钟后重启程序 if (!HmiConfig.IsDevUserEnv) { YUtil.SetTimeout(1000, Restart); } }; }