SkyPump.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 (obj.Length>2 && ( Encoding.ASCII.GetString(obj, 12, 2) == SkyPumpMessage.EOF
  256. || Encoding.ASCII.GetString(obj, 13, 2) == SkyPumpMessage.EOF
  257. || Encoding.ASCII.GetString(obj, 38, 2) == SkyPumpMessage.EOF))
  258. {
  259. string cmd = "";
  260. if (obj.Length == 14 || obj.Length == 40)
  261. cmd = Encoding.ASCII.GetString(obj, 0, 11);
  262. else if (obj.Length == 15)
  263. cmd = Encoding.ASCII.GetString(obj, 0, 12);
  264. if (cmd == SkyPumpMessage.GET_ON)
  265. {
  266. _trigPumpOn.CLK = true;
  267. _timerPumpOn.Start(CHK_PUMP_REC_INTERVAL);
  268. LOG.Info($"[{Module}] {SkyPumpMessage.GET_ON}");
  269. return;
  270. }
  271. if (cmd == SkyPumpMessage.GET_OFF)
  272. {
  273. _trigPumpOff.CLK = true;
  274. _timerPumpOff.Start(CHK_PUMP_REC_INTERVAL);
  275. LOG.Info($"[{Module}] {SkyPumpMessage.GET_OFF}");
  276. return;
  277. }
  278. if (cmd == SkyPumpMessage.GET_DATA)
  279. {
  280. _fdp_Current = Encoding.ASCII.GetString(obj, 11, 2);
  281. _roots_Current = Encoding.ASCII.GetString(obj, 13, 2);
  282. _fdp_Temp = Encoding.ASCII.GetString(obj, 15, 3);
  283. _roots_Temp = Encoding.ASCII.GetString(obj, 18, 3);
  284. _n2Flow = Encoding.ASCII.GetString(obj, 21, 2);
  285. _pressure = Encoding.ASCII.GetString(obj, 23, 4);
  286. _runTime = Encoding.ASCII.GetString(obj, 27, 5);
  287. ParaErrSta1(new BitArray(new byte[] { obj[32] }));
  288. ParaErrSta2(new BitArray(new byte[] { obj[33] }));
  289. ParaRunSta(new BitArray(new byte[] { obj[34] }));
  290. //ParaRunSta(new BitArray(new byte[] { obj[35] }));
  291. ParaErrSta3(new BitArray(new byte[] { obj[36] }));
  292. }
  293. }
  294. else
  295. {
  296. LOG.Info($"[{Module}] Sky Pump 数据无效");
  297. }
  298. }
  299. catch (Exception ex)
  300. {
  301. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  302. {
  303. LOG.Error($"[{Module}] sky pump data para error: [{ex.Message}]");
  304. }
  305. }
  306. }
  307. private void OnErrorOccurred(string obj)
  308. {
  309. StatusDry = SkyPumpState.ERROR;
  310. LOG.Error($"[{Module}] sky pump error: [{obj}]");
  311. }
  312. private void OnPortDataChanged(string obj)
  313. {
  314. if (string.IsNullOrEmpty(obj))
  315. LOG.Error($"[{Module}] sky pump message IsNullOrEmpty");
  316. try
  317. {
  318. string cmd = obj.ToString().Split('\n')[0].Split('\r')[0];
  319. if (cmd == SkyPumpMessage.GET_ON)
  320. {
  321. LOG.Info($"[{Module}] {SkyPumpMessage.GET_ON}");
  322. return;
  323. }
  324. if (cmd == SkyPumpMessage.GET_OFF)
  325. {
  326. LOG.Info($"[{Module}] {SkyPumpMessage.GET_OFF}");
  327. return;
  328. }
  329. if (cmd.Substring(0, 11) == SkyPumpMessage.GET_DATA)
  330. {
  331. _fdp_Current = cmd.Substring(11, 2);
  332. _roots_Current = cmd.Substring(13, 2);
  333. _fdp_Temp = cmd.Substring(15, 3);
  334. _roots_Temp = cmd.Substring(18, 3);
  335. _n2Flow = cmd.Substring(21, 2);
  336. _pressure = cmd.Substring(23, 4);
  337. _runTime = cmd.Substring(27, 5);
  338. ParaErrSta1(new BitArray(new byte[] { Convert.ToByte(cmd[32]) }));
  339. ParaErrSta2(new BitArray(new byte[] { Convert.ToByte(cmd[33]) }));
  340. ParaRunSta(new BitArray(new byte[] { Convert.ToByte(cmd[34]) }));
  341. //ParaRunSta(new BitArray(new byte[] { Convert.ToByte(cmd[35]) }));
  342. ParaErrSta3(new BitArray(new byte[] { Convert.ToByte(cmd[36]) }));
  343. }
  344. }
  345. catch(Exception ex)
  346. {
  347. LOG.Error($"[{Module}] sky pump error: [{ex.Message}]");
  348. }
  349. }
  350. public void ParaErrSta1(BitArray bits)
  351. {
  352. for (int i = 0; i < bits.Count; i++)
  353. {
  354. if (bits[i]) ErrSta1(i);
  355. }
  356. }
  357. public void ParaErrSta2(BitArray bits)
  358. {
  359. for (int i = 0; i < bits.Count; i++)
  360. {
  361. if (bits[i]) ErrSta2(i);
  362. }
  363. }
  364. public void ParaErrSta3(BitArray bits)
  365. {
  366. for (int i = 0; i < bits.Count; i++)
  367. {
  368. if (bits[i]) ErrSta3(i);
  369. }
  370. }
  371. public void ParaRunSta(BitArray bits)
  372. {
  373. string sRes = "sky pump status: ";
  374. for (int i = 0; i < bits.Count; i++)
  375. {
  376. if (bits[i])
  377. sRes += RunStaTure(i);
  378. else
  379. sRes += RunStaFalse(i);
  380. }
  381. _counter++;
  382. if (_counter == _counterMax)
  383. {
  384. _counter = 0;
  385. LOG.Info($"[{Module}] {sRes}");
  386. }
  387. }
  388. public string RunStaTure(int code)
  389. {
  390. switch (code)
  391. {
  392. case 7:
  393. return "远程。";
  394. case 6:
  395. StatusDry = SkyPumpState.ON;
  396. if (_trigPumpOn.Q)
  397. {
  398. EV.PostInfoLog(this.Module, $"Dry Pump打开");
  399. }
  400. if (_trigPumpOff.Q && _timerPumpOff.IsTimeout())
  401. {
  402. _trigPumpOff.RST = true;
  403. EV.PostMessage(Module, EventEnum.DefaultWarning, "Dry Pump 无法关闭");
  404. }
  405. _trigPumpOn.RST = true;
  406. return "干泵开, ";
  407. case 5:
  408. StatusRoose = SkyPumpState.ON;
  409. return "罗茨泵开, ";
  410. case 4:
  411. return "泵冷阀开, ";
  412. case 3:
  413. return "清洗阀开, ";
  414. case 2:
  415. return "有警示, ";
  416. case 1:
  417. return "有故障, ";
  418. case 0:
  419. return "无水, ";
  420. default:
  421. return "";
  422. }
  423. }
  424. public string RunStaFalse(int code)
  425. {
  426. switch (code)
  427. {
  428. case 7:
  429. return "本地。";
  430. case 6:
  431. StatusDry = SkyPumpState.OFF;
  432. if (_trigPumpOn.Q && _timerPumpOn.IsTimeout())
  433. {
  434. _trigPumpOn.RST = true;
  435. EV.PostMessage(Module, EventEnum.DefaultWarning, "Dry Pump 无法打开");
  436. }
  437. if (_trigPumpOff.Q)
  438. {
  439. EV.PostInfoLog(this.Module, $"Dry Pump关闭");
  440. }
  441. _trigPumpOff.RST = true;
  442. return "干泵关, ";
  443. case 5:
  444. StatusRoose = SkyPumpState.OFF;
  445. return "罗茨泵关, ";
  446. case 4:
  447. return "泵冷阀关, ";
  448. case 3:
  449. return "清洗阀关, ";
  450. case 2:
  451. return "无警示, ";
  452. case 1:
  453. return "无故障, ";
  454. case 0:
  455. return "有水, ";
  456. default:
  457. return "";
  458. }
  459. }
  460. public void ErrSta1(int error)
  461. {
  462. switch (error)
  463. {
  464. case 7:
  465. LOG.Error($"[{Module}]上泵变频器除通讯外故障");
  466. break;
  467. case 6:
  468. LOG.Error($"[{Module}]下泵变频器除通讯外故障");
  469. break;
  470. case 5:
  471. LOG.Error($"[{Module}]上泵接触器");
  472. break;
  473. case 4:
  474. LOG.Error($"[{Module}]下泵接触器");
  475. break;
  476. case 3:
  477. LOG.Error($"[{Module}]上泵电机过热");
  478. break;
  479. case 2:
  480. LOG.Error($"[{Module}]下泵电机过热");
  481. break;
  482. case 1:
  483. LOG.Error($"[{Module}]急停");
  484. break;
  485. case 0:
  486. LOG.Error($"[{Module}]水冷");
  487. break;
  488. default:
  489. break;
  490. }
  491. }
  492. public void ErrSta2(int error)
  493. {
  494. switch (error)
  495. {
  496. case 7:
  497. LOG.Error($"[{Module}]下泵变频器通讯故障");
  498. break;
  499. case 6:
  500. LOG.Error($"[{Module}]上泵变频器通讯故障");
  501. break;
  502. case 5:
  503. LOG.Error($"[{Module}]排气压力");
  504. break;
  505. case 4:
  506. LOG.Error($"[{Module}]氮气流量");
  507. break;
  508. case 3:
  509. LOG.Error($"[{Module}]下泵电流");
  510. break;
  511. case 2:
  512. LOG.Error($"[{Module}]上泵电流");
  513. break;
  514. case 1:
  515. LOG.Error($"[{Module}]下泵温度");
  516. break;
  517. case 0:
  518. LOG.Error($"[{Module}]上泵温度");
  519. break;
  520. default:
  521. break;
  522. }
  523. }
  524. public void ErrSta3(int error)
  525. {
  526. switch (error)
  527. {
  528. case 7:
  529. case 6:
  530. case 5:
  531. case 4:
  532. case 3:
  533. case 2:
  534. break;
  535. case 1:
  536. LOG.Error($"[{Module}]下泵温度传感器未连接");
  537. break;
  538. case 0:
  539. LOG.Error($"[{Module}]上泵温度传感器未连接");
  540. break;
  541. default:
  542. break;
  543. }
  544. }
  545. public override void Monitor()
  546. {
  547. try
  548. {
  549. // 状态查询
  550. if (_timerQueryStatus.IsTimeout() && this.StatusDry != SkyPumpState.ERROR)
  551. {
  552. this.SendCmd(SkyPumpMessage.READ_DATA);
  553. _timerQueryStatus.Start(CHK_ST_INTERVAL);
  554. }
  555. if (_timerReceiveStatus.IsTimeout() && this.StatusDry != SkyPumpState.ERROR)
  556. {
  557. _trigReceiveStatus.CLK = true;
  558. if(_trigReceiveStatus.Q)
  559. {
  560. EV.PostMessage(Module, EventEnum.DefaultWarning, "Dry Pump 没有回复");
  561. }
  562. }
  563. _trigOnOff.CLK = IsRunning;
  564. //第一次检测到打开了,开始计时
  565. if (_trigOnOff.R)
  566. {
  567. _total = TotalDays;
  568. _fromLast = DaysFromLastPM;
  569. _timerTotal.Start(0);
  570. _timerFromLast.Start(0);
  571. }
  572. //第一次检测到从打开到关闭状态
  573. if (_trigOnOff.T)
  574. {
  575. }
  576. //如果开着,就更新SC
  577. if (_trigOnOff.M)
  578. {
  579. TotalDays = _total + _timerTotal.GetElapseTime() / 1000 / 60 / 60;
  580. DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000 / 60 / 60;
  581. }
  582. if (PMIntervalDays > 0)
  583. {
  584. _trigPMNeeded.CLK = IsPMNeeded;
  585. if (_trigPMNeeded.Q)
  586. {
  587. if (EnableAlarm)
  588. {
  589. EV.PostAlarmLog($"{Module}", "pump on time value larger than setting interval days");
  590. }
  591. }
  592. }
  593. StatsDataManager.Instance.Increase($"{Module}.PumpOnTime", $"{Module} PumpOnTime", DaysFromLastPM, TotalDays);
  594. }
  595. catch (Exception ex)
  596. {
  597. throw ex;
  598. }
  599. }
  600. private bool SendCmd(string str)
  601. {
  602. return _serial.Write(str + "\r");
  603. }
  604. public override void Reset()
  605. {
  606. _trigPumpOn.RST = true;
  607. _trigPumpOff.RST = true;
  608. _trigReceiveStatus.RST = true;
  609. _trigPMNeeded.RST = true;
  610. //SetPumpOnOff(false);
  611. }
  612. public override void Terminate()
  613. {
  614. //if (StatusDry == SkyPumpState.ON)
  615. // SetPumpOnOff(false);
  616. _serial?.Close();
  617. }
  618. public override void SetPumpOnOff(bool on)
  619. {
  620. SendCmd(on ? SkyPumpMessage.SET_ON : SkyPumpMessage.SET_OFF);
  621. }
  622. }
  623. }