CytEndPoint.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Communications;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.EndPoints.CytEndPoints
  15. {
  16. [Serializable]
  17. public class CytEndPoint : BaseDevice, IDevice
  18. {
  19. public event Action OnError;
  20. public event Action OnEPDTrigger;
  21. public string Address => Connection?.Address;
  22. public bool IsConnected => Connection != null && Connection.IsConnected && !_connection.IsCommunicationError;
  23. public bool Connect()
  24. {
  25. return _connection.Connect();
  26. }
  27. public bool Disconnect()
  28. {
  29. return _connection.Disconnect();
  30. }
  31. public byte SlaveAddress { get; private set; } = 0x01;
  32. public string PortStatus { get; set; } = "Closed";
  33. private CytEndPointConnection _connection;
  34. public CytEndPointConnection Connection
  35. {
  36. get { return _connection; }
  37. }
  38. public List<string> ConfigList
  39. {
  40. get
  41. {
  42. GetConfigList();
  43. return _configs;
  44. }
  45. }
  46. public bool IsTrigger
  47. {
  48. get;
  49. set;
  50. }
  51. private R_TRIG _trigError = new R_TRIG();
  52. private R_TRIG _trigCommunicationError = new R_TRIG();
  53. private R_TRIG _trigRetryConnect = new R_TRIG();
  54. private PeriodicJob _thread;
  55. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  56. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  57. private object _locker = new object();
  58. private bool _enableLog;
  59. private string _address;
  60. private string _scRoot;
  61. private string _currentConfigName;
  62. private string _status;
  63. private List<string> _trendName;
  64. private double[] _trendValue;
  65. private double[] _spectrum;
  66. private double[] _magnitude;
  67. private bool _isEPDConnected;
  68. private string _errorInfo;
  69. private string _epdState;
  70. private string _epdMode;
  71. private bool _isRemote;
  72. private List<string> _configs = new List<string>();
  73. private Stopwatch _runningTimer = new Stopwatch();
  74. public CytEndPoint(string module, string name, string scRoot) : base()
  75. {
  76. _scRoot = scRoot;
  77. base.Module = module;
  78. base.Name = name;
  79. _status = "Unconnected";
  80. _currentConfigName = "--";
  81. }
  82. public bool Initialize()
  83. {
  84. _address = SC.GetStringValue($"{_scRoot}.Address");
  85. _enableLog = SC.GetValue<bool>($"{_scRoot}.EnableLogMessage");
  86. _connection = new CytEndPointConnection(_address);
  87. _connection.EnableLog(_enableLog);
  88. _connection.SetEventHandler(new CytEventHandler(this));
  89. _lstHandler.AddLast(new CytConnectHandler(this));
  90. _lstHandler.AddLast(new CytQueryConfigListHandler(this));
  91. if (_connection.Connect())
  92. {
  93. PortStatus = "Open";
  94. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  95. }
  96. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
  97. DATA.Subscribe($"{Module}.{Name}.Address", () => Address, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  98. DATA.Subscribe($"{Module}.{Name}.IsTrigger", () => IsTrigger);
  99. DATA.Subscribe($"{Module}.{Name}.TrendName", () => _trendName, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  100. DATA.Subscribe($"{Module}.{Name}.TrendValue", () => _trendValue, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  101. DATA.Subscribe($"{Module}.{Name}.Spectrum", () => _spectrum, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  102. DATA.Subscribe($"{Module}.{Name}.Magnitude", () => _magnitude, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  103. DATA.Subscribe($"{Module}.{Name}.ConfigList", () => ConfigList, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  104. DATA.Subscribe($"{Module}.{Name}.CurrentConfigName", () => _currentConfigName,
  105. SubscriptionAttribute.FLAG.IgnoreSaveDB);
  106. DATA.Subscribe($"{Module}.{Name}.Status", () => _epdState, SubscriptionAttribute.FLAG.IgnoreSaveDB);
  107. DATA.Subscribe($"{Module}.{Name}.IsRunning", () => _runningTimer.IsRunning);
  108. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  109. {
  110. Disconnect();
  111. Reset();
  112. Connect();
  113. return true;
  114. });
  115. OP.Subscribe($"{Module}.{Name}.ManualEndPoint", (string cmd, object[] args) => { return true; });
  116. OP.Subscribe($"{Module}.{Name}.EPDConfigName", (out string reason, int time, object[] param) =>
  117. {
  118. reason = string.Empty;
  119. string config = param[0].ToString();
  120. Start(config);
  121. EV.PostInfoLog(Module, $"Set EPD config name {config}");
  122. return true;
  123. });
  124. OP.Subscribe($"{Module}.{Name}.Stop", (string cmd, object[] args) =>
  125. {
  126. Stop();
  127. return true;
  128. });
  129. OP.Subscribe($"{Module}.{Name}.RecipeStart", (string cmd, object[] args) =>
  130. {
  131. RecipeStart();
  132. return true;
  133. });
  134. OP.Subscribe($"{Module}.{Name}.RecipeStop", (string cmd, object[] args) =>
  135. {
  136. RecipeStop();
  137. return true;
  138. });
  139. _thread = new PeriodicJob(300, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  140. return true;
  141. }
  142. Random _rd = new Random();
  143. Stopwatch _sw = new Stopwatch();
  144. private bool OnTimer()
  145. {
  146. try
  147. {
  148. if (!_connection.IsConnected || _connection.IsCommunicationError)
  149. {
  150. lock (_locker)
  151. {
  152. _lstHandler.Clear();
  153. }
  154. _trigRetryConnect.CLK = !_connection.IsConnected;
  155. if (_trigRetryConnect.Q)
  156. {
  157. if (!_connection.Connect())
  158. {
  159. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  160. }
  161. else
  162. {
  163. _lstHandler.AddLast(new CytConnectHandler(this));
  164. _lstHandler.AddLast(new CytQueryConfigListHandler(this));
  165. }
  166. }
  167. return true;
  168. }
  169. HandlerBase handler = null;
  170. if (!_connection.IsBusy)
  171. {
  172. lock (_locker)
  173. {
  174. if (_lstHandler.Count == 0)
  175. {
  176. foreach (var monitorHandler in _lstMonitorHandler)
  177. {
  178. _lstHandler.AddLast(monitorHandler);
  179. }
  180. }
  181. if (_lstHandler.Count > 0)
  182. {
  183. handler = _lstHandler.First.Value;
  184. _lstHandler.RemoveFirst();
  185. }
  186. }
  187. if (handler != null)
  188. {
  189. _connection.Execute(handler);
  190. }
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. LOG.Write(ex);
  196. }
  197. return true;
  198. }
  199. public void Monitor()
  200. {
  201. try
  202. {
  203. _connection.EnableLog(_enableLog);
  204. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  205. if (_trigCommunicationError.Q)
  206. {
  207. EV.PostAlarmLog(Module,
  208. $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. LOG.Write(ex);
  214. }
  215. }
  216. public void Reset()
  217. {
  218. _trigError.RST = true;
  219. _connection.SetCommunicationError(false, "");
  220. _trigCommunicationError.RST = true;
  221. _enableLog = SC.GetValue<bool>($"{_scRoot}.EnableLogMessage");
  222. _trigRetryConnect.RST = true;
  223. }
  224. public void Terminate()
  225. {
  226. }
  227. internal void NoteIsConnected(bool isConnected)
  228. {
  229. _isEPDConnected = isConnected;
  230. }
  231. internal void NoteIsRemote(bool isRemote)
  232. {
  233. _isRemote = isRemote;
  234. }
  235. internal void NoteMode(string mode)
  236. {
  237. _epdMode = mode;
  238. }
  239. internal void NoteState(string state)
  240. {
  241. _epdState = state;
  242. }
  243. internal void NoteConfigList(List<string> cfgList)
  244. {
  245. _configs = cfgList;
  246. }
  247. internal void NoteTrigger(long ticket)
  248. {
  249. EV.PostInfoLog(Module, $"Receive EndPoint notify: trigger ({ticket}) at {_runningTimer.ElapsedMilliseconds / 1000:F1} second");
  250. IsTrigger = true;
  251. }
  252. internal void NoteStop(long ticket)
  253. {
  254. EV.PostInfoLog(Module, $"Receive EndPoint notify: stop at {_runningTimer.ElapsedMilliseconds / 1000:F1} second");
  255. _runningTimer.Stop();
  256. }
  257. internal void NoteSatisfied(long ticket)
  258. {
  259. EV.PostInfoLog(Module, $"Receive EndPoint notify: satisfied at {_runningTimer.ElapsedMilliseconds / 1000:F1} second");
  260. }
  261. internal void NoteNormalizeStart(long ticket)
  262. {
  263. EV.PostInfoLog(Module, $"Receive EndPoint notify: normalize from {_runningTimer.ElapsedMilliseconds / 1000:F1} second");
  264. }
  265. internal void NoteDelayEnd(long ticket)
  266. {
  267. EV.PostInfoLog(Module, $"Receive EndPoint notify: delay end at {_runningTimer.ElapsedMilliseconds / 1000:F1} second");
  268. }
  269. internal void NoteStart(long ticket)
  270. {
  271. EV.PostInfoLog(Module, $"Receive EndPoint notify: start");
  272. _runningTimer.Restart();
  273. GetCurrentConfig();
  274. }
  275. public string Error { get; private set; }
  276. private R_TRIG _trigWarningMessage = new R_TRIG();
  277. public void NoteError(string reason)
  278. {
  279. if (!string.IsNullOrEmpty(reason))
  280. {
  281. _trigWarningMessage.CLK = true;
  282. if (_trigWarningMessage.Q)
  283. {
  284. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  285. }
  286. Error = reason;
  287. }
  288. else
  289. {
  290. Error = null;
  291. }
  292. }
  293. internal void NoteCurrentConfigIntegrationTime(int v)
  294. {
  295. }
  296. internal void NoteCurrentConfigName(string name)
  297. {
  298. _currentConfigName = name;
  299. }
  300. internal void NoteTrendName(List<string> name)
  301. {
  302. _trendName = name;
  303. _trendValue = new double[name.Count];
  304. }
  305. internal void NoteTrendValue(string data)
  306. {
  307. var value = data.Split(',');
  308. if (_trendValue != null)
  309. {
  310. for (int i = 0; i < value.Length && i < _trendValue.Length; i++)
  311. {
  312. if (string.IsNullOrEmpty(value[i]))
  313. {
  314. _trendValue[i] = 0.0;
  315. }
  316. else
  317. {
  318. _trendValue[i] = double.Parse(value[i]);
  319. }
  320. }
  321. }
  322. }
  323. public void GetCurrentConfig()
  324. {
  325. lock (_locker)
  326. {
  327. _lstHandler.AddLast(new CytQueryCurrentConfigHandler(this));
  328. }
  329. var t1 = new Task(() =>
  330. {
  331. bool isReturned = false;
  332. while (!isReturned)
  333. {
  334. lock (_locker)
  335. {
  336. bool done = true;
  337. foreach (var handlerBase in _lstHandler)
  338. {
  339. if (handlerBase is CytQueryCurrentConfigHandler)
  340. {
  341. done = false;
  342. break;
  343. }
  344. }
  345. isReturned = done;
  346. }
  347. if (!isReturned)
  348. Thread.Sleep(100);
  349. }
  350. });
  351. t1.Start();
  352. Task.WaitAll(new Task[] { t1 }, 5000);
  353. }
  354. public void GetConfigList()
  355. {
  356. lock (_locker)
  357. {
  358. _lstHandler.AddLast(new CytQueryConfigListHandler(this));
  359. }
  360. var t1 = new Task(() =>
  361. {
  362. bool isReturned = false;
  363. while (!isReturned)
  364. {
  365. lock (_locker)
  366. {
  367. bool done = true;
  368. foreach (var handlerBase in _lstHandler)
  369. {
  370. if (handlerBase is CytQueryConfigListHandler)
  371. {
  372. done = false;
  373. break;
  374. }
  375. }
  376. isReturned = done;
  377. }
  378. if (!isReturned)
  379. Thread.Sleep(100);
  380. }
  381. });
  382. t1.Start();
  383. Task.WaitAll(new Task[] { t1 }, 5000);
  384. }
  385. public void RecipeStart()
  386. {
  387. lock (_locker)
  388. {
  389. LOG.Write($"{Name} recipe start");
  390. IsTrigger = false;
  391. _lstHandler.AddLast(new CytRecipeStartHandler(this));
  392. }
  393. }
  394. public void RecipeStop()
  395. {
  396. lock (_locker)
  397. {
  398. LOG.Write($"{Name} recipe stop");
  399. _lstHandler.AddLast(new CytRecipeStopHandler(this));
  400. }
  401. }
  402. public void Start(string config)
  403. {
  404. lock (_locker)
  405. {
  406. LOG.Write($"{Name} start");
  407. IsTrigger = false;
  408. _lstHandler.AddLast(new CytStartHandler(this, config));
  409. }
  410. }
  411. public void Stop()
  412. {
  413. lock (_locker)
  414. {
  415. LOG.Write($"{Name} stop");
  416. _lstHandler.AddLast(new CytStopHandler(this));
  417. }
  418. }
  419. }
  420. }