HipaceTurboPump.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using System.Collections.Generic;
  3. using Aitex.Core.Common.DeviceData;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.Communications;
  10. using MECF.Framework.Common.Device.Bases;
  11. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.TurboPumps.PfeifferHipace
  12. {
  13. public class HipaceTurboPump : PumpBase
  14. {
  15. public override bool IsError
  16. {
  17. get { return _errorCode != 0; }
  18. }
  19. public override bool IsOn
  20. {
  21. get { return _isOn; }
  22. }
  23. public override float Speed
  24. {
  25. get { return _speed; }
  26. }
  27. public override float Temperature
  28. {
  29. get { return _temperature; }
  30. }
  31. public override bool IsStable
  32. {
  33. get { return _isAtSpeed; }
  34. }
  35. public override AITPumpData DeviceData
  36. {
  37. get
  38. {
  39. AITPumpData data = new AITPumpData()
  40. {
  41. DeviceName = Name,
  42. DeviceSchematicId = DeviceID,
  43. DisplayName = Display,
  44. DeviceModule = Module,
  45. IsError = IsError,
  46. IsOn = _isOn,
  47. Speed = _speed,
  48. Temperature = _temperature,
  49. AtSpeed = _isAtSpeed,
  50. OverTemp = _isOverTemp,
  51. };
  52. return data;
  53. }
  54. }
  55. private HipaceConnection _connection;
  56. private string _deviceAddress = "001";
  57. private int _speed;
  58. private int _temperature;
  59. private bool _isOn;
  60. private int _power;
  61. private bool _isAccelerate;
  62. private bool _isAtSpeed;
  63. private bool _isOverTemp;
  64. private bool _activeMonitorStatus;
  65. private int _errorCode;
  66. private RD_TRIG _trigPumpOnOff = new RD_TRIG();
  67. private R_TRIG _trigError = new R_TRIG();
  68. private R_TRIG _trigOverTemp = new R_TRIG();
  69. private R_TRIG _trigWarningMessage = new R_TRIG();
  70. private string _lastError = string.Empty;
  71. private R_TRIG _trigCommunicationError = new R_TRIG();
  72. private R_TRIG _trigRetryConnect = new R_TRIG();
  73. private PeriodicJob _thread;
  74. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  75. private object _locker = new object();
  76. private bool _enableLog = true;
  77. private string _scRoot;
  78. public HipaceTurboPump(string module, string name, string scRoot) : base(module, name)
  79. {
  80. _scRoot = scRoot;
  81. _activeMonitorStatus = true;
  82. _power = 0;
  83. }
  84. public override bool Initialize()
  85. {
  86. if(_connection != null && _connection.IsConnected)
  87. {
  88. return true;
  89. }
  90. base.Initialize();
  91. DATA.Subscribe($"{Module}.{Name}.Power", () => _power);
  92. DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => _errorCode);
  93. DATA.Subscribe($"{Module}.{Name}.IsAtSpeed", () => _isAtSpeed);
  94. DATA.Subscribe($"{Module}.{Name}.IsAccelerate", () => _isAccelerate);
  95. string portName = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.Address");
  96. int address = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.DeviceAddress");
  97. _deviceAddress = address.ToString("D3");
  98. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
  99. _connection = new HipaceConnection(portName);
  100. _connection.EnableLog(_enableLog);
  101. if (_connection.Connect())
  102. {
  103. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  104. }
  105. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  106. return true;
  107. }
  108. private bool OnTimer()
  109. {
  110. try
  111. {
  112. //temp comment ??
  113. //_connection.MonitorTimeout();
  114. if (!_connection.IsConnected || _connection.IsCommunicationError)
  115. {
  116. lock (_locker)
  117. {
  118. _lstHandler.Clear();
  119. }
  120. _trigRetryConnect.CLK = !_connection.IsConnected;
  121. if (_trigRetryConnect.Q)
  122. {
  123. _connection.SetPortAddress(SC.GetStringValue($"{Module}.{Name}.Address"));
  124. if (!_connection.Connect())
  125. {
  126. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  127. }
  128. }
  129. return true;
  130. }
  131. HandlerBase handler = null;
  132. if (!_connection.IsBusy)
  133. {
  134. lock (_locker)
  135. {
  136. if (_lstHandler.Count == 0 && _activeMonitorStatus)
  137. {
  138. _lstHandler.AddLast(new HipaceQueryErrorHandler(this, _deviceAddress));
  139. _lstHandler.AddLast(new HipaceQuerySpeedHandler(this, _deviceAddress));
  140. _lstHandler.AddLast(new HipaceQueryTemperatureHandler(this, _deviceAddress));
  141. _lstHandler.AddLast(new HipacePumpStationHandler(this, _deviceAddress, true, true));
  142. _lstHandler.AddLast(new HipaceAtSpeedHandler(this, _deviceAddress));
  143. _lstHandler.AddLast(new HipaceAccelerateHandler(this, _deviceAddress));
  144. _lstHandler.AddLast(new HipaceOverTempHandler(this, _deviceAddress));
  145. }
  146. if (_lstHandler.Count > 0)
  147. {
  148. handler = _lstHandler.First.Value;
  149. _lstHandler.RemoveFirst();
  150. }
  151. }
  152. if (handler != null)
  153. {
  154. _connection.Execute(handler);
  155. }
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. LOG.Write(ex);
  161. }
  162. return true;
  163. }
  164. public override void Monitor()
  165. {
  166. try
  167. {
  168. _connection.EnableLog(_enableLog);
  169. _trigPumpOnOff.CLK = _isOn;
  170. if (_trigPumpOnOff.R)
  171. {
  172. EV.PostInfoLog(Module, $"{Module}.{Name} is on");
  173. }
  174. if (_trigPumpOnOff.T)
  175. {
  176. EV.PostInfoLog(Module, $"{Module}.{Name} is off");
  177. }
  178. _trigError.CLK = IsError;
  179. if (_trigError.Q)
  180. {
  181. EV.PostAlarmLog(Module, $"{Module}.{Name} is error, error code {_errorCode:D3}");
  182. }
  183. _trigOverTemp.CLK = _isOverTemp;
  184. if (_trigOverTemp.Q)
  185. {
  186. EV.PostAlarmLog(Module, $"{Module}.{Name} is over temperature");
  187. }
  188. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  189. if (_trigCommunicationError.Q)
  190. {
  191. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  192. }
  193. }
  194. catch (Exception ex)
  195. {
  196. LOG.Write(ex);
  197. }
  198. }
  199. public override void Reset()
  200. {
  201. _trigError.RST = true;
  202. _trigOverTemp.RST = true;
  203. _trigWarningMessage.RST = true;
  204. _connection.SetCommunicationError(false, "");
  205. _trigCommunicationError.RST = true;
  206. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  207. _trigRetryConnect.RST = true;
  208. base.Reset();
  209. }
  210. public override void SetPumpOnOff(bool isOn)
  211. {
  212. lock (_locker)
  213. {
  214. _lstHandler.AddLast(new HipacePumpStationHandler(this, _deviceAddress, isOn, false));
  215. }
  216. }
  217. public void SetActiveMonitor(bool active)
  218. {
  219. _activeMonitorStatus = active;
  220. }
  221. internal void NoteOnOff(bool isOn)
  222. {
  223. _isOn = isOn;
  224. }
  225. public void SetSpeed(int speed)
  226. {
  227. _speed = speed;
  228. }
  229. public void SetTemperature(int temperature)
  230. {
  231. _temperature = temperature;
  232. }
  233. public void SetErrorCode(int errorCode)
  234. {
  235. _errorCode = errorCode;
  236. }
  237. public void SetAtSpeed(bool atSpeed)
  238. {
  239. _isAtSpeed = atSpeed;
  240. }
  241. public void SetOverTemp(bool overTemp)
  242. {
  243. _isOverTemp = overTemp;
  244. }
  245. public void SetIsAccelerate(bool isAccelerate)
  246. {
  247. _isAccelerate = isAccelerate;
  248. }
  249. public void SetError(string reason)
  250. {
  251. _trigWarningMessage.CLK = true;
  252. if (_trigWarningMessage.Q)
  253. {
  254. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  255. }
  256. }
  257. }
  258. }