IoPressureMeter.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.IOCore;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.RT.Tolerance;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.Event;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Xml;
  16. namespace FurnaceRT.Devices
  17. {
  18. public class IoPressureMeter : BaseDevice, IDevice
  19. {
  20. public double Value
  21. {
  22. get
  23. {
  24. return FeedBack;
  25. }
  26. }
  27. public double FeedBack
  28. {
  29. get
  30. {
  31. if (_aiValue != null)
  32. {
  33. return _isFloatAioType ? _aiValue.FloatValue : _aiValue.Value;
  34. }
  35. return 0;
  36. }
  37. }
  38. public double Precision
  39. {
  40. get
  41. {
  42. return _scPrecision == null ? 1000 : _scPrecision.DoubleValue;
  43. }
  44. }
  45. private AITWaterFlowMeterData DeviceData
  46. {
  47. get
  48. {
  49. AITWaterFlowMeterData data = new AITWaterFlowMeterData()
  50. {
  51. DeviceName = Name,
  52. DeviceSchematicId = DeviceID,
  53. DisplayName = Display,
  54. FeedBack = FeedBack < 0 ? 0 : FeedBack,
  55. Unit = Unit,
  56. };
  57. return data;
  58. }
  59. }
  60. public bool IsWarning
  61. {
  62. get
  63. {
  64. return _checkWarning.Result;
  65. }
  66. }
  67. public bool IsError
  68. {
  69. get
  70. {
  71. return _checkAlarm.Result;
  72. }
  73. }
  74. public double MinPressure
  75. {
  76. get
  77. {
  78. return _scMinValue == null ? 0 : _scMinValue.DoubleValue;
  79. }
  80. }
  81. public double MaxPressure
  82. {
  83. get
  84. {
  85. return _scMaxValue == null ? 0 : _scMaxValue.DoubleValue;
  86. }
  87. }
  88. public int WarningTime
  89. {
  90. get
  91. {
  92. return _scWarningTime == null ? 0 : _scWarningTime.IntValue;
  93. }
  94. }
  95. public int AlarmTime
  96. {
  97. get
  98. {
  99. return _scAlarmTime == null ? 0 : _scAlarmTime.IntValue;
  100. }
  101. }
  102. public bool EnableAlarm
  103. {
  104. get
  105. {
  106. return _scEnableAlarm == null ? true : _scEnableAlarm.BoolValue;
  107. }
  108. }
  109. public bool IsOutOfRange
  110. {
  111. get
  112. {
  113. if (MinPressure < 0.01 && MaxPressure < 0.01)
  114. return false;
  115. return (Value < MinPressure) || (Value > MaxPressure);
  116. }
  117. }
  118. public string Unit { get; set; }
  119. private AIAccessor _aiValue = null;
  120. private string _formatString = "F5";
  121. private SCConfigItem _scMinValue;
  122. private SCConfigItem _scMaxValue;
  123. private SCConfigItem _scEnableAlarm;
  124. private SCConfigItem _scWarningTime;
  125. private SCConfigItem _scAlarmTime;
  126. private SCConfigItem _scPrecision;
  127. private ToleranceChecker _checkWarning = new ToleranceChecker();
  128. private ToleranceChecker _checkAlarm = new ToleranceChecker();
  129. public AlarmEventItem AlarmToleranceWarning { get; set; }
  130. public AlarmEventItem AlarmToleranceAlarm { get; set; }
  131. private bool _isFloatAioType = false;
  132. private float _tuningPercent;
  133. private double _rangeMin = Int16.MinValue * 0.1;
  134. private double _rangeMax = Int16.MaxValue * 0.9;
  135. private double _min = Int16.MinValue * 0.1;
  136. private double _max = Int16.MaxValue * 0.9;
  137. public IoPressureMeter(string module, XmlElement node, string ioModule = "")
  138. {
  139. var attrModule = node.GetAttribute("module");
  140. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  141. Name = node.GetAttribute("id");
  142. Display = node.GetAttribute("display");
  143. DeviceID = node.GetAttribute("schematicId");
  144. Unit = node.GetAttribute("unit");
  145. _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType").ToLower() == "float");
  146. _aiValue = ParseAiNode("aiFeedback", node, ioModule);
  147. string[] range = node.GetAttribute("scale").Split(',');
  148. double.TryParse(range[0], out _rangeMin);
  149. double.TryParse(range[1], out _rangeMax);
  150. string[] physical = node.GetAttribute("physical").Split(',');
  151. double.TryParse(physical[0], out _min);
  152. double.TryParse(physical[1], out _max);
  153. if (node.HasAttribute("formatString"))
  154. _formatString = string.IsNullOrEmpty(node.GetAttribute("formatString")) ? "F5" : node.GetAttribute("formatString");
  155. string scBasePath = node.GetAttribute("scBasePath");
  156. if (string.IsNullOrEmpty(scBasePath))
  157. scBasePath = $"{Module}.{Name}";
  158. else
  159. {
  160. scBasePath = scBasePath.Replace("{module}", Module);
  161. }
  162. //_scale = SC.GetValue<double>($"{scBasePath}.PressureScale");
  163. //_phyScale = SC.GetValue<double>($"{scBasePath}.PhyScale");
  164. _scMinValue = ParseScNode("", node, "", $"{scBasePath}.{Name}.MinValue");
  165. _scMaxValue = ParseScNode("", node, "", $"{scBasePath}.{Name}.MaxValue");
  166. _scEnableAlarm = ParseScNode("", node, "", $"{scBasePath}.{Name}.EnableAlarm");
  167. _scWarningTime = ParseScNode("", node, "", $"{scBasePath}.{Name}.WarningTime");
  168. _scAlarmTime = ParseScNode("", node, "", $"{scBasePath}.{Name}.AlarmTime");
  169. _scPrecision = ParseScNode("", node, "", $"{scBasePath}.{Name}.Precision");
  170. }
  171. public bool Initialize()
  172. {
  173. DATA.Subscribe($"{Module}.{Name}.Value", () => Value);
  174. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  175. //AlarmToleranceWarning = SubscribeAlarm($"{Module}.{Name}.OutOfToleranceWarning", "", ResetWarningChecker, EventLevel.Warning);
  176. //AlarmToleranceError = SubscribeAlarm($"{Module}.{Name}.OutOfToleranceError", "", ResetErrorChecker);
  177. AlarmToleranceWarning = SubscribeAlarm(new AlarmEventItem()
  178. {
  179. EventEnum = $"{Module}.{Name}ToleranceWarning",
  180. Description = $"{Name} tolerance warning ",
  181. Solution = "No information available. Press[Clear] to delete alarm message.",
  182. Explaination = "No information available.",
  183. AutoRecovery = false,
  184. Level = EventLevel.Warning,
  185. Action = EventAction.Clear,
  186. Category = "TubeAlarm",
  187. }, () => { Reset(); return true; });
  188. AlarmToleranceAlarm = SubscribeAlarm(new AlarmEventItem()
  189. {
  190. EventEnum = $"{Module}.{Name}ToleranceAlarm",
  191. Description = $"{Name} tolerance alarm ",
  192. Solution = "No information available. Press[Clear] to delete alarm message.",
  193. Explaination = "No information available.",
  194. AutoRecovery = false,
  195. Level = EventLevel.Alarm,
  196. Action = EventAction.Clear,
  197. Category = "TubeAlarm",
  198. }, () => { Reset(); return true; });
  199. return true;
  200. }
  201. public void Terminate()
  202. {
  203. }
  204. public void SetTuning(float percent)
  205. {
  206. if (percent > 0 && percent <= 100)
  207. _tuningPercent = percent;
  208. }
  209. public void UnsetTuning()
  210. {
  211. _tuningPercent = 0;
  212. }
  213. public void Monitor()
  214. {
  215. if (EnableAlarm && (WarningTime > 0))
  216. {
  217. _checkWarning.Monitor(Value, MinPressure, MaxPressure, WarningTime);
  218. if (_checkWarning.Trig)
  219. {
  220. AlarmToleranceWarning.Description =
  221. $"{Display} out of range [{MinPressure},{MaxPressure}]{Unit} for {WarningTime} seconds";
  222. AlarmToleranceWarning.Set();
  223. }
  224. if (!_checkWarning.Result)
  225. {
  226. AlarmToleranceWarning.Reset();
  227. }
  228. }
  229. else
  230. {
  231. AlarmToleranceWarning.Reset();
  232. }
  233. if (EnableAlarm && (AlarmTime > 0))
  234. {
  235. _checkAlarm.Monitor(Value, MinPressure, MaxPressure, AlarmTime);
  236. if (_checkAlarm.Trig)
  237. {
  238. AlarmToleranceAlarm.Description =
  239. $"{Display} out of range [{MinPressure},{MaxPressure}]{Unit} for {AlarmTime} seconds";
  240. AlarmToleranceAlarm.Set();
  241. }
  242. if (!_checkAlarm.Result)
  243. {
  244. AlarmToleranceAlarm.Reset();
  245. }
  246. }
  247. else
  248. {
  249. AlarmToleranceAlarm.Reset();
  250. }
  251. }
  252. public bool ResetWarningChecker()
  253. {
  254. _checkWarning.RST = true;
  255. return true;
  256. }
  257. public bool ResetErrorChecker()
  258. {
  259. _checkAlarm.RST = true;
  260. return true;
  261. }
  262. public void Reset()
  263. {
  264. _checkWarning.RST = true;
  265. _checkAlarm.RST = true;
  266. AlarmToleranceWarning.Reset();
  267. AlarmToleranceAlarm.Reset();
  268. }
  269. private double GetAIScale()
  270. {
  271. return Converter.Phy2Logic(_isFloatAioType ? _aiValue.FloatValue : _aiValue.Value, _rangeMin, _rangeMax, _min, _max);
  272. }
  273. }
  274. }