FaHost.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Aitex.Common.Util;
  5. using Aitex.Core.RT.Log;
  6. using Aitex.Core.Util;
  7. using FabConnect.SecsGemInterface.Common;
  8. using FabConnect.SecsGemInterface.Common.ToolModel;
  9. using FabConnect.SecsGemInterface.GemModel;
  10. using Virgo_DCommon;
  11. namespace Virgo_DRT.HostWrapper
  12. {
  13. public class FaHost : IFaHost
  14. {
  15. public string FaCommunicationState
  16. {
  17. get { return _gem.CommunicationState.ToString(); }
  18. }
  19. public string FaControlState
  20. {
  21. get { return _gem.ControlState.ToString(); }
  22. }
  23. public string FaControlSubState
  24. {
  25. get
  26. {
  27. return _gem.ControlRemoteSwitch.ToString();
  28. }
  29. }
  30. public bool IsConnected
  31. {
  32. get { return _gem.IsConnected; }
  33. }
  34. public int SpoolingState
  35. {
  36. get
  37. {
  38. return (int)_gem.SpoolingState;
  39. }
  40. }
  41. public string SpoolingActual
  42. {
  43. get
  44. {
  45. return
  46. _gem.GetAttribute(GEMVariables.SpoolCountActual, AttributeType.SV)
  47. ;
  48. }
  49. }
  50. public string SpoolingTotal
  51. {
  52. get { return _gem.GetAttribute(GEMVariables.SpoolCountTotal, AttributeType.SV); }
  53. }
  54. public string SpoolingFullTime
  55. {
  56. get { return _gem.GetAttribute(GEMVariables.SpoolFullTime, AttributeType.SV); }
  57. }
  58. public string SpoolingStartTime
  59. {
  60. get { return _gem.GetAttribute(GEMVariables.SpoolStartTime, AttributeType.SV); }
  61. }
  62. public bool IsSpoolingEnable
  63. {
  64. get { return Convert.ToBoolean(_gem.GetAttribute(GEMVariables.SpoolEnabled, AttributeType.EC)); }
  65. }
  66. private readonly GEMController _gem = new GEMController();
  67. public IHostCallback _equipment;
  68. private HashSet<string> _systemBuildInEc = new HashSet<string>();
  69. private HashSet<string> _systemBuildInVariables = new HashSet<string>();
  70. private PeriodicJob _faMonitorThread;
  71. private FixSizeQueue<FaEventItem> _lstEvent = new FixSizeQueue<FaEventItem>(300);
  72. FALogFileCleaner _logCleaner = new FALogFileCleaner();
  73. public void Initialize(IHostCallback equipment, string modelFile)
  74. {
  75. _equipment = equipment;
  76. _gem.CommunicationStateChanged += OnCommunicationStateChanged;
  77. _gem.RemoteCommandS2F41In += _gem_RemoteCommandS2F41In; ;
  78. _gem.RemoteCommandS2F49In += _gem_RemoteCommandS2F49In; ;
  79. _gem.ControlStateChanged += _gem_ControlStateChanged;
  80. _gem.ShowTrialMessageBox = false;
  81. _gem.Initialize(modelFile, PathManager.GetLogDir());
  82. //get system build in ec
  83. _systemBuildInEc = new HashSet<string>();
  84. _systemBuildInEc.Add("EstablishCommunicationsTimeout");
  85. _systemBuildInEc.Add("MaxSpoolTransmit");
  86. _systemBuildInEc.Add("OverWriteSpool");
  87. _systemBuildInEc.Add("MaxSpoolCapacity");
  88. _systemBuildInEc.Add("SpoolEnabled");
  89. _systemBuildInEc.Add("TimeFormat");
  90. //initial system build in variable
  91. _systemBuildInVariables.Add("AlarmsEnabled");
  92. _systemBuildInVariables.Add("AlarmsSet");
  93. _systemBuildInVariables.Add("Clock");
  94. _systemBuildInVariables.Add("ControlState");
  95. _systemBuildInVariables.Add("EventsEnabled");
  96. _systemBuildInVariables.Add("PPExecName");
  97. _systemBuildInVariables.Add("PreviousProcessState");
  98. _systemBuildInVariables.Add("ProcessState");
  99. _systemBuildInVariables.Add("SpoolCountActual");
  100. _systemBuildInVariables.Add("SpoolCountTotal");
  101. _systemBuildInVariables.Add("SpoolFullTime");
  102. _systemBuildInVariables.Add("SpoolStartTime");
  103. _systemBuildInVariables.Add("SpoolState");
  104. _systemBuildInVariables.Add("SpoolSubstate");
  105. _logCleaner.Run();
  106. _faMonitorThread = new PeriodicJob(200, MonitorFaTask, "Monitor FA Thread", true);
  107. }
  108. private void _gem_ControlStateChanged(object sender, SECsEventArgs e)
  109. {
  110. if (_gem.ControlState == ControlState.OnlineLocal)
  111. {
  112. _gem.SetRemote();
  113. }
  114. }
  115. public void Invoke(string method, object[] args)
  116. {
  117. switch (method)
  118. {
  119. case FACommandName.FAEnable:
  120. _gem.SetEnable();
  121. break;
  122. case FACommandName.FADisable:
  123. _gem.SetDisable();
  124. break;
  125. case FACommandName.FAOnline:
  126. _gem.SetOnline();
  127. break;
  128. case FACommandName.FAOffline:
  129. _gem.SetOffline();
  130. break;
  131. case FACommandName.FALocal:
  132. _gem.SetLocal();
  133. break;
  134. case FACommandName.FARemote:
  135. _gem.SetRemote();
  136. break;
  137. case FACommandName.FAEnableSpooling:
  138. SetEnableSpooling();
  139. break;
  140. case FACommandName.FADisableSpooling:
  141. SetDisableSpooling();
  142. break;
  143. }
  144. }
  145. private bool _gem_RemoteCommandS2F41In(SECsTransaction trans)
  146. {
  147. try
  148. {
  149. bool ret = true;
  150. string reason = string.Empty;
  151. SECsMessage reply = _gem.Services.CustomMessage.CreateMessage(2, 42, false);
  152. string remoteCommandName = trans.Primary.DataItem[0][0].ToString();
  153. switch (remoteCommandName)
  154. {
  155. case "ScanSlot":
  156. {
  157. if (trans.Primary.DataItem[0][1][0][0].ToString() != "PORT_ID" )
  158. throw new Exception("ScanSlot Command Format Not Correct");
  159. string portId = trans.Primary.DataItem[0][1][0][1].ToString();
  160. ret = _equipment.ScanSlot(portId, out reason);
  161. }
  162. break;
  163. }
  164. reply.DataItem.AddList();
  165. if (ret)
  166. {
  167. reply.DataItem[0].Add("HCACK", 0, SECsFormat.Binary);
  168. }
  169. else
  170. {
  171. reply.DataItem[0].Add("HCACK", 3, SECsFormat.Binary);
  172. reply.DataItem[0].Add("CPVALUE", reason, SECsFormat.Ascii);
  173. }
  174. _gem.SendReply(reply, trans.Id);
  175. }
  176. catch (Exception ex)
  177. {
  178. LOG.Write(string.Format("Handle_S2F41 Exception: {0}", ex.Message));
  179. SECsMessage reply = _gem.Services.CustomMessage.CreateMessage(2, 42, false);
  180. reply.DataItem.AddList();
  181. reply.DataItem[0].Add("HCACK", 3/*At least one parameter is invalid*/, SECsFormat.Binary);
  182. reply.DataItem[0].AddList();
  183. reply.DataItem[0][1].AddList();
  184. reply.DataItem[0][1].Add("CPVALUE", ex.Message, SECsFormat.Ascii);
  185. _gem.SendReply(reply, trans.Id);
  186. }
  187. return true;
  188. }
  189. private bool _gem_RemoteCommandS2F49In(SECsTransaction trans)
  190. {
  191. try
  192. {
  193. bool ret = true;
  194. string reason = string.Empty;
  195. SECsMessage reply = _gem.Services.CustomMessage.CreateMessage(2, 50, false);
  196. string remoteCommandName = trans.Primary.DataItem[0][2].ToString();
  197. switch (remoteCommandName.ToUpper())
  198. {
  199. case "START-TRANSFER":
  200. {
  201. if (trans.Primary.DataItem[0][3][0][0].ToString() != "TRANS_MODE" ||
  202. trans.Primary.DataItem[0][3][1][0].ToString() != "CAR_ID" ||
  203. trans.Primary.DataItem[0][3][2][0].ToString() != "LOT_ID" ||
  204. trans.Primary.DataItem[0][3][3][0].ToString() != "SRC_PORT_ID" ||
  205. trans.Primary.DataItem[0][3][4][0].ToString() != "WAF_CNT" ||
  206. trans.Primary.DataItem[0][3][5][0].ToString() != "EXIST_FLAG" ||
  207. trans.Primary.DataItem[0][3][6][0].ToString() != "WAFER_ID" ||
  208. trans.Primary.DataItem[0][3][7][0].ToString() != "LASERMARK1" ||
  209. trans.Primary.DataItem[0][3][8][0].ToString() != "LASERMARK2" ||
  210. trans.Primary.DataItem[0][3][9][0].ToString() != "TRANS_FLAG" ||
  211. trans.Primary.DataItem[0][3][10][0].ToString() != "DEST_PORT_ID" ||
  212. trans.Primary.DataItem[0][3][11][0].ToString() != "DEST_SLOT_NO" ||
  213. trans.Primary.DataItem[0][3][12][0].ToString() != "DEST_PORT_CTGRY" )
  214. throw new Exception("START-TRANSFER Command Format Not Correct");
  215. //HostRecipe recipe = new HostRecipe();
  216. //ret = _equipment.StartTransfer(recipe, out reason);
  217. }
  218. break;
  219. case "PP-SELECT":
  220. {
  221. if (trans.Primary.DataItem[0][3][0][0].ToString() != "RECIPE_ID" ||
  222. trans.Primary.DataItem[0][3][0][1].ToString() != "HostRecipe" )
  223. throw new Exception("PP-SELECT Command Format Not Correct");
  224. string recipeId = trans.Primary.DataItem[0][3][0][1].ToString();
  225. ret = _equipment.PPSelect(recipeId, out reason);
  226. }
  227. break;
  228. }
  229. reply.DataItem.AddList();
  230. if (ret)
  231. {
  232. reply.DataItem[0].Add("HCACK", 0, SECsFormat.Binary);
  233. }
  234. else
  235. {
  236. reply.DataItem[0].Add("HCACK", 3, SECsFormat.Binary);
  237. reply.DataItem[0].Add("CPVALUE", reason, SECsFormat.Ascii);
  238. }
  239. _gem.SendReply(reply, trans.Id);
  240. }
  241. catch (Exception ex)
  242. {
  243. //WriteLog(string.Format("Handle_S2F49 Exception: {0}", ex.Message));
  244. SECsMessage reply = _gem.Services.CustomMessage.CreateMessage(2, 50, false);
  245. reply.DataItem.AddList();
  246. reply.DataItem[0].Add("HCACK", 3/*At least one parameter is invalid*/, SECsFormat.Binary);
  247. reply.DataItem[0].AddList();
  248. reply.DataItem[0][1].AddList();
  249. reply.DataItem[0][1].Add("CPVALUE", ex.Message, SECsFormat.Ascii);
  250. _gem.SendReply(reply, trans.Id);
  251. }
  252. return true;
  253. }
  254. public void Terminate()
  255. {
  256. _faMonitorThread.Stop();
  257. _logCleaner.Stop();
  258. }
  259. public void Enable()
  260. {
  261. _gem.SetEnable();
  262. }
  263. public void Disable()
  264. {
  265. _gem.SetDisable();
  266. }
  267. public void NotifyEvent(string eventName, Dictionary<string, string> dvid)
  268. {
  269. _lstEvent.Enqueue(new FaEventItem(){dvid = dvid,EventName = eventName,IsAlarm = false});
  270. }
  271. public void NotifyAlarm(string alarmName, Dictionary<string, string> dvid)
  272. {
  273. _lstEvent.Enqueue(new FaEventItem() { dvid = dvid, EventName = alarmName, IsAlarm = true });
  274. }
  275. public void SetLocalControl()
  276. {
  277. Task.Factory.StartNew(() =>
  278. {
  279. _gem.SetLocal();
  280. });
  281. }
  282. public void SetRemoteControl()
  283. {
  284. Task.Factory.StartNew(() =>
  285. {
  286. _gem.SetRemote();
  287. });
  288. }
  289. public void SetEnableSpooling()
  290. {
  291. Task.Factory.StartNew(() =>
  292. {
  293. _gem .SetAttribute(GEMVariables.SpoolEnabled, AttributeType.EC, "true");
  294. });
  295. }
  296. public void SetDisableSpooling()
  297. {
  298. Task.Factory.StartNew(() =>
  299. {
  300. _gem .SetAttribute(GEMVariables.SpoolEnabled, AttributeType.EC, "false");
  301. });
  302. }
  303. public bool MonitorFaTask()
  304. {
  305. try
  306. {
  307. FaEventItem ev;
  308. while (_lstEvent.TryDequeue(out ev))
  309. {
  310. if (ev.dvid != null)
  311. {
  312. foreach (var dvid in ev.dvid)
  313. {
  314. SetDVIDValue(dvid.Key, dvid.Value);
  315. }
  316. }
  317. if (ev.IsAlarm)
  318. {
  319. SetAlarm(ev.EventName);
  320. }
  321. else
  322. {
  323. SendEvent(ev.EventName);
  324. }
  325. }
  326. SynchronizeSVIDValue();
  327. }
  328. catch (Exception ex)
  329. {
  330. System.Diagnostics.Trace.WriteLine(ex);
  331. }
  332. return true;
  333. }
  334. private void OnCommunicationStateChanged(object sender, SECsEventArgs e)
  335. {
  336. if (_gem.CommunicationState == CommunicationState.EnabledCommunicating)
  337. {
  338. _gem.SetOnline();
  339. _gem.SetRemote();
  340. }
  341. }
  342. private void SynchronizeSVIDValue()
  343. {
  344. try
  345. {
  346. foreach (SVID sv in _gem.EquipmentModel.StatusVariables.SVIDCollection)
  347. {
  348. if (sv != null && !_systemBuildInVariables.Contains(sv.logicalName))
  349. {
  350. if (sv.valueType == SECSFormats.List)
  351. {
  352. List<string> svData = _equipment.GetListSvidValue(sv.logicalName);
  353. if (svData != null && svData.Count > 0)
  354. {
  355. SECsDataItem data = new SECsDataItem(SECsFormat.List);
  356. foreach (var item in svData)
  357. {
  358. data.Add(item, item);
  359. }
  360. _gem.SetListAttribute(sv.logicalName, AttributeType.SV, data);
  361. }
  362. else
  363. {
  364. SECsDataItem data = new SECsDataItem(SECsFormat.List);
  365. data.Clear();
  366. _gem.SetListAttribute(sv.logicalName, AttributeType.SV, data);
  367. }
  368. }
  369. else
  370. {
  371. string svDataValue = _equipment.GetSvidValue(sv.logicalName);
  372. if (!string.IsNullOrEmpty(svDataValue))
  373. {
  374. if (sv.valueType == SECSFormats.Boolean) svDataValue = ConvertToBoolean(svDataValue).ToString();
  375. _gem.SetAttribute(sv.logicalName, AttributeType.SV, svDataValue);
  376. }
  377. }
  378. }
  379. }
  380. }
  381. catch (Exception ex)
  382. {
  383. LOG.Write("Synchronize FA Model Data exception:" + ex.Message);
  384. }
  385. }
  386. private bool SetDVIDValue(string localName, string value)
  387. {
  388. if (value == null) return false;
  389. //if (!string.IsNullOrEmpty(value))
  390. //{
  391. if (_gem.EquipmentModel == null || !_gem.EquipmentModel.DataVariables.DVIDCollection.IsExistLogicalName(localName))
  392. return false;
  393. foreach (VariableType dv in _gem.EquipmentModel.DataVariables.DVIDCollection)
  394. {
  395. if (dv.logicalName == localName)
  396. {
  397. if (dv.valueType == SECSFormats.Boolean)
  398. {
  399. value = ConvertToBoolean(value).ToString();
  400. }
  401. break;
  402. }
  403. }
  404. _gem.SetAttribute(localName, AttributeType.DV, value);
  405. //}
  406. return true;
  407. }
  408. private bool ConvertToBoolean(string value)
  409. {
  410. if (value == "0" || value.ToLower() == "false") return false;
  411. else return true;
  412. }
  413. private void SendEvent(string eventName)
  414. {
  415. try
  416. {
  417. if (_gem.EquipmentModel != null)
  418. {
  419. var allEvents = _gem.GetAllEnabledEvents();
  420. if (allEvents.Contains(eventName))
  421. {
  422. _gem.SendCollectionEvent(eventName);
  423. }
  424. else
  425. {
  426. //LOG.Write(string.Format("sendEvent failed,not find:", eventName));
  427. }
  428. //LOG.Write(string.Format("【FA2SendEvent--{0}】", eventName));
  429. }
  430. }
  431. catch (Exception ex)
  432. {
  433. LOG.Write("SendEvent Error:" + ex.Message);
  434. }
  435. }
  436. public void SetAlarm(string alarmTag)
  437. {
  438. Task.Factory.StartNew(() =>
  439. {
  440. //if (_gem.IsAlarmSet(alarmTag))
  441. //{
  442. // _gem.ClearAlarm(alarmTag);
  443. //}
  444. var allAlarms = _gem.GetAllEnabledAlarms();
  445. if (allAlarms.Contains(alarmTag))
  446. {
  447. _gem.SetAlarm(alarmTag);
  448. }
  449. else
  450. {
  451. //WriteLog(string.Format("sendAlarm failed,not find:", alarmTag));
  452. }
  453. //WriteLog(string.Format("【FA3SetAlarm--{0}】", alarmTag));
  454. });
  455. }
  456. }
  457. }