KaimeiRFMatch.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using Aitex.Core.Common.DeviceData;
  2. using Aitex.Core.RT.DataCenter;
  3. using Aitex.Core.RT.Event;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Communications;
  9. using MECF.Framework.Common.Device.Bases;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.RFMatchs.Kaimei
  16. {
  17. public class KaimeiRFMatch : RfMatchBase, IConnection
  18. {
  19. public string Address => Connection.Address;
  20. public new bool IsConnected => Connection.IsConnected && !_connection.IsCommunicationError;
  21. public bool Connect()
  22. {
  23. return _connection.Connect();
  24. }
  25. public bool Disconnect()
  26. {
  27. return _connection.Disconnect();
  28. }
  29. public byte SlaveAddress { get; private set; } = 0x01;
  30. public string PortStatus { get; set; } = "Closed";
  31. private KaimeiRFMatchConnection _connection;
  32. public KaimeiRFMatchConnection Connection
  33. {
  34. get { return _connection; }
  35. }
  36. public override AITRfMatchData DeviceData
  37. {
  38. get
  39. {
  40. return new AITRfMatchData()
  41. {
  42. Module = Module,
  43. DeviceName = Name,
  44. DisplayName = Name,
  45. TuneMode1 = TuneMode1,
  46. TunePosition1 = TunePosition1,
  47. LoadPosition1 = LoadPosition1,
  48. LoadPosition1SetPoint = LoadPosition1Setpoint,
  49. TunePosition1SetPoint = TunePosition1Setpoint,
  50. TuneRange = (float)SC.GetValue<double>($"{Module}.{Name}.TuneRange"),
  51. LoadRange = (float)SC.GetValue<double>($"{Module}.{Name}.LoadRange"),
  52. IsInterlockOk = true,
  53. };
  54. }
  55. }
  56. private R_TRIG _trigError = new R_TRIG();
  57. private R_TRIG _trigCommunicationError = new R_TRIG();
  58. private R_TRIG _trigRetryConnect = new R_TRIG();
  59. private PeriodicJob _thread;
  60. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  61. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  62. private DeviceTimer _QueryTimer = new DeviceTimer();
  63. private readonly int _QueryInterval = 2000;
  64. private object _locker = new object();
  65. private bool _enableLog;
  66. private string _address;
  67. private string _scRoot;
  68. public KaimeiRFMatch(string module, string name, string scRoot) : base(module, name)
  69. {
  70. _scRoot = scRoot;
  71. }
  72. public override bool Initialize()
  73. {
  74. base.Initialize();
  75. _address = SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address");
  76. _enableLog = SC.GetValue<bool>($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.EnableLogMessage");
  77. _connection = new KaimeiRFMatchConnection(_address);
  78. _connection.EnableLog(_enableLog);
  79. if (_connection.Connect())
  80. {
  81. PortStatus = "Open";
  82. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  83. }
  84. //_lstMonitorHandler.AddLast(new CommetRFMatchGetActualCapHandler(this));
  85. _thread = new PeriodicJob(100, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  86. _QueryTimer.Start(_QueryInterval);
  87. _lstMonitorHandler.AddLast(new KaimeiRFMatchGetStatusHandler(this));
  88. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
  89. DATA.Subscribe($"{Module}.{Name}.Address", () => Address);
  90. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  91. {
  92. Disconnect();
  93. Connect();
  94. return true;
  95. });
  96. //for recipe
  97. OP.Subscribe($"{Module}.{Name}.SetLoad1", (out string reason, int time, object[] param) =>
  98. {
  99. reason = string.Empty;
  100. if (TuneMode1Setpoint != EnumRfMatchTuneMode.Auto)
  101. SetLoad1(Convert.ToSingle(param[0]));
  102. return true;
  103. });
  104. //for recipe
  105. OP.Subscribe($"{Module}.{Name}.SetTune1", (out string reason, int time, object[] param) =>
  106. {
  107. reason = string.Empty;
  108. if (TuneMode1Setpoint != EnumRfMatchTuneMode.Auto)
  109. SetTune1(Convert.ToSingle(param[0]));
  110. return true;
  111. });
  112. //for recipe
  113. OP.Subscribe($"{Module}.{Name}.SetTuneMode1", (out string reason, int time, object[] param) =>
  114. {
  115. reason = string.Empty;
  116. SetTuneMode1(param[0].ToString().ToUpper() == "AUTO" ? EnumRfMatchTuneMode.Auto : EnumRfMatchTuneMode.Manual);
  117. return true;
  118. });
  119. OP.Subscribe($"{Module}.{Name}.SavePreset", (function, args) =>
  120. {
  121. SavePreset();
  122. return true;
  123. });
  124. return true;
  125. }
  126. private bool OnTimer()
  127. {
  128. try
  129. {
  130. _connection.MonitorTimeout();
  131. if (!_connection.IsConnected || _connection.IsCommunicationError)
  132. {
  133. lock (_locker)
  134. {
  135. _lstHandler.Clear();
  136. }
  137. _trigRetryConnect.CLK = !_connection.IsConnected;
  138. if (_trigRetryConnect.Q)
  139. {
  140. _connection.SetPortAddress(SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address"));
  141. if (!_connection.Connect())
  142. {
  143. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  144. }
  145. else
  146. {
  147. //_lstHandler.AddLast(new CommetRFMatchQueryPinHandler(this, _deviceAddress));
  148. //_lstHandler.AddLast(new CommetRFMatchSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  149. }
  150. }
  151. return true;
  152. }
  153. HandlerBase handler = null;
  154. if (!_connection.IsBusy)
  155. {
  156. lock (_locker)
  157. {
  158. if (_lstHandler.Count == 0)
  159. {
  160. if (_QueryTimer.IsTimeout())
  161. {
  162. foreach (var monitorHandler in _lstMonitorHandler)
  163. {
  164. _lstHandler.AddLast(monitorHandler);
  165. }
  166. _QueryTimer.Start(_QueryInterval);
  167. }
  168. }
  169. if (_lstHandler.Count > 0)
  170. {
  171. handler = _lstHandler.First.Value;
  172. _lstHandler.RemoveFirst();
  173. }
  174. }
  175. if (handler != null)
  176. {
  177. _connection.Execute(handler);
  178. }
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. LOG.Write(ex);
  184. }
  185. return true;
  186. }
  187. public override void Monitor()
  188. {
  189. try
  190. {
  191. //_connection.EnableLog(_enableLog);
  192. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  193. if (_trigCommunicationError.Q)
  194. {
  195. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  196. }
  197. base.Monitor();
  198. }
  199. catch (Exception ex)
  200. {
  201. LOG.Write(ex);
  202. }
  203. }
  204. public override void Reset()
  205. {
  206. _trigError.RST = true;
  207. _connection.SetCommunicationError(false, "");
  208. _trigCommunicationError.RST = true;
  209. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  210. _trigRetryConnect.RST = true;
  211. base.Reset();
  212. }
  213. #region Command Functions
  214. internal void NoteActualCap(float value)
  215. {
  216. }
  217. public override void SetTuneMode1(EnumRfMatchTuneMode enumRfMatchTuneMode)
  218. {
  219. TuneMode1Setpoint = enumRfMatchTuneMode;
  220. //if (SC.ContainsItem("System.IsSimulatorMode") && SC.GetValue<bool>("System.IsSimulatorMode"))
  221. // TuneMode1 = TuneMode1Setpoint;
  222. lock (_locker)
  223. {
  224. _lstHandler.AddLast(new KaimeiRFMatchPresetValveHandler(this, TuneMode1Setpoint, TunePosition1Setpoint, LoadPosition1Setpoint));
  225. }
  226. }
  227. public override void SetLoad1(float load)
  228. {
  229. LoadPosition1Setpoint = load;
  230. //if (SC.ContainsItem("System.IsSimulatorMode") && SC.GetValue<bool>("System.IsSimulatorMode"))
  231. // LoadPosition1 = LoadPosition1Setpoint;
  232. lock (_locker)
  233. {
  234. _lstHandler.AddLast(new KaimeiRFMatchPresetValveHandler(this, TuneMode1Setpoint, TunePosition1Setpoint, LoadPosition1Setpoint));
  235. }
  236. }
  237. public override void SetTune1(float tune)
  238. {
  239. TunePosition1Setpoint = tune;
  240. //if (SC.ContainsItem("System.IsSimulatorMode") && SC.GetValue<bool>("System.IsSimulatorMode"))
  241. // TunePosition1 = TunePosition1Setpoint;
  242. lock (_locker)
  243. {
  244. _lstHandler.AddLast(new KaimeiRFMatchPresetValveHandler(this, TuneMode1Setpoint, TunePosition1Setpoint, LoadPosition1Setpoint));
  245. }
  246. }
  247. public void SavePreset()
  248. {
  249. lock (_locker)
  250. {
  251. _lstHandler.AddLast(new KaimeiRFMatchSetCurrentValveHandler(this));
  252. }
  253. }
  254. #endregion
  255. #region Properties
  256. public string Error { get; private set; }
  257. #endregion
  258. #region Note Functions
  259. private R_TRIG _trigWarningMessage = new R_TRIG();
  260. public void NoteError(string reason)
  261. {
  262. if (reason != null)
  263. {
  264. _trigWarningMessage.CLK = true;
  265. if (_trigWarningMessage.Q)
  266. {
  267. EV.PostAlarmLog(Module, $"{Module}.{Name} error, {reason}");
  268. }
  269. Error = reason;
  270. }
  271. else
  272. {
  273. Error = null;
  274. }
  275. }
  276. #endregion
  277. }
  278. }