StateHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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)
  48. return;
  49. states.ForEach(
  50. state =>
  51. {
  52. if (this.States.ContainsKey(state.Key))
  53. {
  54. this.States[state.Key].DisplayValue = state.DisplayValue;
  55. }
  56. }
  57. );
  58. });
  59. }
  60. /// <summary>
  61. /// Register keys for each page or common page
  62. /// </summary>
  63. /// <param name="keys"></param>
  64. /// <param name="isCommon"></param>
  65. public void Register(List<string> keys, bool isCommon = false)
  66. {
  67. lock (lockObj)
  68. {
  69. if (isCommon)
  70. {
  71. keys.ForEach(e =>
  72. {
  73. if (!this.CommonKeys.Contains(e))
  74. this.CommonKeys.Add(e);
  75. });
  76. }
  77. else
  78. {
  79. this.Keys.Clear();
  80. this.Keys.AddRange(keys);
  81. }
  82. }
  83. }
  84. /// <summary>
  85. ///remove a page's key
  86. /// </summary>
  87. /// <param name="keys"></param>
  88. public void UnRegister(List<string> keys)
  89. {
  90. lock(lockObj)
  91. {
  92. this.Keys.Clear();
  93. }
  94. }
  95. public Dictionary<string, State> States { get; private set; }
  96. public MsgPool looper { get; private set; }
  97. private List<string> Keys { get; set; }
  98. private List<string> CommonKeys { get; set; }
  99. private Func<List<string>, List<State>> GetStates;
  100. private static object lockObj = new object();
  101. }
  102. }