EPDDevice.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. epd.Columns[0].cFunc = pairs[1];
  151. break;
  152. case "PrefilterTime":
  153. epd.Columns[0].nPreFilterTime = int.Parse(pairs[1]);
  154. break;
  155. case "PostfilterTime":
  156. epd.Columns[0].nPostFilterTime = int.Parse(pairs[1]);
  157. break;
  158. case "AlgorithmType":
  159. epd.Columns[0].algorithmType = MapType(pairs[1]);
  160. break;
  161. case "Criteria":
  162. epd.Columns[0].nCriteria = float.Parse(pairs[1]);
  163. break;
  164. case "DelayTime":
  165. epd.Columns[0].nDelayTime = int.Parse(pairs[1]);
  166. break;
  167. case "ValidationTime":
  168. epd.Columns[0].nValidationTime = int.Parse(pairs[1]);
  169. break;
  170. case "ValidationValue":
  171. epd.Columns[0].nValidationValue = int.Parse(pairs[1]);
  172. break;
  173. case "TimeWindow":
  174. epd.Columns[0].nTimeWindow = int.Parse(pairs[1]);
  175. break;
  176. case "MinimalTime":
  177. epd.Columns[0].nMinimalTime = int.Parse(pairs[1]);
  178. break;
  179. case "PostponeTime":
  180. epd.Columns[0].nPostponeTime = int.Parse(pairs[1]);
  181. break;
  182. case "Control":
  183. epd.Columns[0].bControl = Convert.ToBoolean(pairs[1]);
  184. break;
  185. case "Normalization":
  186. epd.Columns[0].bNormalization = Convert.ToBoolean(pairs[1]);
  187. break;
  188. case "EnablePostponePercent":
  189. epd.Columns[0].bPostponePercent = Convert.ToBoolean(pairs[1]);
  190. break;
  191. case "EnableCriterialPercent":
  192. epd.Columns[0].bCriteriaPercent = Convert.ToBoolean(pairs[1]);
  193. break;
  194. case "EnableEventTrigger":
  195. epd.Columns[0].bEvtTrigger = Convert.ToBoolean(pairs[1]);
  196. break;
  197. }
  198. }
  199. EPDClient.Instance.Service.StartByConfig(_channel, index, $"step{index}", epd);
  200. }
  201. catch (Exception ex)
  202. {
  203. LOG.Write(ex);
  204. EV.PostInfoLog(Module, $"{Module} {Name}, Step {index + 1} config values not valid, {ex.Message}");
  205. }
  206. }
  207. private AlgorithmType MapType(string type)
  208. {
  209. switch (type)
  210. {
  211. case "Unknown": return AlgorithmType.ALG_NONE;
  212. case "Above_ABS_Value": return AlgorithmType.ALG_RISE_VALUE;
  213. case "Below_ABS_Value": return AlgorithmType.ALG_FALL_VALUE;
  214. case "Drop_Percent": return AlgorithmType.ALG_FALL_PERCENT;
  215. case "Up_Percent": return AlgorithmType.ALG_RISE_PERCENT;
  216. case "Range_In": return AlgorithmType.ALG_RANGE_IN;
  217. case "Gradient": return AlgorithmType.ALG_GRADIENT;
  218. case "Peek": return AlgorithmType.ALG_PEAK;
  219. case "Valley": return AlgorithmType.ALG_VALLEY;
  220. case "Min_Drop_Percent": return AlgorithmType.ALG_MIN_FALL_PERCENT;
  221. case "Min_Up_Percent": return AlgorithmType.ALG_MIN_RISE_PERCENT;
  222. case "Max_Drop_Percent": return AlgorithmType.ALG_MAX_FALL_PERCENT;
  223. case "Max_Up_Percent": return AlgorithmType.ALG_MAX_RISE_PERCENT;
  224. case "Rise_Fall": return AlgorithmType.ALG_RISE_FALL;
  225. case "Fall_Rise": return AlgorithmType.ALG_FALL_RISE;
  226. }
  227. return AlgorithmType.ALG_NONE;
  228. }
  229. public void StepStop()
  230. {
  231. if (!IsConnected)
  232. {
  233. LOG.Write("EPD not connected, call step stop ignored");
  234. return;
  235. }
  236. EV.PostInfoLog(Module, $"{Module} {Name}, Notify EPD recipe step stopped");
  237. EPDClient.Instance.Service.Stop(_channel);
  238. }
  239. private bool OnTimer()
  240. {
  241. try
  242. {
  243. bool retryConnect = false;
  244. lock (_lockerTrigger)
  245. {
  246. retryConnect = _triggerConnected.M;
  247. if (!_triggerConnected.M)
  248. {
  249. //if (_enableRetry)
  250. {
  251. retryConnect = true;
  252. }
  253. }
  254. if (retryConnect)
  255. {
  256. _connectionCounter++;
  257. if (_connectionCounter > 10000)
  258. _connectionCounter = 1;
  259. _triggerConnected.CLK = _connectionCounter== EPDClient.Instance.Service.Heartbeat(_connectionCounter);
  260. _triggerNotConnected.CLK = !_triggerConnected.M;
  261. if (_triggerConnected.Q)
  262. {
  263. EPDCallbackClient.Instance.Init();
  264. EV.PostInfoLog(Module, $"{Module} {Name}, EPD Connected");
  265. }
  266. if (_triggerConnected.M)
  267. {
  268. _channelStatus = EPDClient.Instance.Service.QueryState(_channel).ToString();
  269. }
  270. if (_triggerNotConnected.Q)
  271. {
  272. EPDCallbackClient.Instance.Stop();
  273. EV.PostWarningLog(Module, $"{Module} {Name}, EPD disconnected");
  274. }
  275. }
  276. }
  277. }
  278. catch (Exception ex)
  279. {
  280. LOG.Write(ex);
  281. }
  282. return true;
  283. }
  284. private void Instance_Notify(int channel, string e)
  285. {
  286. if (_channel != channel)
  287. return;
  288. EV.PostInfoLog(Module, $"{Module} {Name}, EPD Feedback:{e}");
  289. }
  290. private void Instance_Trigger(int channel, TriggerEventArgs e)
  291. {
  292. if (_channel != channel)
  293. return;
  294. _isEnd = true;
  295. EV.PostInfoLog(Module, $"{Module} {Name}, EPD: {e.Channel}.{e.Name} Triggered");
  296. }
  297. public void Monitor()
  298. {
  299. }
  300. public void Terminate()
  301. {
  302. }
  303. public void Reset()
  304. {
  305. lock (_lockerTrigger)
  306. {
  307. if (!_triggerConnected.M)
  308. {
  309. _triggerConnected.RST = true;
  310. _triggerNotConnected.RST = true;
  311. }
  312. }
  313. }
  314. }
  315. public class EPDCallbackClient : Singleton<EPDCallbackClient>, IEPDCallback
  316. {
  317. public event Action<int, string> Notify;
  318. public event Action<int, TriggerEventArgs> Trigger;
  319. private EPDCallbackServiceClient _service;
  320. public EPDCallbackClient()
  321. {
  322. _service = new EPDCallbackServiceClient(this);
  323. }
  324. public void Init()
  325. {
  326. _service.Register();
  327. }
  328. public void Stop()
  329. {
  330. _service.UnRegister();
  331. }
  332. //public void OnNotify(string channel, string message)
  333. //{
  334. //}
  335. //public void OnTrigger(string channel, string name, long ticket)
  336. //{
  337. //}
  338. public void OnNotify(int channel, EPDEventType EventType, string message)
  339. {
  340. Notify?.Invoke(channel, $"{EventType}:{message}");
  341. }
  342. public void OnTrigger(int channel, string name, long ticket)
  343. {
  344. Trigger?.Invoke(channel, new TriggerEventArgs() { Channel = channel, Name = name, Ticket = ticket });
  345. }
  346. }
  347. public class TriggerEventArgs
  348. {
  349. public int Channel { get; set; }
  350. public string Name { get; set; }
  351. public long Ticket { get; set; }
  352. }
  353. public class EPDCallbackServiceClient : DuplexChannelServiceClientWrapper<IEPDCallbackService>
  354. {
  355. private Guid _clientId = Guid.Empty;
  356. public EPDCallbackServiceClient(IEPDCallback callback) : base(new InstanceContext(callback), "Client_IEPDCallbackService", "IEPDCallbackService")
  357. {
  358. }
  359. public void Register()
  360. {
  361. if (_clientId != Guid.Empty)
  362. UnRegister();
  363. _clientId = Guid.NewGuid();
  364. Invoke(x => x.Register(_clientId));
  365. }
  366. public void UnRegister()
  367. {
  368. Invoke(x => x.UnRegister(_clientId));
  369. }
  370. }
  371. }