HeaterBase.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  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.OperationCenter;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.RT.Tolerance;
  15. namespace MECF.Framework.Common.Device.Bases
  16. {
  17. public abstract class HeaterBase : BaseDevice, IDevice
  18. {
  19. public virtual bool IsPowerOn { get; set; }
  20. public virtual float TempSetPoint { get; set; }
  21. public virtual float TempFeedback { get; set; }
  22. public virtual AITHeaterData DeviceData { get; set; }
  23. private float _currentWarningRange;
  24. private float _currentAlarmRange;
  25. protected SCConfigItem _scEnableAlarm;
  26. protected SCConfigItem _scAlarmTime;
  27. protected SCConfigItem _scAlarmRange;
  28. protected SCConfigItem _scWarningTime;
  29. protected SCConfigItem _scWarningRange;
  30. protected ToleranceChecker _toleranceAlarmChecker = new ToleranceChecker();
  31. protected ToleranceChecker _toleranceWarningChecker = new ToleranceChecker();
  32. protected double _currentFineTuningValue;
  33. protected SCConfigItem _scFineTuningEnable;
  34. protected SCConfigItem _scFineTuningValue;
  35. public virtual double FineTuningValue
  36. {
  37. get
  38. {
  39. if (_scFineTuningEnable == null || !_scFineTuningEnable.BoolValue)
  40. return 1;
  41. if (_currentFineTuningValue != 0)
  42. return 1 + _currentFineTuningValue / 100;
  43. return _scFineTuningValue != null ? 1 + _scFineTuningValue.DoubleValue / 100 : 1;
  44. }
  45. }
  46. public virtual bool EnableAlarm
  47. {
  48. get
  49. {
  50. if (_scEnableAlarm != null)
  51. return _scEnableAlarm.BoolValue;
  52. return false;
  53. }
  54. }
  55. public virtual double AlarmTime
  56. {
  57. get
  58. {
  59. if (_scAlarmTime != null)
  60. return _scAlarmTime.DoubleValue;
  61. return 0;
  62. }
  63. }
  64. public virtual double AlarmRange
  65. {
  66. get
  67. {
  68. if (_currentAlarmRange > 0)
  69. return _currentAlarmRange;
  70. if (_scAlarmRange != null)
  71. return _scAlarmRange.DoubleValue;
  72. return 0;
  73. }
  74. }
  75. public virtual double WarningTime
  76. {
  77. get
  78. {
  79. if (_scWarningTime != null)
  80. return _scWarningTime.DoubleValue;
  81. return 0;
  82. }
  83. }
  84. public virtual double WarningRange
  85. {
  86. get
  87. {
  88. if (_currentWarningRange > 0)
  89. return _currentWarningRange;
  90. if (_scWarningRange != null)
  91. return _scWarningRange.DoubleValue;
  92. return 0;
  93. }
  94. }
  95. protected HeaterBase( ) : base( )
  96. {
  97. }
  98. protected HeaterBase(string module, string name, XmlElement node = null, string ioModule = "") : base(module, name, name, name)
  99. {
  100. if (node != null)
  101. {
  102. _scEnableAlarm = ParseScNode("scEnableAlarm", node, ioModule, $"{Module}.{Name}.EnableAlarm");
  103. _scAlarmTime = ParseScNode("scAlarmTime", node, ioModule, $"{Module}.{Name}.AlarmTime");
  104. _scAlarmRange = ParseScNode("scAlarmRange", node, ioModule, $"{Module}.{Name}.AlarmRange");
  105. _scWarningTime = ParseScNode("scWarningTime", node, ioModule, $"{Module}.{Name}.WarningTime");
  106. _scWarningRange = ParseScNode("scWarningRange", node, ioModule, $"{Module}.{Name}.WarningRange");
  107. _scFineTuningValue = ParseScNode("scFineTuningValue", node, ioModule, $"{Module}.FineTuning.{Name}");
  108. _scFineTuningEnable = ParseScNode("scFineTuningEnable", node, ioModule, $"{Module}.FineTuning.IsEnable");
  109. }
  110. }
  111. public virtual bool Initialize()
  112. {
  113. DATA.Subscribe($"{Module}.{Name}.DeviceData", () => DeviceData);
  114. DATA.Subscribe($"{Module}.{Name}.TempFeedback", () => TempFeedback);
  115. DATA.Subscribe($"{Module}.{Name}.TempSetPoint", () => TempSetPoint);
  116. DATA.Subscribe($"{Module}.{Name}.IsPowerOn", () => IsPowerOn);
  117. OP.Subscribe($"{Module}.{Name}.SetPowerOn", (function, args) =>
  118. {
  119. SetPowerOnOff(true);
  120. return true;
  121. });
  122. OP.Subscribe($"{Module}.{Name}.SetPowerOff", (function, args) =>
  123. {
  124. SetPowerOnOff(false);
  125. return true;
  126. });
  127. OP.Subscribe($"{Module}.{Name}.SetPowerOnOff", (function, args) =>
  128. {
  129. bool.TryParse(args[0].ToString(), out bool isOn);
  130. SetPowerOnOff(isOn);
  131. return true;
  132. });
  133. OP.Subscribe($"{Module}.{Name}.SetTemperature", (function, args) =>
  134. {
  135. SetTemperature((float)args[0]);
  136. return true;
  137. });
  138. //for recipe
  139. OP.Subscribe($"{Module}.{Name}.SetTolerance", (out string reason, int time, object[] param) =>
  140. {
  141. reason = string.Empty;
  142. var warning = Convert.ToSingle(param[0]);
  143. var alarm = Convert.ToSingle(param[1]);
  144. SetTolerance((float)warning, (float)alarm);
  145. return true;
  146. });
  147. //for recipe
  148. OP.Subscribe($"{Module}.{Name}.SetFineTuning", (out string reason, int time, object[] param) =>
  149. {
  150. reason = string.Empty;
  151. SetFineTuning(Convert.ToSingle(param[0]));
  152. return true;
  153. });
  154. return true;
  155. }
  156. public virtual void SetFineTuning(float fineTuning)
  157. {
  158. _currentFineTuningValue = fineTuning;
  159. }
  160. public virtual void SetTolerance(float warning, float alarm)
  161. {
  162. _currentWarningRange = warning;
  163. _currentAlarmRange = alarm;
  164. _toleranceAlarmChecker.Reset(AlarmTime);
  165. _toleranceWarningChecker.Reset(WarningTime);
  166. }
  167. public virtual void CheckTolerance()
  168. {
  169. if (!EnableAlarm || TempSetPoint == 0)
  170. return;
  171. _toleranceAlarmChecker.Monitor(TempFeedback, (TempSetPoint * (1 - AlarmRange / 100)), (TempSetPoint * (1 + AlarmRange / 100)), AlarmTime);
  172. _toleranceWarningChecker.Monitor(TempFeedback, (TempSetPoint * (1 - WarningRange / 100)), (TempSetPoint * (1 + WarningRange / 100)), WarningTime);
  173. }
  174. public virtual bool CheckToleranceAlarm()
  175. {
  176. if (!EnableAlarm)
  177. return false;
  178. return _toleranceAlarmChecker.Result;
  179. }
  180. public virtual bool CheckToleranceWarning()
  181. {
  182. if (!EnableAlarm)
  183. return false;
  184. return _toleranceWarningChecker.Result;
  185. }
  186. public virtual void SetPowerOnOff(bool isOn)
  187. {
  188. }
  189. public virtual void SetTemperature(float temperature)
  190. {
  191. }
  192. public virtual void Terminate()
  193. {
  194. }
  195. public virtual void Monitor()
  196. {
  197. CheckTolerance();
  198. }
  199. public virtual void Reset()
  200. {
  201. _toleranceAlarmChecker.Reset(AlarmTime);
  202. _toleranceWarningChecker.Reset(WarningTime);
  203. }
  204. }
  205. }