HanbellPump.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using Aitex.Core.Common.DeviceData;
  6. using Aitex.Core.RT.Device;
  7. using Aitex.Core.RT.Device.Unit;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.RT.OperationCenter;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.Util;
  13. using MECF.Framework.Common.Communications;
  14. using MECF.Framework.Common.Device.Bases;
  15. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Common;
  16. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK;
  17. using Newtonsoft.Json;
  18. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.Hanbell
  19. {
  20. public class HanbellPump : PumpBase, IConnection
  21. {
  22. public string Address { get { return _slaveID; } }
  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 HanbellPumpTCPConnection _connection;
  34. public HanbellPumpTCPConnection 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 = "Pump",
  49. };
  50. return data;
  51. }
  52. }
  53. private R_TRIG _trigError = new R_TRIG();
  54. private R_TRIG _trigWarningMessage = new R_TRIG();
  55. private R_TRIG _trigCommunicationError = new R_TRIG();
  56. private R_TRIG _trigRetryConnect = new R_TRIG();
  57. private PeriodicJob _thread;
  58. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  59. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  60. public List<IOResponse> IOResponseList { get; set; } = new List<IOResponse>();
  61. private object _locker = new object();
  62. private bool _enableLog;
  63. private string _scRoot;
  64. private string _slaveID = "158";
  65. public HanbellPump(string module, string name, string scRoot) : base(module, name)
  66. {
  67. _scRoot = scRoot;
  68. }
  69. private void ResetPropertiesAndResponses()
  70. {
  71. foreach (var ioResponse in IOResponseList)
  72. {
  73. ioResponse.ResonseContent = null;
  74. ioResponse.ResonseRecievedTime = DateTime.Now;
  75. }
  76. }
  77. public override bool Initialize()
  78. {
  79. base.Initialize();
  80. ResetPropertiesAndResponses();
  81. if (_connection != null && _connection.IsConnected)
  82. return true;
  83. string deviceIP = "";
  84. if (!string.IsNullOrEmpty(_scRoot))
  85. {
  86. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
  87. deviceIP = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.DeviceAddress");
  88. }
  89. else
  90. {
  91. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  92. deviceIP = SC.GetStringValue($"{Module}.{Name}.DeviceAddress");
  93. }
  94. _connection = new HanbellPumpTCPConnection(deviceIP);
  95. _connection.EnableLog(_enableLog);
  96. if (_connection.Connect())
  97. {
  98. PortStatus = "Open";
  99. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  100. }
  101. else
  102. {
  103. PortStatus = "Close";
  104. EV.PostInfoLog(Module, $"{Module}.{Name} connect failed");
  105. //return false;
  106. }
  107. System.Threading.Thread.Sleep(500);
  108. _thread = new PeriodicJob(1000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  109. //RequestRegisters(true, 2201, 45);
  110. _lstMonitorHandler.AddFirst(new HanbellPumpRequestRegistersHandler(this, byte.Parse(_slaveID), 2201, 45));
  111. return true;
  112. }
  113. private bool OnTimer()
  114. {
  115. try
  116. {
  117. //return true;
  118. _connection.MonitorTimeout();
  119. if (!_connection.IsConnected || _connection.IsCommunicationError)
  120. {
  121. lock (_locker)
  122. {
  123. _lstHandler.Clear();
  124. }
  125. _trigRetryConnect.CLK = !_connection.IsConnected;
  126. if (_trigRetryConnect.Q)
  127. {
  128. //_connection.SetPortAddress(SC.GetStringValue($"{ScBasePath}.{Name}.Address"));
  129. if (!_connection.Connect())
  130. {
  131. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  132. }
  133. else
  134. {
  135. //_lstHandler.AddLast(new HanbellPumpQueryPinHandler(this, _deviceAddress));
  136. //_lstHandler.AddLast(new HanbellPumpSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  137. }
  138. }
  139. return true;
  140. }
  141. HandlerBase handler = null;
  142. if (!_connection.IsBusy)
  143. {
  144. lock (_locker)
  145. {
  146. if (_lstHandler.Count == 0)
  147. {
  148. foreach (var monitorHandler in _lstMonitorHandler)
  149. {
  150. _lstHandler.AddLast(monitorHandler);
  151. }
  152. }
  153. if (_lstHandler.Count > 0)
  154. {
  155. handler = _lstHandler.First.Value;
  156. _lstHandler.RemoveFirst();
  157. }
  158. }
  159. if (handler != null)
  160. {
  161. _connection.Execute(handler);
  162. }
  163. }
  164. }
  165. catch (Exception ex)
  166. {
  167. LOG.Write(ex);
  168. }
  169. return true;
  170. }
  171. internal void NoteDPCurrentHigh(int value)
  172. {
  173. DPCurrentHigh = value == 1 ? "true" : "false";
  174. }
  175. internal void NoteBPCurrentHigh(int value)
  176. {
  177. BPCurrentHigh = value == 1 ? "true" : "false";
  178. }
  179. internal void NoteDPPower(int power)
  180. {
  181. DPPower = power;
  182. }
  183. internal void NoteBPPower(int power)
  184. {
  185. BPPower = power;
  186. }
  187. internal void NoteDPSpeed(int speed)
  188. {
  189. DPSpeed = speed;
  190. }
  191. internal void NoteBPSpeed(int speed)
  192. {
  193. BPSpeed = speed;
  194. }
  195. public override void Monitor()
  196. {
  197. try
  198. {
  199. //_connection.EnableLog(_enableLog);
  200. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  201. if (_trigCommunicationError.Q)
  202. {
  203. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. LOG.Write(ex);
  209. }
  210. }
  211. internal void NoteVaccumPumpCompleted(bool isOn)
  212. {
  213. if (isOn)
  214. {
  215. VaccumPumpOnCompleted = true;
  216. }
  217. else
  218. {
  219. VaccumPumpOffCompleted = true;
  220. }
  221. }
  222. internal void NoteBPPumpOnOff(bool isOn)
  223. {
  224. if (isOn)
  225. {
  226. BPPumpOnCompleted = true;
  227. }
  228. else
  229. {
  230. BPPumpOffCompleted = true;
  231. }
  232. }
  233. public override void Reset()
  234. {
  235. _trigError.RST = true;
  236. _connection.SetCommunicationError(false, "");
  237. _trigCommunicationError.RST = true;
  238. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  239. _trigRetryConnect.RST = true;
  240. IsError = false;
  241. base.Reset();
  242. }
  243. #region Command Functions
  244. public void PerformRawCommand(string command, string comandArgument)
  245. {
  246. lock (_locker)
  247. {
  248. _lstHandler.AddLast(new HanbellPumpRawCommandHandler(this, byte.Parse(_slaveID), command, comandArgument));
  249. }
  250. }
  251. public override void SetPumpOnOff(bool isOn)
  252. {
  253. if (isOn)
  254. StartVaccumPump();
  255. else
  256. StopVaccumPump();
  257. }
  258. public void StartVaccumPump()
  259. {
  260. lock (_locker)
  261. {
  262. _lstHandler.AddFirst(new HanbellPumpStartVaccumPumpHandler(this, byte.Parse(_slaveID)));
  263. }
  264. }
  265. public void StopVaccumPump()
  266. {
  267. lock (_locker)
  268. {
  269. _lstHandler.AddFirst(new HanbellPumpStopVaccumPumpHandler(this, byte.Parse(_slaveID)));
  270. }
  271. }
  272. public void StartBPPump()
  273. {
  274. lock (_locker)
  275. {
  276. _lstHandler.AddFirst(new HanbellPumpStartBPPumpHandler(this, byte.Parse(_slaveID)));
  277. }
  278. }
  279. public void StopBPPump()
  280. {
  281. lock (_locker)
  282. {
  283. _lstHandler.AddFirst(new HanbellPumpStopBPPumpHandler(this, byte.Parse(_slaveID)));
  284. }
  285. }
  286. public void ResetPump()
  287. {
  288. lock (_locker)
  289. {
  290. _lstHandler.AddFirst(new HanbellPumpResetHandler(this, byte.Parse(_slaveID)));
  291. }
  292. }
  293. #endregion
  294. #region Properties
  295. public string CommunicationMode { get; private set; }
  296. public int DPPower { get; private set; }
  297. public int BPPower { get; private set; }
  298. public int DPSpeed { get; private set; }
  299. public int BPSpeed { get; private set; }
  300. public string BPCurrentHigh { get; private set; }
  301. public string DPCurrentHigh { get; private set; }
  302. public override bool IsOn { get; set; }
  303. public bool VaccumPumpOnCompleted { get; private set; }
  304. public bool VaccumPumpOffCompleted { get; private set; }
  305. public bool BPPumpOnCompleted { get; private set; }
  306. public bool BPPumpOffCompleted { get; private set; }
  307. #endregion
  308. #region Note Functions
  309. internal void NoteRawCommandInfo(string command, string data)
  310. {
  311. var curIOResponse = IOResponseList.Find(res => res.SourceCommand == command);
  312. if (curIOResponse != null)
  313. {
  314. IOResponseList.Remove(curIOResponse);
  315. }
  316. IOResponseList.Add(new IOResponse() { SourceCommand = command, ResonseContent = data, ResonseRecievedTime = DateTime.Now });
  317. }
  318. public void NoteError(string reason)
  319. {
  320. if (reason != null)
  321. {
  322. _trigWarningMessage.CLK = true;
  323. if (_trigWarningMessage.Q)
  324. {
  325. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  326. }
  327. IsError = true;
  328. }
  329. }
  330. #endregion
  331. }
  332. }