领胜LDS 键盘AOI检测项目
xcd
2020-07-15 d3a44f202c0b12cbac67c71129c3c4f480df55b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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.ThrowException;
        }
    }
    #endregion
}