218 lines
9.1 KiB
C#
218 lines
9.1 KiB
C#
using Common;
|
|
using Command;
|
|
using ADP.ViewModels;
|
|
using ADP.ViewModels.Dialogs;
|
|
using ADP.Views;
|
|
using ADP.Views;
|
|
using ADP.Views.Dialogs;
|
|
using Logger;
|
|
using Notifications.Wpf.Core;
|
|
using ORM;
|
|
using Service.Implement;
|
|
using Service.Interface;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using UIShare.PubEvent;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
using UIShare.GlobalVariable;
|
|
using UIShare;
|
|
using System;
|
|
using DeviceCommand.Devices;
|
|
using DeviceCommand.Base;
|
|
using AutoMapper;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using ADP.Profiles;
|
|
using Prism.Dialogs;
|
|
using 加密狗;
|
|
|
|
namespace ADP
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for App.xaml
|
|
/// </summary>
|
|
public partial class App : PrismApplication
|
|
{
|
|
protected override Window CreateShell()
|
|
{
|
|
//UI线程未捕获异常处理事件
|
|
this.DispatcherUnhandledException += OnDispatcherUnhandledException;
|
|
//Task线程内未捕获异常处理事件
|
|
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
|
|
////多线程异常
|
|
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
|
|
return Container.Resolve<ShellView>();
|
|
}
|
|
private void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
LoggerHelper.Error(e.Exception.Message, e.Exception.StackTrace);
|
|
}
|
|
|
|
private void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
|
|
{
|
|
LoggerHelper.Error(e.Exception.Message, e.Exception.StackTrace);
|
|
}
|
|
|
|
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
//记录dump文件
|
|
Exception ex = e.ExceptionObject as Exception;
|
|
MiniDump.TryDump($"dumps\\Error_{DateTime.Now:yyyy-MM-dd HH-mm-ss-ms}.dmp", MiniDump.Option.WithFullMemory, ex);
|
|
}
|
|
protected override void OnInitialized()
|
|
{
|
|
// 向命令库 CommandDialog 注入弹窗能力(命令库是纯 .NET 类库,不引用 WPF,这里通过委托实现依赖倒置)
|
|
var dialogService = Container.Resolve<IDialogService>();
|
|
CommandDialog.弹窗处理器 = (弹窗类型, 弹窗详细, 是否阻塞, 自动关闭秒数, ct) =>
|
|
{
|
|
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
CancellationTokenRegistration registration = default;
|
|
if (是否阻塞)
|
|
{
|
|
registration = ct.Register(() => tcs.TrySetCanceled(ct));
|
|
}
|
|
// 命令可能在后台线程执行,统一切回 UI 线程弹窗
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
var param = new DialogParameters
|
|
{
|
|
{ "Title", 弹窗类型 switch
|
|
{
|
|
CommandDialog.DialogType.Warning => "警告",
|
|
CommandDialog.DialogType.Error => "错误",
|
|
_ => "信息提示"
|
|
} },
|
|
{ "Message", 弹窗详细 ?? string.Empty },
|
|
{ "Icon", 弹窗类型 switch
|
|
{
|
|
CommandDialog.DialogType.Warning => "warn",
|
|
CommandDialog.DialogType.Error => "error",
|
|
_ => "info"
|
|
} },
|
|
{ "ShowOk", true },
|
|
{ "AutoCloseSeconds", (double)自动关闭秒数 }
|
|
};
|
|
if (是否阻塞)
|
|
{
|
|
// 阻塞:用户手动关闭或自动关闭后才继续执行步骤
|
|
dialogService.ShowDialog("MessageBox", param, _ =>
|
|
{
|
|
registration.Dispose();
|
|
tcs.TrySetResult(true);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
// 非阻塞:弹出后步骤立即继续(不等关闭回调)
|
|
dialogService.Show("MessageBox", param, _ => { });
|
|
tcs.TrySetResult(true);
|
|
}
|
|
});
|
|
return tcs.Task;
|
|
};
|
|
|
|
// 配置全局日志分发器:按 CurrentScope 路由到对应 LogArea
|
|
var globalInfo = Container.Resolve<GlobalInfo>();
|
|
LoggerHelper.Progress = new ScopeLogDispatcher(globalInfo);
|
|
|
|
//初始化数据库
|
|
DatabaseConfig.SetTenant(10001);
|
|
DatabaseConfig.InitSqlite();
|
|
DatabaseConfig.CreateDatabaseAndCheckConnection(createDatabase: true, checkConnection: true);
|
|
SqlSugarContext.InitDatabase();
|
|
//显示登录窗口
|
|
var login = Container.Resolve<LoginModuleView>();
|
|
var re = Container.Resolve<IRegionManager>();
|
|
RegionManager.SetRegionManager(login, re);
|
|
RegionManager.SetRegionManager(Application.Current.MainWindow, re);
|
|
login.Show();
|
|
}
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
if (System.Diagnostics.Debugger.IsAttached)
|
|
{
|
|
base.OnStartup(e);
|
|
return;
|
|
}
|
|
|
|
string strUserSn = "ACC77E6F627236F3D02FA8EA1BE35794";
|
|
string strApiKey = "9185203127F9DE4FB4BC2F073F6CC583D9BED1B6713EF00A0C06625EDD0FAE862164280F5B2AADC090C4878E96DDFE05DDE3952F9B6F1FA483A340FB34A5D83B";
|
|
|
|
Byte[] buffer = new Byte[256];
|
|
buffer = System.Text.Encoding.Default.GetBytes(strUserSn);
|
|
uint retcode;
|
|
retcode = 加密狗驱动类.验证加密狗SN(buffer);
|
|
if (retcode != 0)
|
|
{
|
|
MessageBox.Show(string.Format("设置UserSN失败 error code: 0x{0:x}", retcode));
|
|
return;
|
|
}
|
|
|
|
uint count = 0;
|
|
retcode = 加密狗API类.VikeyFind(ref count);
|
|
if (retcode != 0 || count == 0)
|
|
{
|
|
MessageBox.Show("未找到加密狗");
|
|
return;
|
|
}
|
|
|
|
buffer = System.Text.Encoding.Default.GetBytes(strApiKey);
|
|
retcode = 加密狗驱动类.验证加密狗APIkey(0, buffer);
|
|
if (retcode != 0)
|
|
{
|
|
MessageBox.Show(string.Format("设置api密钥失败 error code: 0x{0:x}", retcode));
|
|
return;
|
|
}
|
|
|
|
var remaining = 加密狗驱动类.检查剩余时间(0);
|
|
if (remaining <= TimeSpan.Zero)
|
|
{
|
|
MessageBox.Show("加密狗已到期或未能读取时间,请联系供应商。", "加密狗验证失败", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
return;
|
|
}
|
|
|
|
base.OnStartup(e);
|
|
}
|
|
protected override void RegisterRequiredTypes(IContainerRegistry containerRegistry)
|
|
{
|
|
base.RegisterRequiredTypes(containerRegistry);
|
|
//注册全局变量
|
|
containerRegistry.RegisterScoped<SystemConfig>();
|
|
containerRegistry.RegisterScoped<StepRunning>();
|
|
containerRegistry.RegisterScoped<ScopedContext>();
|
|
containerRegistry.RegisterScoped<DeviceManager>();
|
|
containerRegistry.RegisterSingleton<GlobalInfo>();
|
|
containerRegistry.RegisterSingleton<HardwareDataBroadcaster>();
|
|
containerRegistry.RegisterSingleton<CANSignalBroadcaster>();
|
|
//注册AutoMapper
|
|
var config = new MapperConfiguration(
|
|
cfg => cfg.AddProfile<AutoMapperProfile>(),
|
|
NullLoggerFactory.Instance
|
|
);
|
|
containerRegistry.RegisterSingleton<IMapper>(() => config.CreateMapper());
|
|
}
|
|
protected override void RegisterTypes(IContainerRegistry containerRegistry)
|
|
{
|
|
//注册弹窗
|
|
containerRegistry.RegisterDialog<MessageBoxView, MessageBoxViewModel>("MessageBox");
|
|
// 注册通知管理器
|
|
INotificationManager NotificationManager = new NotificationManager();
|
|
containerRegistry.RegisterInstance<INotificationManager>(NotificationManager);
|
|
// 注册仓储
|
|
containerRegistry.RegisterScoped(typeof(SqlSugarRepository<>));
|
|
//注册服务
|
|
containerRegistry.Register<IMonitorValueService, MonitorValueService>();
|
|
containerRegistry.Register<ITestReportService, TestReportService>();
|
|
}
|
|
//指定模块加载方式(需要手动将模块生成的dll放入Modules文件夹中)
|
|
protected override IModuleCatalog CreateModuleCatalog()
|
|
{
|
|
//指定模块加载方式为从文件夹中以反射发现并加载module(推荐用法)
|
|
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
|
|
}
|
|
}
|
|
}
|