using Aurora.Api; using AuroraCellTracker; using AuroraLaserScanner; using DB; using Hermes; using Matrox.MatroxImagingLibrary; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using VisionCommon; using WeldingCommon.Alarm; using WeldingCommon.Data; namespace WeldingCommon.Sequences { class SeqReceiveCellInfo { static readonly Logger Log = new Logger("SeqReceiveCellInfo"); const string SEQ_ENTER = "[SeqReceiveCellInfo] SEQ_ENTER"; // inputParam 으로 받자. const string SEQ_CHECK_INIT_CONDITION = "[SeqReceiveCellInfo] Check Init Condition"; // Check Init Condition const string SEQ_READ_CELL_INFO = "[SeqReceiveCellInfo] Read CellInfo"; const string SEQ_ADAPT_CELL_INFO = "[SeqReceiveCellInfo] Adapt CellInfo"; const string SEQ_SEND_CELL_INFO_TO_PLC = "[SeqReceiveCellInfo] SendC ellInfo To PLC"; const string SEQ_SAVE_RESULT_DATA = "[SeqReceiveCellInfo] Save Result Data"; const string SEQ_WRITE_COMPLETION_PLC_BIT = "[SeqReceiveCellInfo] Write Completion PLC Bit"; const string SEQ_COMPLETE = "[SeqReceiveCellInfo] SEQ_COMPLETE"; // 완료 static class ContextKey { public const string KEY_SEQUENCE_RESULT = "KEY_SEQUENCE_RESULT_SeqReceiveCellInfo"; public const string KEY_SEQUENCE_START_TIME = "KEY_SEQUENCE_START_TIME_SeqReceiveCellInfo"; } private const string REG_PATH = @"Software\H52A\H52A_ADPT_CELL"; private SeqReceiveCellInfo_InitParam _InitParam; private CellTracker _CellTracker; public SequenceBuilder Builder() { var sb = new SequenceBuilder(1000, true); ////var sb = new SequenceBuilder(1000, false); // With log sb.AddAction(SEQ_ENTER, Sequence_Enter); sb.AddDecision(SEQ_CHECK_INIT_CONDITION, CheckInitCondition); //분기 sb.AddAction(SEQ_READ_CELL_INFO, ReadCellInfo); sb.AddAction(SEQ_ADAPT_CELL_INFO, AdaptCellInfo); sb.AddAction(SEQ_SEND_CELL_INFO_TO_PLC, SendCellInfoToPLC); sb.AddAction(SEQ_SAVE_RESULT_DATA, SaveResultData); // 해당 공정의 정보를 기록 함. sb.AddAction(SEQ_WRITE_COMPLETION_PLC_BIT, WriteCompletionPLCBit); // 정상적으로 종료 되면, complete bit write sb.AddAction(SEQ_COMPLETE, Complete); sb.Flows(sb.Init > sb[SEQ_ENTER] > sb[SEQ_CHECK_INIT_CONDITION] > sb[SEQ_READ_CELL_INFO] > sb[SEQ_ADAPT_CELL_INFO] > sb[SEQ_SEND_CELL_INFO_TO_PLC] > sb[SEQ_SAVE_RESULT_DATA] > sb[SEQ_WRITE_COMPLETION_PLC_BIT] > sb[SEQ_COMPLETE]); sb.Flows(sb[SEQ_CHECK_INIT_CONDITION] > sb[SEQ_COMPLETE] > sb.Final); sb.OnStopped(OnStopped); sb.OnForceError(OnForceError); //sb.Draw("SeqReceiveCellInfo.pdf"); return sb; } public static void SaveString(string keyName, string value) { using (RegistryKey key = Registry.CurrentUser.CreateSubKey(REG_PATH)) { key?.SetValue(keyName, value, RegistryValueKind.String); } } public static string LoadString(string keyName, string defaultValue = "") { using (RegistryKey key = Registry.CurrentUser.OpenSubKey(REG_PATH)) { return key?.GetValue(keyName, defaultValue)?.ToString() ?? defaultValue; } } private SequenceActionResult Sequence_Enter(SequenceContext context, string id, SequenceActionResult param) { Log.I("SeqReceiveCellInfo Start"); _InitParam = param.Param as SeqReceiveCellInfo_InitParam; InitDev(context); if (!CheckDeviceReady(context)) { //Check Device AlarmManager.Alarm(AlarmCode.ALARM_SEQUENCE_ReceiveCellInfo_DeviceNotReady); Log.E("The devices are not active."); return null; } if (InitSequenceVariable(context) == false) { AlarmManager.Alarm(AlarmCode.ALARM_SEQUENCE_ReceiveCellInfo_InitVariableError); return null; } return SequenceAction.Ok(); } private bool InitSequenceVariable(SequenceContext context) { return true; } private void InitDev(SequenceContext context) { //Device Init _CellTracker = context.GetCellTracker(); } private bool CheckDeviceReady(SequenceContext context) { List deviceListToCheck = new List() { CommonDeviceIds.CellTracker }; bool isAllDeviceActivated = true; foreach (string item in deviceListToCheck) { if (!context.Devices[item].IsActivated) { isAllDeviceActivated = isAllDeviceActivated && false; } } return isAllDeviceActivated; } private bool CheckCondition() { if (CheckCondition_Interlock()) { if (InitializeDeviceStatus()) { return true; } } return false; } private bool CheckCondition_Interlock() { // PLC, Data, IO 등 Interlock checking return true; } private bool InitializeDeviceStatus() { // format 유지, ReceiveCellInfo 없음 return true; } private string CheckInitCondition(SequenceContext context, string id, ICollection branches, SequenceActionResult param) { if (!CheckCondition()) { AlarmManager.Alarm(AlarmCode.ALARM_SEQUENCE_ReceiveCellInfo_CheckInitCondition); Log.E("Condition is not in state to start sequence."); context.PutValue(ContextKey.KEY_SEQUENCE_RESULT, false); return SEQ_COMPLETE; } var startTime = DateTime.Now; context.PutValue(ContextKey.KEY_SEQUENCE_START_TIME, startTime); Log.D("ReceiveCellInfo Sequence CheckInitCondition"); return SEQ_READ_CELL_INFO; } private SequenceActionResult ReadCellInfo(SequenceContext context, string id, SequenceActionResult param) { _CellTracker.ReceiveData(); return SequenceAction.Ok(); } private SequenceActionResult AdaptCellInfo(SequenceContext context, string id, SequenceActionResult param) { var cellInfoStrings = _CellTracker.GetCellInfoStrings(); Status.GetProcessingWeldingData().WorkCellId = cellInfoStrings.CellID; Status.GetProcessingWeldingData().WorkLotId = cellInfoStrings.LotID; SaveString("WorkCellID", Status.GetProcessingWeldingData().WorkCellId); SaveString("WorkLotID", Status.GetProcessingWeldingData().WorkLotId); Log.I(string.Format($"AdaptCellInfo CellID : {cellInfoStrings.CellID} LotID : {cellInfoStrings.LotID}")); return SequenceAction.Ok(); } private SequenceActionResult SendCellInfoToPLC(SequenceContext context, string id, SequenceActionResult param) { // PLC 약속된 주소에 CellID, LotId 를 써준다. return SequenceAction.Ok(); } private SequenceActionResult SaveResultData(SequenceContext context, string id, SequenceActionResult param) { context.PutValue(ContextKey.KEY_SEQUENCE_RESULT, true); return SequenceAction.Ok(); } private SequenceActionResult WriteCompletionPLCBit(SequenceContext context, string id, SequenceActionResult param) { // 완료 비트 write 하는 Node 추가 필요. format 유지, ReceiveCellInfo 없음 Log.D("ReceiveCellInfo Sequence WriteCompletionPLCBit"); return SequenceAction.Ok(); } private SequenceActionResult Complete(SequenceContext context, string id, SequenceActionResult param) { RestoreDeviceStatus(); Log.I("ReceiveCellInfo Complete"); context.TryGetValue(ContextKey.KEY_SEQUENCE_RESULT, out bool seqResult); SeqReceiveCellInfo_OutParam result = new SeqReceiveCellInfo_OutParam() { Success = seqResult }; if (seqResult == false) { AlarmManager.Alarm(AlarmCode.ALARM_SEQUENCE_ReceiveCellInfo_ResultFailed); } Log.D("ReceiveCellInfo Sequence Complete"); return SequenceAction.Ok(result); } private bool RestoreDeviceStatus() { return true; } private void OnForceError(SequenceContext arg1, string arg2) { Log.I("SequenceAction Ok : OnForceError"); } private void OnStopped(SequenceContext obj) { Log.I("SequenceAction Ok : OnStopped"); } } public class SeqReceiveCellInfo_InitParam : InitParam { } public class SeqReceiveCellInfo_OutParam : OutParam { } }