| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- 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
- {
- /// <summary>
- /// 异常处理工具集
- /// </summary>
- public class ProcessExceptions
- {
- private readonly static ILogger log = LogFactory.Get();
- private readonly static IMachineBoardControl deviceBoardControl = ObjectFactory.Resolve<IMachineBoardControl>();
- private readonly static ClientCacher clientCacher = ObjectFactory.Resolve<ClientCacher>();
- private readonly static MachineCacher machineCacher = ObjectFactory.Resolve<MachineCacher>();
- 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;
- }
- }
- /// <summary>
- /// 创建一个异常弹窗
- /// </summary>
- /// <param name="exceptionBase"></param>
- /// <param name="exceptionConfig"></param>
- /// <param name="dialogName"></param>
- 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}");
- }
- /// <summary>
- /// 创建一个提醒弹窗
- /// </summary>
- /// <param name="title">提醒标题</param>
- /// <param name="message">提醒内容</param>
- 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);
- /// <summary>
- /// 关闭特定Id的对话框
- /// </summary>
- /// <param name="id"></param>
- public static void CloseDialog(string id) => clientCacher.CloseDialog(id);
- /// <summary>
- /// 关闭所有对话框
- /// </summary>
- 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;
- }
- }
- }
- }
|