SkyPump.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Xml;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.RT.DataCenter;
  7. using Aitex.Core.RT.Device;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.IOCore;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.RT.OperationCenter;
  12. using Aitex.Core.RT.SCCore;
  13. using Aitex.Core.RT.Tolerance;
  14. using Aitex.Core.Util;
  15. using MECF.Framework.Common.Communications;
  16. using MECF.Framework.Common.DataCenter;
  17. using MECF.Framework.Common.Device.Bases;
  18. using MECF.Framework.Common.Equipment;
  19. using VirgoCommon;
  20. namespace VirgoRT.Devices
  21. {
  22. //interface IPumpControl
  23. //{
  24. // bool IsRunning { get; }
  25. // void TurnON();
  26. // void TurnOFF();
  27. //}
  28. public enum SkyPumpState { ON = 0, OFF, Connected, Disconnected, Unknown, ERROR }
  29. static class SkyPumpMessage
  30. {
  31. public const string EOF = "\r\n";
  32. public const string SET_ON = "@00CON_FDP_ON";
  33. public const string SET_OFF = "@00CON_FDP_OFF";
  34. public const string GET_ON = "@00FDP_ONOK"; // \0\r\n
  35. public const string GET_OFF = "@00FDP_OFFOK"; // \0\r\n
  36. public const string READ_DATA = "@00READ_RUN_PARA";
  37. public const string GET_DATA = "@00RUN_PARA";
  38. }
  39. class SkyPump : PumpBase
  40. {
  41. // ----------------------------Fields--------------------------
  42. //
  43. private const ushort CHK_ST_INTERVAL = 1000;
  44. private const ushort CHK_REC_INTERVAL = 20 * 1000;
  45. private const ushort CHK_PUMP_REC_INTERVAL = 10 * 1000;
  46. private double _total;
  47. private double _fromLast;
  48. private const string EOF = "\r";
  49. private const char MSG_DELIMITER = ' ';
  50. private readonly string _PortNum = "COM91";
  51. private const int _counterMax = 30;
  52. private int _counter = 0;
  53. private string _fdp_Current = null;
  54. private string _roots_Current = null;
  55. private string _fdp_Temp = null;
  56. private string _roots_Temp = null;
  57. private string _n2Flow = null;
  58. private string _pressure = null;
  59. private string _runTime = null;
  60. private readonly AsyncSerialPort _serial;
  61. private readonly DeviceTimer _timerQueryStatus = new DeviceTimer();
  62. private readonly DeviceTimer _timerReceiveStatus = new DeviceTimer();
  63. private readonly DeviceTimer _timerPumpOn = new DeviceTimer();
  64. private readonly DeviceTimer _timerPumpOff = new DeviceTimer();
  65. private readonly DeviceTimer _timerTotal = new DeviceTimer();
  66. private readonly DeviceTimer _timerFromLast = new DeviceTimer();
  67. private readonly R_TRIG _trigPumpOn = new R_TRIG();
  68. private readonly R_TRIG _trigPumpOff = new R_TRIG();
  69. private readonly R_TRIG _trigReceiveStatus = new R_TRIG();
  70. private readonly RD_TRIG _trigOnOff = new RD_TRIG();
  71. private readonly R_TRIG _trigPMNeeded = new R_TRIG();
  72. private StatsDataItemRFAndPump _statPumpOnTime;
  73. // --------------------------Properties------------------------
  74. //
  75. public SkyPumpState StatusDry { get; set; }
  76. public SkyPumpState StatusRoose { get; set; }
  77. public string FDP_Current
  78. {
  79. get
  80. {
  81. return _fdp_Current;
  82. }
  83. }
  84. public string ROOTS_Current
  85. {
  86. get
  87. {
  88. return _roots_Current;
  89. }
  90. }
  91. public string FDP_Temp
  92. {
  93. get
  94. {
  95. return _fdp_Temp;
  96. }
  97. }
  98. public string ROOTS_Temp
  99. {
  100. get
  101. {
  102. return _roots_Temp;
  103. }
  104. }
  105. public string N2Flow
  106. {
  107. get
  108. {
  109. return _n2Flow;
  110. }
  111. }
  112. public string Pressure
  113. {
  114. get
  115. {
  116. return _pressure;
  117. }
  118. }
  119. public string RunTime
  120. {
  121. get
  122. {
  123. return _runTime;
  124. }
  125. }
  126. public string LastPMTime
  127. {
  128. get
  129. {
  130. return _statPumpOnTime != null ? _statPumpOnTime.LastPMTime.ToString() : "";
  131. }
  132. }
  133. public double DaysFromLastPM
  134. {
  135. get
  136. {
  137. return _statPumpOnTime == null ? 0 : _statPumpOnTime.fromLastPM;
  138. }
  139. set
  140. {
  141. if (_statPumpOnTime != null)
  142. _statPumpOnTime.fromLastPM = value;
  143. }
  144. }
  145. public double TotalDays
  146. {
  147. get
  148. {
  149. return _statPumpOnTime != null ? _statPumpOnTime.Total : 0;
  150. }
  151. set
  152. {
  153. if (_statPumpOnTime != null)
  154. _statPumpOnTime.Total = value;
  155. }
  156. }
  157. public double PMIntervalDays
  158. {
  159. get
  160. {
  161. return _statPumpOnTime != null ? _statPumpOnTime.PMInterval : 0;
  162. }
  163. }
  164. public bool IsPMNeeded
  165. {
  166. get
  167. {
  168. return DaysFromLastPM > PMIntervalDays;
  169. }
  170. }
  171. public bool EnableAlarm
  172. {
  173. get
  174. {
  175. return _statPumpOnTime == null || _statPumpOnTime.AlarmEnable;
  176. }
  177. }
  178. [Subscription(AITPumpProperty.IsRunning)]
  179. public override bool IsRunning
  180. {
  181. get
  182. {
  183. return StatusDry == SkyPumpState.ON;
  184. }
  185. }
  186. [Subscription(AITPumpProperty.IsError)]
  187. public override bool IsError
  188. {
  189. get
  190. {
  191. return StatusDry == SkyPumpState.ERROR || StatusDry == SkyPumpState.Disconnected;
  192. }
  193. }
  194. public override AITPumpData DeviceData
  195. {
  196. get
  197. {
  198. AITPumpData deviceData = new AITPumpData
  199. {
  200. DeviceName = Name,
  201. DeviceModule = Module,
  202. DeviceSchematicId = DeviceID,
  203. DisplayName = Display,
  204. IsError = false,
  205. IsWarning = false,
  206. IsOn = IsRunning,
  207. //WaterFlow = WaterFlowValue,
  208. IsDryPumpEnable = true,
  209. IsN2PressureEnable = false,
  210. IsWaterFlowEnable = false,
  211. //WaterFlowWarning = WaterFlowWarning,
  212. //WaterFlowAlarm = WaterFlowAlarm,
  213. //N2PressureAlarm = N2PressureAlarm,
  214. //N2PressureWarning = N2PressureWarning,
  215. };
  216. return deviceData;
  217. }
  218. }
  219. // --------------------------Constructor-----------------------
  220. //
  221. public SkyPump(ModuleName mod) : base(mod.ToString(), VirgoDevice.MainPump.ToString(), "SKY pump", "")
  222. {
  223. _PortNum = SC.GetStringValue($"{mod}.DryPump.Port");
  224. StatusDry = SkyPumpState.Unknown;
  225. _serial = new AsyncSerialPort(_PortNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r\n", false);
  226. }
  227. public override bool Initialize()
  228. {
  229. base.Initialize();
  230. if (!_serial.Open())
  231. {
  232. StatusDry = SkyPumpState.Disconnected;
  233. EV.PostAlarmLog(this.Module, "Sky Pump串口无法打开");
  234. return false;
  235. }
  236. _statPumpOnTime = StatsDataManager.Instance.GetItemRFAndPump($"{Module}.PumpOnTime");
  237. StatusDry = SkyPumpState.Connected;
  238. _trigPumpOn.RST = true;
  239. _trigPumpOff.RST = true;
  240. _trigReceiveStatus.RST = true;
  241. _serial.OnDataChanged += OnPortDataChanged;
  242. _serial.OnBinaryDataChanged += OnPortBinaryDataChanged;
  243. _serial.OnErrorHappened += OnErrorOccurred;
  244. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  245. _timerReceiveStatus.Start(CHK_REC_INTERVAL);
  246. return true;
  247. }
  248. private void OnPortBinaryDataChanged(byte[] obj)
  249. {
  250. try
  251. {
  252. _timerReceiveStatus.Stop();
  253. _timerReceiveStatus.Start(CHK_REC_INTERVAL);
  254. _trigReceiveStatus.RST = true;
  255. ////if (SC.GetValue<bool>("System.IsSimulatorMode"))
  256. ////{
  257. //// if (obj.Length != 40 || obj.Length != 15 || obj.Length != 14)
  258. //// return;
  259. ////}
  260. //bool condition1 = obj.Length >= 14 && Encoding.ASCII.GetString(obj, 12, 2) == SkyPumpMessage.EOF;
  261. //bool condition2 = obj.Length >= 15 && Encoding.ASCII.GetString(obj, 13, 2) == SkyPumpMessage.EOF;
  262. //bool condition3 = obj.Length >= 40 && Encoding.ASCII.GetString(obj, 38, 2) == SkyPumpMessage.EOF;
  263. //bool valid = obj.Length > 2 && (condition1
  264. // || condition2
  265. // || condition3);
  266. var strData = System.Text.Encoding.ASCII.GetString(obj);
  267. if (strData.Contains(SkyPumpMessage.EOF))
  268. //if (valid)
  269. {
  270. string cmd = "";
  271. if (obj.Length == 14 || obj.Length >= 40)
  272. cmd = Encoding.ASCII.GetString(obj, 0, 11);
  273. else if (obj.Length == 15)
  274. cmd = Encoding.ASCII.GetString(obj, 0, 12);
  275. if (cmd == SkyPumpMessage.GET_ON)
  276. {
  277. _trigPumpOn.CLK = true;
  278. _timerPumpOn.Start(CHK_PUMP_REC_INTERVAL);
  279. LOG.Info($"[{Module}] {SkyPumpMessage.GET_ON}");
  280. return;
  281. }
  282. if (cmd == SkyPumpMessage.GET_OFF)
  283. {
  284. _trigPumpOff.CLK = true;
  285. _timerPumpOff.Start(CHK_PUMP_REC_INTERVAL);
  286. LOG.Info($"[{Module}] {SkyPumpMessage.GET_OFF}");
  287. return;
  288. }
  289. if (cmd == SkyPumpMessage.GET_DATA)
  290. {
  291. _fdp_Current = Encoding.ASCII.GetString(obj, 11, 2);
  292. _roots_Current = Encoding.ASCII.GetString(obj, 13, 2);
  293. _fdp_Temp = Encoding.ASCII.GetString(obj, 15, 3);
  294. _roots_Temp = Encoding.ASCII.GetString(obj, 18, 3);
  295. _n2Flow = Encoding.ASCII.GetString(obj, 21, 2);
  296. _pressure = Encoding.ASCII.GetString(obj, 23, 4);
  297. _runTime = Encoding.ASCII.GetString(obj, 27, 5);
  298. ParaErrSta1(new BitArray(new byte[] { obj[32] }));
  299. ParaErrSta2(new BitArray(new byte[] { obj[33] }));
  300. ParaRunSta(new BitArray(new byte[] { obj[34] }));
  301. //ParaRunSta(new BitArray(new byte[] { obj[35] }));
  302. ParaErrSta3(new BitArray(new byte[] { obj[36] }));
  303. }
  304. }
  305. else
  306. {
  307. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  308. {
  309. LOG.Info($"[{Module}] Sky Pump 数据无效:[{strData}]");
  310. }
  311. }
  312. }
  313. catch (Exception ex)
  314. {
  315. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  316. {
  317. LOG.Error($"[{Module}] sky pump data para error: [{ex.Message}]");
  318. }
  319. }
  320. }
  321. private void OnErrorOccurred(string obj)
  322. {
  323. StatusDry = SkyPumpState.ERROR;
  324. LOG.Error($"[{Module}] sky pump error: [{obj}]");
  325. }
  326. private void OnPortDataChanged(string obj)
  327. {
  328. if (string.IsNullOrEmpty(obj))
  329. LOG.Error($"[{Module}] sky pump message IsNullOrEmpty");
  330. try
  331. {
  332. string cmd = obj.ToString().Split('\n')[0].Split('\r')[0];
  333. if (cmd == SkyPumpMessage.GET_ON)
  334. {
  335. LOG.Info($"[{Module}] {SkyPumpMessage.GET_ON}");
  336. return;
  337. }
  338. if (cmd == SkyPumpMessage.GET_OFF)
  339. {
  340. LOG.Info($"[{Module}] {SkyPumpMessage.GET_OFF}");
  341. return;
  342. }
  343. if (cmd.Substring(0, 11) == SkyPumpMessage.GET_DATA)
  344. {
  345. _fdp_Current = cmd.Substring(11, 2);
  346. _roots_Current = cmd.Substring(13, 2);
  347. _fdp_Temp = cmd.Substring(15, 3);
  348. _roots_Temp = cmd.Substring(18, 3);
  349. _n2Flow = cmd.Substring(21, 2);
  350. _pressure = cmd.Substring(23, 4);
  351. _runTime = cmd.Substring(27, 5);
  352. ParaErrSta1(new BitArray(new byte[] { Convert.ToByte(cmd[32]) }));
  353. ParaErrSta2(new BitArray(new byte[] { Convert.ToByte(cmd[33]) }));
  354. ParaRunSta(new BitArray(new byte[] { Convert.ToByte(cmd[34]) }));
  355. //ParaRunSta(new BitArray(new byte[] { Convert.ToByte(cmd[35]) }));
  356. ParaErrSta3(new BitArray(new byte[] { Convert.ToByte(cmd[36]) }));
  357. }
  358. }
  359. catch(Exception ex)
  360. {
  361. LOG.Error($"[{Module}] sky pump error: [{ex.Message}]");
  362. }
  363. }
  364. public void ParaErrSta1(BitArray bits)
  365. {
  366. for (int i = 0; i < bits.Count; i++)
  367. {
  368. if (bits[i]) ErrSta1(i);
  369. }
  370. }
  371. public void ParaErrSta2(BitArray bits)
  372. {
  373. for (int i = 0; i < bits.Count; i++)
  374. {
  375. if (bits[i]) ErrSta2(i);
  376. }
  377. }
  378. public void ParaErrSta3(BitArray bits)
  379. {
  380. for (int i = 0; i < bits.Count; i++)
  381. {
  382. if (bits[i]) ErrSta3(i);
  383. }
  384. }
  385. public void ParaRunSta(BitArray bits)
  386. {
  387. string sRes = "sky pump status: ";
  388. for (int i = 0; i < bits.Count; i++)
  389. {
  390. if (bits[i])
  391. sRes += RunStaTure(i);
  392. else
  393. sRes += RunStaFalse(i);
  394. }
  395. _counter++;
  396. if (_counter == _counterMax)
  397. {
  398. _counter = 0;
  399. //LOG.Info($"[{Module}] {sRes}");
  400. }
  401. }
  402. public string RunStaTure(int code)
  403. {
  404. switch (code)
  405. {
  406. case 7:
  407. return "远程。";
  408. case 6:
  409. StatusDry = SkyPumpState.ON;
  410. if (_trigPumpOn.Q)
  411. {
  412. EV.PostInfoLog(this.Module, $"Dry Pump打开");
  413. }
  414. if (_trigPumpOff.Q && _timerPumpOff.IsTimeout())
  415. {
  416. _trigPumpOff.RST = true;
  417. EV.PostMessage(Module, EventEnum.DefaultWarning, "Dry Pump 无法关闭");
  418. }
  419. _trigPumpOn.RST = true;
  420. return "干泵开, ";
  421. case 5:
  422. StatusRoose = SkyPumpState.ON;
  423. return "罗茨泵开, ";
  424. case 4:
  425. return "泵冷阀开, ";
  426. case 3:
  427. return "清洗阀开, ";
  428. case 2:
  429. return "有警示, ";
  430. case 1:
  431. return "有故障, ";
  432. case 0:
  433. return "无水, ";
  434. default:
  435. return "";
  436. }
  437. }
  438. public string RunStaFalse(int code)
  439. {
  440. switch (code)
  441. {
  442. case 7:
  443. return "本地。";
  444. case 6:
  445. StatusDry = SkyPumpState.OFF;
  446. if (_trigPumpOn.Q && _timerPumpOn.IsTimeout())
  447. {
  448. _trigPumpOn.RST = true;
  449. EV.PostMessage(Module, EventEnum.DefaultWarning, "Dry Pump 无法打开");
  450. }
  451. if (_trigPumpOff.Q)
  452. {
  453. EV.PostInfoLog(this.Module, $"Dry Pump关闭");
  454. }
  455. _trigPumpOff.RST = true;
  456. return "干泵关, ";
  457. case 5:
  458. StatusRoose = SkyPumpState.OFF;
  459. return "罗茨泵关, ";
  460. case 4:
  461. return "泵冷阀关, ";
  462. case 3:
  463. return "清洗阀关, ";
  464. case 2:
  465. return "无警示, ";
  466. case 1:
  467. return "无故障, ";
  468. case 0:
  469. return "有水, ";
  470. default:
  471. return "";
  472. }
  473. }
  474. public void ErrSta1(int error)
  475. {
  476. switch (error)
  477. {
  478. case 7:
  479. LOG.Error($"[{Module}]上泵变频器除通讯外故障");
  480. break;
  481. case 6:
  482. LOG.Error($"[{Module}]下泵变频器除通讯外故障");
  483. break;
  484. case 5:
  485. LOG.Error($"[{Module}]上泵接触器");
  486. break;
  487. case 4:
  488. LOG.Error($"[{Module}]下泵接触器");
  489. break;
  490. case 3:
  491. LOG.Error($"[{Module}]上泵电机过热");
  492. break;
  493. case 2:
  494. LOG.Error($"[{Module}]下泵电机过热");
  495. break;
  496. case 1:
  497. LOG.Error($"[{Module}]急停");
  498. break;
  499. case 0:
  500. LOG.Error($"[{Module}]水冷");
  501. break;
  502. default:
  503. break;
  504. }
  505. }
  506. public void ErrSta2(int error)
  507. {
  508. switch (error)
  509. {
  510. case 7:
  511. LOG.Error($"[{Module}]下泵变频器通讯故障");
  512. break;
  513. case 6:
  514. LOG.Error($"[{Module}]上泵变频器通讯故障");
  515. break;
  516. case 5:
  517. LOG.Error($"[{Module}]排气压力");
  518. break;
  519. case 4:
  520. LOG.Error($"[{Module}]氮气流量");
  521. break;
  522. case 3:
  523. LOG.Error($"[{Module}]下泵电流");
  524. break;
  525. case 2:
  526. LOG.Error($"[{Module}]上泵电流");
  527. break;
  528. case 1:
  529. LOG.Error($"[{Module}]下泵温度");
  530. break;
  531. case 0:
  532. LOG.Error($"[{Module}]上泵温度");
  533. break;
  534. default:
  535. break;
  536. }
  537. }
  538. public void ErrSta3(int error)
  539. {
  540. switch (error)
  541. {
  542. case 7:
  543. case 6:
  544. case 5:
  545. case 4:
  546. case 3:
  547. case 2:
  548. break;
  549. case 1:
  550. LOG.Error($"[{Module}]下泵温度传感器未连接");
  551. break;
  552. case 0:
  553. LOG.Error($"[{Module}]上泵温度传感器未连接");
  554. break;
  555. default:
  556. break;
  557. }
  558. }
  559. public override void Monitor()
  560. {
  561. try
  562. {
  563. // 状态查询
  564. if (_timerQueryStatus.IsTimeout() && this.StatusDry != SkyPumpState.ERROR)
  565. {
  566. this.SendCmd(SkyPumpMessage.READ_DATA);
  567. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  568. }
  569. if (_timerReceiveStatus.IsTimeout() && this.StatusDry != SkyPumpState.ERROR)
  570. {
  571. _trigReceiveStatus.CLK = true;
  572. if(_trigReceiveStatus.Q)
  573. {
  574. EV.PostMessage(Module, EventEnum.DefaultWarning, "Dry Pump 没有回复");
  575. }
  576. }
  577. _trigOnOff.CLK = IsRunning;
  578. //第一次检测到打开了,开始计时
  579. if (_trigOnOff.R)
  580. {
  581. _total = TotalDays;
  582. _fromLast = DaysFromLastPM;
  583. _timerTotal.Start(0);
  584. _timerFromLast.Start(0);
  585. }
  586. //第一次检测到从打开到关闭状态
  587. if (_trigOnOff.T)
  588. {
  589. }
  590. //如果开着,就更新SC
  591. if (_trigOnOff.M)
  592. {
  593. TotalDays = _total + _timerTotal.GetElapseTime() / 1000 / 60 / 60;
  594. DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000 / 60 / 60;
  595. }
  596. if (PMIntervalDays > 0)
  597. {
  598. _trigPMNeeded.CLK = IsPMNeeded;
  599. if (_trigPMNeeded.Q)
  600. {
  601. if (EnableAlarm)
  602. {
  603. EV.PostAlarmLog(Module, "pump on time value larger than setting interval days");
  604. }
  605. }
  606. }
  607. StatsDataManager.Instance.Increase($"{Module}.PumpOnTime", $"{Module} PumpOnTime", DaysFromLastPM, TotalDays);
  608. }
  609. catch (Exception ex)
  610. {
  611. throw ex;
  612. }
  613. }
  614. private bool SendCmd(string str)
  615. {
  616. return _serial.Write(str + "\r");
  617. }
  618. public override void Reset()
  619. {
  620. _trigPumpOn.RST = true;
  621. _trigPumpOff.RST = true;
  622. _trigReceiveStatus.RST = true;
  623. _trigPMNeeded.RST = true;
  624. //SetPumpOnOff(false);
  625. }
  626. public override void Terminate()
  627. {
  628. //if (StatusDry == SkyPumpState.ON)
  629. // SetPumpOnOff(false);
  630. _serial?.Close();
  631. }
  632. public override void SetPumpOnOff(bool on)
  633. {
  634. SendCmd(on ? SkyPumpMessage.SET_ON : SkyPumpMessage.SET_OFF);
  635. }
  636. }
  637. }