IoPressureMeter3.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System.Xml;
  2. using Aitex.Core.Common.DeviceData;
  3. using Aitex.Core.RT.DataCenter;
  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. namespace Aitex.Core.RT.Device.Unit
  11. {
  12. public class IoPressureMeter3 : BaseDevice, IDevice, IPressureMeter
  13. {
  14. public double Value
  15. {
  16. get
  17. {
  18. return FeedBack;
  19. }
  20. }
  21. public double FeedBack
  22. {
  23. get
  24. {
  25. double value = _isFloatAioType ? _aiValue.FloatValue : _aiValue.Value;
  26. if (_tuningPercent > 0 && _tuningPercent < 100)
  27. {
  28. value = value * (1 - _tuningPercent/100.0);
  29. }
  30. return value;
  31. }
  32. }
  33. public double Precision
  34. {
  35. get
  36. {
  37. return _scPrecision == null ? 1000 : _scPrecision.DoubleValue;
  38. }
  39. }
  40. private AITPressureMeterData DeviceData
  41. {
  42. get
  43. {
  44. AITPressureMeterData data = new AITPressureMeterData()
  45. {
  46. DeviceName = Name,
  47. DeviceSchematicId = DeviceID,
  48. DisplayName = Display,
  49. FeedBack = Value,
  50. Unit = Unit,
  51. FormatString = _formatString,
  52. DisplayWithUnit = true,
  53. IsError = IsError,
  54. IsWarning = IsWarning,
  55. Precision = Precision,
  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. public IoPressureMeter3(string module, XmlElement node, string ioModule = "")
  134. {
  135. var attrModule = node.GetAttribute("module");
  136. base.Module = string.IsNullOrEmpty(attrModule) ? module : attrModule;
  137. Name = node.GetAttribute("id");
  138. Display = node.GetAttribute("display");
  139. DeviceID = node.GetAttribute("schematicId");
  140. Unit = node.GetAttribute("unit");
  141. _isFloatAioType = !string.IsNullOrEmpty(node.GetAttribute("aioType")) && (node.GetAttribute("aioType") == "float");
  142. _aiValue = ParseAiNode("aiValue", node, ioModule);
  143. if (node.HasAttribute("formatString"))
  144. _formatString = string.IsNullOrEmpty(node.GetAttribute("formatString")) ? "F5" : node.GetAttribute("formatString");
  145. string scBasePath = node.GetAttribute("scBasePath");
  146. if (string.IsNullOrEmpty(scBasePath))
  147. scBasePath = $"{Module}.{Name}";
  148. else
  149. {
  150. scBasePath = scBasePath.Replace("{module}", Module);
  151. }
  152. _scMinValue = ParseScNode("", node, "", $"{scBasePath}.{Name}.MinValue");
  153. _scMaxValue = ParseScNode("", node, "", $"{scBasePath}.{Name}.MaxValue");
  154. _scEnableAlarm = ParseScNode("", node, "", $"{scBasePath}.{Name}.EnableAlarm");
  155. _scWarningTime = ParseScNode("", node, "", $"{scBasePath}.{Name}.WarningTime");
  156. _scAlarmTime = ParseScNode("", node, "", $"{scBasePath}.{Name}.AlarmTime");
  157. _scPrecision = ParseScNode("", node, "", $"{scBasePath}.{Name}.Precision");
  158. }
  159. public bool Initialize()
  160. {
  161. DATA.Subscribe($"{Module}.{Name}.Value", () => Value);
  162. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  163. //AlarmToleranceWarning = SubscribeAlarm($"{Module}.{Name}.OutOfToleranceWarning", "", ResetWarningChecker, EventLevel.Warning);
  164. //AlarmToleranceError = SubscribeAlarm($"{Module}.{Name}.OutOfToleranceError", "", ResetErrorChecker);
  165. AlarmToleranceWarning = SubscribeAlarm(new AlarmEventItem()
  166. {
  167. EventEnum = $"{Module}.{Name}ToleranceWarning",
  168. Description = $"{Name} tolerance warning ",
  169. Solution = "No information available. Press[Clear] to delete alarm message.",
  170. Explaination = "No information available.",
  171. AutoRecovery = false,
  172. Level = EventLevel.Warning,
  173. Action = EventAction.Clear,
  174. Category = "TubeAlarm",
  175. }, () => { Reset(); return true; });
  176. AlarmToleranceAlarm = SubscribeAlarm(new AlarmEventItem()
  177. {
  178. EventEnum = $"{Module}.{Name}ToleranceAlarm",
  179. Description = $"{Name} tolerance alarm ",
  180. Solution = "No information available. Press[Clear] to delete alarm message.",
  181. Explaination = "No information available.",
  182. AutoRecovery = false,
  183. Level = EventLevel.Alarm,
  184. Action = EventAction.Clear,
  185. Category = "TubeAlarm",
  186. }, () => { Reset(); return true; });
  187. return true;
  188. }
  189. public void Terminate()
  190. {
  191. }
  192. public void SetTuning(float percent)
  193. {
  194. if (percent>0 && percent<=100)
  195. _tuningPercent = percent;
  196. }
  197. public void UnsetTuning()
  198. {
  199. _tuningPercent = 0;
  200. }
  201. public void Monitor()
  202. {
  203. if (EnableAlarm && (WarningTime > 0))
  204. {
  205. _checkWarning.Monitor(Value, MinPressure, MaxPressure, WarningTime);
  206. if (_checkWarning.Trig)
  207. {
  208. AlarmToleranceWarning.Description =
  209. $"{Display} out of range [{MinPressure},{MaxPressure}]{Unit} for {WarningTime} seconds";
  210. AlarmToleranceWarning.Set();
  211. }
  212. if (!_checkWarning.Result)
  213. {
  214. AlarmToleranceWarning.Reset();
  215. }
  216. }
  217. else
  218. {
  219. AlarmToleranceWarning.Reset();
  220. }
  221. if (EnableAlarm && (AlarmTime > 0))
  222. {
  223. _checkAlarm.Monitor(Value, MinPressure, MaxPressure, AlarmTime);
  224. if (_checkAlarm.Trig)
  225. {
  226. AlarmToleranceAlarm.Description =
  227. $"{Display} out of range [{MinPressure},{MaxPressure}]{Unit} for {AlarmTime} seconds";
  228. AlarmToleranceAlarm.Set();
  229. }
  230. if (!_checkAlarm.Result)
  231. {
  232. AlarmToleranceAlarm.Reset();
  233. }
  234. }
  235. else
  236. {
  237. AlarmToleranceAlarm.Reset();
  238. }
  239. }
  240. public bool ResetWarningChecker()
  241. {
  242. _checkWarning.RST = true;
  243. return true;
  244. }
  245. public bool ResetErrorChecker()
  246. {
  247. _checkAlarm.RST = true;
  248. return true;
  249. }
  250. public void Reset()
  251. {
  252. _checkWarning.RST = true;
  253. _checkAlarm.RST = true;
  254. AlarmToleranceWarning.Reset();
  255. AlarmToleranceAlarm.Reset();
  256. }
  257. }
  258. }