SmcHRSChiller.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.OperationCenter;
  5. using Aitex.Core.RT.SCCore;
  6. using Aitex.Core.Util;
  7. using MECF.Framework.Common.CommonData.DeviceData;
  8. using MECF.Framework.Common.Communications;
  9. using MECF.Framework.Common.Device.Bases;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO.Ports;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Chillers.SmcHRSChillers
  17. {
  18. [Serializable]
  19. public class SmcHRSChiller : ChillerBase, IConnection
  20. {
  21. public string Address => Connection?.Address;
  22. public override bool IsConnected => Connection != null && Connection.IsConnected && !_connection.IsCommunicationError;
  23. public bool Connect()
  24. {
  25. return _connection.Connect();
  26. }
  27. public bool Disconnect()
  28. {
  29. return _connection.Disconnect();
  30. }
  31. public byte SlaveAddress { get; private set; } = 0x01;
  32. public string PortStatus { get; set; } = "Closed";
  33. private SmcHRSChillerConnection _connection;
  34. public SmcHRSChillerConnection Connection
  35. {
  36. get { return _connection; }
  37. }
  38. public override AITChillerData1 DeviceData
  39. {
  40. get
  41. {
  42. return new AITChillerData1()
  43. {
  44. Module = Module,
  45. DeviceName = Name,
  46. DisplayName = Name,
  47. IsCH1On = IsCH1On,
  48. IsCH2On = IsCH2On,
  49. IsCH1Alarm = IsCH1Alarm,
  50. IsCH2Alarm = IsCH2Alarm,
  51. CH1Temperature = CH1TemperatureFeedback,
  52. CH2Temperature = CH2TemperatureFeedback,
  53. CH1TemperatureSetPoint = CH1TemperatureSetpoint,
  54. CH2TemperatureSetPoint = CH2TemperatureSetpoint,
  55. CH1WaterFlow = CH1WaterFlow,
  56. CH2WaterFlow = CH2WaterFlow,
  57. FormatString = "f1",
  58. TemperatureHighLimit = TemperatureHighLimit,
  59. TemperatureLowLimit = TemperatureLowLimit,
  60. };
  61. }
  62. }
  63. public override float TemperatureHighLimit => _scTemperatureMaxValue == null ? 0 : (float)_scTemperatureMaxValue.DoubleValue;
  64. public override float TemperatureLowLimit => _scTemperatureMinValue == null ? 0 : (float)_scTemperatureMinValue.DoubleValue;
  65. private R_TRIG _trigError = new R_TRIG();
  66. private R_TRIG _trigCommunicationError = new R_TRIG();
  67. private R_TRIG _trigRetryConnect = new R_TRIG();
  68. private PeriodicJob _thread;
  69. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  70. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  71. private DeviceTimer _QueryTimer = new DeviceTimer();
  72. private readonly int _QueryInterval = 2000;
  73. private object _locker = new object();
  74. private bool _enableLog;
  75. private string _address;
  76. private string _scRoot;
  77. private SCConfigItem _scTemperatureMaxValue;
  78. private SCConfigItem _scTemperatureMinValue;
  79. private R_TRIG[] _trigAlarm = new R_TRIG[32];
  80. public SmcHRSChiller(string module, string name, string scRoot) : base()
  81. {
  82. _scRoot = scRoot;
  83. base.Module = module;
  84. base.Name = name;
  85. for (int i = 0; i < _trigAlarm.Length; i++)
  86. {
  87. _trigAlarm[i] = new R_TRIG();
  88. }
  89. }
  90. public override bool Initialize()
  91. {
  92. base.Initialize();
  93. _scTemperatureMaxValue = SC.GetConfigItem($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.TemperatureMaxValue");
  94. _scTemperatureMinValue = SC.GetConfigItem($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.TemperatureMinValue");
  95. _address = SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address");
  96. _enableLog = SC.GetValue<bool>($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.EnableLogMessage");
  97. _connection = new SmcHRSChillerConnection(_address);
  98. _connection.EnableLog(_enableLog);
  99. if (_connection.Connect())
  100. {
  101. PortStatus = "Open";
  102. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  103. }
  104. //_lstMonitorHandler.AddLast(new CommetRFMatchGetActualCapHandler(this));
  105. _thread = new PeriodicJob(2000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  106. _QueryTimer.Start(_QueryInterval);
  107. _lstMonitorHandler.AddLast(new SmcHRSChillerGetStatusHandler(this));
  108. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
  109. DATA.Subscribe($"{Module}.{Name}.Address", () => Address);
  110. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  111. {
  112. Disconnect();
  113. Connect();
  114. return true;
  115. });
  116. return true;
  117. }
  118. public bool Initialize(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  119. {
  120. base.Initialize();
  121. //_scTemperatureMaxValue = SC.GetConfigItem($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.TemperatureMaxValue");
  122. //_scTemperatureMinValue = SC.GetConfigItem($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.TemperatureMinValue");
  123. //_address = SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address");
  124. _enableLog = true;// SC.GetValue<bool>($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.EnableLogMessage");
  125. _connection = new SmcHRSChillerConnection(portName, baudRate, dataBits, parity, stopBits);
  126. _connection.EnableLog(_enableLog);
  127. if (_connection.Connect())
  128. {
  129. PortStatus = "Open";
  130. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  131. }
  132. _thread = new PeriodicJob(1000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  133. _QueryTimer.Start(_QueryInterval);
  134. //_lstMonitorHandler.AddLast(new SmcHRZChillerGetStatusHandler(this));
  135. return true;
  136. }
  137. private bool OnTimer()
  138. {
  139. try
  140. {
  141. //_connection.MonitorTimeout();
  142. if (!_connection.IsConnected || _connection.IsCommunicationError)
  143. {
  144. lock (_locker)
  145. {
  146. _lstHandler.Clear();
  147. }
  148. _trigRetryConnect.CLK = !_connection.IsConnected;
  149. if (_trigRetryConnect.Q)
  150. {
  151. _connection.SetPortAddress(SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address"));
  152. if (!_connection.Connect())
  153. {
  154. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  155. }
  156. else
  157. {
  158. //_lstHandler.AddLast(new SmcHRZChillerQueryPinHandler(this, _deviceAddress));
  159. //_lstHandler.AddLast(new SmcHRZChillerSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  160. }
  161. }
  162. return true;
  163. }
  164. HandlerBase handler = null;
  165. if (!_connection.IsBusy)
  166. {
  167. lock (_locker)
  168. {
  169. if (_lstHandler.Count == 0)
  170. {
  171. foreach (var monitorHandler in _lstMonitorHandler)
  172. {
  173. _lstHandler.AddLast(monitorHandler);
  174. }
  175. }
  176. if (_lstHandler.Count > 0)
  177. {
  178. handler = _lstHandler.First.Value;
  179. _lstHandler.RemoveFirst();
  180. }
  181. }
  182. if (handler != null)
  183. {
  184. _connection.Execute(handler);
  185. }
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. LOG.Write(ex);
  191. }
  192. return true;
  193. }
  194. public override void Monitor()
  195. {
  196. try
  197. {
  198. _connection.EnableLog(_enableLog);
  199. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  200. if (_trigCommunicationError.Q)
  201. {
  202. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. LOG.Write(ex);
  208. }
  209. }
  210. internal void NoteIsWarning(bool isWarning)
  211. {
  212. IsCH1Warning = isWarning;
  213. }
  214. internal void NotePressUnit(bool pressUnit)
  215. {
  216. }
  217. internal void NoteIsRemote(bool isRemote)
  218. {
  219. IsCH1Remote = isRemote;
  220. }
  221. internal void NoteIsTempReady(bool isTempReady)
  222. {
  223. }
  224. internal void NoteIsTimeout(bool isTimeout)
  225. {
  226. }
  227. internal void NoteFlowUnit(bool flowUnit)
  228. {
  229. }
  230. internal void NoteIsFault(bool isFault)
  231. {
  232. if (IsCH1Alarm != isFault)
  233. {
  234. IsCH1Alarm = isFault;
  235. }
  236. }
  237. internal void NoteIsRun(bool isRun)
  238. {
  239. if (IsCH1On != isRun)
  240. {
  241. IsCH1On = isRun;
  242. }
  243. }
  244. public override void Reset()
  245. {
  246. _trigError.RST = true;
  247. _connection.SetCommunicationError(false, "");
  248. _trigCommunicationError.RST = true;
  249. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  250. _trigRetryConnect.RST = true;
  251. for (int i = 0; i < _trigAlarm.Length; i++)
  252. {
  253. _trigAlarm[i].RST = true;
  254. }
  255. base.Reset();
  256. }
  257. #region Command Functions
  258. public void SetTemperature(string angle)
  259. {
  260. lock (_locker)
  261. {
  262. //_lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWAA", angle));
  263. }
  264. }
  265. public void SetTempHighWarning(string angle)
  266. {
  267. lock (_locker)
  268. {
  269. //_lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWAA", angle));
  270. }
  271. }
  272. public void SetTempLowWarning(string angle)
  273. {
  274. lock (_locker)
  275. {
  276. //_lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWAA", angle));
  277. }
  278. }
  279. public void SetRemoteMode(string angle)
  280. {
  281. lock (_locker)
  282. {
  283. //_lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWS", angle));
  284. }
  285. }
  286. public void SetRunMode(string waferSize)
  287. {
  288. lock (_locker)
  289. {
  290. // _lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWS", waferSize));
  291. }
  292. }
  293. public void QueryAllTemp()
  294. {
  295. lock (_locker)
  296. {
  297. _lstHandler.AddLast(new SmcHRSChillerGetStatusHandler(this));
  298. }
  299. }
  300. public override void SetChillerCH1OnOff(bool isOn)
  301. {
  302. lock (_locker)
  303. {
  304. _lstHandler.AddLast(new SmcHRSChillerSetOnOffHandler(this, isOn));
  305. }
  306. }
  307. public override void SetChillerCH2OnOff(bool isOn)
  308. {
  309. lock (_locker)
  310. {
  311. _lstHandler.AddLast(new SmcHRSChillerSetOnOffHandler(this, isOn));
  312. }
  313. }
  314. public override void SetChillerCH1Temperature(float temp)
  315. {
  316. lock (_locker)
  317. {
  318. _lstHandler.AddLast(new SmcHRSChillerSetTemperatureHandler(this, temp));
  319. }
  320. }
  321. public override void SetChillerCH2Temperature(float temp)
  322. {
  323. lock (_locker)
  324. {
  325. _lstHandler.AddLast(new SmcHRSChillerSetTemperatureHandler(this, temp));
  326. }
  327. }
  328. #endregion
  329. #region Properties
  330. public string Error { get; private set; }
  331. #endregion
  332. #region Note Functions
  333. private R_TRIG _trigWarningMessage = new R_TRIG();
  334. public void NoteError(string reason)
  335. {
  336. if (!string.IsNullOrEmpty(reason))
  337. {
  338. _trigWarningMessage.CLK = true;
  339. if (_trigWarningMessage.Q)
  340. {
  341. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  342. }
  343. Error = reason;
  344. }
  345. else
  346. {
  347. Error = null;
  348. }
  349. }
  350. public void NoteTemp(float value)
  351. {
  352. CH1TemperatureFeedback = value;
  353. }
  354. public void NoteTempSetpoint(float value)
  355. {
  356. CH1TemperatureSetpoint = value;
  357. }
  358. public void NotePressure(float value)
  359. {
  360. }
  361. public void NoteErrorFlag(int index, string error)
  362. {
  363. if (!string.IsNullOrEmpty(error))
  364. {
  365. _trigAlarm[index].CLK = true;
  366. if (_trigAlarm[index].Q)
  367. {
  368. EV.PostWarningLog(Module, $"{Module}.{Name} error, {error}");
  369. }
  370. Error = error;
  371. }
  372. else
  373. {
  374. Error = null;
  375. }
  376. }
  377. #endregion
  378. }
  379. }