VATS651Handler.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. using Aitex.Core.Common.DeviceData;
  2. using MECF.Framework.Common.Communications;
  3. using Newtonsoft.Json.Linq;
  4. using System.Linq;
  5. using System.Text;
  6. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.ThrottleValves.VAT
  7. {
  8. public abstract class VATS651Handler : HandlerBase
  9. {
  10. protected VATS651 Device { get; }
  11. public string _command;
  12. protected string _parameter;
  13. protected VATS651Handler(VATS651 device, string command, string parameter = null)
  14. : base(BuildMessage(command, parameter))
  15. {
  16. Device = device;
  17. _command = command;
  18. _parameter = parameter;
  19. Name = command;
  20. }
  21. private static string _endLine = "\r\n";
  22. private static string BuildMessage(string command, string parameter)
  23. {
  24. string commandContent = string.IsNullOrEmpty(parameter) ? $"{command}" : $"{command}{parameter}";
  25. return commandContent + _endLine.ToString();
  26. }
  27. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  28. {
  29. VATS651Message response = msg as VATS651Message;
  30. ResponseMessage = msg;
  31. if (response.IsError)
  32. {
  33. Device.NoteError(response.Data);
  34. transactionComplete = true;
  35. return false;
  36. }
  37. Device.NoteError(null);
  38. if (response.IsAck)
  39. {
  40. SetState(EnumHandlerState.Acked);
  41. if (response.Data.IndexOf(_command) == 0)
  42. {
  43. SetState(EnumHandlerState.Completed);
  44. transactionComplete = true;
  45. return true;
  46. }
  47. }
  48. transactionComplete = false;
  49. return false;
  50. }
  51. }
  52. public class VATS651RawCommandHandler : VATS651Handler
  53. {
  54. public VATS651RawCommandHandler(VATS651 device, string command, string parameter = null)
  55. : base(device, command, parameter)
  56. {
  57. }
  58. public override bool HandleMessage(MessageBase msg, out bool handled)
  59. {
  60. if (base.HandleMessage(msg, out handled))
  61. {
  62. var result = msg as VATS651Message;
  63. Device.NoteRawCommandInfo(_command, result.RawMessage);
  64. }
  65. return true;
  66. }
  67. }
  68. public class VATS651SimpleActionHandler : VATS651Handler
  69. {
  70. public VATS651SimpleActionHandler(VATS651 device, string command, string parameter = null)
  71. : base(device, command, parameter)
  72. {
  73. }
  74. public override bool HandleMessage(MessageBase msg, out bool handled)
  75. {
  76. if (base.HandleMessage(msg, out handled))
  77. {
  78. Device.NoteActionCompleted(_command);
  79. }
  80. return true;
  81. }
  82. }
  83. public class VATS651SimpleSetHandler : VATS651Handler
  84. {
  85. public VATS651SimpleSetHandler(VATS651 device, string command, string parameter = null)
  86. : base(device, command, parameter)
  87. {
  88. }
  89. public override bool HandleMessage(MessageBase msg, out bool handled)
  90. {
  91. if (base.HandleMessage(msg, out handled))
  92. {
  93. Device.NoteSetCompleted(_command, _parameter);
  94. }
  95. return true;
  96. }
  97. }
  98. public class VATS651SimpleQueryHandler : VATS651Handler
  99. {
  100. public VATS651SimpleQueryHandler(VATS651 device, string command, string parameter = null)
  101. : base(device, command, parameter)
  102. {
  103. }
  104. public override bool HandleMessage(MessageBase msg, out bool handled)
  105. {
  106. if (base.HandleMessage(msg, out handled))
  107. {
  108. var vatMsg = msg as VATS651Message;
  109. string result = vatMsg.Data.Substring(_command.Length);
  110. Device.NoteQueryResult(_command, result);
  111. }
  112. return true;
  113. }
  114. }
  115. public class VATS651CloseValveHandler : VATS651Handler
  116. {
  117. public VATS651CloseValveHandler(VATS651 device)
  118. : base(device, "C:", "")
  119. {
  120. }
  121. public override bool HandleMessage(MessageBase msg, out bool handled)
  122. {
  123. if (base.HandleMessage(msg, out handled))
  124. {
  125. }
  126. return true;
  127. }
  128. }
  129. public class VATS651OpenValveHandler : VATS651Handler
  130. {
  131. public VATS651OpenValveHandler(VATS651 device)
  132. : base(device, "O:", "")
  133. {
  134. }
  135. public override bool HandleMessage(MessageBase msg, out bool handled)
  136. {
  137. if (base.HandleMessage(msg, out handled))
  138. {
  139. }
  140. return true;
  141. }
  142. }
  143. public class VATS651HoldValveHandler : VATS651Handler
  144. {
  145. public VATS651HoldValveHandler(VATS651 device)
  146. : base(device, "H:", "")
  147. {
  148. }
  149. public override bool HandleMessage(MessageBase msg, out bool handled)
  150. {
  151. if (base.HandleMessage(msg, out handled))
  152. {
  153. }
  154. return true;
  155. }
  156. }
  157. public class VATS651SetPositionHandler : VATS651Handler
  158. {
  159. public VATS651SetPositionHandler(VATS651 device, int position)
  160. : base(device, "R:", position.ToString("D6"))
  161. {
  162. }
  163. public override bool HandleMessage(MessageBase msg, out bool handled)
  164. {
  165. if (base.HandleMessage(msg, out handled))
  166. {
  167. }
  168. return true;
  169. }
  170. }
  171. public class VATS651SetPressureHandler : VATS651Handler
  172. {
  173. public VATS651SetPressureHandler(VATS651 device, int position)
  174. : base(device, "S:", position.ToString("D8"))
  175. {
  176. }
  177. public override bool HandleMessage(MessageBase msg, out bool handled)
  178. {
  179. if (base.HandleMessage(msg, out handled))
  180. {
  181. }
  182. return true;
  183. }
  184. }
  185. public class VATS651SetAccessModeHandler : VATS651Handler
  186. {
  187. //c:01aa
  188. //c:01
  189. //aa 01 = remote operation, change to local enabled
  190. public VATS651SetAccessModeHandler(VATS651 device)
  191. : base(device, "c:01", "01")
  192. {
  193. }
  194. public override bool HandleMessage(MessageBase msg, out bool handled)
  195. {
  196. if (base.HandleMessage(msg, out handled))
  197. {
  198. }
  199. return true;
  200. }
  201. }
  202. public class VATS651SetValveSpeedHandler : VATS651Handler
  203. {
  204. //V:00aaaa
  205. //V:
  206. //aaaa valve speed, 1 ... 1000 (1 = min. speed, 1000 = max. speed)
  207. public VATS651SetValveSpeedHandler(VATS651 device, int speed)
  208. : base(device, "V:", "00" + speed.ToString("D4"))
  209. {
  210. }
  211. public override bool HandleMessage(MessageBase msg, out bool handled)
  212. {
  213. if (base.HandleMessage(msg, out handled))
  214. {
  215. }
  216. return true;
  217. }
  218. }
  219. public class VATS651SetSensorDelayHandler : VATS651Handler
  220. {
  221. //s:02A00c
  222. //s:02
  223. //c = 0.00…1.00
  224. public VATS651SetSensorDelayHandler(VATS651 device, float delay)
  225. : base(device, "s:02", "A00" + delay.ToString())
  226. {
  227. }
  228. public override bool HandleMessage(MessageBase msg, out bool handled)
  229. {
  230. if (base.HandleMessage(msg, out handled))
  231. {
  232. }
  233. return true;
  234. }
  235. }
  236. public class VATS651SetRampTimeHandler : VATS651Handler
  237. {
  238. //s:02A01c
  239. //s:02
  240. //c = 0.00…1’000’000.0
  241. public VATS651SetRampTimeHandler(VATS651 device, float delay)
  242. : base(device, "s:02", "A01" + delay.ToString())
  243. {
  244. }
  245. public override bool HandleMessage(MessageBase msg, out bool handled)
  246. {
  247. if (base.HandleMessage(msg, out handled))
  248. {
  249. }
  250. return true;
  251. }
  252. }
  253. public class VATS651SetGainFactorHandler : VATS651Handler
  254. {
  255. //s:02A04c
  256. //s:02
  257. //c = 0.0001…7.5
  258. public VATS651SetGainFactorHandler(VATS651 device, float delay)
  259. : base(device, "s:02", "A04" + delay.ToString())
  260. {
  261. }
  262. public override bool HandleMessage(MessageBase msg, out bool handled)
  263. {
  264. if (base.HandleMessage(msg, out handled))
  265. {
  266. }
  267. return true;
  268. }
  269. }
  270. public class VATS651ResetHandler : VATS651Handler
  271. {
  272. //c:82aa
  273. //c:82
  274. //aa 00 = reset service request bit from WARNINGS 01 = reset FATAL ERROR (restart control unit)
  275. public VATS651ResetHandler(VATS651 device)
  276. : base(device, "c:82", "01")
  277. {
  278. }
  279. public override bool HandleMessage(MessageBase msg, out bool handled)
  280. {
  281. if (base.HandleMessage(msg, out handled))
  282. {
  283. }
  284. return true;
  285. }
  286. }
  287. public class VATS651QueryPositionHandler : VATS651Handler
  288. {
  289. //A:
  290. //A:aaaaaa
  291. public VATS651QueryPositionHandler(VATS651 device)
  292. : base(device, "A:", "")
  293. {
  294. }
  295. public override bool HandleMessage(MessageBase msg, out bool handled)
  296. {
  297. if (base.HandleMessage(msg, out handled))
  298. {
  299. var vatMsg = msg as VATS651Message;
  300. string result = vatMsg.Data.Substring(_command.Length);
  301. Device.NoteQueryResult(_command, result);
  302. }
  303. return true;
  304. }
  305. }
  306. public class VATS651QueryPressureHandler : VATS651Handler
  307. {
  308. //P:
  309. //P:saaaaaaa
  310. //s sign, 0 for positive readings, - for negative readings
  311. public VATS651QueryPressureHandler(VATS651 device)
  312. : base(device, "P:", "")
  313. {
  314. }
  315. public override bool HandleMessage(MessageBase msg, out bool handled)
  316. {
  317. if (base.HandleMessage(msg, out handled))
  318. {
  319. var vatMsg = msg as VATS651Message;
  320. string result = vatMsg.Data.Substring(_command.Length);
  321. Device.NoteQueryResult(_command, result);
  322. }
  323. return true;
  324. }
  325. }
  326. public class VATS651QueryStatusHandler : VATS651Handler
  327. {
  328. //i:76
  329. //i:76xxxxxxsyyyyyyyabc
  330. //xxxxxx position
  331. //s sign, 0 for positive readings, - for negative readings
  332. //yyyyyyy pressure
  333. //a Access Mode
  334. //b Control Mode
  335. //c Warning
  336. public VATS651QueryStatusHandler(VATS651 device)
  337. : base(device, "i:76", "")
  338. {
  339. }
  340. public override bool HandleMessage(MessageBase msg, out bool handled)
  341. {
  342. if (base.HandleMessage(msg, out handled))
  343. {
  344. var vatMsg = msg as VATS651Message;
  345. string result = vatMsg.Data.Substring(_command.Length);
  346. if (result.Length == 17)
  347. {
  348. var pos = result.Substring(0, 6);
  349. int.TryParse(pos, out int posValue);
  350. Device.SetPositionFeedback(posValue);
  351. var sign = result.Substring(6, 1);
  352. var press = result.Substring(7, 7);
  353. int.TryParse(press, out int pressValue);
  354. Device.SetPressureFeedback(pressValue * (sign == "-" ? -1 : 1));
  355. var remote = result.Substring(14, 1);
  356. var controlMode = result.Substring(15, 1);
  357. if (controlMode == "2")
  358. Device.Mode = PressureCtrlMode.TVPositionCtrl;
  359. else if (controlMode == "3")
  360. Device.Mode = PressureCtrlMode.TVClose;
  361. if (controlMode == "4")
  362. Device.Mode = PressureCtrlMode.TVOpen;
  363. if (controlMode == "5")
  364. Device.Mode = PressureCtrlMode.TVPressureCtrl;
  365. var warning = result.Substring(16, 1);
  366. }
  367. }
  368. return true;
  369. }
  370. }
  371. public class VATS651QueryErrorStatusHandler : VATS651Handler
  372. {
  373. //i:50
  374. //i:50abc
  375. //abc error code
  376. public VATS651QueryErrorStatusHandler(VATS651 device)
  377. : base(device, "i:50", "")
  378. {
  379. }
  380. public override bool HandleMessage(MessageBase msg, out bool handled)
  381. {
  382. if (base.HandleMessage(msg, out handled))
  383. {
  384. var vatMsg = msg as VATS651Message;
  385. string result = vatMsg.Data.Substring(_command.Length);
  386. Device.NoteQueryResult(_command, result);
  387. }
  388. return true;
  389. }
  390. }
  391. public class VATS651QuerySensorDelayHandler : VATS651Handler
  392. {
  393. //i:02A00
  394. //i:02A00c
  395. //c = 0.00…1.00
  396. public VATS651QuerySensorDelayHandler(VATS651 device)
  397. : base(device, "i:02A00", "")
  398. {
  399. }
  400. public override bool HandleMessage(MessageBase msg, out bool handled)
  401. {
  402. if (base.HandleMessage(msg, out handled))
  403. {
  404. var vatMsg = msg as VATS651Message;
  405. string result = vatMsg.Data.Substring(_command.Length);
  406. float.TryParse(result, out float sensorDelay);
  407. }
  408. return true;
  409. }
  410. }
  411. public class VATS651QueryRampTimeHandler : VATS651Handler
  412. {
  413. //i:02A01
  414. //i:02A01c
  415. //c = 0.00…1’000’000.0
  416. public VATS651QueryRampTimeHandler(VATS651 device)
  417. : base(device, "i:02A01", "")
  418. {
  419. }
  420. public override bool HandleMessage(MessageBase msg, out bool handled)
  421. {
  422. if (base.HandleMessage(msg, out handled))
  423. {
  424. var vatMsg = msg as VATS651Message;
  425. string result = vatMsg.Data.Substring(_command.Length);
  426. float.TryParse(result, out float rampTime);
  427. }
  428. return true;
  429. }
  430. }
  431. public class VATS651QueryRampModeHandler : VATS651Handler
  432. {
  433. //i:02A02
  434. //i:02A02c
  435. //c = 0 or 1
  436. public VATS651QueryRampModeHandler(VATS651 device)
  437. : base(device, "i:02A02", "")
  438. {
  439. }
  440. public override bool HandleMessage(MessageBase msg, out bool handled)
  441. {
  442. if (base.HandleMessage(msg, out handled))
  443. {
  444. var vatMsg = msg as VATS651Message;
  445. string result = vatMsg.Data.Substring(_command.Length);
  446. int.TryParse(result, out int rampMode);
  447. }
  448. return true;
  449. }
  450. }
  451. public class VATS651QueryGainFactorHandler : VATS651Handler
  452. {
  453. //i:02A04
  454. //i:02A04c
  455. //c = 0.0001…7.5
  456. public VATS651QueryGainFactorHandler(VATS651 device)
  457. : base(device, "i:02A04", "")
  458. {
  459. }
  460. public override bool HandleMessage(MessageBase msg, out bool handled)
  461. {
  462. if (base.HandleMessage(msg, out handled))
  463. {
  464. var vatMsg = msg as VATS651Message;
  465. string result = vatMsg.Data.Substring(_command.Length);
  466. float.TryParse(result, out float gainFactor);
  467. }
  468. return true;
  469. }
  470. }
  471. public class VATS651QueryLearnStatusHandler : VATS651Handler
  472. {
  473. //i:32
  474. //i:32abcdefgh
  475. //a Running 0 = No; 1 = Yes
  476. //b Data set present; 0 = Ok 1 = No (Learn necessary)
  477. //c Abortion 0 = Ok, Learn completed; 1 = Abort by user; 2 = Abort by control unit
  478. //d Open pressure 0 = Ok; 1 = > 50% learn pressure limit (gas flow too high); 2 = < 0 (no gas flow or zero done with gas flow)
  479. //e Close pressure 0 = OK; 1 = < 10% learn pressure limit (gas flow too low)
  480. //f Pressure raising 0 = Ok; 1 = pressure not raising during LEARN (gasflow missing)
  481. //g Pressure stability 0 = OK; 1 = sensor unstable during LEARN
  482. //h Reserved do not use
  483. public VATS651QueryLearnStatusHandler(VATS651 device)
  484. : base(device, "i:32", "")
  485. {
  486. }
  487. public override bool HandleMessage(MessageBase msg, out bool handled)
  488. {
  489. if (base.HandleMessage(msg, out handled))
  490. {
  491. var vatMsg = msg as VATS651Message;
  492. string result = vatMsg.Data.Substring(_command.Length);
  493. if (result.Length == 8)
  494. {
  495. string info = "";
  496. bool isLearning = result[0] == '1';
  497. if (result[1] == '0')
  498. info += "Data set present;";
  499. else
  500. info += "Data set not present;";
  501. if (result[2] == '0')
  502. info += "Learn completed automaticly;";
  503. else if (result[2] == '1')
  504. info += "Learn abort by user;";
  505. else
  506. info += "Learn abort control unit;";
  507. if (result[3] == '0')
  508. info += "Open pressure ok;";
  509. else if (result[3] == '1')
  510. info += "Open pressure gas flow too high;";
  511. else
  512. info += "Open pressure no gas flow or zero done with gas flow;";
  513. if (result[4] == '0')
  514. info += "Close pressure ok;";
  515. else
  516. info += "Close pressure gas flow too low);";
  517. if (result[5] == '0')
  518. info += "Pressure raising ok;";
  519. else
  520. info += "Pressure not raising during LEARN ;";
  521. if (result[6] == '0')
  522. info += "Pressure stability ok;";
  523. else
  524. info += "sensor unstable during LEARN;";
  525. Device.SetLearnStatus(isLearning, info);
  526. }
  527. }
  528. return true;
  529. }
  530. }
  531. public class VATS651ZeroHandler : VATS651Handler
  532. {
  533. //Z:
  534. //Z:
  535. public VATS651ZeroHandler(VATS651 device)
  536. : base(device, "Z:", "")
  537. {
  538. }
  539. public override bool HandleMessage(MessageBase msg, out bool handled)
  540. {
  541. if (base.HandleMessage(msg, out handled))
  542. {
  543. }
  544. return true;
  545. }
  546. }
  547. public class VATS651LearnHandler : VATS651Handler
  548. {
  549. //L:0aaaaaaa
  550. //L:
  551. public VATS651LearnHandler(VATS651 device, int pressureLimit)
  552. : base(device, "L:", "0" + pressureLimit.ToString("D7"))
  553. {
  554. }
  555. public override bool HandleMessage(MessageBase msg, out bool handled)
  556. {
  557. if (base.HandleMessage(msg, out handled))
  558. {
  559. }
  560. return true;
  561. }
  562. }
  563. }