FfuMayAir.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections.Generic;
  3. using Aitex.Core.RT.DataCenter;
  4. using Aitex.Core.RT.Device;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.Communications;
  10. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.MayAir
  11. {
  12. public class FfuMayAir : BaseDevice, IConnection
  13. {
  14. private FfuMayAirConnection _connection;
  15. private string _deviceAddress = "001";
  16. private bool _isOn;
  17. private bool _activeMonitorStatus;
  18. private int _errorCode;
  19. private RD_TRIG _trigPumpOnOff = new RD_TRIG();
  20. private R_TRIG _trigError = new R_TRIG();
  21. private R_TRIG _trigOverTemp = new R_TRIG();
  22. private R_TRIG _trigWarningMessage = new R_TRIG();
  23. private string _lastError = string.Empty;
  24. private R_TRIG _trigCommunicationError = new R_TRIG();
  25. private R_TRIG _trigRetryConnect = new R_TRIG();
  26. private PeriodicJob _thread;
  27. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  28. private object _locker = new object();
  29. private bool _enableLog = true;
  30. private string _scRoot;
  31. public string Address
  32. {
  33. get
  34. {
  35. return "";
  36. }
  37. }
  38. public bool IsConnected
  39. {
  40. get
  41. {
  42. return _connection!=null && _connection.IsConnected;
  43. }
  44. }
  45. public bool Connect()
  46. {
  47. return true;
  48. }
  49. public bool Disconnect()
  50. {
  51. return true;
  52. }
  53. public FfuMayAir(string module, string name, string scRoot) : base(module, name, name, name)
  54. {
  55. _scRoot = scRoot;
  56. _activeMonitorStatus = true;
  57. }
  58. public void QuerySpeed()
  59. {
  60. }
  61. public void ResetDevice()
  62. {
  63. }
  64. public void QueryError()
  65. {
  66. _lstHandler.AddLast(new FfuMayAirQueryStatusHandler(this, 1, 0));
  67. EV.PostInfoLog(Module, "Query error");
  68. }
  69. public bool Initialize()
  70. {
  71. //DATA.Subscribe($"{Module}.{Name}.Power", () => _power);
  72. //DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => _errorCode);
  73. //DATA.Subscribe($"{Module}.{Name}.IsAtSpeed", () => _isAtSpeed);
  74. //DATA.Subscribe($"{Module}.{Name}.IsAccelerate", () => _isAccelerate);
  75. string portName = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.Address");
  76. int address = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.DeviceAddress");
  77. _deviceAddress = address.ToString("D3");
  78. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
  79. _connection = new FfuMayAirConnection(portName);
  80. _connection.EnableLog(_enableLog);
  81. if (_connection.Connect())
  82. {
  83. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  84. }
  85. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  86. return true;
  87. }
  88. private bool OnTimer()
  89. {
  90. try
  91. {
  92. _connection.MonitorTimeout();
  93. if (!_connection.IsConnected || _connection.IsCommunicationError)
  94. {
  95. lock (_locker)
  96. {
  97. _lstHandler.Clear();
  98. }
  99. _trigRetryConnect.CLK = !_connection.IsConnected;
  100. if (_trigRetryConnect.Q)
  101. {
  102. _connection.SetPortAddress(SC.GetStringValue($"{Module}.{Name}.Address"));
  103. if (!_connection.Connect())
  104. {
  105. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  106. }
  107. }
  108. return true;
  109. }
  110. HandlerBase handler = null;
  111. if (!_connection.IsBusy)
  112. {
  113. lock (_locker)
  114. {
  115. if (_lstHandler.Count == 0 && _activeMonitorStatus)
  116. {
  117. }
  118. if (_lstHandler.Count > 0)
  119. {
  120. handler = _lstHandler.First.Value;
  121. _lstHandler.RemoveFirst();
  122. }
  123. }
  124. if (handler != null)
  125. {
  126. _connection.Execute(handler);
  127. }
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. LOG.Write(ex);
  133. }
  134. return true;
  135. }
  136. internal void NoteError()
  137. {
  138. }
  139. public void Monitor()
  140. {
  141. try
  142. {
  143. _connection.EnableLog(_enableLog);
  144. _trigPumpOnOff.CLK = _isOn;
  145. if (_trigPumpOnOff.R)
  146. {
  147. EV.PostInfoLog(Module, $"{Module}.{Name} is on");
  148. }
  149. if (_trigPumpOnOff.T)
  150. {
  151. EV.PostInfoLog(Module, $"{Module}.{Name} is off");
  152. }
  153. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  154. if (_trigCommunicationError.Q)
  155. {
  156. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. LOG.Write(ex);
  162. }
  163. }
  164. public void Reset()
  165. {
  166. _trigError.RST = true;
  167. _trigOverTemp.RST = true;
  168. _trigWarningMessage.RST = true;
  169. _connection.SetCommunicationError(false, "");
  170. _trigCommunicationError.RST = true;
  171. _enableLog = SC.GetValue<bool>($"{Module}.{Name}.EnableLogMessage");
  172. _trigRetryConnect.RST = true;
  173. }
  174. public void SetPumpOnOff(bool isOn)
  175. {
  176. lock (_locker)
  177. {
  178. }
  179. }
  180. public void SetActiveMonitor(bool active)
  181. {
  182. _activeMonitorStatus = active;
  183. }
  184. internal void NoteOnOff(bool isOn)
  185. {
  186. _isOn = isOn;
  187. }
  188. public void SetSpeed(float speed)
  189. {
  190. }
  191. public void SetErrorCode(int errorCode)
  192. {
  193. _errorCode = errorCode;
  194. }
  195. public void SetError(string reason)
  196. {
  197. _trigWarningMessage.CLK = true;
  198. if (_trigWarningMessage.Q)
  199. {
  200. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  201. }
  202. }
  203. }
  204. }