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
///
/// 工序设备运行切面 用于修饰DeviceBase的SetAndRun
///
[Serializable]
public sealed class DeviceRunAspect : OnMethodBoundaryAspect
{
//LogHelper logHelper = new LogHelper();
///
/// 设备操作前
///
///
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
}
}
///
/// 设备完成操作后
///
///
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
}
}
}
///
/// 设备状态变动切面 主要监控设备的大状态变动
///
[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
}