123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Aitex.Common.Util;
- using Aitex.Core.RT.Log;
- using Aitex.Core.Util;
- using FabConnect.SecsGemInterface.Common;
- using FabConnect.SecsGemInterface.Common.ToolModel;
- using FabConnect.SecsGemInterface.GemModel;
- using Virgo_DCommon;
- namespace Virgo_DRT.HostWrapper
- {
- public class FaHost : IFaHost
- {
- public string FaCommunicationState
- {
- get { return _gem.CommunicationState.ToString(); }
- }
- public string FaControlState
- {
- get { return _gem.ControlState.ToString(); }
- }
- public string FaControlSubState
- {
- get
- {
- return _gem.ControlRemoteSwitch.ToString();
- }
- }
- public bool IsConnected
- {
- get { return _gem.IsConnected; }
- }
- public int SpoolingState
- {
- get
- {
- return (int)_gem.SpoolingState;
- }
- }
- public string SpoolingActual
- {
- get
- {
- return
- _gem.GetAttribute(GEMVariables.SpoolCountActual, AttributeType.SV)
- ;
- }
- }
- public string SpoolingTotal
- {
- get { return _gem.GetAttribute(GEMVariables.SpoolCountTotal, AttributeType.SV); }
- }
- public string SpoolingFullTime
- {
- get { return _gem.GetAttribute(GEMVariables.SpoolFullTime, AttributeType.SV); }
- }
- public string SpoolingStartTime
- {
- get { return _gem.GetAttribute(GEMVariables.SpoolStartTime, AttributeType.SV); }
- }
- public bool IsSpoolingEnable
- {
- get { return Convert.ToBoolean(_gem.GetAttribute(GEMVariables.SpoolEnabled, AttributeType.EC)); }
- }
- private readonly GEMController _gem = new GEMController();
- public IHostCallback _equipment;
- private HashSet<string> _systemBuildInEc = new HashSet<string>();
- private HashSet<string> _systemBuildInVariables = new HashSet<string>();
- private PeriodicJob _faMonitorThread;
- private FixSizeQueue<FaEventItem> _lstEvent = new FixSizeQueue<FaEventItem>(300);
- FALogFileCleaner _logCleaner = new FALogFileCleaner();
- public void Initialize(IHostCallback equipment, string modelFile)
- {
- _equipment = equipment;
- _gem.CommunicationStateChanged += OnCommunicationStateChanged;
- _gem.RemoteCommandS2F41In += _gem_RemoteCommandS2F41In; ;
- _gem.RemoteCommandS2F49In += _gem_RemoteCommandS2F49In; ;
- _gem.ControlStateChanged += _gem_ControlStateChanged;
- _gem.ShowTrialMessageBox = false;
-
- _gem.Initialize(modelFile, PathManager.GetLogDir());
- //get system build in ec
- _systemBuildInEc = new HashSet<string>();
- _systemBuildInEc.Add("EstablishCommunicationsTimeout");
- _systemBuildInEc.Add("MaxSpoolTransmit");
- _systemBuildInEc.Add("OverWriteSpool");
- _systemBuildInEc.Add("MaxSpoolCapacity");
- _systemBuildInEc.Add("SpoolEnabled");
- _systemBuildInEc.Add("TimeFormat");
- //initial system build in variable
- _systemBuildInVariables.Add("AlarmsEnabled");
- _systemBuildInVariables.Add("AlarmsSet");
- _systemBuildInVariables.Add("Clock");
- _systemBuildInVariables.Add("ControlState");
- _systemBuildInVariables.Add("EventsEnabled");
- _systemBuildInVariables.Add("PPExecName");
- _systemBuildInVariables.Add("PreviousProcessState");
- _systemBuildInVariables.Add("ProcessState");
- _systemBuildInVariables.Add("SpoolCountActual");
- _systemBuildInVariables.Add("SpoolCountTotal");
- _systemBuildInVariables.Add("SpoolFullTime");
- _systemBuildInVariables.Add("SpoolStartTime");
- _systemBuildInVariables.Add("SpoolState");
- _systemBuildInVariables.Add("SpoolSubstate");
- _logCleaner.Run();
- _faMonitorThread = new PeriodicJob(200, MonitorFaTask, "Monitor FA Thread", true);
- }
- private void _gem_ControlStateChanged(object sender, SECsEventArgs e)
- {
- if (_gem.ControlState == ControlState.OnlineLocal)
- {
- _gem.SetRemote();
- }
- }
- public void Invoke(string method, object[] args)
- {
- switch (method)
- {
- case FACommandName.FAEnable:
- _gem.SetEnable();
- break;
- case FACommandName.FADisable:
- _gem.SetDisable();
- break;
- case FACommandName.FAOnline:
- _gem.SetOnline();
- break;
- case FACommandName.FAOffline:
- _gem.SetOffline();
- break;
- case FACommandName.FALocal:
- _gem.SetLocal();
- break;
- case FACommandName.FARemote:
- _gem.SetRemote();
- break;
- case FACommandName.FAEnableSpooling:
- SetEnableSpooling();
- break;
- case FACommandName.FADisableSpooling:
- SetDisableSpooling();
- break;
- }
- }
- private bool _gem_RemoteCommandS2F41In(SECsTransaction trans)
- {
- try
- {
- bool ret = true;
- string reason = string.Empty;
- SECsMessage reply = _gem.Services.CustomMessage.CreateMessage(2, 42, false);
-
- string remoteCommandName = trans.Primary.DataItem[0][0].ToString();
- switch (remoteCommandName)
- {
- case "ScanSlot":
- {
- if (trans.Primary.DataItem[0][1][0][0].ToString() != "PORT_ID" )
- throw new Exception("ScanSlot Command Format Not Correct");
- string portId = trans.Primary.DataItem[0][1][0][1].ToString();
- ret = _equipment.ScanSlot(portId, out reason);
- }
- break;
- }
- reply.DataItem.AddList();
- if (ret)
- {
- reply.DataItem[0].Add("HCACK", 0, SECsFormat.Binary);
- }
- else
- {
- reply.DataItem[0].Add("HCACK", 3, SECsFormat.Binary);
- reply.DataItem[0].Add("CPVALUE", reason, SECsFormat.Ascii);
- }
- _gem.SendReply(reply, trans.Id);
- }
- catch (Exception ex)
- {
- LOG.Write(string.Format("Handle_S2F41 Exception: {0}", ex.Message));
- SECsMessage reply = _gem.Services.CustomMessage.CreateMessage(2, 42, false);
- reply.DataItem.AddList();
- reply.DataItem[0].Add("HCACK", 3/*At least one parameter is invalid*/, SECsFormat.Binary);
- reply.DataItem[0].AddList();
- reply.DataItem[0][1].AddList();
- reply.DataItem[0][1].Add("CPVALUE", ex.Message, SECsFormat.Ascii);
- _gem.SendReply(reply, trans.Id);
- }
- return true;
- }
- private bool _gem_RemoteCommandS2F49In(SECsTransaction trans)
- {
- try
- {
- bool ret = true;
- string reason = string.Empty;
- SECsMessage reply = _gem.Services.CustomMessage.CreateMessage(2, 50, false);
- string remoteCommandName = trans.Primary.DataItem[0][2].ToString();
- switch (remoteCommandName.ToUpper())
- {
- case "START-TRANSFER":
- {
- if (trans.Primary.DataItem[0][3][0][0].ToString() != "TRANS_MODE" ||
- trans.Primary.DataItem[0][3][1][0].ToString() != "CAR_ID" ||
- trans.Primary.DataItem[0][3][2][0].ToString() != "LOT_ID" ||
- trans.Primary.DataItem[0][3][3][0].ToString() != "SRC_PORT_ID" ||
- trans.Primary.DataItem[0][3][4][0].ToString() != "WAF_CNT" ||
- trans.Primary.DataItem[0][3][5][0].ToString() != "EXIST_FLAG" ||
- trans.Primary.DataItem[0][3][6][0].ToString() != "WAFER_ID" ||
- trans.Primary.DataItem[0][3][7][0].ToString() != "LASERMARK1" ||
- trans.Primary.DataItem[0][3][8][0].ToString() != "LASERMARK2" ||
- trans.Primary.DataItem[0][3][9][0].ToString() != "TRANS_FLAG" ||
- trans.Primary.DataItem[0][3][10][0].ToString() != "DEST_PORT_ID" ||
- trans.Primary.DataItem[0][3][11][0].ToString() != "DEST_SLOT_NO" ||
- trans.Primary.DataItem[0][3][12][0].ToString() != "DEST_PORT_CTGRY" )
- throw new Exception("START-TRANSFER Command Format Not Correct");
- //HostRecipe recipe = new HostRecipe();
-
- //ret = _equipment.StartTransfer(recipe, out reason);
- }
- break;
- case "PP-SELECT":
- {
- if (trans.Primary.DataItem[0][3][0][0].ToString() != "RECIPE_ID" ||
- trans.Primary.DataItem[0][3][0][1].ToString() != "HostRecipe" )
- throw new Exception("PP-SELECT Command Format Not Correct");
- string recipeId = trans.Primary.DataItem[0][3][0][1].ToString();
-
- ret = _equipment.PPSelect(recipeId, out reason);
- }
- break;
- }
- reply.DataItem.AddList();
- if (ret)
- {
- reply.DataItem[0].Add("HCACK", 0, SECsFormat.Binary);
- }
- else
- {
- reply.DataItem[0].Add("HCACK", 3, SECsFormat.Binary);
- reply.DataItem[0].Add("CPVALUE", reason, SECsFormat.Ascii);
- }
- _gem.SendReply(reply, trans.Id);
- }
- catch (Exception ex)
- {
- //WriteLog(string.Format("Handle_S2F49 Exception: {0}", ex.Message));
- SECsMessage reply = _gem.Services.CustomMessage.CreateMessage(2, 50, false);
- reply.DataItem.AddList();
- reply.DataItem[0].Add("HCACK", 3/*At least one parameter is invalid*/, SECsFormat.Binary);
- reply.DataItem[0].AddList();
- reply.DataItem[0][1].AddList();
- reply.DataItem[0][1].Add("CPVALUE", ex.Message, SECsFormat.Ascii);
- _gem.SendReply(reply, trans.Id);
- }
- return true;
- }
- public void Terminate()
- {
- _faMonitorThread.Stop();
- _logCleaner.Stop();
- }
- public void Enable()
- {
- _gem.SetEnable();
- }
- public void Disable()
- {
- _gem.SetDisable();
- }
- public void NotifyEvent(string eventName, Dictionary<string, string> dvid)
- {
- _lstEvent.Enqueue(new FaEventItem(){dvid = dvid,EventName = eventName,IsAlarm = false});
- }
- public void NotifyAlarm(string alarmName, Dictionary<string, string> dvid)
- {
- _lstEvent.Enqueue(new FaEventItem() { dvid = dvid, EventName = alarmName, IsAlarm = true });
- }
- public void SetLocalControl()
- {
- Task.Factory.StartNew(() =>
- {
- _gem.SetLocal();
- });
- }
- public void SetRemoteControl()
- {
- Task.Factory.StartNew(() =>
- {
- _gem.SetRemote();
- });
- }
- public void SetEnableSpooling()
- {
- Task.Factory.StartNew(() =>
- {
- _gem .SetAttribute(GEMVariables.SpoolEnabled, AttributeType.EC, "true");
- });
- }
- public void SetDisableSpooling()
- {
- Task.Factory.StartNew(() =>
- {
- _gem .SetAttribute(GEMVariables.SpoolEnabled, AttributeType.EC, "false");
- });
- }
- public bool MonitorFaTask()
- {
- try
- {
- FaEventItem ev;
- while (_lstEvent.TryDequeue(out ev))
- {
- if (ev.dvid != null)
- {
- foreach (var dvid in ev.dvid)
- {
- SetDVIDValue(dvid.Key, dvid.Value);
- }
- }
- if (ev.IsAlarm)
- {
- SetAlarm(ev.EventName);
- }
- else
- {
- SendEvent(ev.EventName);
- }
- }
- SynchronizeSVIDValue();
- }
- catch (Exception ex)
- {
- System.Diagnostics.Trace.WriteLine(ex);
- }
- return true;
- }
- private void OnCommunicationStateChanged(object sender, SECsEventArgs e)
- {
- if (_gem.CommunicationState == CommunicationState.EnabledCommunicating)
- {
- _gem.SetOnline();
- _gem.SetRemote();
- }
- }
- private void SynchronizeSVIDValue()
- {
- try
- {
- foreach (SVID sv in _gem.EquipmentModel.StatusVariables.SVIDCollection)
- {
- if (sv != null && !_systemBuildInVariables.Contains(sv.logicalName))
- {
- if (sv.valueType == SECSFormats.List)
- {
- List<string> svData = _equipment.GetListSvidValue(sv.logicalName);
- if (svData != null && svData.Count > 0)
- {
- SECsDataItem data = new SECsDataItem(SECsFormat.List);
- foreach (var item in svData)
- {
- data.Add(item, item);
- }
- _gem.SetListAttribute(sv.logicalName, AttributeType.SV, data);
- }
- else
- {
- SECsDataItem data = new SECsDataItem(SECsFormat.List);
- data.Clear();
- _gem.SetListAttribute(sv.logicalName, AttributeType.SV, data);
- }
- }
- else
- {
- string svDataValue = _equipment.GetSvidValue(sv.logicalName);
- if (!string.IsNullOrEmpty(svDataValue))
- {
- if (sv.valueType == SECSFormats.Boolean) svDataValue = ConvertToBoolean(svDataValue).ToString();
- _gem.SetAttribute(sv.logicalName, AttributeType.SV, svDataValue);
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- LOG.Write("Synchronize FA Model Data exception:" + ex.Message);
- }
- }
-
- private bool SetDVIDValue(string localName, string value)
- {
- if (value == null) return false;
- //if (!string.IsNullOrEmpty(value))
- //{
- if (_gem.EquipmentModel == null || !_gem.EquipmentModel.DataVariables.DVIDCollection.IsExistLogicalName(localName))
- return false;
- foreach (VariableType dv in _gem.EquipmentModel.DataVariables.DVIDCollection)
- {
- if (dv.logicalName == localName)
- {
- if (dv.valueType == SECSFormats.Boolean)
- {
- value = ConvertToBoolean(value).ToString();
- }
- break;
- }
- }
- _gem.SetAttribute(localName, AttributeType.DV, value);
- //}
- return true;
- }
- private bool ConvertToBoolean(string value)
- {
- if (value == "0" || value.ToLower() == "false") return false;
- else return true;
- }
- private void SendEvent(string eventName)
- {
- try
- {
- if (_gem.EquipmentModel != null)
- {
- var allEvents = _gem.GetAllEnabledEvents();
- if (allEvents.Contains(eventName))
- {
- _gem.SendCollectionEvent(eventName);
- }
- else
- {
- //LOG.Write(string.Format("sendEvent failed,not find:", eventName));
- }
- //LOG.Write(string.Format("【FA2SendEvent--{0}】", eventName));
- }
- }
- catch (Exception ex)
- {
- LOG.Write("SendEvent Error:" + ex.Message);
- }
- }
- public void SetAlarm(string alarmTag)
- {
- Task.Factory.StartNew(() =>
- {
- //if (_gem.IsAlarmSet(alarmTag))
- //{
- // _gem.ClearAlarm(alarmTag);
- //}
- var allAlarms = _gem.GetAllEnabledAlarms();
- if (allAlarms.Contains(alarmTag))
- {
- _gem.SetAlarm(alarmTag);
- }
- else
- {
- //WriteLog(string.Format("sendAlarm failed,not find:", alarmTag));
- }
- //WriteLog(string.Format("【FA3SetAlarm--{0}】", alarmTag));
- });
- }
- }
- }
|