EPDDevice.cs 16 KB

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