AeOr4000T.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.OperationCenter;
  10. using Aitex.Core.RT.SCCore;
  11. using Aitex.Core.Util;
  12. using MECF.Framework.Common.Communications;
  13. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Temperatures.AEs
  14. {
  15. public class AEOR4000T : BaseDevice, IConnection, IDevice
  16. {
  17. private AEOR4000TConnection _connection;
  18. private bool _activeMonitorStatus;
  19. private int _errorCode;
  20. private R_TRIG _trigCommunicationError = new R_TRIG();
  21. private R_TRIG _trigRetryConnect = new R_TRIG();
  22. private PeriodicJob _thread;
  23. private int tempCount = 1;
  24. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  25. private object _locker = new object();
  26. private bool _enableLog = true;
  27. private string _scRoot;
  28. public double AETemp1 { get; set; }
  29. public double AETemp2 { get; set; }
  30. public double AETemp3 { get; set; }
  31. public string Address
  32. {
  33. get; set;
  34. }
  35. public bool IsConnected
  36. {
  37. get
  38. {
  39. return _connection != null && _connection.IsConnected;
  40. }
  41. }
  42. public bool Connect()
  43. {
  44. return true;
  45. }
  46. public bool Disconnect()
  47. {
  48. return true;
  49. }
  50. public AEOR4000T(string module, string name, string scRoot) : base(module, name, name, name)
  51. {
  52. _scRoot = scRoot;
  53. _activeMonitorStatus = true;
  54. }
  55. ~AEOR4000T()
  56. {
  57. _connection.Disconnect();
  58. }
  59. //public TempOmron(string module, string name, string scRoot) : base(module, name, name, name)
  60. //{
  61. // //tempCount = count;
  62. // _scRoot = scRoot;
  63. // _activeMonitorStatus = true;
  64. //}
  65. public void QueryTemp()
  66. {
  67. _lstHandler.AddLast(new AETempReadCommandHandler(this, "OUT", "1"));
  68. }
  69. public void ResetDevice()
  70. {
  71. }
  72. public void QueryError()
  73. {
  74. EV.PostInfoLog(Module, "Query error");
  75. }
  76. public bool Initialize()
  77. {
  78. // string portName = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.Address");
  79. string portName = SC.GetStringValue($"{Name}.Address");
  80. Address = portName;
  81. //int address = SC.GetValue<int>($"{_scRoot}.{Name}.DeviceAddress");
  82. _enableLog = SC.GetValue<bool>($"{Name}.EnableLogMessage");
  83. _connection = new AEOR4000TConnection(portName);
  84. _connection.EnableLog(_enableLog);
  85. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 5;
  86. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
  87. if (sleep <= 0 || sleep > 10)
  88. sleep = 2;
  89. int retry = 0;
  90. do
  91. {
  92. _connection.Disconnect();
  93. Thread.Sleep(sleep * 1000);
  94. if (_connection.Connect())
  95. {
  96. EV.PostInfoLog(Module, $"{Name} connected");
  97. break;
  98. }
  99. if (count > 0 && retry++ > count)
  100. {
  101. LOG.Write($"Retry connect {Module}.{Name} stop retry.");
  102. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name}.");
  103. break;
  104. }
  105. Thread.Sleep(sleep * 1000);
  106. LOG.Write($"Retry connect {Module}.{Name} for the {retry + 1} time.");
  107. } while (true);
  108. _thread = new PeriodicJob(200, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  109. //调换 2 3 midle outter
  110. DATA.Subscribe($"{Module}.{Name}.AETemp1", () => AETemp1);
  111. DATA.Subscribe($"{Module}.{Name}.AETemp2", () => AETemp3);
  112. DATA.Subscribe($"{Module}.{Name}.AETemp3", () => AETemp2);
  113. //DATA.Subscribe($"{Module}.{Name}.SettingTemp", () => SettingTemp);
  114. //OP.Subscribe($"{Module}.{Name}.WriteConfigData", SetCofig);
  115. //
  116. ConnectionManager.Instance.Subscribe($"{Name}", this);
  117. return true;
  118. }
  119. private bool OnTimer()
  120. {
  121. try
  122. {
  123. _connection.MonitorTimeout();
  124. if (!_connection.IsConnected || _connection.IsCommunicationError)
  125. {
  126. //
  127. lock (_locker)
  128. {
  129. _lstHandler.Clear();
  130. }
  131. _trigRetryConnect.CLK = !_connection.IsConnected;
  132. if (_trigRetryConnect.Q)
  133. {
  134. //_connection.SetPortAddress(SC.GetStringValue($"{_scRoot}.{Name}.Address"));
  135. if (!_connection.Connect())
  136. {
  137. //qbh 20220330
  138. //OP.DoOperation($"{Module}.PMAETemp.SetPyroCommunicationError", true);
  139. LOG.Write($"{Module}.PMAETemp.SetPyroCommunicationError");
  140. //在Process模式和PreProcess模式下AE掉线直接Abort
  141. string moduleStatus = DATA.Poll($"{Module}.Status").ToString();
  142. if (moduleStatus == "PreProcess" || moduleStatus == "Process")
  143. {
  144. OP.DoOperation($"{Module}.Abort");
  145. EV.PostWarningLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  146. }
  147. else
  148. {
  149. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  150. }
  151. }
  152. else
  153. {
  154. //qbh 20220330
  155. //OP.DoOperation($"{Module}.PMAETemp.SetPyroCommunicationError", false);
  156. LOG.Write($"{Module}.PMAETemp.SetPyroCommunicationError,reconnected.");
  157. }
  158. }
  159. _connection.ForceClear();
  160. return true;
  161. }
  162. HandlerBase handler = null;
  163. //if (!_connection.IsBusy)
  164. //{
  165. lock (_locker)
  166. {
  167. if (_lstHandler.Count == 0)
  168. QueryTemp();
  169. if (_lstHandler.Count > 0 && !_connection.IsBusy)
  170. {
  171. handler = _lstHandler.First.Value;
  172. _lstHandler.RemoveFirst();
  173. if (handler != null)
  174. {
  175. _connection.Execute(handler);
  176. }
  177. }
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. LOG.Write(ex);
  183. }
  184. return true;
  185. }
  186. internal void NoteError()
  187. {
  188. }
  189. public void ParseCommandInfo(string command, string Message)
  190. {
  191. switch (command)
  192. {
  193. case "OUT":
  194. {
  195. if (Message != null)
  196. {
  197. if (Message.Contains(" "))
  198. {
  199. var strs = Message.Split(' ');
  200. AETemp1 = Convert.ToDouble(strs[0]);
  201. AETemp2 = Convert.ToDouble(strs[1]);
  202. AETemp3 = Convert.ToDouble(strs[2]);
  203. //AETemp1 = TempFilter2("AETemp1", Convert.ToDouble(strs[0]));
  204. //AETemp2 = TempFilter2("AETemp2", Convert.ToDouble(strs[1]));
  205. //AETemp3 = TempFilter2("AETemp3", Convert.ToDouble(strs[2]));
  206. }
  207. }
  208. }
  209. break;
  210. }
  211. }
  212. #region 滤波
  213. //qbh 20220309
  214. const int iQueCap = 10;
  215. static Queue qWafInner = new Queue(iQueCap);
  216. //static Queue qSusInner = new Queue(iQueCap);
  217. static Queue qWafMiddle = new Queue(iQueCap);
  218. //static Queue qSusMiddle = new Queue(iQueCap);
  219. static Queue qWafOuter = new Queue(iQueCap);
  220. //static Queue qSusOuter = new Queue(iQueCap);
  221. const double dbThres = 20.0;
  222. public static double TempFilter(string sName, double dbNewTemp)
  223. {
  224. //
  225. Queue qTempData = new Queue();
  226. switch (sName)
  227. {
  228. case "AETemp1":
  229. qTempData = qWafInner;
  230. break;
  231. case "AETemp2":
  232. qTempData = qWafMiddle;
  233. break;
  234. case "AETemp3":
  235. qTempData = qWafOuter;
  236. break;
  237. default:
  238. break;
  239. }
  240. //
  241. qTempData.Enqueue(dbNewTemp);
  242. //
  243. if (qTempData.Count < iQueCap)
  244. {
  245. return dbNewTemp;
  246. }
  247. else
  248. {
  249. //
  250. while (qTempData.Count > iQueCap)
  251. {
  252. qTempData.Dequeue();
  253. }
  254. //
  255. List<double> liTemp = new List<double>();
  256. object[] objs = qTempData.ToArray();
  257. foreach (object obj in objs)
  258. {
  259. liTemp.Add((double)obj);
  260. }
  261. liTemp.Sort();
  262. if (liTemp.Count > 0)
  263. {
  264. liTemp.RemoveAt(0);
  265. }
  266. liTemp.Reverse();
  267. if (liTemp.Count > 0)
  268. {
  269. liTemp.RemoveAt(0);
  270. }
  271. //
  272. double dbAvg = 0.0;
  273. foreach (double data in liTemp)
  274. {
  275. dbAvg += data;
  276. }
  277. dbAvg /= liTemp.Count;
  278. //
  279. return dbAvg;
  280. }
  281. //
  282. }
  283. /// <summary>
  284. /// 均方根值法
  285. /// </summary>
  286. /// <param name="sName"></param>
  287. /// <param name="dbNewTemp"></param>
  288. /// <returns></returns>
  289. public static double TempFilter2(string sName, double dbNewTemp)
  290. {
  291. //
  292. Queue qTempData = new Queue();
  293. switch (sName)
  294. {
  295. case "AETemp1":
  296. qTempData = qWafInner;
  297. break;
  298. case "AETemp2":
  299. qTempData = qWafMiddle;
  300. break;
  301. case "AETemp3":
  302. qTempData = qWafOuter;
  303. break;
  304. default:
  305. break;
  306. }
  307. //
  308. qTempData.Enqueue(dbNewTemp);
  309. //
  310. if (qTempData.Count < iQueCap)
  311. {
  312. return dbNewTemp;
  313. }
  314. else
  315. {
  316. //
  317. while (qTempData.Count > iQueCap)
  318. {
  319. qTempData.Dequeue();
  320. }
  321. //
  322. List<double> liTemp = new List<double>();
  323. object[] objs = qTempData.ToArray();
  324. foreach (object obj in objs)
  325. {
  326. liTemp.Add((double)obj);
  327. }
  328. liTemp.Sort();
  329. if (liTemp.Count > 0)
  330. {
  331. liTemp.RemoveAt(0);
  332. }
  333. liTemp.Reverse();
  334. if (liTemp.Count > 0)
  335. {
  336. liTemp.RemoveAt(0);
  337. }
  338. //均方根
  339. double dbAvg = 0.0;
  340. foreach (double data in liTemp)
  341. {
  342. dbAvg += data * data;
  343. }
  344. dbAvg /= liTemp.Count;
  345. dbAvg = Math.Sqrt(dbAvg);
  346. //
  347. return dbAvg;
  348. }
  349. //
  350. }
  351. #endregion
  352. public void Monitor()
  353. {
  354. try
  355. {
  356. _connection.EnableLog(_enableLog);
  357. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  358. if (_trigCommunicationError.Q)
  359. {
  360. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  361. }
  362. }
  363. catch (Exception ex)
  364. {
  365. LOG.Write(ex);
  366. }
  367. }
  368. public void Reset()
  369. {
  370. _connection.SetCommunicationError(false, "");
  371. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  372. _trigCommunicationError.RST = true;
  373. _trigRetryConnect.RST = true;
  374. }
  375. public void SetActiveMonitor(bool active)
  376. {
  377. _activeMonitorStatus = active;
  378. }
  379. public void SetErrorCode(int errorCode)
  380. {
  381. _errorCode = errorCode;
  382. }
  383. public void Terminate()
  384. {
  385. _connection.Disconnect();
  386. }
  387. }
  388. }