Usf500N.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.FlowMeters
  15. {
  16. public class Usf500N : BaseDevice, IConnection, IDevice
  17. {
  18. private Usf500NConnection _connection;
  19. public byte DeviceAddress { get; set; }
  20. public FlowMeterData[] FlowMeterDataSet;
  21. public FlowMeterData GetFlowMeterData(string flowname)
  22. {
  23. foreach(var fdata in FlowMeterDataSet)
  24. {
  25. if (fdata.FlowName == flowname)
  26. return fdata;
  27. }
  28. return new FlowMeterData();
  29. }
  30. private R_TRIG _trigCommunicationError = new R_TRIG();
  31. private R_TRIG _trigRetryConnect = new R_TRIG();
  32. private PeriodicJob _thread;
  33. private LinkedList<HandlerBase> _lstHandler = new LinkedList<HandlerBase>();
  34. private object _locker = new object();
  35. private bool _enableLog = true;
  36. public string Address
  37. {
  38. get; set;
  39. }
  40. public bool IsConnected
  41. {
  42. get
  43. {
  44. return _connection != null && _connection.IsConnected;
  45. }
  46. }
  47. public bool Connect()
  48. {
  49. return true;
  50. }
  51. public bool Disconnect()
  52. {
  53. return true;
  54. }
  55. public Usf500N(string module, string name, Usf500NConnection conn, Tuple<string,int, int>[] addressChannel) : base(module, name, name, name)
  56. {
  57. _connection = conn;
  58. List<FlowMeterData> tempset = new List<FlowMeterData>();
  59. foreach (var addCh in addressChannel)
  60. {
  61. FlowMeterData data = new FlowMeterData(addCh.Item1, addCh.Item2,addCh.Item3);
  62. tempset.Add(data);
  63. }
  64. FlowMeterDataSet = tempset.ToArray();
  65. }
  66. public Usf500N(string module, string name, string comPort, Tuple<string, int, int>[] addressChannel) : base(module, name, name, name)
  67. {
  68. _connection = new Usf500NConnection(comPort);
  69. List<FlowMeterData> tempset = new List<FlowMeterData>();
  70. foreach (var addCh in addressChannel)
  71. {
  72. FlowMeterData data = new FlowMeterData(addCh.Item1, addCh.Item2, addCh.Item3);
  73. tempset.Add(data);
  74. }
  75. FlowMeterDataSet = tempset.ToArray();
  76. }
  77. public void QuerySpeed()
  78. {
  79. foreach(var flowdata in FlowMeterDataSet)
  80. {
  81. _lstHandler.AddLast(new Usf500NQueryFlowRateHandler(this,(byte)flowdata.Address,flowdata.Channel));
  82. _lstHandler.AddLast(new Usf500NQueryFlowVolumeHandler(this, (byte)flowdata.Address, flowdata.Channel));
  83. }
  84. //_lstHandler.AddLast(new FfuAAFQuerySpeedHandler(this, DeviceAddress, GroupAddress));
  85. }
  86. internal void ParseFlowRate(byte address, byte channel, byte[] readOutData)
  87. {
  88. try
  89. {
  90. for (int i = 0; i < FlowMeterDataSet.Length; i++)
  91. {
  92. if (FlowMeterDataSet[i].Address == address && FlowMeterDataSet[i].Channel == channel)
  93. {
  94. int nValue = 0;
  95. for(int j=0;j< readOutData.Length;j++)
  96. {
  97. nValue += readOutData[j] << (8* (readOutData.Length-j-1));
  98. }
  99. FlowMeterDataSet[i].FlowRate = nValue;
  100. }
  101. }
  102. }
  103. catch(Exception ex)
  104. {
  105. LOG.Write(ex);
  106. }
  107. }
  108. internal void ParseFlowVolume(byte address, byte channel, byte[] readOutData)
  109. {
  110. try
  111. {
  112. for (int i = 0; i < FlowMeterDataSet.Length; i++)
  113. {
  114. if (FlowMeterDataSet[i].Address == address && FlowMeterDataSet[i].Channel == channel)
  115. {
  116. int nValue = 0;
  117. for (int j = 0; j < readOutData.Length; j++)
  118. {
  119. nValue += readOutData[j] << (8 * (readOutData.Length - j - 1));
  120. }
  121. FlowMeterDataSet[i].TotalFlowVolume = nValue;
  122. }
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. LOG.Write(ex);
  128. }
  129. }
  130. public bool Initialize()
  131. {
  132. //DATA.Subscribe($"{Module}.{Name}.Power", () => _power);
  133. //DATA.Subscribe($"{Module}.{Name}.ErrorCode", () => _errorCode);
  134. //DATA.Subscribe($"{Module}.{Name}.IsAtSpeed", () => _isAtSpeed);
  135. //DATA.Subscribe($"{Module}.{Name}.IsAccelerate", () => _isAccelerate);
  136. DATA.Subscribe($"{Name}.DeviceAddress", () => DeviceAddress);
  137. DATA.Subscribe($"{Name}.FlowMeterNames", () => GetFlowMeterNames());
  138. if (_connection.Connect())
  139. {
  140. EV.PostInfoLog(Module, $"{Module}.{Name} connected");
  141. }
  142. _thread = new PeriodicJob(1000, OnTimer, $"{Name} MonitorHandler", true);
  143. //DATA.Subscribe($"{Name}.Speed", () => _ffuSpeed);
  144. OP.Subscribe($"{Name}.ResetVolume", (cmd, param) =>
  145. {
  146. if (param.Length <1)
  147. {
  148. EV.PostWarningLog(Module, "Wrong parameter.");
  149. return false;
  150. }
  151. ResetFlowVolumn(param[0].ToString());
  152. EV.PostInfoLog(Module, $"{Name} reset flow volume:{param[0].ToString()}");
  153. return true;
  154. });
  155. ConnectionManager.Instance.Subscribe($"{Name}", this);
  156. return true;
  157. }
  158. private List<string> GetFlowMeterNames()
  159. {
  160. List<string> ret = new List<string>();
  161. if (FlowMeterDataSet == null) return ret;
  162. foreach (var flow in FlowMeterDataSet)
  163. {
  164. ret.Add($"{flow.FlowName},{flow.Address},{flow.Channel},{flow.FlowRate},{flow.TotalFlowVolume}");
  165. }
  166. return ret;
  167. }
  168. public void ResetFlowVolumn(int address, int channel)
  169. {
  170. lock(_locker)
  171. {
  172. _lstHandler.AddLast(new Usf500NResetFlowVolumeHandler(this, (byte)address, channel));
  173. }
  174. }
  175. public void ResetFlowVolumn(string flowName)
  176. {
  177. foreach(var fdata in FlowMeterDataSet)
  178. {
  179. if(flowName == fdata.FlowName)
  180. {
  181. lock (_locker)
  182. {
  183. _lstHandler.AddLast(new Usf500NResetFlowVolumeHandler(this, (byte)fdata.Address, fdata.Channel));
  184. }
  185. }
  186. }
  187. }
  188. private bool OnTimer()
  189. {
  190. try
  191. {
  192. _connection.MonitorTimeout();
  193. if (!_connection.IsConnected)// || _connection.IsCommunicationError
  194. {
  195. lock (_locker)
  196. {
  197. _lstHandler.Clear();
  198. }
  199. //_trigRetryConnect.CLK = !_connection.IsConnected;
  200. //if (_trigRetryConnect.Q)
  201. //{
  202. // //_connection.SetPortAddress(SC.GetStringValue($"{_scRoot}.{Name}.Address"));
  203. // if (!_connection.Connect())
  204. // {
  205. // EV.PostAlarmLog(Module, $"Can not connect with {_connection.Address}, {Module}.{Name}");
  206. // }
  207. //}
  208. return true;
  209. }
  210. HandlerBase handler = null;
  211. if (!_connection.IsBusy)
  212. {
  213. lock (_locker)
  214. {
  215. if (_lstHandler.Count == 0 )
  216. {
  217. QuerySpeed();
  218. }
  219. if (_lstHandler.Count > 0)
  220. {
  221. handler = _lstHandler.First.Value;
  222. _lstHandler.RemoveFirst();
  223. }
  224. }
  225. if (handler != null)
  226. {
  227. _connection.Execute(handler);
  228. }
  229. }
  230. return true;
  231. }
  232. catch (Exception ex)
  233. {
  234. LOG.Write(ex);
  235. }
  236. return true;
  237. }
  238. internal void NoteError()
  239. {
  240. }
  241. public void Monitor()
  242. {
  243. try
  244. {
  245. _connection.EnableLog(_enableLog);
  246. _trigCommunicationError.CLK = _connection.IsCommunicationError;
  247. if (_trigCommunicationError.Q)
  248. {
  249. EV.PostAlarmLog(Module, $"{Module}.{Name} communication error, {_connection.LastCommunicationError}");
  250. }
  251. }
  252. catch (Exception ex)
  253. {
  254. LOG.Write(ex);
  255. }
  256. }
  257. public void Terminate()
  258. {
  259. try
  260. {
  261. if (_connection != null)
  262. {
  263. _connection.Disconnect();
  264. _connection = null;
  265. }
  266. }
  267. catch (Exception ex)
  268. {
  269. LOG.Write(ex);
  270. }
  271. }
  272. public void Reset()
  273. {
  274. _connection.SetCommunicationError(false, "");
  275. _trigCommunicationError.RST = true;
  276. _trigRetryConnect.RST = true;
  277. }
  278. }
  279. public struct FlowMeterData
  280. {
  281. public FlowMeterData(string Name,int address,int channel)
  282. {
  283. FlowName = Name;
  284. Address = address;
  285. Channel = channel;
  286. FlowRate = 0;
  287. TotalFlowVolume = 0;
  288. }
  289. public string FlowName { get; set; }
  290. public int Address { get; }
  291. public int Channel { get; }
  292. public int FlowRate { get; set; }
  293. public int TotalFlowVolume { get; set; }
  294. }
  295. }