PollingManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using MV485.model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MV485.helper
  9. {
  10. public class PollingManager
  11. {
  12. private Dictionary<string, SerialPollingGroup> _groups = new Dictionary<string, SerialPollingGroup>();
  13. //增加了设备明细
  14. public event Action<TSlaveDetail> SlaveDetailAdded;
  15. //改变了设备状态
  16. public event Action<string, RunStatusType> SlaveStatusChanged;
  17. private bool _blPause;
  18. public PollingManager(bool blPause)
  19. {
  20. _blPause = blPause;
  21. }
  22. public void Pause()
  23. {
  24. _blPause = true;
  25. foreach (var kvp in _groups)
  26. {
  27. var group = kvp.Value;
  28. group.Pause();
  29. }
  30. }
  31. public void Resume()
  32. {
  33. _blPause = false;
  34. foreach(var group in _groups.Values)
  35. {
  36. group.Resume();
  37. }
  38. }
  39. //增加设备
  40. public void AddDevice(TSlave device)
  41. {
  42. if(!_groups.TryGetValue(device.PortName,out var group))
  43. {
  44. group = new SerialPollingGroup(device.PortName,_blPause);
  45. group.SlaveDetailAdded += Group_OnAddSlaveDetail;
  46. group.SlaveStatusChanged += Group_OnSlaveRunStatusChanged;
  47. _groups[device.PortName] = group;
  48. group.StartPolling();
  49. }
  50. group.AddDevice(device);
  51. }
  52. private void Group_OnSlaveRunStatusChanged(string slaveId, RunStatusType status)
  53. {
  54. SlaveStatusChanged?.Invoke(slaveId, status);
  55. }
  56. private void Group_OnAddSlaveDetail(TSlaveDetail detail)
  57. {
  58. SlaveDetailAdded?.Invoke(detail);
  59. }
  60. //批量增加
  61. public void AddDevice(ObservableCollection<TSlave> slaveList)
  62. {
  63. foreach(var slave in slaveList)
  64. {
  65. AddDevice(slave);
  66. }
  67. }
  68. public void RemoveDevice(string slaveId)
  69. {
  70. var emptyPorts = new List<string>();
  71. foreach (var kvp in _groups)
  72. {
  73. var port = kvp.Key;
  74. var group = kvp.Value;
  75. group.RemoveDevice(slaveId);
  76. if (!group.HasAnyDevice())
  77. {
  78. group.StopPolling();
  79. emptyPorts.Add(port);
  80. }
  81. }
  82. // 清理空的串口分组
  83. foreach (var port in emptyPorts)
  84. {
  85. _groups.Remove(port);
  86. }
  87. }
  88. public void RemoveAllDevice()
  89. {
  90. var emptyPorts = new List<string>();
  91. foreach (var group in _groups.Values)
  92. {
  93. group.StopPolling();
  94. group.RemoveAllDevice();
  95. //_groups.Remove(group.PortName);
  96. emptyPorts.Add(group.PortName);
  97. }
  98. foreach(var port in emptyPorts)
  99. {
  100. _groups.Remove(port);
  101. }
  102. //for(int i = _groups.Count; i >= 0; i++)
  103. //{
  104. // _groups.Remove(_groups.Keys[]);
  105. //}
  106. }
  107. public void StopAll()
  108. {
  109. foreach(var group in _groups.Values)
  110. {
  111. group.StopPolling();
  112. }
  113. }
  114. ////////////////////////////////////////////////////////////////
  115. }
  116. }