SiasunVCE.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Device.Unit;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.Util;
  13. using MECF.Framework.Common.Communications;
  14. using MECF.Framework.Common.Device.Bases;
  15. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
  16. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK;
  17. using Newtonsoft.Json;
  18. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.VCE.SiasunVCE
  19. {
  20. public class SiasunVCE : VCEBase, IConnection
  21. {
  22. public string Address { get { return _address; } }
  23. public override bool IsConnected => _connection.IsConnected;
  24. public bool Connect()
  25. {
  26. return true;
  27. }
  28. public bool Disconnect()
  29. {
  30. return true;
  31. }
  32. public string PortStatus { get; set; } = "Closed";
  33. private SiasunVCEConnection _connection;
  34. public SiasunVCEConnection Connection
  35. {
  36. get { return _connection; }
  37. }
  38. private R_TRIG _trigError = new R_TRIG();
  39. private R_TRIG _trigCommunicationError = new R_TRIG();
  40. private R_TRIG _trigRetryConnect = new R_TRIG();
  41. private PeriodicJob _thread;
  42. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  43. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  44. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  45. private object _locker = new object();
  46. private bool _enableLog;
  47. private string _scRoot;
  48. private string _portName;
  49. public SiasunVCE(string module, string name, string scRoot) : base(module, name)
  50. {
  51. _scRoot = scRoot;
  52. }
  53. private void ResetPropertiesAndResponses()
  54. {
  55. DoorClosed = null;
  56. DoorOpened = null;
  57. IsHalted = null;
  58. foreach (var ioResponse in IOResponseList)
  59. {
  60. ioResponse.ResonseContent = null;
  61. ioResponse.ResonseRecievedTime = DateTime.Now;
  62. }
  63. }
  64. private string _address = "00";
  65. public override bool Initialize()
  66. {
  67. base.Initialize();
  68. ResetPropertiesAndResponses();
  69. if (_connection != null && _connection.IsConnected)
  70. return true;
  71. if (string.IsNullOrEmpty(_scRoot))
  72. {
  73. _portName = SC.GetStringValue($"{Name}.DeviceAddress");
  74. _enableLog = SC.GetValue<bool>($"{Name}.EnableLogMessage");
  75. }
  76. else
  77. {
  78. _portName = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.DeviceAddress");
  79. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
  80. }
  81. _connection = new SiasunVCEConnection(_portName);
  82. _connection.EnableLog(_enableLog);
  83. if (_connection.Connect())
  84. {
  85. PortStatus = "Open";
  86. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  87. }
  88. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  89. return true;
  90. }
  91. private bool OnTimer()
  92. {
  93. try
  94. {
  95. //return true;
  96. //_connection.MonitorTimeout();
  97. if (!_connection.IsConnected || _connection.IsCommunicationError)
  98. {
  99. lock (_locker)
  100. {
  101. _lstHandler.Clear();
  102. }
  103. _trigRetryConnect.CLK = !_connection.IsConnected;
  104. if (_trigRetryConnect.Q)
  105. {
  106. _connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
  107. if (!_connection.Connect())
  108. {
  109. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  110. }
  111. else
  112. {
  113. //_lstHandler.AddLast(new SiasunVCEQueryPinHandler(this, _deviceAddress));
  114. //_lstHandler.AddLast(new SiasunVCESetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  115. }
  116. }
  117. return true;
  118. }
  119. HandlerBase handler = null;
  120. if (!_connection.IsBusy)
  121. {
  122. lock (_locker)
  123. {
  124. if (_lstHandler.Count == 0)
  125. {
  126. foreach (var monitorHandler in _lstMonitorHandler)
  127. {
  128. _lstHandler.AddLast(monitorHandler);
  129. }
  130. }
  131. if (_lstHandler.Count > 0)
  132. {
  133. handler = _lstHandler.First.Value;
  134. _lstHandler.RemoveFirst();
  135. }
  136. }
  137. if (handler != null)
  138. {
  139. _connection.Execute(handler);
  140. }
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. LOG.Write(ex);
  146. }
  147. return true;
  148. }
  149. internal void NoteCommonActionResult(string command)
  150. {
  151. if (command == "JD")
  152. {
  153. }
  154. else if (command == "JU")
  155. {
  156. }
  157. else if (command == "LOAD")
  158. {
  159. LoadCompleted = true;
  160. }
  161. else if (command == "LP")
  162. {
  163. LoadPosition = true;
  164. }
  165. else if (command == "PI")
  166. {
  167. RetractPosition = true;
  168. }
  169. else if (command == "PICK")
  170. {
  171. Picked = true;
  172. }
  173. else if (command == "PLACE")
  174. {
  175. Placed = true;
  176. }
  177. else if (command == "PO")
  178. {
  179. ExtendPosition = true;
  180. }
  181. else if (command == "UNLOAD")
  182. {
  183. Unloaded = true;
  184. }
  185. }
  186. internal void NoteZAxisPosition(string pos)
  187. {
  188. ZAxisPosition = pos;
  189. }
  190. internal void NoteHomed(bool homed)
  191. {
  192. Homed = homed;
  193. }
  194. internal void NoteMoveResult(string position)
  195. {
  196. MovedPosition = position;
  197. }
  198. public override void Monitor()
  199. {
  200. try
  201. {
  202. //_connection.EnableLog(_enableLog);
  203. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  204. if (_trigCommunicationError.Q)
  205. {
  206. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. LOG.Write(ex);
  212. }
  213. }
  214. public override void Reset()
  215. {
  216. _trigError.RST = true;
  217. _connection.SetCommunicationError(false, "");
  218. _trigCommunicationError.RST = true;
  219. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  220. _trigRetryConnect.RST = true;
  221. base.Reset();
  222. }
  223. public override bool Home(out string reason)
  224. {
  225. return base.Home(out reason);
  226. }
  227. #region Command Functions
  228. public void PerformRawCommand(string commandType)
  229. {
  230. lock (_locker)
  231. {
  232. _lstHandler.AddLast(new SiasunVCERawCommandHandler(this, commandType, ""));
  233. }
  234. }
  235. public void PerformRawCommand(string commandType, string command)
  236. {
  237. lock (_locker)
  238. {
  239. _lstHandler.AddLast(new SiasunVCERawCommandHandler(this, commandType, command));
  240. }
  241. }
  242. internal void NoteCurrentCommunicationMode(string data)
  243. {
  244. CurrentCommunicationMode = data;
  245. }
  246. public void PerformRawCommand(string commandType, string command, string comandArgument)
  247. {
  248. lock (_locker)
  249. {
  250. _lstHandler.AddLast(new SiasunVCERawCommandHandler(this, commandType, command, comandArgument));
  251. }
  252. }
  253. public void MonitorRawCommand(bool isSelected, string commandType, string command, string comandArgument)
  254. {
  255. lock (_locker)
  256. {
  257. string msg = comandArgument == null ? $"{command}\r" : $"{command} {comandArgument}\r";//??
  258. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(SiasunVCERawCommandHandler) && ((SiasunVCERawCommandHandler)handler).IsSendText(commandType, command, comandArgument));
  259. if (isSelected)
  260. {
  261. if (!existHandlers.Any())
  262. _lstMonitorHandler.AddFirst(new SiasunVCERawCommandHandler(this, commandType, command, comandArgument));
  263. }
  264. else
  265. {
  266. if (existHandlers.Any())
  267. {
  268. _lstMonitorHandler.Remove(existHandlers.First());
  269. }
  270. }
  271. }
  272. }
  273. internal void NoteCurrentACEStatus(string data)
  274. {
  275. throw new NotImplementedException();
  276. }
  277. internal void NoteCurrentErrorStatus(string data)
  278. {
  279. CurrentErrorStatus = data;
  280. }
  281. internal void NoteIsLoadPositon(string data)
  282. {
  283. InLoadPositon = data.Last() == 'Y';
  284. }
  285. public void RequestArmRPositon(bool isSelected, string axisPosition)
  286. {
  287. lock (_locker)
  288. {
  289. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestArmRPositonHandler) && handler.SendText.Contains(axisPosition));
  290. if (isSelected)
  291. {
  292. if (!existHandlers.Any())
  293. _lstMonitorHandler.AddFirst(new BrooksACERequestArmRPositonHandler(this, axisPosition));
  294. }
  295. else
  296. {
  297. if (existHandlers.Any())
  298. {
  299. _lstMonitorHandler.Remove(existHandlers.First());
  300. }
  301. }
  302. }
  303. }
  304. internal void NoteCurrentMappingInfo(string data)
  305. {
  306. CurrentMappingInfo = data;
  307. }
  308. internal void NoteIsPlatformPosition(string data)
  309. {
  310. InPlatformPosition = data.Last() == 'Y';
  311. }
  312. public void RequestArmZPositon(bool isSelected, string axisPosition)
  313. {
  314. lock (_locker)
  315. {
  316. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestArmZPositonHandler) && handler.SendText.Contains(axisPosition));
  317. if (isSelected)
  318. {
  319. if (!existHandlers.Any())
  320. _lstMonitorHandler.AddFirst(new BrooksACERequestArmZPositonHandler(this, axisPosition));
  321. }
  322. else
  323. {
  324. if (existHandlers.Any())
  325. {
  326. _lstMonitorHandler.Remove(existHandlers.First());
  327. }
  328. }
  329. }
  330. }
  331. public void RequestCommunicationMode(bool isSelected)
  332. {
  333. lock (_locker)
  334. {
  335. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestCommModeHandler));
  336. if (isSelected)
  337. {
  338. if (!existHandlers.Any())
  339. _lstMonitorHandler.AddFirst(new BrooksACERequestCommModeHandler(this));
  340. }
  341. else
  342. {
  343. if (existHandlers.Any())
  344. {
  345. _lstMonitorHandler.Remove(existHandlers.First());
  346. }
  347. }
  348. }
  349. }
  350. public void RequestErrorStatus(bool isSelected)
  351. {
  352. lock (_locker)
  353. {
  354. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestErrorStatusHandler));
  355. if (isSelected)
  356. {
  357. if (!existHandlers.Any())
  358. _lstMonitorHandler.AddFirst(new BrooksACERequestErrorStatusHandler(this));
  359. }
  360. else
  361. {
  362. if (existHandlers.Any())
  363. {
  364. _lstMonitorHandler.Remove(existHandlers.First());
  365. }
  366. }
  367. }
  368. }
  369. public void RequestVCEStatus(bool isSelected)
  370. {
  371. lock (_locker)
  372. {
  373. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestVCEStatusHandler));
  374. if (isSelected)
  375. {
  376. if (!existHandlers.Any())
  377. _lstMonitorHandler.AddFirst(new BrooksACERequestVCEStatusHandler(this));
  378. }
  379. else
  380. {
  381. if (existHandlers.Any())
  382. {
  383. _lstMonitorHandler.Remove(existHandlers.First());
  384. }
  385. }
  386. }
  387. }
  388. public void RequestLoadPositon(bool isSelected)
  389. {
  390. lock (_locker)
  391. {
  392. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestLoadPositionHandler));
  393. if (isSelected)
  394. {
  395. if (!existHandlers.Any())
  396. _lstMonitorHandler.AddFirst(new BrooksACERequestLoadPositionHandler(this));
  397. }
  398. else
  399. {
  400. if (existHandlers.Any())
  401. {
  402. _lstMonitorHandler.Remove(existHandlers.First());
  403. }
  404. }
  405. }
  406. }
  407. public void RequestMappingInfo(bool isSelected)
  408. {
  409. lock (_locker)
  410. {
  411. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestMappingInfoHandler));
  412. if (isSelected)
  413. {
  414. if (!existHandlers.Any())
  415. _lstMonitorHandler.AddFirst(new BrooksACERequestMappingInfoHandler(this));
  416. }
  417. else
  418. {
  419. if (existHandlers.Any())
  420. {
  421. _lstMonitorHandler.Remove(existHandlers.First());
  422. }
  423. }
  424. }
  425. }
  426. public void RequestPlatformPosition(bool isSelected)
  427. {
  428. lock (_locker)
  429. {
  430. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(BrooksACERequestPlatformPositionHandler));
  431. if (isSelected)
  432. {
  433. if (!existHandlers.Any())
  434. _lstMonitorHandler.AddFirst(new BrooksACERequestPlatformPositionHandler(this));
  435. }
  436. else
  437. {
  438. if (existHandlers.Any())
  439. {
  440. _lstMonitorHandler.Remove(existHandlers.First());
  441. }
  442. }
  443. }
  444. }
  445. public void CloseDoor()
  446. {
  447. lock (_locker)
  448. {
  449. _lstHandler.AddLast(new SiasunVCECloseDoorHandler(this));
  450. }
  451. }
  452. public void OpenDoor()
  453. {
  454. lock (_locker)
  455. {
  456. _lstHandler.AddLast(new SiasunVCEOpenDoorHandler(this));
  457. }
  458. }
  459. public void Load()
  460. {
  461. lock (_locker)
  462. {
  463. //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "LOAD"));
  464. }
  465. }
  466. public void MoveToLoadPositon()
  467. {
  468. lock (_locker)
  469. {
  470. _lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "LP"));
  471. }
  472. }
  473. public void MapCassette()
  474. {
  475. lock (_locker)
  476. {
  477. _lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "MP"));
  478. }
  479. }
  480. public void Move(string axis, string type, string value)
  481. {
  482. lock (_locker)
  483. {
  484. _lstHandler.AddLast(new SiasunVCEMoveHandler(this, axis, type, value));
  485. }
  486. }
  487. public void RoateRAxisToRetractPosition()
  488. {
  489. lock (_locker)
  490. {
  491. //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "PI"));
  492. }
  493. }
  494. public void RoateRAxisToExtendPosition()
  495. {
  496. lock (_locker)
  497. {
  498. //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "PO"));
  499. }
  500. }
  501. public void Pick()
  502. {
  503. lock (_locker)
  504. {
  505. //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "PICK"));
  506. }
  507. }
  508. public void Place()
  509. {
  510. lock (_locker)
  511. {
  512. //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "PLACE"));
  513. }
  514. }
  515. public void Unload()
  516. {
  517. lock (_locker)
  518. {
  519. //_lstHandler.AddLast(new SiasunVCECommonActionHandler(this, "UNLOAD"));
  520. }
  521. }
  522. public void GotoZ(int slot)
  523. {
  524. lock (_locker)
  525. {
  526. _lstHandler.AddLast(new SiasunVCEGotoZHandler(this, slot));
  527. }
  528. }
  529. public void Home()
  530. {
  531. lock (_locker)
  532. {
  533. _lstHandler.AddLast(new SiasunVCEHomeHandler(this));
  534. }
  535. }
  536. public void SetCommunicationMode(string commMode)
  537. {
  538. lock (_locker)
  539. {
  540. //_lstHandler.AddLast(new SiasunVCESetCommunicationModeHandler(this, commMode));
  541. }
  542. }
  543. #endregion
  544. #region Properties
  545. public string DoorClosed { get; private set; }
  546. public string DoorOpened { get; private set; }
  547. public string IsHalted { get; private set; }
  548. public string Error { get; private set; }
  549. public string ArmRPosition { get; private set; }
  550. public string CommunicationMode { get; private set; }
  551. public string ZAxisPosition { get; private set; }
  552. public bool Homed { get; private set; }
  553. public bool LoadCompleted { get; private set; }
  554. public bool LoadPosition { get; private set; }
  555. public bool RetractPosition { get; private set; }
  556. public bool Picked { get; private set; }
  557. public bool Placed { get; private set; }
  558. public bool ExtendPosition { get; private set; }
  559. public bool Unloaded { get; private set; }
  560. public string MovedPosition { get; private set; }
  561. public string ArmZPosition { get; private set; }
  562. public string CurrentCommunicationMode { get; private set; }
  563. public string CurrentErrorStatus { get; private set; }
  564. public bool InLoadPositon { get; private set; }
  565. public string CurrentMappingInfo { get; private set; }
  566. public bool InPlatformPosition { get; private set; }
  567. #endregion
  568. #region Note Functions
  569. private R_TRIG _trigWarningMessage = new R_TRIG();
  570. public void NoteError(string reason)
  571. {
  572. if (reason != null)
  573. {
  574. _trigWarningMessage.CLK = true;
  575. if (_trigWarningMessage.Q)
  576. {
  577. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  578. }
  579. Error = reason;
  580. }
  581. else
  582. {
  583. Error = null;
  584. }
  585. }
  586. internal void NoteDoorClosed(bool isClosed)
  587. {
  588. DoorClosed = isClosed ? "true" : "false";
  589. DoorOpened = isClosed ? "false" : "true";
  590. }
  591. internal void NoteIsHalted(bool isHalted)
  592. {
  593. IsHalted = isHalted ? "true" : "false";
  594. }
  595. internal void NoteRawCommandInfo(string commandType, string command, string data)
  596. {
  597. var curIOResponse = IOResponseList.Find(res => res.SourceCommandName == command);
  598. if (curIOResponse != null)
  599. {
  600. IOResponseList.Remove(curIOResponse);
  601. }
  602. IOResponseList.Add(new IOResponse() { SourceCommandType = commandType, SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
  603. }
  604. internal void NoteArmRPositon(string armRPosition)
  605. {
  606. ArmRPosition = armRPosition;
  607. }
  608. internal void NoteArmZPositon(string armPosition)
  609. {
  610. ArmZPosition = armPosition;
  611. }
  612. internal void NoteCommunicationMode(string mode)
  613. {
  614. CommunicationMode = mode;
  615. }
  616. #endregion
  617. }
  618. }