ThrottleValveBase.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Configuration;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. using Aitex.Core.Common.DeviceData;
  9. using Aitex.Core.RT.DataCenter;
  10. using Aitex.Core.RT.Device;
  11. using Aitex.Core.RT.Event;
  12. using Aitex.Core.RT.IOCore;
  13. using Aitex.Core.RT.OperationCenter;
  14. using Aitex.Core.RT.SCCore;
  15. using Aitex.Core.RT.Tolerance;
  16. using Aitex.Core.Util;
  17. namespace MECF.Framework.Common.Device.Bases
  18. {
  19. public abstract class ThrottleValveBase : BaseDevice, IDevice
  20. {
  21. public virtual PressureCtrlMode Mode { get; set; }
  22. public virtual float PositionFeedback { get; set; }
  23. public virtual float PressureFeedback { get; set; }
  24. public virtual float PressureSetPoint { get; set; }
  25. public virtual float PositionSetPoint { get; set; }
  26. public virtual AITThrottleValveData DeviceData { get; set; }
  27. public uint MaxValuePressure = 100;
  28. public float PressureParam = 0.1f;
  29. public uint MaxValuePosi = 5000;
  30. public float PosiParam = 0.1f;
  31. protected double _currentPressureFineTuningValue;
  32. protected double _currentPositionFineTuningValue;
  33. protected SCConfigItem _scFineTuningEnable;
  34. protected SCConfigItem _scPressureFineTuningValue;
  35. protected SCConfigItem _scPositionFineTuningValue;
  36. private float _currentPressureWarningRange;
  37. private float _currentPressureAlarmRange;
  38. private float _currentPositionWarningRange;
  39. private float _currentPositionAlarmRange;
  40. protected SCConfigItem _scEnableAlarm;
  41. protected SCConfigItem _scPressureAlarmTime;
  42. protected SCConfigItem _scPressureAlarmRange;
  43. protected SCConfigItem _scPressureWarningTime;
  44. protected SCConfigItem _scPressureWarningRange;
  45. protected SCConfigItem _scPositionAlarmTime;
  46. protected SCConfigItem _scPositionAlarmRange;
  47. protected SCConfigItem _scPositionWarningTime;
  48. protected SCConfigItem _scPositionWarningRange;
  49. protected ToleranceChecker _tolerancePressureAlarmChecker = new ToleranceChecker();
  50. protected ToleranceChecker _tolerancePressureWarningChecker = new ToleranceChecker();
  51. protected ToleranceChecker _tolerancePositionAlarmChecker = new ToleranceChecker();
  52. protected ToleranceChecker _tolerancePositionWarningChecker = new ToleranceChecker();
  53. public virtual double PressureFineTuningValue
  54. {
  55. get
  56. {
  57. if (_scFineTuningEnable == null || !_scFineTuningEnable.BoolValue)
  58. return 1;
  59. if (_currentPressureFineTuningValue != 0)
  60. return 1 + _currentPressureFineTuningValue / 100;
  61. return _scPressureFineTuningValue != null ? 1 + _scPressureFineTuningValue.DoubleValue / 100 : 1;
  62. }
  63. }
  64. public virtual double PositionFineTuningValue
  65. {
  66. get
  67. {
  68. if (_scFineTuningEnable == null || !_scFineTuningEnable.BoolValue)
  69. return 1;
  70. if (_currentPositionFineTuningValue != 0)
  71. return 1 + _currentPositionFineTuningValue / 100;
  72. return _scPositionFineTuningValue != null ? 1 + _scPositionFineTuningValue.DoubleValue / 100 : 1;
  73. }
  74. }
  75. public virtual bool EnableAlarm
  76. {
  77. get
  78. {
  79. if (_scEnableAlarm != null)
  80. return _scEnableAlarm.BoolValue;
  81. return false;
  82. }
  83. }
  84. public virtual double PressureAlarmTime
  85. {
  86. get
  87. {
  88. if (_scPressureAlarmTime != null)
  89. return _scPressureAlarmTime.DoubleValue;
  90. return 0;
  91. }
  92. }
  93. public virtual double PressureAlarmRange
  94. {
  95. get
  96. {
  97. if (_currentPressureAlarmRange > 0)
  98. return _currentPressureAlarmRange;
  99. if (_scPressureAlarmRange != null)
  100. return _scPressureAlarmRange.DoubleValue;
  101. return 0;
  102. }
  103. }
  104. public virtual double PressureWarningTime
  105. {
  106. get
  107. {
  108. if (_scPressureWarningTime != null)
  109. return _scPressureWarningTime.DoubleValue;
  110. return 0;
  111. }
  112. }
  113. public virtual double PressureWarningRange
  114. {
  115. get
  116. {
  117. if (_currentPressureWarningRange > 0)
  118. return _currentPressureWarningRange;
  119. if (_scPressureWarningRange != null)
  120. return _scPressureWarningRange.DoubleValue;
  121. return 0;
  122. }
  123. }
  124. public virtual double PositionAlarmTime
  125. {
  126. get
  127. {
  128. if (_scPositionAlarmTime != null)
  129. return _scPositionAlarmTime.DoubleValue;
  130. return 0;
  131. }
  132. }
  133. public virtual double PositionAlarmRange
  134. {
  135. get
  136. {
  137. if (_currentPositionAlarmRange > 0)
  138. return _currentPositionAlarmRange;
  139. if (_scPositionAlarmRange != null)
  140. return _scPositionAlarmRange.DoubleValue;
  141. return 0;
  142. }
  143. }
  144. public virtual double PositionWarningTime
  145. {
  146. get
  147. {
  148. if (_scPositionWarningTime != null)
  149. return _scPositionWarningTime.DoubleValue;
  150. return 0;
  151. }
  152. }
  153. public virtual double PositionWarningRange
  154. {
  155. get
  156. {
  157. if (_currentPositionWarningRange > 0)
  158. return _currentPositionWarningRange;
  159. if (_scPositionWarningRange != null)
  160. return _scPositionWarningRange.DoubleValue;
  161. return 0;
  162. }
  163. }
  164. protected ThrottleValveBase() : base()
  165. {
  166. }
  167. protected ThrottleValveBase(string module, string name, XmlElement node = null, string ioModule = "") : base(module, name, name, name)
  168. {
  169. if (node != null)
  170. {
  171. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{Module}.{Name}.EnableAlarm");
  172. _scPressureAlarmTime = ParseScNode("scPressureAlarmTime", node, ioModule, $"{Module}.{Name}.PressureAlarmTime");
  173. _scPressureAlarmRange = ParseScNode("scPressureAlarmRange", node, ioModule, $"{Module}.{Name}.PressureAlarmRange");
  174. _scPressureWarningTime = ParseScNode("scPressureWarningTime", node, ioModule, $"{Module}.{Name}.PressureWarningTime");
  175. _scPressureWarningRange = ParseScNode("scPressureWarningRange", node, ioModule, $"{Module}.{Name}.PressureWarningRange");
  176. _scPositionAlarmTime = ParseScNode("scPositionAlarmTime", node, ioModule, $"{Module}.{Name}.PositionAlarmTime");
  177. _scPositionAlarmRange = ParseScNode("scPositionAlarmRange", node, ioModule, $"{Module}.{Name}.PositionAlarmRange");
  178. _scPositionWarningTime = ParseScNode("scPositionWarningTime", node, ioModule, $"{Module}.{Name}.PositionWarningTime");
  179. _scPositionWarningRange = ParseScNode("scPositionWarningRange", node, ioModule, $"{Module}.{Name}.PositionWarningRange");
  180. _scPressureFineTuningValue = ParseScNode("scPressureFineTuningValue", node, ioModule, $"{Module}.FineTuning.{Name}Pressure");
  181. _scPositionFineTuningValue = ParseScNode("scPositionFineTuningValue", node, ioModule, $"{Module}.FineTuning.{Name}Position");
  182. _scFineTuningEnable = ParseScNode("scFineTuningEnable", node, ioModule, $"{Module}.FineTuning.IsEnable");
  183. }
  184. }
  185. public virtual bool Initialize()
  186. {
  187. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  188. DATA.Subscribe($"{Module}.{Name}.ControlMode", () => (int)Mode);
  189. DATA.Subscribe($"{Module}.{Name}.PositionFeedback", () => PositionFeedback);
  190. DATA.Subscribe($"{Module}.{Name}.PressureFeedback", () => PressureFeedback);
  191. DATA.Subscribe($"{Module}.{Name}.PositionSetPoint", () => PositionSetPoint);
  192. DATA.Subscribe($"{Module}.{Name}.PressureSetPoint", () => PressureSetPoint);
  193. OP.Subscribe($"{Module}.{Name}.SetPressure", (function, args) =>
  194. {
  195. SetPressure(Convert.ToSingle(args[0]));
  196. return true;
  197. });
  198. OP.Subscribe($"{Module}.{Name}.SetPosition", (function, args) =>
  199. {
  200. SetPosition(Convert.ToSingle(args[0]));
  201. return true;
  202. });
  203. OP.Subscribe($"{Module}.{Name}.SetOpen", (function, args) =>
  204. {
  205. SetOpen( );
  206. return true;
  207. });
  208. OP.Subscribe($"{Module}.{Name}.SetClose", (function, args) =>
  209. {
  210. SetClose( );
  211. return true;
  212. });
  213. OP.Subscribe($"{Module}.{Name}.SetMode", (function, args) =>
  214. {
  215. if (!Enum.TryParse((string)args[0], out PressureCtrlMode mode))
  216. {
  217. EV.PostWarningLog(Module, $"Argument {args[0]}not valid");
  218. return false;
  219. }
  220. SetControlMode(mode);
  221. return true;
  222. });
  223. //for recipe
  224. OP.Subscribe($"{Module}.{Name}.SetFineTuning", (out string reason, int time, object[] param) =>
  225. {
  226. reason = string.Empty;
  227. SetFineTuning(Convert.ToSingle(param[0]), Convert.ToSingle(param[1]));
  228. return true;
  229. });
  230. //for recipe
  231. OP.Subscribe($"{Module}.{Name}.SetTolerance", (out string reason, int time, object[] param) =>
  232. {
  233. reason = string.Empty;
  234. var pressureWarning = Convert.ToSingle(param[0]);
  235. var pressureAlarm = Convert.ToSingle(param[1]);
  236. var positionWarning = Convert.ToSingle(param[2]);
  237. var positionAlarm = Convert.ToSingle(param[3]);
  238. SetTolerance((float)pressureWarning, (float)pressureAlarm, (float)positionWarning, (float)positionAlarm);
  239. return true;
  240. });
  241. return true;
  242. }
  243. public virtual void SetFineTuning(float pressureFineTuning, float positionFineTuning)
  244. {
  245. _currentPressureFineTuningValue = pressureFineTuning;
  246. _currentPositionFineTuningValue = positionFineTuning;
  247. }
  248. public virtual void SetTolerance(float pressureWarning, float pressureAlarm, float positionWarning, float positionAlarm)
  249. {
  250. _currentPressureWarningRange = pressureWarning;
  251. _currentPressureAlarmRange = pressureAlarm;
  252. _tolerancePressureAlarmChecker.Reset(PressureAlarmTime);
  253. _tolerancePressureWarningChecker.Reset(PressureWarningTime);
  254. _currentPositionWarningRange = positionWarning;
  255. _currentPositionAlarmRange = positionAlarm;
  256. _tolerancePositionAlarmChecker.Reset(PositionAlarmTime);
  257. _tolerancePositionWarningChecker.Reset(PositionWarningTime);
  258. }
  259. public virtual void CheckTolerance()
  260. {
  261. if (!EnableAlarm)
  262. return;
  263. if(Mode == PressureCtrlMode.TVPressureCtrl && PressureSetPoint > 0)
  264. {
  265. _tolerancePressureAlarmChecker.Monitor(PressureFeedback, (PressureSetPoint * (1 - PressureAlarmRange / 100)), (PressureSetPoint * (1 + PressureAlarmRange / 100)), PressureAlarmTime);
  266. _tolerancePressureWarningChecker.Monitor(PressureFeedback, (PressureSetPoint * (1 - PressureWarningRange / 100)), (PressureSetPoint * (1 + PressureWarningRange / 100)), PressureWarningTime);
  267. }
  268. if (Mode == PressureCtrlMode.TVPositionCtrl && PositionSetPoint > 0)
  269. {
  270. _tolerancePressureAlarmChecker.Monitor(PositionFeedback, (PositionSetPoint * (1 - PositionAlarmRange / 100)), (PositionSetPoint * (1 + PositionAlarmRange / 100)), PositionAlarmTime);
  271. _tolerancePressureWarningChecker.Monitor(PositionFeedback, (PositionSetPoint * (1 - PositionWarningRange / 100)), (PositionSetPoint * (1 + PositionWarningRange / 100)), PositionWarningTime);
  272. }
  273. }
  274. public virtual bool CheckPressureToleranceAlarm()
  275. {
  276. if (!EnableAlarm)
  277. return false;
  278. return _tolerancePressureAlarmChecker.Result;
  279. }
  280. public virtual bool CheckPressureToleranceWarning()
  281. {
  282. if (!EnableAlarm)
  283. return false;
  284. return _tolerancePressureWarningChecker.Result;
  285. }
  286. public virtual bool CheckPositionToleranceAlarm()
  287. {
  288. if (!EnableAlarm)
  289. return false;
  290. return _tolerancePositionAlarmChecker.Result;
  291. }
  292. public virtual bool CheckPositionToleranceWarning()
  293. {
  294. if (!EnableAlarm)
  295. return false;
  296. return _tolerancePositionWarningChecker.Result;
  297. }
  298. public virtual void SetClose()
  299. {
  300. throw new NotImplementedException();
  301. }
  302. public virtual void SetOpen()
  303. {
  304. throw new NotImplementedException();
  305. }
  306. public virtual void SetControlMode(PressureCtrlMode mode)
  307. {
  308. }
  309. public virtual void SetPressure(float isOn)
  310. {
  311. }
  312. public virtual void SetPosition(float position)
  313. {
  314. }
  315. public virtual void Terminate()
  316. {
  317. }
  318. public virtual void Monitor()
  319. {
  320. CheckTolerance();
  321. }
  322. public virtual void Reset()
  323. {
  324. }
  325. }
  326. }