FfuAAF.cs 12 KB

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