PfeifferPumpA100.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using Aitex.Core.Common;
  6. using Aitex.Core.Common.DeviceData;
  7. using Aitex.Core.RT.DataCenter;
  8. using Aitex.Core.RT.Device;
  9. using Aitex.Core.RT.Device.Unit;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.Log;
  12. using Aitex.Core.RT.OperationCenter;
  13. using Aitex.Core.RT.SCCore;
  14. using Aitex.Core.Util;
  15. using MECF.Framework.Common.Communications;
  16. using MECF.Framework.Common.Device.Bases;
  17. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
  18. using Newtonsoft.Json;
  19. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.PfeifferPumpA100
  20. {
  21. public class PfeifferPumpA100 : PumpBase, IConnection
  22. {
  23. public string Address => Connection.Address;
  24. public bool IsConnected => Connection.IsConnected && !_connection.IsCommunicationError;
  25. public bool Connect()
  26. {
  27. return _connection.Connect();
  28. }
  29. public bool Disconnect()
  30. {
  31. return _connection.Disconnect();
  32. }
  33. public string PortStatus { get; set; } = "Closed";
  34. private PfeifferPumpA100Connection _connection;
  35. public PfeifferPumpA100Connection Connection
  36. {
  37. get { return _connection; }
  38. }
  39. public override AITPumpData DeviceData
  40. {
  41. get
  42. {
  43. AITPumpData data = new AITPumpData()
  44. {
  45. IsOn = IsOn,
  46. IsError = IsError,
  47. OverTemp = IsOverTemperature,
  48. DeviceModule = Module,
  49. DeviceName = Name,
  50. };
  51. return data;
  52. }
  53. }
  54. public string ADR = "000";
  55. private R_TRIG _trigError = new R_TRIG();
  56. private R_TRIG _trigCommunicationError = new R_TRIG();
  57. private R_TRIG _trigRetryConnect = new R_TRIG();
  58. private PeriodicJob _thread;
  59. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  60. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  61. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  62. private object _locker = new object();
  63. private bool _enableLog;
  64. //private string _address;
  65. private string _scRoot;
  66. private string _portName;
  67. public PfeifferPumpA100(string module, string name, string scRoot) : base(module, name)
  68. {
  69. _scRoot = scRoot;
  70. //_address = "000";
  71. }
  72. private void ResetPropertiesAndResponses()
  73. {
  74. foreach (var ioResponse in IOResponseList)
  75. {
  76. ioResponse.ResonseContent = null;
  77. ioResponse.ResonseRecievedTime = DateTime.Now;
  78. }
  79. }
  80. public override bool Initialize()
  81. {
  82. base.Initialize();
  83. ResetPropertiesAndResponses();
  84. if (_connection != null && _connection.IsConnected)
  85. return true;
  86. _portName = SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address");
  87. _enableLog = SC.GetValue<bool>($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.EnableLogMessage");
  88. _connection = new PfeifferPumpA100Connection(_portName);
  89. _connection.EnableLog(_enableLog);
  90. if (_connection.Connect())
  91. {
  92. PortStatus = "Open";
  93. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  94. }
  95. _thread = new PeriodicJob(1000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  96. _lstMonitorHandler.AddLast(new PfeifferPumpA100ReadPumpStatusHandler(this));
  97. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
  98. DATA.Subscribe($"{Module}.{Name}.Address", () => Address);
  99. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  100. {
  101. Disconnect();
  102. Connect();
  103. return true;
  104. });
  105. return true;
  106. }
  107. public bool InitConnection(string portName, int bautRate, int dataBits, Parity parity, StopBits stopBits)
  108. {
  109. _connection = new PfeifferPumpA100Connection(portName, bautRate, dataBits, parity, stopBits);
  110. if (_connection.Connect())
  111. {
  112. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  113. }
  114. _thread = new PeriodicJob(1000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  115. return true;
  116. }
  117. internal void NoteSetParaCompleted()
  118. {
  119. }
  120. private bool OnTimer()
  121. {
  122. try
  123. {
  124. //_connection.MonitorTimeout();
  125. if (!_connection.IsConnected || _connection.IsCommunicationError)
  126. {
  127. lock (_locker)
  128. {
  129. _lstHandler.Clear();
  130. }
  131. _trigRetryConnect.CLK = !_connection.IsConnected;
  132. if (_trigRetryConnect.Q)
  133. {
  134. _connection.SetPortAddress(_portName);
  135. if (!_connection.Connect())
  136. {
  137. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  138. }
  139. else
  140. {
  141. //_lstHandler.AddLast(new PfeifferPumpA100QueryPinHandler(this, _deviceAddress));
  142. //_lstHandler.AddLast(new PfeifferPumpA100SetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  143. }
  144. }
  145. return true;
  146. }
  147. HandlerBase handler = null;
  148. if (!_connection.IsBusy)
  149. {
  150. lock (_locker)
  151. {
  152. if (_lstHandler.Count == 0)
  153. {
  154. foreach (var monitorHandler in _lstMonitorHandler)
  155. {
  156. _lstHandler.AddLast(monitorHandler);
  157. }
  158. }
  159. if (_lstHandler.Count > 0)
  160. {
  161. handler = _lstHandler.First.Value;
  162. _lstHandler.RemoveFirst();
  163. }
  164. }
  165. if (handler != null)
  166. {
  167. _connection.Execute(handler);
  168. }
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. LOG.Write(ex);
  174. }
  175. return true;
  176. }
  177. public override void Monitor()
  178. {
  179. try
  180. {
  181. //_connection.EnableLog(_enableLog);
  182. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  183. if (_trigCommunicationError.Q)
  184. {
  185. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. LOG.Write(ex);
  191. }
  192. }
  193. public override void Reset()
  194. {
  195. _trigError.RST = true;
  196. _connection.SetCommunicationError(false, "");
  197. _trigCommunicationError.RST = true;
  198. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  199. _trigRetryConnect.RST = true;
  200. base.Reset();
  201. }
  202. #region Command Functions
  203. public void PerformRawCommand(string command)
  204. {
  205. lock (_locker)
  206. {
  207. _lstHandler.AddLast(new PfeifferPumpA100RawCommandHandler(this, command, null));
  208. }
  209. }
  210. public void PerformRawCommand(string command, string parameter)
  211. {
  212. lock (_locker)
  213. {
  214. _lstHandler.AddLast(new PfeifferPumpA100RawCommandHandler(this, command, parameter));
  215. }
  216. }
  217. public override void SetPumpOnOff(bool isOn)
  218. {
  219. lock (_locker)
  220. {
  221. _lstHandler.AddLast(new PfeifferPumpA100SimpleSwitchHandler(this, "SYS", isOn ? "ON" : "OFF"));
  222. }
  223. }
  224. public void EchoOnOff(string parameter)
  225. {
  226. lock (_locker)
  227. {
  228. _lstHandler.AddLast(new PfeifferPumpA100SimpleSwitchHandler(this, "ECH", parameter));
  229. }
  230. }
  231. internal void NoteSwitchCompleted(string command, bool value)
  232. {
  233. //if (command == "ECH")
  234. // SetEchoOnOff = value;
  235. //else if (command == "SYS")
  236. // SetPumpOnOff = value;
  237. }
  238. #endregion
  239. #region Properties
  240. public string Error { get; private set; }
  241. #endregion
  242. #region Note Functions
  243. private R_TRIG _trigWarningMessage = new R_TRIG();
  244. public void NoteError(string reason)
  245. {
  246. if (reason != null)
  247. {
  248. _trigWarningMessage.CLK = true;
  249. if (_trigWarningMessage.Q)
  250. {
  251. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  252. }
  253. Error = reason;
  254. }
  255. else
  256. {
  257. Error = null;
  258. }
  259. }
  260. #endregion
  261. }
  262. }