FfuAAF.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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.Aire
  15. {
  16. public class FfuAAF : BaseDevice, IConnection, IDevice
  17. {
  18. private FfuAAFConnection _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 = true;
  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 FfuAAF(string scRoot, string module, string name, string port) : 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 FfuAAFSetGroupAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x00, newGroupAddress }));
  88. }
  89. public void GetGroupAddress()
  90. {
  91. _lstHandler.AddLast(new FfuAAFGetGroupAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x00 }));
  92. }
  93. public void SetDeviceAddress(byte newDeviceAddress)
  94. {
  95. _lstHandler.AddLast(new FfuAAFSetAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x01, newDeviceAddress }));
  96. }
  97. public void GetDeviceAddress()
  98. {
  99. _lstHandler.AddLast(new FfuAAFGetAddressHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x01 }));
  100. }
  101. public void QueryMaxSpeed1()
  102. {
  103. _lstHandler.AddLast(new FfuAAFQueryMaxSpeed1Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x08 }));
  104. }
  105. public void QueryMaxSpeed2()
  106. {
  107. _lstHandler.AddLast(new FfuAAFQueryMaxSpeed2Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x09 }));
  108. }
  109. public void QueryMaxSpeed3()
  110. {
  111. _lstHandler.AddLast(new FfuAAFQueryMaxSpeed3Handler(this, DeviceAddress, GroupAddress, new byte[] { 0x0a }));
  112. }
  113. /// <summary>
  114. /// 查转速因子
  115. /// </summary>
  116. public void QuerySpeedFactor()
  117. {
  118. _lstHandler.AddLast(new FfuAAFQuerySpeedFactorHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x11 }));
  119. }
  120. public void SetSpeedFactor(byte newSpeedFactor)
  121. {
  122. _lstHandler.AddLast(new FfuAAFSetSpeedFactorHandler(this, DeviceAddress, GroupAddress, new byte[] { 0x11, newSpeedFactor }));
  123. }
  124. public void QuerySpeed()
  125. {
  126. _lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, DeviceAddress, GroupAddress));
  127. }
  128. public void SetSpeed(int speed)
  129. {
  130. _lstHandler.AddLast(new FfuAAFSetSpeedHandler(this, DeviceAddress, GroupAddress, new byte[] { (byte)(speed * 250.0 / MaxSpeed2) }));
  131. }
  132. public void ResetDevice()
  133. {
  134. _lstHandler.AddLast(new FfuAAFResetDeviceHandler(this, DeviceAddress, GroupAddress));
  135. }
  136. public void QueryError()
  137. {
  138. _lstHandler.AddLast(new FfuAAFQueryStatusHandler(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. //DATA.Subscribe($"{Module}.{Name}.Power", () => _power);
  163. //DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => _errorCode);
  164. //DATA.Subscribe($"{Module}.{Name}.IsAtSpeed", () => _isAtSpeed);
  165. //DATA.Subscribe($"{Module}.{Name}.IsAccelerate", () => _isAccelerate);
  166. string portName = SC.GetStringValue($"{_scRoot}.{Module}.{Name}.Address");
  167. Address = portName;
  168. int address = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.DeviceAddress");
  169. DeviceAddress = (byte)address;
  170. DATA.Subscribe($"{Name}.DeviceAddress", () => DeviceAddress);
  171. GroupAddress = (byte)SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.GroupAddress");
  172. DATA.Subscribe($"{Name}.GroupAddress", () => GroupAddress);
  173. Status = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Status");
  174. DATA.Subscribe($"{Name}.Status", () => StatusStringList[ChangedStatus(Status)]);
  175. _deviceAddress = address.ToString("D3");
  176. _enableLog = SC.GetValue<bool>($"{_scRoot}.{Module}.{Name}.EnableLogMessage");
  177. Speedfactor = (byte)SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Speedfactor");
  178. DATA.Subscribe($"{Name}.Speedfactor", () => Speedfactor);
  179. // _nMaxSpeed = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed1");
  180. MaxSpeed1 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed1");
  181. DATA.Subscribe($"{Name}.MaxSpeed1", () => MaxSpeed1);
  182. MaxSpeed2 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed2");
  183. DATA.Subscribe($"{Name}.MaxSpeed2", () => MaxSpeed2);
  184. MaxSpeed3 = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.MaxSpeed3");
  185. DATA.Subscribe($"{Name}.MaxSpeed3", () => MaxSpeed3);
  186. _connection = new FfuAAFConnection(portName);
  187. _connection.EnableLog(_enableLog);
  188. if (_connection.Connect())
  189. {
  190. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  191. }
  192. _thread = new PeriodicJob(1000, OnTimer, $"{Name} MonitorHandler", true);
  193. _ffuSpeed = SC.GetValue<int>($"{_scRoot}.{Module}.{Name}.Speed");
  194. DATA.Subscribe($"{Name}.Speed", () => _ffuSpeed);
  195. OP.Subscribe($"{Name}SetFFUGroupAddress", (cmd, param) =>
  196. {
  197. if (int.TryParse((string)param[0], out var groupAddress))
  198. {
  199. EV.PostWarningLog(Module, "Set FFU Group Address.");
  200. return false;
  201. }
  202. int.TryParse((string)param[2], out var newgroupAddress);
  203. GroupAddress = (byte)newgroupAddress;
  204. SetGroupAddress((byte)newgroupAddress);
  205. EV.PostInfoLog(Module, $"{Name}Set FFU Group Address {newgroupAddress}");
  206. return true;
  207. });
  208. OP.Subscribe($"{Name}GetFFUGroupAddress", (cmd, param) =>
  209. {
  210. if (int.TryParse((string)param[0], out var groupAddress))
  211. {
  212. EV.PostWarningLog(Module, "Get FFU Group Address.");
  213. return false;
  214. }
  215. GetGroupAddress();
  216. EV.PostInfoLog(Module, $"{Name}Get FFU Group Address {groupAddress}");
  217. return true;
  218. });
  219. OP.Subscribe($"{Name}SetFFUDeviceAddress", (cmd, param) =>
  220. {
  221. if (int.TryParse((string)param[0], out var groupAddress))
  222. {
  223. EV.PostWarningLog(Module, "Set FFU Device Address.");
  224. return false;
  225. }
  226. int.TryParse((string)param[2], out var newAddress);
  227. SetDeviceAddress((byte)newAddress);
  228. EV.PostInfoLog(Module, $"{Name}Set FFU Device Address {newAddress}");
  229. return true;
  230. });
  231. OP.Subscribe($"{Name}GetFFUDeviceAddress", (cmd, param) =>
  232. {
  233. if (int.TryParse((string)param[0], out var groupAddress))
  234. {
  235. EV.PostWarningLog(Module, "Get FFU Device Address.");
  236. return false;
  237. }
  238. GetDeviceAddress();
  239. EV.PostInfoLog(Module, $"{Name}Get FFU Device Address {groupAddress}");
  240. return true;
  241. });
  242. OP.Subscribe($"{Name}SetFFUSpeedfactor", (cmd, param) =>
  243. {
  244. if (int.TryParse((string)param[0], out var groupAddress))
  245. {
  246. EV.PostWarningLog(Module, "Set FFU speed factor.");
  247. return false;
  248. }
  249. int.TryParse((string)param[2], out var newSpeedFactor);
  250. SetSpeedFactor((byte)newSpeedFactor);
  251. EV.PostInfoLog(Module, $"{Name}Set FFU speed factor {newSpeedFactor}");
  252. return true;
  253. });
  254. OP.Subscribe($"{Name}GetFFUSpeedfactor", (cmd, param) =>
  255. {
  256. if (int.TryParse((string)param[0], out var groupAddress))
  257. {
  258. EV.PostWarningLog(Module, "Get FFU speed factor.");
  259. return false;
  260. }
  261. QuerySpeedFactor();
  262. EV.PostInfoLog(Module, $"{Name}Get FFU speed factor {groupAddress}");
  263. return true;
  264. });
  265. OP.Subscribe($"{Name}GetFFUStatus", (cmd, param) =>
  266. {
  267. if (int.TryParse((string)param[0], out var groupAddress))
  268. {
  269. EV.PostWarningLog(Module, "Get FFU Status.");
  270. return false;
  271. }
  272. QueryError();
  273. EV.PostInfoLog(Module, $"{Name}Get FFU Status {groupAddress}");
  274. return true;
  275. });
  276. OP.Subscribe($"{Name}SetFFUSpeed", (cmd, param) =>
  277. {
  278. if (int.TryParse((string)param[0], out var groupAddress))
  279. {
  280. EV.PostWarningLog(Module, "invalid speed.");
  281. return false;
  282. }
  283. int.TryParse((string)param[2], out var speed);
  284. //SetSpeed((int)(speed * 250 * 1.0 / 1440));
  285. SetSpeed(speed);
  286. EV.PostInfoLog(Module, $"{Name} speed set to {speed}");
  287. return true;
  288. });
  289. OP.Subscribe($"{Name}GetFFUSpeed", (cmd, param) =>
  290. {
  291. if (int.TryParse((string)param[0], out var groupAddress))
  292. {
  293. EV.PostWarningLog(Module, "FFU Query speed.");
  294. return false;
  295. }
  296. QuerySpeed();
  297. EV.PostInfoLog(Module, $"{Name}FFU Query Speed {groupAddress}");
  298. return true;
  299. });
  300. OP.Subscribe($"{Name}GetFFUMaxSpeed1", (cmd, param) =>
  301. {
  302. if (int.TryParse((string)param[0], out var groupAddress))
  303. {
  304. EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed1.");
  305. return false;
  306. }
  307. QueryMaxSpeed1();
  308. EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed1 {groupAddress}");
  309. return true;
  310. });
  311. OP.Subscribe($"{Name}GetFFUMaxSpeed2", (cmd, param) =>
  312. {
  313. if (int.TryParse((string)param[0], out var groupAddress))
  314. {
  315. EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed2.");
  316. return false;
  317. }
  318. QueryMaxSpeed2();
  319. EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed2 {groupAddress}");
  320. return true;
  321. });
  322. OP.Subscribe($"{Name}GetFFUMaxSpeed3", (cmd, param) =>
  323. {
  324. if (int.TryParse((string)param[0], out var groupAddress))
  325. {
  326. EV.PostWarningLog(Module, "Get FFU GetFFUMaxSpeed3.");
  327. return false;
  328. }
  329. QueryMaxSpeed3();
  330. EV.PostInfoLog(Module, $"{Name}Get FFU GetFFUMaxSpeed3 {groupAddress}");
  331. return true;
  332. });
  333. OP.Subscribe($"{Name}SetFFUReset", (cmd, param) =>
  334. {
  335. if (int.TryParse((string)param[0], out var groupAddress))
  336. {
  337. EV.PostWarningLog(Module, "Set FFU Reset.");
  338. return false;
  339. }
  340. ResetDevice();
  341. EV.PostInfoLog(Module, $"{Name}Set FFU Reset {groupAddress}");
  342. return true;
  343. });
  344. OP.Subscribe($"{Name}.SetFFUSpeedBySC", (cmd, param) =>
  345. {
  346. int speed = SC.GetValue<int>("System.FFUFanSpeedSetPoint");
  347. SetSpeed(speed);
  348. EV.PostInfoLog(Module, $"Set {Name} speed");
  349. return true;
  350. });
  351. ConnectionManager.Instance.Subscribe($"{Name}", this);
  352. return true;
  353. }
  354. private bool OnTimer()
  355. {
  356. try
  357. {
  358. _connection.MonitorTimeout();
  359. if (!_connection.IsConnected)// || _connection.IsCommunicationError
  360. {
  361. lock (_locker)
  362. {
  363. _lstHandler.Clear();
  364. }
  365. _trigRetryConnect.CLK = !_connection.IsConnected;
  366. if (_trigRetryConnect.Q)
  367. {
  368. _connection.SetPortAddress(SC.GetStringValue($"{_scRoot}.{Name}.Address"));
  369. if (!_connection.Connect())
  370. {
  371. EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  372. }
  373. }
  374. return true;
  375. }
  376. HandlerBase handler = null;
  377. if (!_connection.IsBusy)
  378. {
  379. lock (_locker)
  380. {
  381. if (_lstHandler.Count == 0 && _activeMonitorStatus)
  382. {
  383. QuerySpeed();
  384. }
  385. if (_lstHandler.Count > 0)
  386. {
  387. handler = _lstHandler.First.Value;
  388. _lstHandler.RemoveFirst();
  389. }
  390. }
  391. if (handler != null)
  392. {
  393. _connection.Execute(handler);
  394. }
  395. }
  396. return true;
  397. }
  398. catch (Exception ex)
  399. {
  400. LOG.Write(ex);
  401. }
  402. return true;
  403. }
  404. internal void NoteError()
  405. {
  406. }
  407. public void Monitor()
  408. {
  409. try
  410. {
  411. _connection.EnableLog(_enableLog);
  412. _trigPumpOnOff.CLK = _isOn;
  413. if (_trigPumpOnOff.R)
  414. {
  415. EV.PostInfoLog(Module, $"{Module}.{Name} is on");
  416. }
  417. if (_trigPumpOnOff.T)
  418. {
  419. EV.PostInfoLog(Module, $"{Module}.{Name} is off");
  420. }
  421. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  422. if (_trigCommunicationError.Q)
  423. {
  424. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  425. }
  426. }
  427. catch (Exception ex)
  428. {
  429. LOG.Write(ex);
  430. }
  431. }
  432. public void Terminate()
  433. {
  434. try
  435. {
  436. if (_connection != null)
  437. {
  438. _connection.Disconnect();
  439. _connection = null;
  440. }
  441. }
  442. catch (Exception ex)
  443. {
  444. LOG.Write(ex);
  445. }
  446. }
  447. public void Reset()
  448. {
  449. _trigError.RST = true;
  450. _trigOverTemp.RST = true;
  451. _trigWarningMessage.RST = true;
  452. _connection.SetCommunicationError(false, "");
  453. _trigCommunicationError.RST = true;
  454. _enableLog = SC.GetValue<bool>($"System.{Module}.{Name}.EnableLogMessage");
  455. _trigRetryConnect.RST = true;
  456. }
  457. public void SetPumpOnOff(bool isOn)
  458. {
  459. lock (_locker)
  460. {
  461. }
  462. }
  463. public void SetActiveMonitor(bool active)
  464. {
  465. _activeMonitorStatus = active;
  466. }
  467. internal void NoteOnOff(bool isOn)
  468. {
  469. _isOn = isOn;
  470. }
  471. public void SetSpeed(float speed)
  472. {
  473. lock (_locker)
  474. {
  475. _lstHandler.Clear();
  476. if (_connection.IsBusy) _connection.ForceClear();
  477. List<byte> cmddata = new List<byte>() { 0x35, 0x40, 0x0, (byte)(speed * 250.0 / _nMaxSpeed) };
  478. byte checksum = ModRTU_CRC(cmddata.ToArray());
  479. cmddata.Add(checksum);
  480. _connection.SendMessage(cmddata.ToArray());
  481. }
  482. }
  483. public void SetErrorCode(int errorCode)
  484. {
  485. _errorCode = errorCode;
  486. }
  487. public void SetError(string reason)
  488. {
  489. _trigWarningMessage.CLK = true;
  490. if (_trigWarningMessage.Q)
  491. {
  492. EV.PostWarningLog(Module, $"{Module}.{Name} error, {reason}");
  493. }
  494. }
  495. private static byte ModRTU_CRC(byte[] buffer)
  496. {
  497. //ushort crc = 0xFFFF;
  498. // var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
  499. var buf = buffer;
  500. var len = buffer.Length;
  501. byte temp = buffer[0];
  502. for (int i = 1; i < buffer.Length; i++)
  503. {
  504. temp = (byte)(temp ^ buffer[i]);
  505. }
  506. return (byte)~temp;
  507. }
  508. }
  509. }