ProcessExceptions.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using SKMC.Api.Client;
  2. using SKMC.Api.Client.Model;
  3. using SKMC.Api.Machine;
  4. using SKMC.Api.Machine.Control;
  5. using SKMC.Api.Common;
  6. using SKMC.Api.Common.Exceptions;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using Prism.Services.Dialogs;
  14. namespace SKMC.Api.Process.Control
  15. {
  16. /// <summary>
  17. /// 异常处理工具集
  18. /// </summary>
  19. public class ProcessExceptions
  20. {
  21. private readonly static ILogger log = LogFactory.Get();
  22. private readonly static IMachineBoardControl deviceBoardControl = ObjectFactory.Resolve<IMachineBoardControl>();
  23. private readonly static ClientCacher clientCacher = ObjectFactory.Resolve<ClientCacher>();
  24. private readonly static MachineCacher machineCacher = ObjectFactory.Resolve<MachineCacher>();
  25. public const string DefaultViewName = "ProcessExceptionsView";
  26. public const string NoticeViewName = "ProcessNotificationView";
  27. public static string CreateExceptionDialog(ExceptionShow exceptionShow, string dialogName = DefaultViewName)
  28. {
  29. if (exceptionShow == null) return null;
  30. if (dialogName != null)
  31. {
  32. return clientCacher.ShowExceptionDialog(dialogName, exceptionShow);
  33. }
  34. else
  35. {
  36. CreateExceptionDialog(exceptionShow);
  37. return null;
  38. }
  39. }
  40. /// <summary>
  41. /// 创建一个异常弹窗
  42. /// </summary>
  43. /// <param name="exceptionBase"></param>
  44. /// <param name="exceptionConfig"></param>
  45. /// <param name="dialogName"></param>
  46. public static string CreateExceptionDialog(ExceptionBase exceptionBase, ExceptionConfig exceptionConfig, string dialogName = DefaultViewName)
  47. {
  48. if (exceptionBase == null) return null;
  49. if (exceptionConfig == null) exceptionConfig = GetExceptionConfiger(exceptionBase);
  50. ExceptionShow exceptionShow = Convert(exceptionBase, exceptionConfig);
  51. return CreateExceptionDialog(exceptionShow, dialogName);
  52. }
  53. public static void CreateExceptionDialog(Exception exception)
  54. {
  55. MessageBox.Show($"出现未知异常: {exception.Message}, Detail: {exception.StackTrace}");
  56. }
  57. public static void CreateExceptionDialog(ExceptionShow exceptionShow)
  58. {
  59. MessageBox.Show($"出现异常, Code:{exceptionShow.Code}, Name: {exceptionShow.Name}, Level: {exceptionShow.Level}");
  60. }
  61. /// <summary>
  62. /// 创建一个提醒弹窗
  63. /// </summary>
  64. /// <param name="title">提醒标题</param>
  65. /// <param name="message">提醒内容</param>
  66. public static string CreateNotificationDialog(string title, string message, byte level = 0)
  67. {
  68. DialogParameters parameters = new DialogParameters();
  69. parameters.Add("model", new ClientNotification
  70. {
  71. Head = title,
  72. Detail = message,
  73. DateTime = DateTime.Now,
  74. Level = level
  75. });
  76. return clientCacher.ShowCommonDialog(NoticeViewName, parameters, null, false);
  77. }
  78. public static bool IsDialogShow(string id) => clientCacher.IsDialogShow(id);
  79. /// <summary>
  80. /// 关闭特定Id的对话框
  81. /// </summary>
  82. /// <param name="id"></param>
  83. public static void CloseDialog(string id) => clientCacher.CloseDialog(id);
  84. /// <summary>
  85. /// 关闭所有对话框
  86. /// </summary>
  87. public static void CloseAllDialogs() => clientCacher.CloseAllDialogs();
  88. private static ExceptionShow Convert(ExceptionBase exceptionBase, ExceptionConfig exceptionConfig)
  89. {
  90. ExceptionShow exceptionShow = new ExceptionShow(exceptionConfig)
  91. {
  92. Uid = Guid.NewGuid().ToString(),
  93. EncounterTime = DateTime.Now,
  94. RetryAction = exceptionBase.RetryAction,
  95. IgnoreAction = exceptionBase.IgnoreAction,
  96. AbortAction = exceptionBase.AbortAction,
  97. Level = exceptionBase.Level,
  98. Detail = exceptionBase.Detail
  99. };
  100. return exceptionShow;
  101. }
  102. private static ExceptionConfig GetExceptionConfiger(ExceptionBase exceptionBase)
  103. {
  104. // 先根据内部异常Type关联
  105. ExceptionConfig exceptionConfiger = machineCacher.GetException(exceptionBase.Type);
  106. // 再根据项目异常Code关联
  107. if (exceptionConfiger == null) exceptionConfiger = machineCacher.GetException(exceptionBase.Code);
  108. return exceptionConfiger;
  109. }
  110. public static string WrapThrows(Exception exception, string dialogViewName = DefaultViewName)
  111. {
  112. if (exception == null) return null;
  113. //deviceStatus.StatusBeforeAlarm = deviceStatus.Status;
  114. if (typeof(ExceptionBase).IsAssignableFrom(exception.GetType()))
  115. {
  116. ExceptionBase exceptionBase = exception as ExceptionBase;
  117. ExceptionConfig exceptionConfiger = GetExceptionConfiger(exceptionBase);
  118. exceptionBase.Level = exceptionConfiger.Level;
  119. // 停机异常
  120. if (exceptionConfiger.Level == 4)
  121. {
  122. deviceBoardControl.IsAlarm = true;
  123. deviceBoardControl.StopAction.Invoke();
  124. log.Error($"严重异常: {exception}");
  125. //if (deviceStatus.Status == (byte)DeviceStatusEnum.START)
  126. //{
  127. //}
  128. return CreateExceptionDialog(exceptionBase, exceptionConfiger, dialogViewName);
  129. }
  130. // 普通异常
  131. if (exceptionConfiger.Level == 2)
  132. {
  133. //if (deviceStatus.Status == (byte)DeviceStatusEnum.START)
  134. //{
  135. //}
  136. deviceBoardControl.IsAlarm = true;
  137. deviceBoardControl.PauseAction.Invoke();
  138. log.Error($"普通异常: {exception},{exception.StackTrace}");
  139. return CreateExceptionDialog (exceptionBase, exceptionConfiger, dialogViewName);
  140. }
  141. // 单站异常
  142. if (exceptionConfiger.Level == 1)
  143. {
  144. if (exceptionBase.StationId > 0)
  145. {
  146. deviceBoardControl.PauseOneAction.Invoke(exceptionBase.StationId);
  147. }
  148. log.Error($"单站异常: {exception}");
  149. return CreateExceptionDialog (exceptionBase, exceptionConfiger, dialogViewName);
  150. }
  151. else
  152. {
  153. log.Error($"其他异常: {exception}");
  154. return null;
  155. }
  156. }
  157. // 未知异常
  158. else
  159. {
  160. deviceBoardControl.IsAlarm = true;
  161. deviceBoardControl.StopAction.Invoke();
  162. log.Error($"未知异常: {exception.Message}", exception);
  163. CreateExceptionDialog(exception);
  164. return null;
  165. }
  166. }
  167. }
  168. }