using Bro.Common.Base;
|
using Newtonsoft.Json;
|
using PostSharp.Aspects;
|
using System;
|
|
namespace Bro.Common.Helper
|
{
|
public static class JsonHelper
|
{
|
public static JsonSerializerSettings JsonSetWithType = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All };
|
}
|
|
#region PostSharp AOP related
|
|
/// <summary>
|
/// 工序设备运行切面 用于修饰DeviceBase的SetAndRun
|
/// </summary>
|
[Serializable]
|
public sealed class DeviceRunAspect : DeviceExceptionAspect
|
{
|
/// <summary>
|
/// 设备操作前
|
/// </summary>
|
/// <param name="args"></param>
|
public override void OnEntry(MethodExecutionArgs args)
|
{
|
#region 检查设备状态
|
DeviceBase device = args.Instance as DeviceBase;
|
if (device.CurrentState != EnumHelper.DeviceState.DSOpen)
|
{
|
throw new ProcessException($"{device.Name}不在开启状态,无法操作!");
|
}
|
#endregion
|
|
#region 启用日志
|
device.LogAsync(DateTime.Now, $"{device.Name}开始{args.Method.Name}", "");
|
#endregion
|
}
|
|
/// <summary>
|
/// 设备完成操作后
|
/// </summary>
|
/// <param name="args"></param>
|
public override void OnSuccess(MethodExecutionArgs args)
|
{
|
DeviceBase device = args.Instance as DeviceBase;
|
|
#region 完成日志
|
device.LogAsync(DateTime.Now, $"{device.Name}完成{args.Method.Name}", "");
|
#endregion
|
}
|
}
|
|
[Serializable]
|
public class DeviceExceptionAspect : OnMethodBoundaryAspect
|
{
|
public override void OnException(MethodExecutionArgs args)
|
{
|
DeviceBase device = args.Instance as DeviceBase;
|
if (device.CurrentState != EnumHelper.DeviceState.DSExcept)
|
{
|
device.CurrentState = EnumHelper.DeviceState.DSExcept;
|
device.CurrentStateToBe = EnumHelper.DeviceState.DSExcept;
|
Exception ex = args.Exception;
|
|
#region 通知设备的状态变动
|
device.OnExceptionOccured?.Invoke(DateTime.Now, ex);
|
#endregion
|
|
#region 异常日志记录
|
device.LogAsync(DateTime.Now, $"{device.Name}异常信息", ex.GetExceptionMessage());
|
#endregion
|
}
|
|
args.FlowBehavior = FlowBehavior.ThrowException;
|
}
|
}
|
|
[Serializable]
|
public class ProcessExceptionAspect : OnExceptionAspect
|
{
|
public override void OnException(MethodExecutionArgs args)
|
{
|
Exception ex = args.Exception;
|
|
//if (!(ex is ProcessException))
|
//{
|
// new ProcessException(ex);
|
//}
|
|
args.FlowBehavior = FlowBehavior.Return;
|
}
|
}
|
#endregion
|
}
|