FfuAAF.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  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 System;
  10. using System.Collections.Generic;
  11. using System.Threading;
  12. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
  13. {
  14. public class FfuAAF : BaseDevice, IConnection, IDevice
  15. {
  16. private FfuAAFConnection _connection;
  17. private string _deviceAddress = "001";
  18. private bool _isOn;
  19. private bool _activeMonitorStatus;
  20. private int _nMaxSpeed;
  21. public int NMaxSpeed { get => _nMaxSpeed; }
  22. private int _errorCode;
  23. private RD_TRIG _trigPumpOnOff = new RD_TRIG();
  24. private R_TRIG _trigError = new R_TRIG();
  25. private R_TRIG _trigOverTemp = new R_TRIG();
  26. private R_TRIG _trigWarningMessage = new R_TRIG();
  27. private string _lastError = string.Empty;
  28. private R_TRIG _trigCommunicationError = new R_TRIG();
  29. private R_TRIG _trigRetryConnect = new R_TRIG();
  30. private PeriodicJob _thread;
  31. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  32. private object _locker = new object();
  33. private bool _enableLog
  34. {
  35. get
  36. {
  37. if (SC.ContainsItem($"{_scRoot}.{Name}.EnableLogMessage"))
  38. return SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  39. return false;
  40. }
  41. }
  42. private string _scRoot;
  43. private int _ffu1Speed = 88;
  44. private int _ffu2Speed = 77;
  45. private int _ffu3Speed;
  46. private int _ffuCount;
  47. public int FFU1Speed
  48. {
  49. get { return _ffu1Speed; }
  50. set { _ffu1Speed = value; }
  51. }
  52. public int FFU2Speed
  53. {
  54. get { return _ffu2Speed; }
  55. set { _ffu2Speed = value; }
  56. }
  57. public int FFU3Speed
  58. {
  59. get { return _ffu3Speed; }
  60. set { _ffu3Speed = value; }
  61. }
  62. public string Address
  63. { get; set; }
  64. public bool IsConnected
  65. {
  66. get
  67. {
  68. return _connection != null && _connection.IsConnected;
  69. }
  70. }
  71. public bool Connect()
  72. {
  73. return true;
  74. }
  75. public bool Disconnect()
  76. {
  77. return true;
  78. }
  79. public FfuAAF(string module, string name, string scRoot) : base(module, name, name, name)
  80. {
  81. _scRoot = scRoot;
  82. _activeMonitorStatus = true;
  83. _ffuCount = 1;
  84. }
  85. public FfuAAF(string module, string name, string scRoot, int ffucount) : base(module, name, name, name)
  86. {
  87. _scRoot = scRoot;
  88. _activeMonitorStatus = true;
  89. _ffuCount = ffucount;
  90. }
  91. ~FfuAAF()
  92. {
  93. _connection.Disconnect();
  94. }
  95. public void QuerySpeedFanaddress(int fanaddress)
  96. {
  97. lock (_locker)
  98. {
  99. _lstHandler.Clear();
  100. if (_connection.IsBusy) _connection.ForceClear();
  101. List<byte> cmddata = new List<byte>() { 0x15, (byte)(0x20 + fanaddress), 0x1 };
  102. byte checksum = ModRTU_CRC(cmddata.ToArray());
  103. cmddata.Add(checksum);
  104. _connection.SendMessage(cmddata.ToArray());
  105. }
  106. }
  107. public void QuerySpeed()
  108. {
  109. _lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, 1, 1));
  110. if (_ffuCount == 2)
  111. {
  112. _lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, 3, 1));
  113. }
  114. if (_ffuCount == 3)
  115. {
  116. _lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, 5, 1));
  117. }
  118. }
  119. public void ResetDevice()
  120. {
  121. }
  122. public void QueryError()
  123. {
  124. _lstHandler.AddLast(new FfuAAFQueryStatusHandler(this, 1, 1));
  125. _lstHandler.AddLast(new FfuAAFQueryStatusHandler(this, 3, 1));
  126. EV.PostInfoLog(Module, "Query error");
  127. }
  128. public bool Initialize()
  129. {
  130. //DATA.Subscribe($"{Module}.{Name}.Power", () => _power);
  131. //DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => _errorCode);
  132. //DATA.Subscribe($"{Module}.{Name}.IsAtSpeed", () => _isAtSpeed);
  133. //DATA.Subscribe($"{Module}.{Name}.IsAccelerate", () => _isAccelerate);
  134. string portName = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  135. Address = portName;
  136. int address = SC.GetValue<int>($"{_scRoot}.{Name}.DeviceAddress");
  137. _deviceAddress = address.ToString("D3");
  138. _nMaxSpeed = SC.GetValue<int>($"{_scRoot}.{Name}.MaxSpeed");
  139. _connection = new FfuAAFConnection(portName);
  140. _connection.EnableLog(_enableLog);
  141. int count = SC.ContainsItem("System.ComPortRetryCount") ? SC.GetValue<int>("System.ComPortRetryCount") : 5;
  142. int sleep = SC.ContainsItem("System.ComPortRetryDelayTime") ? SC.GetValue<int>("System.ComPortRetryDelayTime") : 2;
  143. if (sleep <= 0 || sleep > 10)
  144. sleep = 2;
  145. int retry = 0;
  146. do
  147. {
  148. _connection.Disconnect();
  149. Thread.Sleep(sleep * 1000);
  150. if (_connection.Connect())
  151. {
  152. EV.PostInfoLog(Module, $"{Name} connected");
  153. break;
  154. }
  155. if (count > 0 && retry++ > count)
  156. {
  157. LOG.Write($"Retry connect {Module}.{Name} stop retry.");
  158. EV.PostAlarmLog(Module, $"Can't connect to {Module}.{Name}.");
  159. break;
  160. }
  161. Thread.Sleep(sleep * 1000);
  162. LOG.Write($"Retry connect {Module}.{Name} for the {retry + 1} time.");
  163. } while (true);
  164. _thread = new PeriodicJob(1000, OnTimer, $"{Name} MonitorHandler", true);
  165. DATA.Subscribe($"{Name}.FFU1Speed", () => _ffu1Speed);
  166. DATA.Subscribe($"{Name}.FFU2Speed", () => _ffu2Speed);
  167. DATA.Subscribe($"{Name}.FFU3Speed", () => _ffu3Speed);
  168. OP.Subscribe($"{Name}.SetSpeed", (cmd, param) =>
  169. {
  170. if (!int.TryParse((string)param[0], out var speed))
  171. {
  172. EV.PostWarningLog(Module, "invalid speed.");
  173. return false;
  174. }
  175. if (speed > NMaxSpeed)
  176. {
  177. EV.PostWarningLog(Module, $"invalid speed.");
  178. return false;
  179. }
  180. SetSpeed(speed, 1);
  181. EV.PostInfoLog(Module, $"{Name} speed set to {speed}");
  182. return true;
  183. });
  184. OP.Subscribe($"{Name}.SetFFUSpeedBySC", (cmd, param) =>
  185. {
  186. int speed = SC.GetValue<int>("System.FFUFanSpeedSetPoint");
  187. SetSpeed(speed);
  188. EV.PostInfoLog(Module, $"Set {Name} speed");
  189. return true;
  190. });
  191. OP.Subscribe("FFU1.SetFFUSpeedBySC", (cmd, param) =>
  192. {
  193. int speed = SC.GetValue<int>("System.FFU1FanSpeedSetPoint");
  194. SetSpeed(speed, 1);
  195. EV.PostInfoLog(Module, "Set FFU1 speed");
  196. return true;
  197. });
  198. OP.Subscribe("FFU2.SetFFUSpeedBySC", (cmd, param) =>
  199. {
  200. int speed = SC.GetValue<int>("System.FFU2FanSpeedSetPoint");
  201. SetSpeed(speed, 3);
  202. EV.PostInfoLog(Module, "Set FFU2 speed");
  203. return true;
  204. });
  205. ConnectionManager.Instance.Subscribe($"{Name}", this);
  206. return true;
  207. }
  208. private bool OnTimer()
  209. {
  210. try
  211. {
  212. _connection.EnableLog(_enableLog);
  213. _connection.MonitorTimeout();
  214. _trigRetryConnect.CLK = !_connection.IsConnected;
  215. if (!_connection.IsConnected || _connection.IsCommunicationError)
  216. {
  217. _connection.ForceClear();
  218. lock (_locker)
  219. {
  220. _lstHandler.Clear();
  221. }
  222. if (_trigRetryConnect.Q)
  223. {
  224. _connection.SetPortAddress(SC.GetStringValue($"{_scRoot}.{Name}.Address"));
  225. if (!_connection.Connect())
  226. {
  227. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  228. }
  229. }
  230. return true;
  231. }
  232. if (_lstHandler.Count == 0)
  233. {
  234. lock (_locker)
  235. QuerySpeed();
  236. }
  237. if (!_connection.IsBusy)
  238. {
  239. HandlerBase handler = null;
  240. lock (_locker)
  241. {
  242. if (_lstHandler.Count > 0)
  243. {
  244. handler = _lstHandler.First.Value;
  245. _lstHandler.RemoveFirst();
  246. }
  247. }
  248. if (handler != null)
  249. {
  250. _connection.Execute(handler);
  251. }
  252. }
  253. }
  254. catch (Exception ex)
  255. {
  256. LOG.Write(ex);
  257. }
  258. return true;
  259. }
  260. internal void NoteError()
  261. {
  262. }
  263. public void Monitor()
  264. {
  265. }
  266. public void Reset()
  267. {
  268. _trigError.RST = true;
  269. _trigOverTemp.RST = true;
  270. _trigWarningMessage.RST = true;
  271. _connection.SetCommunicationError(false, "");
  272. _trigCommunicationError.RST = true;
  273. _connection.EnableLog(_enableLog);
  274. _trigRetryConnect.RST = true;
  275. }
  276. public void SetSpeed(float speed)
  277. {
  278. lock (_locker)
  279. {
  280. _lstHandler.AddLast(new FfuAAFSetSpeedHandler(this, 0, 1, Convert.ToInt32(speed)));
  281. //_lstHandler.Clear();
  282. //if (_connection.IsBusy) _connection.ForceClear();
  283. //List<byte> cmddata = new List<byte>() { 0x35, 0x40, 0x0, (byte)(speed * 250 / _nMaxSpeed) };
  284. //byte checksum = ModRTU_CRC(cmddata.ToArray());
  285. //cmddata.Add(checksum);
  286. //_connection.SendMessage(cmddata.ToArray());
  287. }
  288. }
  289. public void SetSpeed(float speed, int ffuAddress)
  290. {
  291. lock (_locker)
  292. {
  293. _lstHandler.AddLast(new FfuAAFSetSpeedHandler(this, ffuAddress, 1, Convert.ToInt32(speed)));
  294. //_lstHandler.Clear();
  295. //if (_connection.IsBusy) _connection.ForceClear();
  296. //List<byte> cmddata = new List<byte>() { 0x35, (byte)(0x40+ ffuAddress), 0x01, (byte)(speed * 250 / _nMaxSpeed) };
  297. //byte checksum = ModRTU_CRC(cmddata.ToArray());
  298. //cmddata.Add(checksum);
  299. //_connection.SendMessage(cmddata.ToArray());
  300. }
  301. }
  302. public void SetErrorCode(int errorCode)
  303. {
  304. _errorCode = errorCode;
  305. }
  306. public void SetError(string reason)
  307. {
  308. _trigWarningMessage.CLK = true;
  309. if (_trigWarningMessage.Q)
  310. {
  311. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  312. }
  313. }
  314. private static byte ModRTU_CRC(byte[] buffer)
  315. {
  316. //ushort crc = 0xFFFF;
  317. // var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
  318. var buf = buffer;
  319. var len = buffer.Length;
  320. byte temp = buffer[0];
  321. for (int i = 1; i < buffer.Length; i++)
  322. {
  323. temp = (byte)(temp ^ buffer[i]);
  324. }
  325. return (byte)~temp;
  326. }
  327. public void Terminate()
  328. {
  329. _connection.Disconnect();
  330. }
  331. }
  332. }