EPDDevice.cs 15 KB

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