StateHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Threading;
  8. using System.Threading;
  9. using OpenSEMI.Core.Msg;
  10. namespace OpenSEMI.ClientBase.Handlers
  11. {
  12. public class StatesHandler : IHandler
  13. {
  14. public StatesHandler(Func<List<string>, List<State>> funcGetStates,List<string> allkeys)
  15. {
  16. this.Keys = new List<string>();
  17. this.CommonKeys = new List<string>();
  18. this.States = new Dictionary<string, State>();
  19. this.looper = new MsgPool(500, Do);
  20. //define an external function to let client implement
  21. this.GetStates = funcGetStates;
  22. //init states list
  23. allkeys.ForEach(key => { this.States.Add(key, new State() { Key = key, DisplayValue = string.Empty }); });
  24. }
  25. public void Handle()
  26. {
  27. this.looper.Run();
  28. }
  29. public void Do(MsgPool pool)
  30. {
  31. List<string> summarykeys = new List<string>();
  32. lock (lockObj)
  33. {
  34. if (this.CommonKeys.Count == 0 && this.Keys.Count == 0)
  35. return;
  36. if (Application.Current == null || Application.Current.Dispatcher == null)
  37. return;
  38. //common and current page keys
  39. if (this.CommonKeys.Count > 0)
  40. summarykeys.AddRange(this.CommonKeys);
  41. if (this.Keys.Count > 0)
  42. summarykeys.AddRange(this.Keys);
  43. }
  44. List<State> states = GetStates(summarykeys);
  45. Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
  46. {
  47. if (states == null) return;
  48. states.ForEach(
  49. state =>
  50. {
  51. if (this.States.ContainsKey(state.Key))
  52. {
  53. this.States[state.Key].DisplayValue = state.DisplayValue;
  54. }
  55. }
  56. );
  57. });
  58. }
  59. /// <summary>
  60. /// Register keys for each page or common page
  61. /// </summary>
  62. /// <param name="keys"></param>
  63. /// <param name="isCommon"></param>
  64. public void Register(List<string> keys, bool isCommon = false)
  65. {
  66. lock (lockObj)
  67. {
  68. if (isCommon)
  69. {
  70. keys.ForEach(e =>
  71. {
  72. if (!this.CommonKeys.Contains(e))
  73. this.CommonKeys.Add(e);
  74. });
  75. }
  76. else
  77. {
  78. this.Keys.Clear();
  79. this.Keys.AddRange(keys);
  80. }
  81. }
  82. }
  83. /// <summary>
  84. ///remove a page's key
  85. /// </summary>
  86. /// <param name="keys"></param>
  87. public void UnRegister(List<string> keys)
  88. {
  89. lock(lockObj)
  90. {
  91. this.Keys.Clear();
  92. }
  93. }
  94. public Dictionary<string, State> States { get; private set; }
  95. public MsgPool looper { get; private set; }
  96. private List<string> Keys { get; set; }
  97. private List<string> CommonKeys { get; set; }
  98. private Func<List<string>, List<State>> GetStates;
  99. private static object lockObj = new object();
  100. }
  101. }