using SKMC.Api.Client; using SKMC.Api.Client.Model; using SKMC.Api.Machine; using SKMC.Api.Machine.Control; using SKMC.Api.Common; using SKMC.Api.Common.Exceptions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using Prism.Services.Dialogs; using SKMC.Api.Common.Logger; namespace SKMC.Api.Process.Control { /// /// 异常处理工具集 /// public class ProcessExceptions { private readonly static ILogger log = LogFactory.Get(); private readonly static IMachineBoardControl deviceBoardControl = ObjectFactory.Resolve(); private readonly static ClientCacher clientCacher = ObjectFactory.Resolve(); private readonly static MachineCacher machineCacher = ObjectFactory.Resolve(); public const string DefaultViewName = "ProcessExceptionsView"; public const string NoticeViewName = "ProcessNotificationView"; public static string CreateExceptionDialog(ExceptionShow exceptionShow, string dialogName = DefaultViewName) { if (exceptionShow == null) return null; if (dialogName != null) { return clientCacher.ShowExceptionDialog(dialogName, exceptionShow); } else { CreateExceptionDialog(exceptionShow); return null; } } /// /// 创建一个异常弹窗 /// /// /// /// public static string CreateExceptionDialog(ExceptionBase exceptionBase, ExceptionConfig exceptionConfig, string dialogName = DefaultViewName) { if (exceptionBase == null) return null; if (exceptionConfig == null) exceptionConfig = GetExceptionConfiger(exceptionBase); ExceptionShow exceptionShow = Convert(exceptionBase, exceptionConfig); return CreateExceptionDialog(exceptionShow, dialogName); } public static void CreateExceptionDialog(Exception exception) { MessageBox.Show($"出现未知异常: {exception.Message}, Detail: {exception.StackTrace}"); } public static void CreateExceptionDialog(ExceptionShow exceptionShow) { MessageBox.Show($"出现异常, Code:{exceptionShow.Code}, Name: {exceptionShow.Name}, Level: {exceptionShow.Level}"); } /// /// 创建一个提醒弹窗 /// /// 提醒标题 /// 提醒内容 public static string CreateNotificationDialog(string title, string message, byte level = 0) { DialogParameters parameters = new DialogParameters(); parameters.Add("model", new ClientNotification { Head = title, Detail = message, DateTime = DateTime.Now, Level = level }); return clientCacher.ShowCommonDialog(NoticeViewName, parameters, null, false); } public static bool IsDialogShow(string id) => clientCacher.IsDialogShow(id); /// /// 关闭特定Id的对话框 /// /// public static void CloseDialog(string id) => clientCacher.CloseDialog(id); /// /// 关闭所有对话框 /// public static void CloseAllDialogs() => clientCacher.CloseAllDialogs(); private static ExceptionShow Convert(ExceptionBase exceptionBase, ExceptionConfig exceptionConfig) { ExceptionShow exceptionShow = new ExceptionShow(exceptionConfig) { Uid = Guid.NewGuid().ToString(), EncounterTime = DateTime.Now, RetryAction = exceptionBase.RetryAction, IgnoreAction = exceptionBase.IgnoreAction, AbortAction = exceptionBase.AbortAction, Level = exceptionBase.Level, Detail = exceptionBase.Detail }; return exceptionShow; } private static ExceptionConfig GetExceptionConfiger(ExceptionBase exceptionBase) { // 先根据内部异常Type关联 ExceptionConfig exceptionConfiger = machineCacher.GetException(exceptionBase.Type); // 再根据项目异常Code关联 if (exceptionConfiger == null) exceptionConfiger = machineCacher.GetException(exceptionBase.Code); return exceptionConfiger; } public static string WrapThrows(Exception exception, string dialogViewName = DefaultViewName) { if (exception == null) return null; //deviceStatus.StatusBeforeAlarm = deviceStatus.Status; if (typeof(ExceptionBase).IsAssignableFrom(exception.GetType())) { ExceptionBase exceptionBase = exception as ExceptionBase; ExceptionConfig exceptionConfiger = GetExceptionConfiger(exceptionBase); exceptionBase.Level = exceptionConfiger.Level; // 停机异常 if (exceptionConfiger.Level == 4) { deviceBoardControl.IsAlarm = true; deviceBoardControl.StopAction.Invoke(); log.Error($"严重异常: {exception}"); //if (deviceStatus.Status == (byte)DeviceStatusEnum.START) //{ //} return CreateExceptionDialog(exceptionBase, exceptionConfiger, dialogViewName); } // 普通异常 if (exceptionConfiger.Level == 2) { //if (deviceStatus.Status == (byte)DeviceStatusEnum.START) //{ //} deviceBoardControl.IsAlarm = true; deviceBoardControl.PauseAction.Invoke(); log.Error($"普通异常: {exception},{exception.StackTrace}"); return CreateExceptionDialog (exceptionBase, exceptionConfiger, dialogViewName); } // 单站异常 if (exceptionConfiger.Level == 1) { if (exceptionBase.StationId > 0) { deviceBoardControl.PauseOneAction.Invoke(exceptionBase.StationId); } log.Error($"单站异常: {exception}"); return CreateExceptionDialog (exceptionBase, exceptionConfiger, dialogViewName); } else { log.Error($"其他异常: {exception}"); return null; } } // 未知异常 else { deviceBoardControl.IsAlarm = true; deviceBoardControl.StopAction.Invoke(); log.Error($"未知异常: {exception.Message}", exception); CreateExceptionDialog(exception); return null; } } } }