PumpMagpower.cs 11 KB

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