FfuAAFNew.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using System;
  2. using System.Collections.Generic;
  3. using Aitex.Core.RT.DataCenter;
  4. using Aitex.Core.RT.Device;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.OperationCenter;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.Util;
  10. using MECF.Framework.Common.Communications;
  11. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.MayAir;
  12. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
  13. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK;
  14. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
  15. {
  16. public class FfuAAFNew : BaseDevice, IConnection, IDevice
  17. {
  18. private FfuAAFNewConnection _connection;
  19. private string _deviceAddress = "001";
  20. public byte DeviceAddress { get; set; }
  21. public byte GroupAddress { get; set; }
  22. public byte Speedfactor { get; set; }
  23. public int MaxSpeed1 { get; set; }
  24. public int MaxSpeed2 { get; set; }
  25. public int MaxSpeed3 { get; set; }
  26. public int Status { get; set; }
  27. private bool _isOn;
  28. private bool _activeMonitorStatus;
  29. private int _nMaxSpeed = 0;
  30. public int NMaxSpeed { get => _nMaxSpeed; }
  31. private int _errorCode;
  32. private RD_TRIG _trigPumpOnOff = new RD_TRIG();
  33. private R_TRIG _trigError = new R_TRIG();
  34. private R_TRIG _trigOverTemp = new R_TRIG();
  35. private R_TRIG _trigWarningMessage = new R_TRIG();
  36. private string _lastError = string.Empty;
  37. private R_TRIG _trigCommunicationError = new R_TRIG();
  38. private R_TRIG _trigRetryConnect = new R_TRIG();
  39. private PeriodicJob _thread;
  40. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  41. private object _locker = new object();
  42. private bool _enableLog => SC.GetValue<bool>($"{_scRoot}.{Name}.EnableLogMessage");
  43. private string _scRoot;
  44. //private AsyncSerial serialPort;
  45. private int _ffuSpeed;
  46. private string[] StatusStringList = new string[] { "normal", "running fault", "stop fault" };
  47. public int Speed
  48. {
  49. get { return _ffuSpeed; }
  50. set { _ffuSpeed = value; }
  51. }
  52. public string Address
  53. {
  54. get; set;
  55. }
  56. public bool IsConnected
  57. {
  58. get
  59. {
  60. return _connection != null && _connection.IsConnected;
  61. }
  62. }
  63. public bool Connect()
  64. {
  65. return true;
  66. }
  67. public bool Disconnect()
  68. {
  69. return true;
  70. }
  71. public FfuAAFNew(string scRoot, string module, string name) : base(module, name, name, name)
  72. {
  73. _scRoot = scRoot;
  74. _activeMonitorStatus = true;
  75. //serialPort = new AsyncSerial(port, 9600, 8);
  76. //serialPort.OnDataChanged += (OnDataChanged);
  77. //serialPort.OnErrorHappened += (OnErrorHandler);
  78. }
  79. private void OnErrorHandler(ErrorEventArgs args)
  80. {
  81. }
  82. private void OnDataChanged(string message)
  83. {
  84. }
  85. public void SetGroupAddress(byte newGroupAddress)
  86. {
  87. _lstHandler.AddLast(new FfuAAFNewSetGroupAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x00, newGroupAddress }));
  88. }
  89. public void GetGroupAddress()
  90. {
  91. _lstHandler.AddLast(new FfuAAFNewGetGroupAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x00 }));
  92. }
  93. public void SetDeviceAddress(byte newDeviceAddress)
  94. {
  95. _lstHandler.AddLast(new FfuAAFNewSetAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x01, newDeviceAddress }));
  96. }
  97. public void GetDeviceAddress()
  98. {
  99. _lstHandler.AddLast(new FfuAAFNewGetAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x01 }));
  100. }
  101. public void QueryMaxSpeed1()
  102. {
  103. _lstHandler.AddLast(new FfuAAFNewQueryMaxSpeed1Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x08 }));
  104. }
  105. public void QueryMaxSpeed2()
  106. {
  107. _lstHandler.AddLast(new FfuAAFNewQueryMaxSpeed2Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x09 }));
  108. }
  109. public void QueryMaxSpeed3()
  110. {
  111. _lstHandler.AddLast(new FfuAAFNewQueryMaxSpeed3Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x0a }));
  112. }
  113. /// <summary>
  114. /// 查转速因子
  115. /// </summary>
  116. public void QuerySpeedFactor()
  117. {
  118. _lstHandler.AddLast(new FfuAAFNewQuerySpeedFactorHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x11 }));
  119. }
  120. public void SetSpeedFactor(byte newSpeedFactor)
  121. {
  122. _lstHandler.AddLast(new FfuAAFNewSetSpeedFactorHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x11, newSpeedFactor }));
  123. }
  124. public void QuerySpeed()
  125. {
  126. _lstHandler.AddLast(new FfuAAFNewQuerySpeedHandler(this, DeviceAddress, GroupAddress));
  127. }
  128. public void SetSpeed(int speed)
  129. {
  130. _lstHandler.AddLast(new FfuAAFNewSetSpeedHandler(this, DeviceAddress, GroupAddress, new byte[] { (byte)(speed * 250.0 / MaxSpeed2) }));
  131. }
  132. public void ResetDevice()
  133. {
  134. _lstHandler.AddLast(new FfuAAFNewResetDeviceHandler(this, DeviceAddress, GroupAddress));
  135. }
  136. public void QueryError()
  137. {
  138. _lstHandler.AddLast(new FfuAAFNewQueryStatusHandler(this, DeviceAddress, GroupAddress));
  139. EV.PostInfoLog(Module, "Query error");
  140. }
  141. private int ChangedStatus(int value)
  142. {
  143. int returnValue = 0;
  144. switch (value)
  145. {
  146. case 0:
  147. returnValue = 0;
  148. break;
  149. case 0x80:
  150. returnValue = 1;
  151. break;
  152. case 0x10:
  153. returnValue = 2;
  154. break;
  155. default:
  156. break;
  157. }
  158. return returnValue;
  159. }
  160. public bool Initialize()
  161. {
  162. string portName = SC.GetStringValue($"{_scRoot}.{Name}.Address");
  163. Address = portName;
  164. int address = SC.GetValue<int>($"{_scRoot}.{Name}.DeviceAddress");
  165. DeviceAddress = (byte)address;
  166. DATA.Subscribe($"{Name}.DeviceAddress", () => DeviceAddress);
  167. GroupAddress = (byte)SC.GetValue<int>($"{_scRoot}.{Name}.GroupAddress");
  168. DATA.Subscribe($"{Name}.GroupAddress", () => GroupAddress);
  169. Status = SC.GetValue<int>($"{_scRoot}.{Name}.Status");
  170. DATA.Subscribe($"{Name}.Status", () => StatusStringList[ChangedStatus(Status)]);
  171. _deviceAddress = address.ToString("D3");
  172. Speedfactor = (byte)SC.GetValue<int>($"{_scRoot}.{Name}.Speedfactor");
  173. DATA.Subscribe($"{Name}.Speedfactor", () => Speedfactor);
  174. MaxSpeed1 = SC.GetValue<int>($"{_scRoot}.{Name}.MaxSpeed1");
  175. DATA.Subscribe($"{Name}.MaxSpeed1", () => MaxSpeed1);
  176. MaxSpeed2 = SC.GetValue<int>($"{_scRoot}.{Name}.MaxSpeed2");
  177. DATA.Subscribe($"{Name}.MaxSpeed2", () => MaxSpeed2);
  178. MaxSpeed3 = SC.GetValue<int>($"{_scRoot}.{Name}.MaxSpeed3");
  179. DATA.Subscribe($"{Name}.MaxSpeed3", () => MaxSpeed3);
  180. _connection = new FfuAAFNewConnection(portName);
  181. _connection.EnableLog(_enableLog);
  182. if (_connection.Connect())
  183. {
  184. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  185. }
  186. _thread = new PeriodicJob(1000, OnTimer, $"{Name} MonitorHandler", true);
  187. _ffuSpeed = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Speed");
  188. DATA.Subscribe($"{Name}.Speed", () => _ffuSpeed);
  189. OP.Subscribe($"{Name}.SetFFUGroupAddress", (cmd, param) =>
  190. {
  191. if (int.TryParse((string)param[0], out var groupAddress))
  192. {
  193. EV.PostWarningLog(Module, "Set FFU Group Address.");
  194. return false;
  195. }
  196. int.TryParse((string)param[2], out var newgroupAddress);
  197. GroupAddress = (byte)newgroupAddress;
  198. SetGroupAddress((byte)newgroupAddress);
  199. EV.PostInfoLog(Module, $"{Name}Set FFU Group Address {newgroupAddress}");
  200. return true;
  201. });
  202. OP.Subscribe($"{Name}.GetFFUGroupAddress", (cmd, param) =>
  203. {
  204. if (int.TryParse((string)param[0], out var groupAddress))
  205. {
  206. EV.PostWarningLog(Module, "Get FFU Group Address.");
  207. return false;
  208. }
  209. GetGroupAddress();
  210. EV.PostInfoLog(Module, $"{Name}Get FFU Group Address {groupAddress}");
  211. return true;
  212. });
  213. OP.Subscribe($"{Name}.SetFFUDeviceAddress", (cmd, param) =>
  214. {
  215. if (int.TryParse((string)param[0], out var groupAddress))
  216. {
  217. EV.PostWarningLog(Module, "Set FFU Device Address.");
  218. return false;
  219. }
  220. int.TryParse((string)param[2], out var newAddress);
  221. SetDeviceAddress((byte)newAddress);
  222. EV.PostInfoLog(Module, $"{Name}Set FFU Device Address {newAddress}");
  223. return true;
  224. });
  225. OP.Subscribe($"{Name}.GetFFUDeviceAddress", (cmd, param) =>
  226. {
  227. if (int.TryParse((string)param[0], out var groupAddress))
  228. {
  229. EV.PostWarningLog(Module, "Get FFU Device Address.");
  230. return false;
  231. }
  232. GetDeviceAddress();
  233. EV.PostInfoLog(Module, $"{Name}Get FFU Device Address {groupAddress}");
  234. return true;
  235. });
  236. OP.Subscribe($"{Name}.SetFFUSpeedfactor", (cmd, param) =>
  237. {
  238. if (int.TryParse((string)param[0], out var groupAddress))
  239. {
  240. EV.PostWarningLog(Module, "Set FFU speed factor.");
  241. return false;
  242. }
  243. int.TryParse((string)param[2], out var newSpeedFactor);
  244. SetSpeedFactor((byte)newSpeedFactor);
  245. EV.PostInfoLog(Module, $"{Name}Set FFU speed factor {newSpeedFactor}");
  246. return true;
  247. });
  248. OP.Subscribe($"{Name}.GetFFUSpeedfactor", (cmd, param) =>
  249. {
  250. if (int.TryParse((string)param[0], out var groupAddress))
  251. {
  252. EV.PostWarningLog(Module, "Get FFU speed factor.");
  253. return false;
  254. }
  255. QuerySpeedFactor();
  256. EV.PostInfoLog(Module, $"{Name}Get FFU speed factor {groupAddress}");
  257. return true;
  258. });
  259. OP.Subscribe($"{Name}.GetFFUStatus", (cmd, param) =>
  260. {
  261. if (int.TryParse((string)param[0], out var groupAddress))
  262. {
  263. EV.PostWarningLog(Module, "Get FFU Status.");
  264. return false;
  265. }
  266. QueryError();
  267. EV.PostInfoLog(Module, $"{Name}Get FFU Status {groupAddress}");
  268. return true;
  269. });
  270. OP.Subscribe($"{Name}.SetFFUSpeed", (cmd, param) =>
  271. {
  272. if (param == null)
  273. {
  274. int speed = SC.GetValue<int>($"{_scRoot}.{Name}.FanSpeedSetPoint");
  275. SetSpeed(speed);
  276. EV.PostInfoLog(Module, $"{Name} speed set to {speed}");
  277. return true;
  278. }
  279. else
  280. {
  281. if (int.TryParse((string)param[0], out var groupAddress))
  282. {
  283. EV.PostWarningLog(Module, "invalid speed.");
  284. return false;
  285. }
  286. int.TryParse((string)param[2], out var speed);
  287. SetSpeed(speed);
  288. EV.PostInfoLog(Module, $"{Name} speed set to {speed}");
  289. return true;
  290. }
  291. });
  292. OP.Subscribe($"{Name}.GetFFUSpeed", (cmd, param) =>
  293. {
  294. if (int.TryParse((string)param[0], out var groupAddress))
  295. {
  296. EV.PostWarningLog(Module, "FFU Query speed.");
  297. return false;
  298. }
  299. QuerySpeed();
  300. EV.PostInfoLog(Module, $"{Name}FFU Query Speed {groupAddress}");
  301. return true;
  302. });
  303. OP.Subscribe($"{Name}.GetFFUMaxSpeed1", (cmd, param) =>
  304. {
  305. if (int.TryParse((string)param[0], out var groupAddress))
  306. {
  307. EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed1.");
  308. return false;
  309. }
  310. QueryMaxSpeed1();
  311. EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed1 {groupAddress}");
  312. return true;
  313. });
  314. OP.Subscribe($"{Name}.GetFFUMaxSpeed2", (cmd, param) =>
  315. {
  316. if (int.TryParse((string)param[0], out var groupAddress))
  317. {
  318. EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed2.");
  319. return false;
  320. }
  321. QueryMaxSpeed2();
  322. EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed2 {groupAddress}");
  323. return true;
  324. });
  325. OP.Subscribe($"{Name}.GetFFUMaxSpeed3", (cmd, param) =>
  326. {
  327. if (int.TryParse((string)param[0], out var groupAddress))
  328. {
  329. EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed3.");
  330. return false;
  331. }
  332. QueryMaxSpeed3();
  333. EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed3 {groupAddress}");
  334. return true;
  335. });
  336. OP.Subscribe($"{Name}.SetFFUReset", (cmd, param) =>
  337. {
  338. if (int.TryParse((string)param[0], out var groupAddress))
  339. {
  340. EV.PostWarningLog(Module, "Set FFU Reset.");
  341. return false;
  342. }
  343. ResetDevice();
  344. EV.PostInfoLog(Module, $"{Name}Set FFU Reset {groupAddress}");
  345. return true;
  346. });
  347. OP.Subscribe($"{Name}.SetFFUSpeedBySC", (cmd, param) =>
  348. {
  349. int speed = SC.GetValue<int>($"{_scRoot}.{Name}.FanSpeedSetPoint");
  350. SetSpeed(speed);
  351. EV.PostInfoLog(Module, $"Set {Name} speed");
  352. return true;
  353. });
  354. ConnectionManager.Instance.Subscribe($"{Name}", this);
  355. return true;
  356. }
  357. private bool OnTimer()
  358. {
  359. try
  360. {
  361. _connection.EnableLog(_enableLog);
  362. _connection.MonitorTimeout();
  363. if (!_connection.IsConnected)// || _connection.IsCommunicationError
  364. {
  365. lock (_locker)
  366. {
  367. _lstHandler.Clear();
  368. }
  369. _trigRetryConnect.CLK = !_connection.IsConnected;
  370. if (_trigRetryConnect.Q)
  371. {
  372. _connection.SetPortAddress(SC.GetStringValue($"{_scRoot}.{Name}.Address"));
  373. if (!_connection.Connect())
  374. {
  375. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  376. }
  377. }
  378. return true;
  379. }
  380. HandlerBase handler = null;
  381. if (!_connection.IsBusy)
  382. {
  383. lock (_locker)
  384. {
  385. if (_lstHandler.Count == 0 && _activeMonitorStatus)
  386. {
  387. QuerySpeed();
  388. }
  389. if (_lstHandler.Count > 0)
  390. {
  391. handler = _lstHandler.First.Value;
  392. _lstHandler.RemoveFirst();
  393. }
  394. }
  395. if (handler != null)
  396. {
  397. _connection.Execute(handler);
  398. }
  399. }
  400. return true;
  401. }
  402. catch (Exception ex)
  403. {
  404. LOG.Write(ex);
  405. }
  406. return true;
  407. }
  408. internal void NoteError()
  409. {
  410. }
  411. public void Monitor()
  412. {
  413. try
  414. {
  415. _connection.EnableLog(_enableLog);
  416. _trigPumpOnOff.CLK = _isOn;
  417. if (_trigPumpOnOff.R)
  418. {
  419. EV.PostInfoLog(Module, $"{Module}.{Name} is on");
  420. }
  421. if (_trigPumpOnOff.T)
  422. {
  423. EV.PostInfoLog(Module, $"{Module}.{Name} is off");
  424. }
  425. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  426. if (_trigCommunicationError.Q)
  427. {
  428. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  429. }
  430. }
  431. catch (Exception ex)
  432. {
  433. LOG.Write(ex);
  434. }
  435. }
  436. public void Terminate()
  437. {
  438. try
  439. {
  440. if (_connection != null)
  441. {
  442. _connection.Disconnect();
  443. _connection = null;
  444. }
  445. }
  446. catch (Exception ex)
  447. {
  448. LOG.Write(ex);
  449. }
  450. }
  451. public void Reset()
  452. {
  453. _trigError.RST = true;
  454. _trigOverTemp.RST = true;
  455. _trigWarningMessage.RST = true;
  456. _connection.SetCommunicationError(false, "");
  457. _trigCommunicationError.RST = true;
  458. _trigRetryConnect.RST = true;
  459. }
  460. public void SetPumpOnOff(bool isOn)
  461. {
  462. lock (_locker)
  463. {
  464. }
  465. }
  466. public void SetActiveMonitor(bool active)
  467. {
  468. _activeMonitorStatus = active;
  469. }
  470. internal void NoteOnOff(bool isOn)
  471. {
  472. _isOn = isOn;
  473. }
  474. public void SetSpeed(float speed)
  475. {
  476. lock (_locker)
  477. {
  478. _lstHandler.Clear();
  479. if (_connection.IsBusy) _connection.ForceClear();
  480. List<byte> cmddata = new List<byte>() { 0x35, 0x40, 0x0, (byte)(speed * 250.0 / _nMaxSpeed) };
  481. byte checksum = ModRTU_CRC(cmddata.ToArray());
  482. cmddata.Add(checksum);
  483. _connection.SendMessage(cmddata.ToArray());
  484. }
  485. }
  486. public void SetErrorCode(int errorCode)
  487. {
  488. _errorCode = errorCode;
  489. }
  490. public void SetError(string reason)
  491. {
  492. _trigWarningMessage.CLK = true;
  493. if (_trigWarningMessage.Q)
  494. {
  495. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  496. }
  497. }
  498. private static byte ModRTU_CRC(byte[] buffer)
  499. {
  500. //ushort crc = 0xFFFF;
  501. // var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
  502. var buf = buffer;
  503. var len = buffer.Length;
  504. byte temp = buffer[0];
  505. for (int i = 1; i < buffer.Length; i++)
  506. {
  507. temp = (byte)(temp ^ buffer[i]);
  508. }
  509. return (byte)~temp;
  510. }
  511. }
  512. }