EPDDevice.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security;
  5. using System.ServiceModel;
  6. using System.Text;
  7. using Aitex.Core.RT.DataCenter;
  8. using Aitex.Core.RT.Device;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.RT.OperationCenter;
  12. using Aitex.Core.RT.SCCore;
  13. using Aitex.Core.Util;
  14. using Aitex.Core.WCF;
  15. using EPInterface;
  16. using EPInterface.Data;
  17. using EPInterface.Datas;
  18. using Venus_RT.Devices;
  19. using Venus_Core;
  20. using MECF.Framework.Common.Equipment;
  21. namespace Aitex.RT.Device.Custom
  22. {
  23. public class EPDDevice : JetEPDBase
  24. {
  25. private PeriodicJob _monitorThead;
  26. private R_TRIG _triggerConnected = new R_TRIG();
  27. private R_TRIG _triggerNotConnected = new R_TRIG();
  28. private int _channel;
  29. private string _channelStatus;
  30. private object _lockerTrigger = new object();
  31. private int _connectionCounter = 0;
  32. private bool _isEnd = false;
  33. public override bool Captured { get { return _isEnd; } }
  34. public override bool IsEPDConnected { get { return _triggerConnected.M; } }
  35. public override EDPStatus Status { get; }
  36. public override List<string> CFGFileList { get; }
  37. public EPDDevice(ModuleName mod)// :
  38. // base(module, name, name, name)
  39. {
  40. Name = VenusDevice.EndPoint.ToString();
  41. Module = mod.ToString();
  42. }
  43. public override bool Initialize()
  44. {
  45. _channel = SC.GetValue<int>($"{Module}.EPD.ChannelNumber");
  46. DATA.Subscribe($"{Module}.{Name}.IsConnected", ()=> IsEPDConnected);
  47. DATA.Subscribe($"{Module}.{Name}.CurrentChannel", () => _channel);
  48. DATA.Subscribe($"{Module}.{Name}.ChannelStatus", () => _channelStatus);
  49. OP.Subscribe($"{Module}.{Name}.SetConfig", (out string reason, int time, object[] args) => {
  50. if (!IsEPDConnected)
  51. {
  52. LOG.Write(eEvent.WARN_ENDPOINT, Module, $"{Module} {Name} not connected, can not set config");
  53. reason = $"{Module} {Name} not connected, can not set config";
  54. return false;
  55. }
  56. _isEnd = false;
  57. StepStart(args[1].ToString(), Convert.ToInt32(args[0]));
  58. reason = "";
  59. return true;
  60. });
  61. _monitorThead = new PeriodicJob(100, OnTimer, "EPDMonitor", true);
  62. EPDCallbackClient.Instance.Notify += Instance_Notify;
  63. EPDCallbackClient.Instance.Trigger += Instance_Trigger;
  64. return true;
  65. }
  66. public override void RecipeStart(string recipeName)
  67. {
  68. if (!IsEPDConnected)
  69. {
  70. LOG.Write( eEvent.ERR_ENDPOINT, Module, "EPD not connected, call recipe start ignored");
  71. return;
  72. }
  73. EV.PostInfoLog(Module, $"{Module} {Name}, Notify EPD recipe {recipeName} start");
  74. EPDClient.Instance.Service.RecipeStart(_channel, recipeName);
  75. }
  76. public override void RecipeStop()
  77. {
  78. if (!IsEPDConnected)
  79. {
  80. LOG.Write(eEvent.ERR_ENDPOINT, Module, "EPD not connected, call recipe start ignored");
  81. return;
  82. }
  83. EV.PostInfoLog(Module, $"{Module} {Name}, Notify EPD recipe stopped");
  84. EPDClient.Instance.Service.RecipeStop(_channel);
  85. }
  86. /*
  87. * ExposureTime=222;WaveLengthA=2;BinningA=3;WaveLengthB=4;BinningB=6;WaveLengthC=5;BinningC=8;
  88. * WaveLengthD=7;BinningD=9;Fd=1;PrefilterTime=2;PostfilterTime=3;AlgorithmType=Valley;
  89. * Criteria=4;DelayTime=5;ValidationTime=6;ValidationValue=7;
  90. * TimeWindow=8;MinimalTime=9;PostponeTime=10;Control=11;Normalization=12;
  91. * EnablePostponePercent=True;EnableCriterialPercent=True;
  92. * TriggerMode=System.Windows.Controls.ComboBoxItem: Event;IsFaultIfNoTrigger=True;
  93. */
  94. public override void StepStart(string config, int index)
  95. {
  96. if (!IsEPDConnected)
  97. {
  98. LOG.Write(eEvent.ERR_ENDPOINT, Module, "EPD not connected, call step start ignored");
  99. return;
  100. }
  101. EV.PostInfoLog(Module, $"{Module} {Name}, Notify EPD recipe step {index+1} start");
  102. EV.PostInfoLog(Module, $"{Module} {Name}, EPD config {config}");
  103. try
  104. {
  105. EPDConfig epd = new EPDConfig();
  106. epd.nParameterCount = 1;
  107. string[] items = config.Split(';');
  108. foreach (var item in items)
  109. {
  110. if (string.IsNullOrEmpty(item))
  111. continue;
  112. string[] pairs = item.Split('=');
  113. if (pairs.Length != 2)
  114. continue;
  115. switch (pairs[0])
  116. {
  117. case "ExposureTime":
  118. epd.Columns[0].nCCDExposureTime = int.Parse(pairs[1]);
  119. break;
  120. case "WaveLengthA":
  121. epd.Columns[0].nWaveLength[0] = ushort.Parse(pairs[1]);
  122. break;
  123. case "BinningA":
  124. epd.Columns[0].nBinning[0] = ushort.Parse(pairs[1]);
  125. break;
  126. case "WaveLengthB":
  127. epd.Columns[0].nWaveLength[1] = ushort.Parse(pairs[1]);
  128. break;
  129. case "BinningB":
  130. epd.Columns[0].nBinning[1] = ushort.Parse(pairs[1]);
  131. break;
  132. case "WaveLengthC":
  133. epd.Columns[0].nWaveLength[2] = ushort.Parse(pairs[1]);
  134. break;
  135. case "BinningC":
  136. epd.Columns[0].nBinning[2] = ushort.Parse(pairs[1]);
  137. break;
  138. case "WaveLengthD":
  139. epd.Columns[0].nWaveLength[3] = ushort.Parse(pairs[1]);
  140. break;
  141. case "BinningD":
  142. epd.Columns[0].nBinning[3] = ushort.Parse(pairs[1]);
  143. break;
  144. case "Fd":
  145. epd.Columns[0].cFunc = pairs[1];
  146. break;
  147. case "PrefilterTime":
  148. epd.Columns[0].nPreFilterTime = int.Parse(pairs[1]);
  149. break;
  150. case "PostfilterTime":
  151. epd.Columns[0].nPostFilterTime = int.Parse(pairs[1]);
  152. break;
  153. case "AlgorithmType":
  154. epd.Columns[0].algorithmType = MapType(pairs[1]);
  155. break;
  156. case "Criteria":
  157. epd.Columns[0].nCriteria = float.Parse(pairs[1]);
  158. break;
  159. case "DelayTime":
  160. epd.Columns[0].nDelayTime = int.Parse(pairs[1]);
  161. break;
  162. case "ValidationTime":
  163. epd.Columns[0].nValidationTime = int.Parse(pairs[1]);
  164. break;
  165. case "ValidationValue":
  166. epd.Columns[0].nValidationValue = int.Parse(pairs[1]);
  167. break;
  168. case "TimeWindow":
  169. epd.Columns[0].nTimeWindow = int.Parse(pairs[1]);
  170. break;
  171. case "MinimalTime":
  172. epd.Columns[0].nMinimalTime = int.Parse(pairs[1]);
  173. break;
  174. case "PostponeTime":
  175. epd.Columns[0].nPostponeTime = int.Parse(pairs[1]);
  176. break;
  177. case "Control":
  178. epd.Columns[0].bControl = Convert.ToBoolean(pairs[1]);
  179. break;
  180. case "Normalization":
  181. epd.Columns[0].bNormalization = Convert.ToBoolean(pairs[1]);
  182. break;
  183. case "EnablePostponePercent":
  184. epd.Columns[0].bPostponePercent = Convert.ToBoolean(pairs[1]);
  185. break;
  186. case "EnableCriterialPercent":
  187. epd.Columns[0].bCriteriaPercent = Convert.ToBoolean(pairs[1]);
  188. break;
  189. case "EnableEventTrigger":
  190. epd.Columns[0].bEvtTrigger = Convert.ToBoolean(pairs[1]);
  191. break;
  192. }
  193. }
  194. EPDClient.Instance.Service.StartByConfig(_channel, index, $"step{index}", epd);
  195. }
  196. catch (Exception ex)
  197. {
  198. LOG.WriteExeption(ex);
  199. }
  200. }
  201. private AlgorithmType MapType(string type)
  202. {
  203. switch (type)
  204. {
  205. case "Unknown": return AlgorithmType.ALG_NONE;
  206. case "Above_ABS_Value": return AlgorithmType.ALG_RISE_VALUE;
  207. case "Below_ABS_Value": return AlgorithmType.ALG_FALL_VALUE;
  208. case "Drop_Percent": return AlgorithmType.ALG_FALL_PERCENT;
  209. case "Up_Percent": return AlgorithmType.ALG_RISE_PERCENT;
  210. case "Range_In": return AlgorithmType.ALG_RANGE_IN;
  211. case "Gradient": return AlgorithmType.ALG_GRADIENT;
  212. case "Peek": return AlgorithmType.ALG_PEAK;
  213. case "Valley": return AlgorithmType.ALG_VALLEY;
  214. case "Min_Drop_Percent": return AlgorithmType.ALG_MIN_FALL_PERCENT;
  215. case "Min_Up_Percent": return AlgorithmType.ALG_MIN_RISE_PERCENT;
  216. case "Max_Drop_Percent": return AlgorithmType.ALG_MAX_FALL_PERCENT;
  217. case "Max_Up_Percent": return AlgorithmType.ALG_MAX_RISE_PERCENT;
  218. case "Rise_Fall": return AlgorithmType.ALG_RISE_FALL;
  219. case "Fall_Rise": return AlgorithmType.ALG_FALL_RISE;
  220. }
  221. return AlgorithmType.ALG_NONE;
  222. }
  223. public override void StepStop()
  224. {
  225. if (!IsEPDConnected)
  226. {
  227. LOG.Write(eEvent.ERR_ENDPOINT, Module, "EPD not connected, call step stop ignored");
  228. return;
  229. }
  230. EV.PostInfoLog(Module, $"{Module} {Name}, Notify EPD recipe step stopped");
  231. EPDClient.Instance.Service.Stop(_channel);
  232. }
  233. private bool OnTimer()
  234. {
  235. try
  236. {
  237. bool retryConnect = false;
  238. lock (_lockerTrigger)
  239. {
  240. retryConnect = _triggerConnected.M;
  241. if (!_triggerConnected.M)
  242. {
  243. //if (_enableRetry)
  244. {
  245. retryConnect = true;
  246. }
  247. }
  248. if (retryConnect)
  249. {
  250. _connectionCounter++;
  251. if (_connectionCounter > 10000)
  252. _connectionCounter = 1;
  253. _triggerConnected.CLK = _connectionCounter== EPDClient.Instance.Service.Heartbeat(_connectionCounter);
  254. _triggerNotConnected.CLK = !_triggerConnected.M;
  255. if (_triggerConnected.Q)
  256. {
  257. EPDCallbackClient.Instance.Init();
  258. EV.PostInfoLog(Module, $"{Module} {Name}, EPD Connected");
  259. }
  260. if (_triggerConnected.M)
  261. {
  262. _channelStatus = EPDClient.Instance.Service.QueryState(_channel).ToString();
  263. }
  264. if (_triggerNotConnected.Q)
  265. {
  266. EPDCallbackClient.Instance.Stop();
  267. EV.PostWarningLog(Module, $"{Module} {Name}, EPD disconnected");
  268. }
  269. }
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. LOG.WriteExeption(ex);
  275. }
  276. return true;
  277. }
  278. public override void QueryConfigList()
  279. {
  280. //_socketClient.QueryConfigList();
  281. }
  282. private void Instance_Notify(int channel, string e)
  283. {
  284. if (_channel != channel)
  285. return;
  286. EV.PostInfoLog(Module, $"{Module} {Name}, EPD Feedback:{e}");
  287. }
  288. private void Instance_Trigger(int channel, TriggerEventArgs e)
  289. {
  290. if (_channel != channel)
  291. return;
  292. _isEnd = true;
  293. EV.PostInfoLog(Module, $"{Module} {Name}, EPD: {e.Channel}.{e.Name} Triggered");
  294. }
  295. public override void Monitor()
  296. {
  297. }
  298. public override void Terminate()
  299. {
  300. }
  301. public override void Reset()
  302. {
  303. lock (_lockerTrigger)
  304. {
  305. if (!_triggerConnected.M)
  306. {
  307. _triggerConnected.RST = true;
  308. _triggerNotConnected.RST = true;
  309. }
  310. }
  311. }
  312. }
  313. public class EPDCallbackClient : Singleton<EPDCallbackClient>, IEPDCallback
  314. {
  315. public event Action<int, string> Notify;
  316. public event Action<int, TriggerEventArgs> Trigger;
  317. private EPDCallbackServiceClient _service;
  318. public EPDCallbackClient()
  319. {
  320. _service = new EPDCallbackServiceClient(this);
  321. }
  322. public void Init()
  323. {
  324. _service.Register();
  325. }
  326. public void Stop()
  327. {
  328. _service.UnRegister();
  329. }
  330. //public void OnNotify(string channel, string message)
  331. //{
  332. //}
  333. //public void OnTrigger(string channel, string name, long ticket)
  334. //{
  335. //}
  336. public void OnNotify(int channel, EPDEventType EventType, string message)
  337. {
  338. Notify?.Invoke(channel, $"{EventType}:{message}");
  339. }
  340. public void OnTrigger(int channel, string name, long ticket)
  341. {
  342. Trigger?.Invoke(channel, new TriggerEventArgs() { Channel = channel, Name = name, Ticket = ticket });
  343. }
  344. }
  345. public class TriggerEventArgs
  346. {
  347. public int Channel { get; set; }
  348. public string Name { get; set; }
  349. public long Ticket { get; set; }
  350. }
  351. public class EPDCallbackServiceClient : DuplexChannelServiceClientWrapper<IEPDCallbackService>
  352. {
  353. private Guid _clientId = Guid.Empty;
  354. public EPDCallbackServiceClient(IEPDCallback callback) : base(new InstanceContext(callback), "Client_IEPDCallbackService", "IEPDCallbackService")
  355. {
  356. }
  357. public void Register()
  358. {
  359. if (_clientId != Guid.Empty)
  360. UnRegister();
  361. _clientId = Guid.NewGuid();
  362. Invoke(x => x.Register(_clientId));
  363. }
  364. public void UnRegister()
  365. {
  366. Invoke(x => x.UnRegister(_clientId));
  367. }
  368. }
  369. }