ProcessExceptions.cs 7.3 KB

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