EdwardsPump.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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.EdwardsPump
  19. {
  20. public class EdwardsPump : PumpBase, IConnection
  21. {
  22. public string Address => Connection.Address;
  23. public bool IsConnected => Connection.IsConnected && !_connection.IsCommunicationError;
  24. public bool Connect()
  25. {
  26. return _connection.Connect();
  27. }
  28. public bool Disconnect()
  29. {
  30. return _connection.Disconnect();
  31. }
  32. public string PortStatus { get; set; } = "Closed";
  33. private EdwardsPumpConnection _connection;
  34. public EdwardsPumpConnection Connection
  35. {
  36. get { return _connection; }
  37. }
  38. public override AITPumpData DeviceData
  39. {
  40. get
  41. {
  42. AITPumpData data = new AITPumpData()
  43. {
  44. IsOn = IsOn,
  45. IsError = IsError,
  46. OverTemp = IsOverTemperature,
  47. DeviceModule = Module,
  48. DeviceName = Name,
  49. };
  50. return data;
  51. }
  52. }
  53. private R_TRIG _trigError = new R_TRIG();
  54. private R_TRIG _trigCommunicationError = new R_TRIG();
  55. private R_TRIG _trigRetryConnect = new R_TRIG();
  56. private PeriodicJob _thread;
  57. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  58. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  59. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  60. private object _locker = new object();
  61. private bool _enableLog;
  62. private string _portName;
  63. private string _scRoot;
  64. public EdwardsPump(string module, string name, string scRoot) : base(module, name)
  65. {
  66. _scRoot = scRoot;
  67. }
  68. private void ResetPropertiesAndResponses()
  69. {
  70. foreach (var ioResponse in IOResponseList)
  71. {
  72. ioResponse.ResonseContent = null;
  73. ioResponse.ResonseRecievedTime = DateTime.Now;
  74. }
  75. }
  76. public override bool Initialize()
  77. {
  78. base.Initialize();
  79. ResetPropertiesAndResponses();
  80. if (_connection != null && _connection.IsConnected)
  81. return true;
  82. if (_connection != null && _connection.IsConnected)
  83. _connection.Disconnect();
  84. _portName = SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.DeviceAddress");
  85. _enableLog = SC.GetValue<bool>($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.EnableLogMessage");
  86. _connection = new EdwardsPumpConnection(_portName);
  87. _connection.EnableLog(_enableLog);
  88. if (_connection.Connect())
  89. {
  90. PortStatus = "Open";
  91. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  92. }
  93. _thread = new PeriodicJob(1000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  94. lock (_locker)
  95. {
  96. _lstHandler.AddLast(new EdwardsGetControlHandler(this));
  97. _lstHandler.AddLast(new EdwardsFormatModeHandler(this, true));
  98. }
  99. _lstMonitorHandler.AddLast(new EdwardsQueryPumpStatusHandler(this));
  100. return true;
  101. }
  102. internal void NoteSetParaCompleted()
  103. {
  104. }
  105. private bool OnTimer()
  106. {
  107. try
  108. {
  109. //_connection.MonitorTimeout();
  110. if (!_connection.IsConnected || _connection.IsCommunicationError)
  111. {
  112. lock (_locker)
  113. {
  114. _lstHandler.Clear();
  115. }
  116. _trigRetryConnect.CLK = !_connection.IsConnected;
  117. if (_trigRetryConnect.Q)
  118. {
  119. _connection.SetPortAddress(_portName);
  120. if (!_connection.Connect())
  121. {
  122. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  123. }
  124. else
  125. {
  126. lock (_locker)
  127. {
  128. _lstHandler.AddLast(new EdwardsGetControlHandler(this));
  129. _lstHandler.AddLast(new EdwardsFormatModeHandler(this, true));
  130. }
  131. }
  132. }
  133. return true;
  134. }
  135. HandlerBase handler = null;
  136. if (!_connection.IsBusy)
  137. {
  138. lock (_locker)
  139. {
  140. if (_lstHandler.Count == 0)
  141. {
  142. foreach (var monitorHandler in _lstMonitorHandler)
  143. {
  144. _lstHandler.AddLast(monitorHandler);
  145. }
  146. }
  147. if (_lstHandler.Count > 0)
  148. {
  149. handler = _lstHandler.First.Value;
  150. _lstHandler.RemoveFirst();
  151. }
  152. }
  153. if (handler != null)
  154. {
  155. _connection.Execute(handler);
  156. }
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. LOG.Write(ex);
  162. }
  163. return true;
  164. }
  165. public override void Monitor()
  166. {
  167. try
  168. {
  169. //_connection.EnableLog(_enableLog);
  170. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  171. if (_trigCommunicationError.Q)
  172. {
  173. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. LOG.Write(ex);
  179. }
  180. }
  181. public override void Reset()
  182. {
  183. _trigError.RST = true;
  184. _connection.SetCommunicationError(false, "");
  185. _trigCommunicationError.RST = true;
  186. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  187. _trigRetryConnect.RST = true;
  188. base.Reset();
  189. }
  190. #region Command Functions
  191. internal void NoteSwitchCompleted()
  192. {
  193. ActionCommandCompleted = true;
  194. }
  195. public override void SetPumpOnOff(bool isOn)
  196. {
  197. lock (_locker)
  198. {
  199. if(isOn)
  200. _lstHandler.AddLast(new EdwardsSwitchOnPumpHandler(this));
  201. else
  202. _lstHandler.AddLast(new EdwardsSwitchOffPumpHandler(this));
  203. }
  204. }
  205. #endregion
  206. #region Properties
  207. public string Error { get; private set; }
  208. public bool ActionCommandCompleted { get; private set; }
  209. private string _systemType;
  210. //private Dictionary<string, Dictionary<string, ParaResponse>> AlarmStatusDict = new Dictionary<string, Dictionary<string, ParaResponse>>();
  211. private Dictionary<int, ParaResponse> AlarmStatusDict = new Dictionary<int, ParaResponse>();
  212. private Dictionary<int, ParaResponse> BitfieldStatusDict = new Dictionary<int, ParaResponse>();
  213. private Dictionary<int, ParaResponse> ParaValueDict = new Dictionary<int, ParaResponse>();
  214. public ParaResponse GetAlarmStatus(int parameter)
  215. {
  216. if (AlarmStatusDict.ContainsKey(parameter))
  217. return AlarmStatusDict[parameter];
  218. else
  219. return null;
  220. }
  221. internal void NoteAlarmStatus(string parameter, string data)
  222. {
  223. try
  224. {
  225. int iPara = int.Parse(parameter);
  226. if (AlarmStatusDict.ContainsKey(iPara))
  227. {
  228. AlarmStatusDict[iPara] = new ParaResponse(parameter);
  229. }
  230. else
  231. {
  232. AlarmStatusDict.Add(iPara, new ParaResponse(data));
  233. }
  234. }
  235. catch (Exception ex)
  236. {
  237. NoteError("Invalid Alarm Status");
  238. LOG.Error($"Invalid Alarm Status: {data}, Exception info: {ex}");
  239. }
  240. }
  241. private int Str2Int(string sValue)
  242. {
  243. if (string.IsNullOrEmpty(sValue))
  244. throw new Exception("empty int value");
  245. if(sValue.Length > 1)
  246. {
  247. sValue = sValue.TrimStart('0');
  248. }
  249. return int.Parse(sValue);
  250. }
  251. #endregion
  252. #region Note Functions
  253. private R_TRIG _trigWarningMessage = new R_TRIG();
  254. public void NoteError(string reason)
  255. {
  256. if (reason != null)
  257. {
  258. _trigWarningMessage.CLK = true;
  259. if (_trigWarningMessage.Q)
  260. {
  261. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  262. }
  263. Error = reason;
  264. }
  265. else
  266. {
  267. Error = null;
  268. }
  269. }
  270. internal void NoteRawCommandInfo(string command, string data)
  271. {
  272. var curIOResponse = IOResponseList.Find(res => res.SourceCommand == command);
  273. if (curIOResponse != null)
  274. {
  275. IOResponseList.Remove(curIOResponse);
  276. }
  277. IOResponseList.Add(new IOResponse() { SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
  278. }
  279. #endregion
  280. }
  281. public enum PumpRunStatus
  282. {
  283. Stopped,
  284. Running
  285. }
  286. public enum ValveStatus
  287. {
  288. Closed,
  289. Opened
  290. }
  291. public enum PumpLLStatus
  292. {
  293. Off,
  294. On
  295. }
  296. public enum ControlMode
  297. {
  298. Local,
  299. Remote
  300. }
  301. public enum DefectResult
  302. {
  303. OK,
  304. Warning,
  305. Hazard
  306. }
  307. public class ParaResponse
  308. {
  309. public ParaResponse(string paraRes)
  310. {
  311. var paraArray = paraRes.Split(',');
  312. priorityLevel = int.Parse(paraArray[0]);
  313. alarmType = int.Parse(paraArray[1]);
  314. bitfiledStatus = int.Parse(paraArray[2]);
  315. }
  316. public int priorityLevel;
  317. public int alarmType;
  318. public int bitfiledStatus;
  319. }
  320. }