DWPressure.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Device;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.RT.OperationCenter;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.Util;
  11. using MECF.Framework.Common.Communications;
  12. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.MayAir;
  13. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
  14. {
  15. public class DWPressure : BaseDevice, IConnection
  16. {
  17. private DWPressureConnection _connection;
  18. private bool _activeMonitorStatus;
  19. private int _errorCode;
  20. private R_TRIG _trigCommunicationError = new R_TRIG();
  21. private R_TRIG _trigRetryConnect = new R_TRIG();
  22. private PeriodicJob _thread;
  23. private int pressureCount = 1;
  24. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  25. private object _locker = new object();
  26. private bool _enableLog
  27. {
  28. get
  29. {
  30. if(SC.ContainsItem($"{_scRoot}.{Name}.EnableLogMessage"))
  31. return SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  32. return false;
  33. }
  34. }
  35. private string _scRoot;
  36. private int _pressureValue1 = 0;
  37. private int _pressureValue2 = 0;
  38. private int _pressureValue3 = 0;
  39. public int PressureValue1
  40. {
  41. get { return _pressureValue1; }
  42. set { _pressureValue1 = value; }
  43. }
  44. public int PressureValue2
  45. {
  46. get { return _pressureValue2; }
  47. set { _pressureValue2 = value; }
  48. }
  49. public int PressureValue3
  50. {
  51. get { return _pressureValue3; }
  52. set { _pressureValue3 = value; }
  53. }
  54. public string Address
  55. {
  56. get;set;
  57. }
  58. public bool IsConnected
  59. {
  60. get
  61. {
  62. return _connection != null && _connection.IsConnected;
  63. }
  64. }
  65. public bool Connect()
  66. {
  67. return true;
  68. }
  69. public bool Disconnect()
  70. {
  71. return true;
  72. }
  73. public DWPressure(string module, string name, string scRoot) : base(module, name, name, name)
  74. {
  75. _scRoot = scRoot;
  76. _activeMonitorStatus = true;
  77. }
  78. ~DWPressure()
  79. {
  80. _connection.Disconnect();
  81. }
  82. public DWPressure(string module, string name, string scRoot, int count) : base(module, name, name, name)
  83. {
  84. pressureCount = count;
  85. _scRoot = scRoot;
  86. _activeMonitorStatus = true;
  87. }
  88. public void QueryPressure()
  89. {
  90. if (pressureCount == 1)
  91. { _lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 1)); }
  92. if (pressureCount == 2)
  93. {
  94. _lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 1));
  95. _lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 3));
  96. }
  97. if (pressureCount == 3)
  98. {
  99. _lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 1));
  100. _lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 3));
  101. _lstHandler.AddLast(new DWPressureQueryPressureHandler(this, 5));
  102. }
  103. }
  104. public void ResetDevice()
  105. {
  106. }
  107. public void QueryError()
  108. {
  109. EV.PostInfoLog(Module, "Query error");
  110. }
  111. public bool Initialize()
  112. {
  113. //DATA.Subscribe($"{Module}.{Name}.Power", () => _power);
  114. //DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => _errorCode);
  115. //DATA.Subscribe($"{Module}.{Name}.IsAtSpeed", () => _isAtSpeed);
  116. //DATA.Subscribe($"{Module}.{Name}.IsAccelerate", () => _isAccelerate);
  117. string portName = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  118. Address = portName;
  119. int address = SC.GetValue<int>($"{_scRoot}.{Name}.DeviceAddress");
  120. _connection = new DWPressureConnection(portName);
  121. _connection.EnableLog(_enableLog);
  122. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 5;
  123. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
  124. if (sleep <= 0 || sleep > 10)
  125. sleep = 2;
  126. int retry = 0;
  127. do
  128. {
  129. _connection.Disconnect();
  130. Thread.Sleep(sleep * 1000);
  131. if (_connection.Connect())
  132. {
  133. EV.PostInfoLog(Module, $"{Name} connected");
  134. break;
  135. }
  136. if (count > 0 && retry++ > count)
  137. {
  138. LOG.Write($"Retry connect {Module}.{Name} stop retry.");
  139. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name}.");
  140. break;
  141. }
  142. Thread.Sleep(sleep * 1000);
  143. LOG.Write($"Retry connect {Module}.{Name} for the {retry + 1} time.");
  144. } while (true);
  145. _thread = new PeriodicJob(200, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  146. DATA.Subscribe($"{Name}.DiffPressure1", () => _pressureValue1);
  147. DATA.Subscribe($"{Name}.DiffPressure2", () => _pressureValue2);
  148. DATA.Subscribe($"{Name}.DiffPressure3", () => _pressureValue3);
  149. DATA.Subscribe($"{Name}.DiffPressureCount", () => pressureCount);
  150. ConnectionManager.Instance.Subscribe($"{Name}", this);
  151. return true;
  152. }
  153. private bool OnTimer()
  154. {
  155. try
  156. {
  157. _connection.EnableLog(_enableLog);
  158. _connection.MonitorTimeout();
  159. if (!_connection.IsConnected || _connection.IsCommunicationError)
  160. {
  161. lock (_locker)
  162. {
  163. _lstHandler.Clear();
  164. }
  165. _trigRetryConnect.CLK = !_connection.IsConnected;
  166. if (_trigRetryConnect.Q)
  167. {
  168. _connection.SetPortAddress(SC.GetStringValue($"{_scRoot}.{Name}.Address"));
  169. if (!_connection.Connect())
  170. {
  171. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  172. }
  173. }
  174. _connection.ForceClear();
  175. return true;
  176. }
  177. HandlerBase handler = null;
  178. //if (!_connection.IsBusy)
  179. //{
  180. lock (_locker)
  181. {
  182. if(_lstHandler.Count == 0)
  183. QueryPressure();
  184. if(_lstHandler.Count > 0 && !_connection.IsBusy)
  185. {
  186. handler = _lstHandler.First.Value;
  187. _lstHandler.RemoveFirst();
  188. if (handler != null)
  189. {
  190. _connection.Execute(handler);
  191. }
  192. }
  193. }
  194. //}
  195. }
  196. catch (Exception ex)
  197. {
  198. LOG.Write(ex);
  199. }
  200. return true;
  201. }
  202. internal void NoteError()
  203. {
  204. }
  205. public void Monitor()
  206. {
  207. try
  208. {
  209. _connection.EnableLog(_enableLog);
  210. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  211. if (_trigCommunicationError.Q)
  212. {
  213. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  214. }
  215. }
  216. catch (Exception ex)
  217. {
  218. LOG.Write(ex);
  219. }
  220. }
  221. public void Reset()
  222. {
  223. _connection.SetCommunicationError(false, "");
  224. _trigCommunicationError.RST = true;
  225. _connection.EnableLog(_enableLog);
  226. _trigRetryConnect.RST = true;
  227. }
  228. public void SetPumpOnOff(bool isOn)
  229. {
  230. lock (_locker)
  231. {
  232. }
  233. }
  234. public void SetActiveMonitor(bool active)
  235. {
  236. _activeMonitorStatus = active;
  237. }
  238. public void SetErrorCode(int errorCode)
  239. {
  240. _errorCode = errorCode;
  241. }
  242. public void Terminate()
  243. {
  244. _connection.Disconnect();
  245. }
  246. }
  247. }