KITZTHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.SCCore;
  3. using MECF.Framework.Common.Communications;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.ThrottleValves.KITZ
  11. {
  12. //PCH-C6
  13. public abstract class KITZHandler : HandlerBase
  14. {
  15. public KITZThrottleValve ThrottleValveDevice { get; }
  16. protected KITZHandler(KITZThrottleValve throttleValve, bool isQuery, string parameter, string data = "")
  17. : base(BuildMessage(isQuery, parameter, data))
  18. {
  19. ThrottleValveDevice = throttleValve;
  20. }
  21. private static string BuildMessage(bool isQuery, string parameter, string data)
  22. {
  23. string msg = string.Empty;
  24. if (isQuery)
  25. msg = parameter.ToLower() + ":";
  26. else
  27. msg = parameter.ToUpper() + ":" + data;
  28. return $"{msg}\r\n";
  29. }
  30. public override bool HandleMessage(MessageBase msg, out bool handled)
  31. {
  32. ResponseMessage = msg;
  33. handled = true;
  34. return true;
  35. }
  36. }
  37. //remote switch
  38. public class RemoteHandler : KITZHandler
  39. {
  40. public RemoteHandler(KITZThrottleValve throttleValve)
  41. : base(throttleValve, false, "REMOTE")
  42. {
  43. Name = "Remote";
  44. }
  45. public override bool HandleMessage(MessageBase msg, out bool handled)
  46. {
  47. var result = msg as KITZMessage;
  48. handled = false;
  49. if (!result.IsResponse) return true;
  50. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper())
  51. {
  52. ThrottleValveDevice.SetError(result.RawMessage);
  53. }
  54. ResponseMessage = msg;
  55. handled = true;
  56. return true;
  57. }
  58. }
  59. //Calibration
  60. public class CalibrationHandler : KITZHandler
  61. {
  62. public CalibrationHandler(KITZThrottleValve throttleValve)
  63. : base(throttleValve, false, "CAL")
  64. {
  65. Name = "Cal";
  66. }
  67. public override bool HandleMessage(MessageBase msg, out bool handled)
  68. {
  69. var result = msg as KITZMessage;
  70. handled = false;
  71. if (!result.IsResponse) return true;
  72. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper())
  73. {
  74. ThrottleValveDevice.SetError(result.RawMessage);
  75. }
  76. ResponseMessage = msg;
  77. handled = true;
  78. return true;
  79. }
  80. }
  81. //Close
  82. public class CloseHandler : KITZHandler
  83. {
  84. public CloseHandler(KITZThrottleValve throttleValve)
  85. : base(throttleValve, false, "C")
  86. {
  87. Name = "C";
  88. }
  89. public override bool HandleMessage(MessageBase msg, out bool handled)
  90. {
  91. var result = msg as KITZMessage;
  92. handled = false;
  93. if (!result.IsResponse) return true;
  94. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper())
  95. {
  96. ThrottleValveDevice.SetError(result.RawMessage);
  97. }
  98. ResponseMessage = msg;
  99. handled = true;
  100. return true;
  101. }
  102. }
  103. //Open
  104. public class OpenHandler : KITZHandler
  105. {
  106. public OpenHandler(KITZThrottleValve throttleValve)
  107. : base(throttleValve, false, "O")
  108. {
  109. Name = "O";
  110. }
  111. public override bool HandleMessage(MessageBase msg, out bool handled)
  112. {
  113. var result = msg as KITZMessage;
  114. handled = false;
  115. if (!result.IsResponse) return true;
  116. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper())
  117. {
  118. ThrottleValveDevice.SetError(result.RawMessage);
  119. }
  120. ResponseMessage = msg;
  121. handled = true;
  122. return true;
  123. }
  124. }
  125. //Position control
  126. public class SetPositionHandler : KITZHandler
  127. {
  128. public SetPositionHandler(KITZThrottleValve throttleValve, float position)
  129. : base(throttleValve, false, "POS", position.ToString("f1"))
  130. {
  131. Name = "POS";
  132. }
  133. public override bool HandleMessage(MessageBase msg, out bool handled)
  134. {
  135. var result = msg as KITZMessage;
  136. handled = false;
  137. if (!result.IsResponse) return true;
  138. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper())
  139. {
  140. ThrottleValveDevice.SetError(result.RawMessage);
  141. }
  142. ResponseMessage = msg;
  143. handled = true;
  144. return true;
  145. }
  146. }
  147. //Pressure control
  148. public class SetPressureHandler : KITZHandler
  149. {
  150. //PRS: (a), (b), (c), (d), (e)
  151. //a:Map No.
  152. //b:Table No.
  153. //c:DB No.
  154. //d:RAMP Mode No.
  155. //e:Target pressure
  156. public SetPressureHandler(KITZThrottleValve throttleValve, float pressure, int dbNo = 1)
  157. : base(throttleValve, false, "PRS", $"1,1,{dbNo},1," + pressure.ToString("f3"))
  158. {
  159. Name = "PRS";
  160. }
  161. public override bool HandleMessage(MessageBase msg, out bool handled)
  162. {
  163. var result = msg as KITZMessage;
  164. handled = false;
  165. if (!result.IsResponse) return true;
  166. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper())
  167. {
  168. ThrottleValveDevice.SetError(result.RawMessage);
  169. }
  170. ResponseMessage = msg;
  171. handled = true;
  172. return true;
  173. }
  174. }
  175. //State
  176. public class QueryStateHandler : KITZHandler
  177. {
  178. public QueryStateHandler(KITZThrottleValve throttleValve)
  179. : base(throttleValve, true, "sts")
  180. {
  181. Name = "sts";
  182. }
  183. public override bool HandleMessage(MessageBase msg, out bool handled)
  184. {
  185. var result = msg as KITZMessage;
  186. handled = false;
  187. if (!result.IsResponse) return true;
  188. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper() || string.IsNullOrEmpty(result.Parameter))
  189. {
  190. ThrottleValveDevice.SetError(result.RawMessage);
  191. }
  192. string[] parameters = Regex.Split(result.Parameter, ",");
  193. if (parameters.Length < 4)
  194. {
  195. ThrottleValveDevice.SetError(result.RawMessage);
  196. return false;
  197. }
  198. var accessMode = parameters[0];
  199. var controlMode = parameters[1];
  200. var warningInfo = parameters[2];
  201. var cycleCounter = parameters[3];
  202. if (controlMode.ToUpper() == "CLOSE")
  203. {
  204. ThrottleValveDevice.Mode = PressureCtrlMode.TVClose;
  205. }
  206. else if (controlMode.ToUpper() == "OPEN")
  207. {
  208. ThrottleValveDevice.Mode = PressureCtrlMode.TVOpen;
  209. }
  210. else if (controlMode.ToUpper() == "PRESS")
  211. {
  212. ThrottleValveDevice.Mode = PressureCtrlMode.TVPressureCtrl;
  213. }
  214. else if (controlMode.ToUpper() == "POS")
  215. {
  216. ThrottleValveDevice.Mode = PressureCtrlMode.TVPositionCtrl;
  217. }
  218. else if (controlMode.ToUpper() == "CALIB")
  219. {
  220. ThrottleValveDevice.Mode = PressureCtrlMode.TVCalib;
  221. }
  222. if (warningInfo.Contains("1") && ThrottleValveDevice.Mode != PressureCtrlMode.TVCalib)
  223. {
  224. ThrottleValveDevice.SetError(result.RawMessage);
  225. return false;
  226. }
  227. ResponseMessage = msg;
  228. handled = true;
  229. return true;
  230. }
  231. }
  232. public class QueryPressureHandler : KITZHandler
  233. {
  234. public QueryPressureHandler(KITZThrottleValve throttleValve)
  235. : base(throttleValve, true, "prs")
  236. {
  237. Name = "prs";
  238. }
  239. public override bool HandleMessage(MessageBase msg, out bool handled)
  240. {
  241. var result = msg as KITZMessage;
  242. handled = false;
  243. if (!result.IsResponse) return true;
  244. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper() || string.IsNullOrEmpty(result.Parameter))
  245. {
  246. ThrottleValveDevice.SetError(result.RawMessage);
  247. }
  248. if (!float.TryParse(result.Parameter, out float pressure))
  249. {
  250. //ThrottleValveDevice.SetErrorCode(1);
  251. }
  252. else
  253. {
  254. ThrottleValveDevice.PressureFeedback = pressure;
  255. }
  256. ResponseMessage = msg;
  257. handled = true;
  258. return true;
  259. }
  260. }
  261. public class QueryPositionHandler : KITZHandler
  262. {
  263. public QueryPositionHandler(KITZThrottleValve throttleValve)
  264. : base(throttleValve, true, "pos")
  265. {
  266. Name = "pos";
  267. }
  268. public override bool HandleMessage(MessageBase msg, out bool handled)
  269. {
  270. var result = msg as KITZMessage;
  271. handled = false;
  272. if (!result.IsResponse) return true;
  273. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper() || string.IsNullOrEmpty(result.Parameter))
  274. {
  275. ThrottleValveDevice.SetError(result.RawMessage);
  276. }
  277. if (!float.TryParse(result.Parameter, out float position))
  278. {
  279. //ThrottleValveDevice.SetErrorCode(1);
  280. }
  281. else
  282. {
  283. ThrottleValveDevice.PositionFeedback = position;
  284. }
  285. ResponseMessage = msg;
  286. handled = true;
  287. return true;
  288. }
  289. }
  290. public class CalbrationHandler : KITZHandler
  291. {
  292. public CalbrationHandler(KITZThrottleValve throttleValve)
  293. : base(throttleValve, false, "CAL")
  294. {
  295. Name = "CAL";
  296. }
  297. public override bool HandleMessage(MessageBase msg, out bool handled)
  298. {
  299. var result = msg as KITZMessage;
  300. handled = false;
  301. if (!result.IsResponse) return true;
  302. if (result.IsError || result.IsFormatError || result.IsNak || result.Command.ToUpper() != Name.ToUpper())
  303. {
  304. ThrottleValveDevice.SetError(result.RawMessage);
  305. }
  306. ResponseMessage = msg;
  307. handled = true;
  308. return true;
  309. }
  310. }
  311. }