SkyPump.cs 22 KB

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