SkySGMPump.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Communications;
  5. using MECF.Framework.Common.Device.Bases;
  6. using MECF.Framework.Common.Event;
  7. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.SkySGMPump
  14. {
  15. public class SkySGMPump : PumpBase, IConnection
  16. {
  17. public string Address { get { return ""; } }
  18. public string PortName { get; set; }
  19. public bool IsConnected { get; }
  20. public bool Connect()
  21. {
  22. return true;
  23. }
  24. public bool Disconnect()
  25. {
  26. return true;
  27. }
  28. public string PortStatus { get; set; } = "Closed";
  29. private SkySGMPumpConnection _connection;
  30. public SkySGMPumpConnection Connection
  31. {
  32. get { return _connection; }
  33. }
  34. private R_TRIG _trigError = new R_TRIG();
  35. private R_TRIG _trigCommunicationError = new R_TRIG();
  36. private R_TRIG _trigRetryConnect = new R_TRIG();
  37. private PeriodicJob _thread;
  38. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  39. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  40. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  41. //public bool HasAlarm => throw new NotImplementedException();
  42. //public event Action<string, AlarmEventItem> OnDeviceAlarmStateChanged;
  43. private object _locker = new object();
  44. //private bool _enableLog;
  45. //private string _address;
  46. private string _scRoot;
  47. public SkySGMPump(string module, string name, string scRoot, string portName) : base()
  48. {
  49. Module = module;
  50. Name = name;
  51. _scRoot = scRoot;
  52. PortName = portName;
  53. }
  54. private bool OnTimer()
  55. {
  56. try
  57. {
  58. //return true;
  59. _connection.MonitorTimeout();
  60. if (!_connection.IsConnected || _connection.IsCommunicationError)
  61. {
  62. lock (_locker)
  63. {
  64. _lstHandler.Clear();
  65. }
  66. _trigRetryConnect.CLK = !_connection.IsConnected;
  67. if (_trigRetryConnect.Q)
  68. {
  69. if (!_connection.Connect())
  70. {
  71. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  72. }
  73. else
  74. {
  75. }
  76. }
  77. return true;
  78. }
  79. HandlerBase handler = null;
  80. if (!_connection.IsBusy)
  81. {
  82. lock (_locker)
  83. {
  84. if (_lstHandler.Count == 0)
  85. {
  86. foreach (var monitorHandler in _lstMonitorHandler)
  87. {
  88. _lstHandler.AddLast(monitorHandler);
  89. }
  90. }
  91. if (_lstHandler.Count > 0)
  92. {
  93. handler = _lstHandler.First.Value;
  94. _lstHandler.RemoveFirst();
  95. }
  96. }
  97. if (handler != null)
  98. {
  99. _connection.Execute(handler);
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. LOG.Write(ex);
  106. }
  107. return true;
  108. }
  109. public override bool Initialize()
  110. {
  111. _connection = new SkySGMPumpConnection(_scRoot, "\r\n");
  112. //_connection.EnableLog(_enableLog);
  113. if (_connection.Connect())
  114. {
  115. PortStatus = "Open";
  116. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  117. }
  118. else
  119. {
  120. PortStatus = "Close";
  121. EV.PostInfoLog(Module, $"{Module}.{Name} connect failed");
  122. }
  123. //_lstMonitorHandler.AddLast(new SkySGMPumpStartHandler(this));
  124. //_lstMonitorHandler.AddLast(new SkySGMPumpStopHandler(this));
  125. _lstMonitorHandler.AddLast(new SkySGMPumpReadRunParaHandler(this));
  126. _thread = new PeriodicJob(1000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  127. return true;
  128. }
  129. public override void Monitor()
  130. {
  131. return;
  132. }
  133. public override void Reset()
  134. {
  135. _trigError.RST = true;
  136. _connection.SetCommunicationError(false, "");
  137. _trigCommunicationError.RST = true;
  138. _trigWarningMessage.RST = true;
  139. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  140. _trigRetryConnect.RST = true;
  141. //base.Reset();
  142. }
  143. #region Command Functions
  144. public void PerformRawCommand(string command, string completeEvent, string comandArgument)
  145. {
  146. lock (_locker)
  147. {
  148. _lstHandler.AddLast(new SkySGMPumpRawCommandHandler(this, command, completeEvent));
  149. }
  150. }
  151. public void Start()
  152. {
  153. lock (_locker)
  154. {
  155. _lstHandler.AddLast(new SkySGMPumpStartHandler(this));
  156. }
  157. }
  158. public void Stop()
  159. {
  160. lock (_locker)
  161. {
  162. _lstHandler.AddLast(new SkySGMPumpStopHandler(this));
  163. }
  164. }
  165. internal void NoteStart(bool value)
  166. {
  167. PumpStarted = value;
  168. }
  169. public void MonitorRawCommand(bool isSelected, string command, string completeEvent, string parameter)
  170. {
  171. lock (_locker)
  172. {
  173. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(SkySGMPumpRawCommandHandler) && ((SkySGMPumpHandler)handler)._command == command);
  174. if (isSelected)
  175. {
  176. if (!existHandlers.Any())
  177. _lstMonitorHandler.AddFirst(new SkySGMPumpRawCommandHandler(this, command, completeEvent));
  178. }
  179. else
  180. {
  181. if (existHandlers.Any())
  182. {
  183. _lstMonitorHandler.Remove(existHandlers.First());
  184. }
  185. }
  186. }
  187. }
  188. public void ReadRunParameters(bool isSelected)
  189. {
  190. lock (_locker)
  191. {
  192. var existHandlers = _lstMonitorHandler.Where(handler => handler.GetType() == typeof(SkySGMPumpReadRunParaHandler));
  193. if (isSelected)
  194. {
  195. if (!existHandlers.Any())
  196. _lstMonitorHandler.AddFirst(new SkySGMPumpReadRunParaHandler(this));
  197. }
  198. else
  199. {
  200. if (existHandlers.Any())
  201. {
  202. _lstMonitorHandler.Remove(existHandlers.First());
  203. }
  204. }
  205. }
  206. }
  207. #endregion
  208. #region Properties
  209. public string Error { get; private set; }
  210. public bool PumpStarted { get; private set; }
  211. public string FDP_Current { get; private set; }
  212. public string ROOTS_Current { get; private set; }
  213. public string FDP_Temp { get; private set; }
  214. public string ROOTS_Temp { get; private set; }
  215. public string N2_Flow { get; private set; }
  216. public string Pressure { get; private set; }
  217. public string RunTime { get; private set; }
  218. public string ErrSta1 { get; private set; }
  219. public string ErrSta2 { get; private set; }
  220. public string RunSta1 { get; private set; }
  221. public string RunSta2 { get; private set; }
  222. public string ErrSta3 { get; private set; }
  223. internal void NoteRunPara(string data)
  224. {
  225. var str = data.ToArray();
  226. FDP_Current = data.Substring(11, 2);
  227. ROOTS_Current = data.Substring(13, 2);
  228. FDP_Temp = data.Substring(15, 3);
  229. ROOTS_Temp = data.Substring(18, 3);
  230. N2_Flow = data.Substring(21, 2);
  231. Pressure = data.Substring(23, 4);
  232. RunTime = data.Substring(27, 5);
  233. ErrSta1 = data.Substring(32, 1);
  234. ErrSta2 = data.Substring(33, 1);
  235. RunSta1 = data.Substring(34, 1);
  236. RunSta2 = data.Substring(35, 1);
  237. ErrSta3 = data.Substring(36, 1);
  238. }
  239. #endregion
  240. #region Note Functions
  241. private R_TRIG _trigWarningMessage = new R_TRIG();
  242. public void NoteError(string reason)
  243. {
  244. if (reason != null)
  245. {
  246. _trigWarningMessage.CLK = true;
  247. if (_trigWarningMessage.Q)
  248. {
  249. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  250. }
  251. Error = reason;
  252. }
  253. else
  254. {
  255. Error = null;
  256. }
  257. }
  258. internal void NoteRawCommandInfo(string command, string data)
  259. {
  260. var curIOResponse = IOResponseList.Find(res => res.SourceCommandName == command);
  261. if (curIOResponse != null)
  262. {
  263. IOResponseList.Remove(curIOResponse);
  264. }
  265. IOResponseList.Add(new IOResponse() { SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
  266. }
  267. //public void Terminate()
  268. //{
  269. // throw new NotImplementedException();
  270. //}
  271. #endregion
  272. }
  273. }