SmcHRZChiller.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.SmcHRZChillers
  17. {
  18. [Serializable]
  19. public class SmcHRZChiller : 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 SmcHRZChillerConnection _connection;
  34. public SmcHRZChillerConnection 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. private R_TRIG _trigError = new R_TRIG();
  64. private R_TRIG _trigCommunicationError = new R_TRIG();
  65. private R_TRIG _trigRetryConnect = new R_TRIG();
  66. private PeriodicJob _thread;
  67. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  68. private LinkedList<HandlerBase> _lstMonitorHandler = new LinkedList<HandlerBase>();
  69. private DeviceTimer _QueryTimer = new DeviceTimer();
  70. private readonly int _QueryInterval = 2000;
  71. private object _locker = new object();
  72. private bool _enableLog;
  73. private string _address;
  74. private string _scRoot;
  75. private R_TRIG[] _trigAlarm = new R_TRIG[32];
  76. public SmcHRZChiller(string module, string name, string scRoot) : base()
  77. {
  78. _scRoot = scRoot;
  79. base.Module = module;
  80. base.Name = name;
  81. for (int i = 0; i < _trigAlarm.Length; i++)
  82. {
  83. _trigAlarm[i] = new R_TRIG();
  84. }
  85. }
  86. public override bool Initialize()
  87. {
  88. base.Initialize();
  89. _address = SC.GetStringValue($"{_scRoot}.Address");
  90. _enableLog = SC.GetValue<bool>($"{_scRoot}.EnableLogMessage");
  91. _connection = new SmcHRZChillerConnection(_address);
  92. _connection.EnableLog(_enableLog);
  93. if (_connection.Connect())
  94. {
  95. PortStatus = "Open";
  96. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  97. }
  98. //_lstMonitorHandler.AddLast(new CommetRFMatchGetActualCapHandler(this));
  99. _thread = new PeriodicJob(2000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  100. _QueryTimer.Start(_QueryInterval);
  101. _lstMonitorHandler.AddLast(new SmcHRZChillerGetStatusHandler(this));
  102. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => IsConnected);
  103. DATA.Subscribe($"{Module}.{Name}.Address", () => Address);
  104. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  105. {
  106. Disconnect();
  107. Reset();
  108. Connect();
  109. return true;
  110. });
  111. return true;
  112. }
  113. public bool Initialize(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  114. {
  115. base.Initialize();
  116. //_scTemperatureMaxValue = SC.GetConfigItem($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.TemperatureMaxValue");
  117. //_scTemperatureMinValue = SC.GetConfigItem($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.TemperatureMinValue");
  118. //_address = SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address");
  119. _enableLog = true;// SC.GetValue<bool>($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.EnableLogMessage");
  120. _connection = new SmcHRZChillerConnection(portName, baudRate, dataBits, parity, stopBits);
  121. _connection.EnableLog(_enableLog);
  122. if (_connection.Connect())
  123. {
  124. PortStatus = "Open";
  125. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  126. }
  127. _thread = new PeriodicJob(2000, OnTimer, $"{Module}.{Name} MonitorHandler", true);
  128. _QueryTimer.Start(_QueryInterval);
  129. //_lstMonitorHandler.AddLast(new SmcHRZChillerGetStatusHandler(this));
  130. return true;
  131. }
  132. private bool OnTimer()
  133. {
  134. try
  135. {
  136. //_connection.MonitorTimeout();
  137. if (!_connection.IsConnected || _connection.IsCommunicationError)
  138. {
  139. lock (_locker)
  140. {
  141. _lstHandler.Clear();
  142. }
  143. _trigRetryConnect.CLK = !_connection.IsConnected;
  144. if (_trigRetryConnect.Q)
  145. {
  146. _connection.SetPortAddress(SC.GetStringValue($"{(!string.IsNullOrEmpty(_scRoot) ? _scRoot + "." : "")}{Module}.{Name}.Address"));
  147. if (!_connection.Connect())
  148. {
  149. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  150. }
  151. else
  152. {
  153. //_lstHandler.AddLast(new SmcHRZChillerQueryPinHandler(this, _deviceAddress));
  154. //_lstHandler.AddLast(new SmcHRZChillerSetCommModeHandler(this, _deviceAddress, EnumRfPowerCommunicationMode.Host));
  155. }
  156. }
  157. return true;
  158. }
  159. HandlerBase handler = null;
  160. if (!_connection.IsBusy)
  161. {
  162. lock (_locker)
  163. {
  164. if (_lstHandler.Count == 0)
  165. {
  166. foreach (var monitorHandler in _lstMonitorHandler)
  167. {
  168. _lstHandler.AddLast(monitorHandler);
  169. }
  170. }
  171. if (_lstHandler.Count > 0)
  172. {
  173. handler = _lstHandler.First.Value;
  174. _lstHandler.RemoveFirst();
  175. }
  176. }
  177. if (handler != null)
  178. {
  179. _connection.Execute(handler);
  180. }
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. LOG.Write(ex);
  186. }
  187. return true;
  188. }
  189. public override void Monitor()
  190. {
  191. try
  192. {
  193. _connection.EnableLog(_enableLog);
  194. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  195. if (_trigCommunicationError.Q)
  196. {
  197. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. LOG.Write(ex);
  203. }
  204. }
  205. internal void NoteIsWarning(bool isWarning)
  206. {
  207. IsCH1Warning = isWarning;
  208. }
  209. internal void NotePressUnit(bool pressUnit)
  210. {
  211. }
  212. internal void NoteIsRemote(bool isRemote)
  213. {
  214. IsCH1Remote = isRemote;
  215. }
  216. internal void NoteIsTempReady(bool isTempReady)
  217. {
  218. }
  219. internal void NoteIsTimeout(bool isTimeout)
  220. {
  221. }
  222. internal void NoteFlowUnit(bool flowUnit)
  223. {
  224. }
  225. internal void NoteIsFault(bool isFault)
  226. {
  227. if (IsCH1Alarm != isFault)
  228. {
  229. IsCH1Alarm = isFault;
  230. }
  231. }
  232. internal void NoteIsRun(bool isRun)
  233. {
  234. if (IsCH1On != isRun)
  235. {
  236. IsCH1On = isRun;
  237. }
  238. }
  239. public override void Reset()
  240. {
  241. _trigError.RST = true;
  242. _connection.SetCommunicationError(false, "");
  243. _trigCommunicationError.RST = true;
  244. //_enableLog = SC.GetValue<bool>($"{ScBasePath}.{Name}.EnableLogMessage");
  245. _trigRetryConnect.RST = true;
  246. for (int i = 0; i < _trigAlarm.Length; i++)
  247. {
  248. _trigAlarm[i].RST = true;
  249. }
  250. base.Reset();
  251. }
  252. #region Command Functions
  253. public void SetTemperature(string angle)
  254. {
  255. lock (_locker)
  256. {
  257. //_lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWAA", angle));
  258. }
  259. }
  260. public void SetTempHighWarning(string angle)
  261. {
  262. lock (_locker)
  263. {
  264. //_lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWAA", angle));
  265. }
  266. }
  267. public void SetTempLowWarning(string angle)
  268. {
  269. lock (_locker)
  270. {
  271. //_lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWAA", angle));
  272. }
  273. }
  274. public void SetRemoteMode(string angle)
  275. {
  276. lock (_locker)
  277. {
  278. //_lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWS", angle));
  279. }
  280. }
  281. public void SetRunMode(string waferSize)
  282. {
  283. lock (_locker)
  284. {
  285. // _lstHandler.AddLast(new SmcHRZChillerSimpleSetHandler(this, "SWS", waferSize));
  286. }
  287. }
  288. public void QueryAllTemp()
  289. {
  290. lock (_locker)
  291. {
  292. _lstHandler.AddLast(new SmcHRZChillerGetStatusHandler(this));
  293. }
  294. }
  295. public override void SetChillerCH1OnOff(bool isOn)
  296. {
  297. lock (_locker)
  298. {
  299. _lstHandler.AddLast(new SmcHRZChillerSetOnOffHandler(this, isOn));
  300. }
  301. }
  302. public override void SetChillerCH2OnOff(bool isOn)
  303. {
  304. lock (_locker)
  305. {
  306. _lstHandler.AddLast(new SmcHRZChillerSetOnOffHandler(this, isOn));
  307. }
  308. }
  309. public override void SetChillerCH1Temperature(float temp)
  310. {
  311. lock (_locker)
  312. {
  313. _lstHandler.AddLast(new SmcHRZChillerSetTemperatureHandler(this, temp));
  314. }
  315. }
  316. public override void SetChillerCH2Temperature(float temp)
  317. {
  318. lock (_locker)
  319. {
  320. _lstHandler.AddLast(new SmcHRZChillerSetTemperatureHandler(this, temp));
  321. }
  322. }
  323. #endregion
  324. #region Properties
  325. public string Error { get; private set; }
  326. #endregion
  327. #region Note Functions
  328. private R_TRIG _trigWarningMessage = new R_TRIG();
  329. public void NoteError(string reason)
  330. {
  331. if (!string.IsNullOrEmpty(reason))
  332. {
  333. _trigWarningMessage.CLK = true;
  334. if (_trigWarningMessage.Q)
  335. {
  336. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  337. }
  338. Error = reason;
  339. }
  340. else
  341. {
  342. Error = null;
  343. }
  344. }
  345. public void NoteTemp(float value)
  346. {
  347. CH1TemperatureFeedback = value;
  348. }
  349. public void NotePressure(float value)
  350. {
  351. }
  352. public void NoteErrorFlag(int index, string error)
  353. {
  354. if (!string.IsNullOrEmpty(error))
  355. {
  356. _trigAlarm[index].CLK = true;
  357. if (_trigAlarm[index].Q)
  358. {
  359. EV.PostWarningLog(Module, $"{Module}.{Name} error, {error}");
  360. }
  361. Error = error;
  362. }
  363. else
  364. {
  365. Error = null;
  366. }
  367. }
  368. #endregion
  369. }
  370. }