SkyPump.cs 22 KB

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