PlcBindingManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using PlcCom;
  2. using PlcComponent;
  3. using Sunny.UI.Win32;
  4. using System;
  5. using System.CodeDom;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using Timer = System.Windows.Forms.Timer;
  14. public class PlcBindingManager
  15. {
  16. private readonly List<ISingleOutputComponent> singleOutputComponents =new List<ISingleOutputComponent>();
  17. private readonly List<ISingleInputComponent> singleInputComponents = new List<ISingleInputComponent>();
  18. private readonly List<IMultiOutputComponent> multiOutputComponents = new List<IMultiOutputComponent>();
  19. private readonly List<IPlcMultiInputComponent> multiInputComponents = new List<IPlcMultiInputComponent>();
  20. private List<IPlcUiControl> controlsWithInvisibilityProperty = new List<IPlcUiControl>();
  21. private List<IPlcUiControl> controlsWithDisabilityProperty = new List<IPlcUiControl>();
  22. private readonly List<string> readAddressesOfSingleOutputComs = new List<string>();
  23. private readonly List<string> writeAddressesOfSingleInputComs = new List<string>();
  24. private TimeSpan TimeSpanReadOverTime = TimeSpan.FromSeconds(1);
  25. private List<Timer> timers=new List<Timer>();
  26. private Dictionary<string, List<IPlcUiControl>> dictInvisibilityTriggerNode_Controls;
  27. private Dictionary<string, List<IPlcUiControl>> dictDisabilityTriggerNode_Controls;
  28. public void Bind(List<IPlcBindableComponent> components,List<IPlcUiControl> controls)
  29. {
  30. foreach (var c in components)
  31. {
  32. if (c is ISingleOutputComponent soc)
  33. {
  34. if (soc.ReadNode != null && !string.IsNullOrEmpty(soc.ReadNode.Value))
  35. {
  36. singleOutputComponents.Add(soc);
  37. //readAddressesOfSingleOutputComs.Add(soc.ReadNode.Value);
  38. }
  39. }
  40. if (c is ISingleInputComponent sic)
  41. {
  42. if (sic.WriteNode != null && !string.IsNullOrEmpty(sic.WriteNode.Value))
  43. {
  44. singleInputComponents.Add(sic);
  45. writeAddressesOfSingleInputComs.Add(sic.WriteNode.Value);
  46. }
  47. }
  48. if (c is IMultiOutputComponent moc)
  49. {
  50. if (moc.ReadNodes != null)
  51. multiOutputComponents.Add(moc);
  52. }
  53. if (c is IPlcMultiInputComponent mic)
  54. {
  55. if (mic.WriteNodes != null)
  56. multiInputComponents.Add(mic);
  57. }
  58. }
  59. foreach (var item in controls)
  60. {
  61. if (item.InvisibilityTriggerNode != null)
  62. controlsWithInvisibilityProperty.Add(item);
  63. if (item.DisabilityTriggerNode != null)
  64. controlsWithDisabilityProperty.Add(item);
  65. }
  66. dictInvisibilityTriggerNode_Controls = controlsWithInvisibilityProperty
  67. .GroupBy(c => c.InvisibilityTriggerNode.Value)
  68. .ToDictionary(g => g.Key, g => g.ToList());
  69. dictDisabilityTriggerNode_Controls = controlsWithDisabilityProperty
  70. .GroupBy(c => c.DisabilityTriggerNode.Value)
  71. .ToDictionary(g => g.Key, g => g.ToList());
  72. }
  73. public void SetPlcContext(PlcContex context, int intervalMs = 100)
  74. {
  75. SetBingControlsProtocol(context.Protocol);
  76. if (context.Protocol.IsConnected)
  77. {
  78. /*********************************剔除绑定单个地址的无效组件地址,并开始刷新组件*********************************************/
  79. RemoveInvalidNodes(context, singleOutputComponents);//单个刷新一次,把无法读取的控件剔除掉
  80. readAddressesOfSingleOutputComs.AddRange(singleOutputComponents.Select(c=>c.ReadNode.Value).ToList());
  81. /**********************************剔除无效的控件不可见触发地址,并开始刷新组件********************************************/
  82. RemoveInvalidNodes(context, dictInvisibilityTriggerNode_Controls);//单个刷新一次,把无法读取的控件剔除掉
  83. /**********************************剔除无效的控件不可用触发地址,并开始刷新组件********************************************/
  84. RemoveInvalidNodes(context, dictDisabilityTriggerNode_Controls);//单个刷新一次,把无法读取的控件剔除掉
  85. if (singleOutputComponents.Count > 0 || dictInvisibilityTriggerNode_Controls.Count > 0 || dictDisabilityTriggerNode_Controls.Count > 0)
  86. {
  87. Timer _timer;
  88. _timer = new Timer();
  89. _timer.Interval = intervalMs;
  90. _timer.Tick += async (s, e) =>{
  91. ((Timer)s).Stop();
  92. if(singleOutputComponents.Count > 0) await RefreshSingleOutputComponentsByListAsync(context);
  93. if (dictInvisibilityTriggerNode_Controls.Count > 0) await RefreshInvisibilityTriggerNodeAsync(context, dictInvisibilityTriggerNode_Controls);
  94. if (dictDisabilityTriggerNode_Controls.Count > 0) await RefreshDisabilityTriggerNodeAsync(context, dictDisabilityTriggerNode_Controls);
  95. ((Timer)s).Start(); };// RemoveInvalidNodes(context);
  96. _timer.Start();
  97. timers.Add(_timer);
  98. }
  99. /**********************************剔除绑定多个地址的无效组件地址,并开始刷新组件********************************************/
  100. foreach (var c in multiOutputComponents)
  101. {
  102. if (c.BackgroundRefreshEnable)
  103. {
  104. var invalidNodes= c.RemoveInvalidNodes();
  105. foreach (var node in invalidNodes)
  106. {
  107. ReadOneNodeFailed.Invoke(c, node, 0);
  108. }
  109. if (c.ReadNodes.Count > 0)
  110. {
  111. var timer = new Timer();
  112. timer.Interval = c.ReadInterval;
  113. timer.Tick += async (s, e) => { ((Timer)s).Stop(); await RefreshMultiOutputComponentsByListAsync(context, c); ((Timer)s).Start(); };
  114. timer.Start();
  115. timers.Add(timer);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. void SetBingControlsProtocol(IPlcComProtocol plcComProtocol)
  122. {
  123. foreach (var component in singleOutputComponents)
  124. {
  125. component.Protocol = plcComProtocol;
  126. }
  127. foreach (var component in singleInputComponents)
  128. {
  129. component.Protocol = plcComProtocol;
  130. }
  131. foreach (var component in multiOutputComponents)
  132. {
  133. component.Protocol = plcComProtocol;
  134. }
  135. foreach (var component in multiInputComponents)
  136. {
  137. component.Protocol = plcComProtocol;
  138. }
  139. }
  140. public event Action ReadOK;
  141. public event Action<IPlcBindableComponent,Node,uint> ReadOneNodeFailed;
  142. public event Action<List<string>, uint> ReadMultiNodesFailed;
  143. public event Action ReadOverTime;
  144. /// <summary>
  145. /// 逐个读取单个输出的控件,目的是剔除掉无法读取的控件
  146. /// </summary>
  147. /// <param name="context"></param>
  148. private List<ISingleOutputComponent> RemoveInvalidNodes(PlcContex context, List<ISingleOutputComponent> components)
  149. {
  150. var toRemove = new List<ISingleOutputComponent>();
  151. List<string> strings = components.Select(n => n.ReadNode.Value).ToList();
  152. try
  153. {
  154. var rt= context.Protocol.ReadValue(strings, out List<object> results);
  155. if(rt==0)
  156. {
  157. for(int i = 0; i < strings.Count; i++)
  158. {
  159. if (results[i]==null)
  160. {
  161. toRemove.Add(components[i]);
  162. }
  163. }
  164. }
  165. }
  166. catch (Exception)
  167. {
  168. }
  169. foreach (var item in toRemove)
  170. {
  171. components.Remove(item);
  172. ReadOneNodeFailed.Invoke(item, item.ReadNode, 0);
  173. }
  174. return components;
  175. }
  176. private Dictionary<string, List<IPlcUiControl>> RemoveInvalidNodes(PlcContex context, Dictionary<string, List<IPlcUiControl>> keyValuePairs)
  177. {
  178. var toRemove = new List<(string,List<IPlcUiControl>)>();
  179. List<string> strings = keyValuePairs.Select(k => k.Key).ToList();
  180. try
  181. {
  182. var rt = context.Protocol.ReadValue(strings, out List<object> results);
  183. if (rt == 0)
  184. {
  185. for (int i = 0; i < strings.Count; i++)
  186. {
  187. if (results[i] == null)
  188. {
  189. toRemove.Add((strings[i],keyValuePairs[strings[i]]));
  190. keyValuePairs.Remove(strings[i]);
  191. }
  192. }
  193. }
  194. }
  195. catch (Exception)
  196. {
  197. }
  198. foreach (var item in toRemove)
  199. {
  200. foreach (var c in item.Item2)
  201. {
  202. ReadOneNodeFailed.Invoke((IPlcBindableComponent)c, new Node(item.Item1), 0);
  203. }
  204. }
  205. return keyValuePairs;
  206. }
  207. private int _isReading = 0; // 0 = idle, 1 = busy
  208. /// <summary>
  209. /// 批量读取页面中单个输出的控件
  210. /// </summary>
  211. /// <param name="context"></param>
  212. private async Task RefreshSingleOutputComponentsByListAsync(PlcContex context)
  213. {
  214. if (Interlocked.CompareExchange(ref _isReading, 1, 0) == 0)
  215. {
  216. try
  217. {
  218. List<object> results;
  219. Stopwatch stopwatch = Stopwatch.StartNew();
  220. var task = Task.Run(() =>
  221. {
  222. var readRt = context.Protocol.ReadValue(readAddressesOfSingleOutputComs, out results);
  223. return (readRt, results);
  224. });
  225. // 使用超时等待
  226. if (await Task.WhenAny(task, Task.Delay(TimeSpanReadOverTime)) == task)
  227. {
  228. // 在超时前完成
  229. var rt = await task;
  230. if (rt.readRt == 0)
  231. {
  232. ReadOK?.Invoke();
  233. for (int i = 0; i < rt.results.Count; i++)
  234. {
  235. singleOutputComponents[i].UpdateValue(rt.results[i]);
  236. }
  237. }
  238. else
  239. {
  240. ReadMultiNodesFailed?.Invoke(readAddressesOfSingleOutputComs, rt.readRt);
  241. }
  242. }
  243. else
  244. {
  245. ReadOverTime?.Invoke();
  246. }
  247. }
  248. finally
  249. {
  250. _isReading = 0; // 重置
  251. }
  252. }
  253. }
  254. private async Task RefreshMultiOutputComponentsByListAsync(PlcContex context, IMultiOutputComponent c)
  255. {
  256. if (Interlocked.CompareExchange(ref _isReading, 1, 0) == 0)
  257. {
  258. try
  259. {
  260. Stopwatch stopwatch = Stopwatch.StartNew();
  261. List<string> strings = c.ReadNodes.Select(n => n.Value).ToList();
  262. var task = Task.Run(() =>
  263. {
  264. var readRt = context.Protocol.ReadValue(strings, out List<object> results);
  265. return (readRt, results);
  266. });
  267. // 使用超时等待
  268. if (await Task.WhenAny(task, Task.Delay(TimeSpanReadOverTime)) == task)
  269. {
  270. // 在超时前完成
  271. var rt = await task;
  272. if (rt.readRt == 0)
  273. {
  274. ReadOK?.Invoke();
  275. c.UpdateValues(c.ReadNodes, rt.results);
  276. }
  277. else
  278. {
  279. ReadMultiNodesFailed?.Invoke(strings, rt.readRt);
  280. }
  281. }
  282. else
  283. {
  284. ReadOverTime?.Invoke();
  285. }
  286. }
  287. finally
  288. {
  289. _isReading = 0; // 重置
  290. }
  291. }
  292. }
  293. private List<string> invisibilityTriggerNode;
  294. private bool _isCacheValid = false;
  295. private void EnsureInvisibilityTriggerNodes(Dictionary<string, List<IPlcUiControl>> dictNodeControls)
  296. {
  297. if (!_isCacheValid)
  298. {
  299. invisibilityTriggerNode = dictNodeControls.ToList().Select(kv => kv.Key).ToList();
  300. _isCacheValid = true;
  301. }
  302. }
  303. private async Task RefreshInvisibilityTriggerNodeAsync(PlcContex context, Dictionary<string, List<IPlcUiControl>> dictNodeControls)
  304. {
  305. if (Interlocked.CompareExchange(ref _isReading, 1, 0) == 0)
  306. {
  307. try
  308. {
  309. EnsureInvisibilityTriggerNodes(dictNodeControls);
  310. List<object> results;
  311. Stopwatch stopwatch = Stopwatch.StartNew();
  312. var task = Task.Run(() =>
  313. {
  314. var readRt = context.Protocol.ReadValue(invisibilityTriggerNode, out results);
  315. return (readRt, results);
  316. });
  317. // 使用超时等待
  318. if (await Task.WhenAny(task, Task.Delay(TimeSpanReadOverTime)) == task)
  319. {
  320. // 在超时前完成
  321. var rt = await task;
  322. if (rt.readRt == 0)
  323. {
  324. for (int i = 0; i < invisibilityTriggerNode.Count; i++)
  325. {
  326. if (rt.results[i] is bool b)
  327. {
  328. foreach (var control in dictNodeControls[invisibilityTriggerNode[i]])
  329. {
  330. control.Invisibility = control.ValueForInvisibility == b;
  331. }
  332. }
  333. }
  334. }
  335. else
  336. {
  337. ReadMultiNodesFailed?.Invoke(invisibilityTriggerNode, rt.readRt);
  338. }
  339. }
  340. }
  341. finally
  342. {
  343. _isReading = 0; // 重置
  344. }
  345. }
  346. }
  347. private List<string> disabilityTriggerNode;
  348. private bool _isCacheValid2 = false;
  349. private void EnsureDisabilityTriggerNodes(Dictionary<string, List<IPlcUiControl>> dictNodeControls)
  350. {
  351. if (!_isCacheValid2)
  352. {
  353. disabilityTriggerNode = dictNodeControls.ToList().Select(kv => kv.Key).ToList();
  354. _isCacheValid2 = true;
  355. }
  356. }
  357. private async Task RefreshDisabilityTriggerNodeAsync(PlcContex context, Dictionary<string, List<IPlcUiControl>> dictNodeControls)
  358. {
  359. if (Interlocked.CompareExchange(ref _isReading, 1, 0) == 0)
  360. {
  361. try
  362. {
  363. EnsureDisabilityTriggerNodes(dictNodeControls);
  364. List<object> results;
  365. Stopwatch stopwatch = Stopwatch.StartNew();
  366. var task = Task.Run(() =>
  367. {
  368. var readRt = context.Protocol.ReadValue(disabilityTriggerNode, out results);
  369. return (readRt, results);
  370. });
  371. // 使用超时等待
  372. if (await Task.WhenAny(task, Task.Delay(TimeSpanReadOverTime)) == task)
  373. {
  374. // 在超时前完成
  375. var rt = await task;
  376. if (rt.readRt == 0)
  377. {
  378. for (int i = 0; i < disabilityTriggerNode.Count; i++)
  379. {
  380. if (rt.results[i] is bool b)
  381. {
  382. foreach (var control in dictNodeControls[disabilityTriggerNode[i]])
  383. {
  384. control.Disability = control.ValueForDisability && b;
  385. }
  386. }
  387. }
  388. }
  389. else
  390. {
  391. ReadMultiNodesFailed?.Invoke(disabilityTriggerNode, rt.readRt);
  392. }
  393. }
  394. }
  395. finally
  396. {
  397. _isReading = 0; // 重置
  398. }
  399. }
  400. }
  401. public void Stop()
  402. {
  403. foreach (var item in timers)
  404. {
  405. item.Stop();
  406. }
  407. }
  408. }