SkyPump.cs 11 KB

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