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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Bro.Common.Base;
using Bro.Common.Interface;
using static Bro.Common.Helper.EnumHelper;
using PostSharp.Aspects;
//using PostSharp.Laos;
 
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 : OnMethodBoundaryAspect
    {
        //LogHelper logHelper = new LogHelper();
        /// <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 UserException(string.Format("{0}设备不在开启状态,无法操作!", device.Name), EnumHelper.LogType.Exception_Error);
 
                return;
            }
            #endregion
 
            IOperationConfig config = args.Arguments.FirstOrDefault() as IOperationConfig;
            if (config == null)
            {
                throw new ProcessException("设备操作转入参数类型错误!", null);
            }
 
            if (device.DeviceMode == DeviceMode.Run)
            {
                #region 通知流程控制工序开始
                //Client.InvokeAsync("DeviceHub", "RunStart", JsonConvert.SerializeObject(config, JsonHelper.JsonSetWithType));
 
                //Client.InvokeAsync("DeviceHub", "RunStart", config);
 
                //StateMachine.OnProcessStart?.Invoke(JsonConvert.SerializeObject(config, JsonHelper.JsonSetWithType));
                //StateMachine.OnProcessStart?.Invoke(config);
                #endregion
 
                #region 日志操作
                //Task.Run(() =>
                //{
                //    if (!string.IsNullOrWhiteSpace(config.ProcessId))
                //    {
                //        PRC_PROCESS process = new ProcessHelper().GetProcessById(config.ProcessId);
                //        if (process != null && process.IS_LOG == 1)
                //        {
                //            LOG_INFO log = new LOG_INFO();
                //            log.LOG_TYPE = (int)LogType.Info_Process;
                //            log.LOG_TITLE = string.Format("设备名称:{0};开始工序:{1}", device.Name, config.ProcessName);
                //            log.LOG_TIME = DateTime.Now;
                //            logHelper.NewLog("ProcessStart", log);
                //        }
                //    }
                //});
                #endregion
            }
        }
 
        /// <summary>
        /// 设备完成操作后
        /// </summary>
        /// <param name="args"></param>
        public override void OnSuccess(MethodExecutionArgs args)
        {
            DeviceBase device = args.Instance as DeviceBase;
 
            if (device.DeviceMode == DeviceMode.Run)
            {
                IOperationConfig inputConfig = args.Arguments.FirstOrDefault() as IOperationConfig;
                IOperationConfig outputConfig = args.ReturnValue as IOperationConfig;
 
                if (outputConfig == null)
                {
                    outputConfig = new OperationConfigCommon();
                }
 
                //工序编号传递
                //outputConfig.ProcessId = inputConfig.ProcessId;
                //outputConfig.ProcessName = inputConfig.ProcessName;
 
                #region 通知流程控制本道工序结束 开始下道工序
                //Client.InvokeAsync("DeviceHub", "RunEnd", JsonConvert.SerializeObject(outputConfig, JsonHelper.JsonSetWithType));
                //Client.InvokeAsync("DeviceHub", "RunEnd", outputConfig);
                //StateMachine.OnProcessEnd?.Invoke(JsonConvert.SerializeObject(outputConfig, JsonHelper.JsonSetWithType));
                //StateMachine.OnProcessEnd?.Invoke(outputConfig);
                #endregion
 
                #region 日志操作
                //Task.Run(() =>
                //{
                //    PRC_PROCESS process = new ProcessHelper().GetProcessById(inputConfig.ProcessId);
                //    if (process != null && process.IS_LOG == 1)
                //    {
                //        LOG_INFO log = new LOG_INFO();
                //        log.LOG_TYPE = (int)LogType.Info_Process;
                //        log.LOG_TITLE = string.Format("设备名称:{0};结束工序:{1}", device.Name, inputConfig.ProcessName);
                //        log.LOG_TIME = DateTime.Now;
                //        logHelper.NewLog("ProcessEnd", log);
                //    }
                //});
                #endregion
            }
        }
    }
 
    /// <summary>
    /// 设备状态变动切面 主要监控设备的大状态变动
    /// </summary>
    [Serializable]
    public class DeviceStateChangedAspect : OnMethodBoundaryAspect
    {
        public override void OnSuccess(MethodExecutionArgs args)
        {
            DeviceBase device = args.Instance as DeviceBase;
 
            #region 通知设备的状态变动
            if (device.CurrentState != EnumHelper.DeviceState.TBD)
            {
                //device.OnDeviceStateChanged?.Invoke(device.CurrentState);
            }
            #endregion
 
            #region 日志操作
            #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.OnDeviceException?.Invoke(device, ex);
                #endregion
 
                #region 异常日志记录
                #endregion
            }
 
            args.FlowBehavior = FlowBehavior.ThrowException;
        }
    }
 
    [Serializable]
    public class ProcessExceptionAspect : OnExceptionAspect
    {
        public override void OnException(MethodExecutionArgs args)
        {
            //base.OnException(args);
 
            Exception ex = args.Exception;
 
            if (!(ex is ProcessException))
            {
                new ProcessException(ex);
            }
 
            args.FlowBehavior = FlowBehavior.Return;
        }
    }
    #endregion
}