AppContext.partials.CoinKernelViewModels.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using NTMiner.Core;
  2. using NTMiner.Vms;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. namespace NTMiner {
  7. public partial class AppContext {
  8. public class CoinKernelViewModels : ViewModelBase {
  9. public static readonly CoinKernelViewModels Instance = new CoinKernelViewModels();
  10. private readonly Dictionary<Guid, CoinKernelViewModel> _dicById = new Dictionary<Guid, CoinKernelViewModel>();
  11. private CoinKernelViewModels() {
  12. #if DEBUG
  13. Write.Stopwatch.Start();
  14. #endif
  15. VirtualRoot.AddEventPath<ServerContextReInitedEvent>("ServerContext刷新后刷新VM内存", LogEnum.DevConsole,
  16. action: message => {
  17. _dicById.Clear();
  18. Init();
  19. });
  20. VirtualRoot.AddEventPath<ServerContextVmsReInitedEvent>("ServerContext的VM集刷新后刷新视图界面", LogEnum.DevConsole,
  21. action: message => {
  22. OnPropertyChanged(nameof(AllCoinKernels));
  23. });
  24. BuildEventPath<CoinKernelAddedEvent>("添加了币种内核后刷新VM内存", LogEnum.DevConsole,
  25. action: (message) => {
  26. var coinKernelVm = new CoinKernelViewModel(message.Target);
  27. _dicById.Add(message.Target.GetId(), coinKernelVm);
  28. OnPropertyChanged(nameof(AllCoinKernels));
  29. if (AppContext.Instance.CoinVms.TryGetCoinVm((Guid)message.Target.CoinId, out CoinViewModel coinVm)) {
  30. coinVm.OnPropertyChanged(nameof(CoinViewModel.CoinKernel));
  31. coinVm.OnPropertyChanged(nameof(CoinViewModel.CoinKernels));
  32. coinVm.OnPropertyChanged(nameof(NTMiner.Vms.CoinViewModel.IsSupported));
  33. }
  34. var kernelVm = coinKernelVm.Kernel;
  35. if (kernelVm != null) {
  36. kernelVm.OnPropertyChanged(nameof(kernelVm.CoinKernels));
  37. kernelVm.OnPropertyChanged(nameof(kernelVm.CoinVms));
  38. kernelVm.OnPropertyChanged(nameof(kernelVm.SupportedCoinVms));
  39. kernelVm.OnPropertyChanged(nameof(kernelVm.SupportedCoins));
  40. }
  41. });
  42. BuildEventPath<CoinKernelUpdatedEvent>("更新了币种内核后刷新VM内存", LogEnum.DevConsole,
  43. action: (message) => {
  44. CoinKernelViewModel entity = _dicById[message.Target.GetId()];
  45. var supportedGpu = entity.SupportedGpu;
  46. Guid dualCoinGroupId = entity.DualCoinGroupId;
  47. entity.Update(message.Target);
  48. if (supportedGpu != entity.SupportedGpu) {
  49. var coinKernels = AllCoinKernels.Where(a => a.KernelId == entity.Id);
  50. foreach (var coinKernel in coinKernels) {
  51. if (AppContext.Instance.CoinVms.TryGetCoinVm(coinKernel.CoinId, out CoinViewModel coinVm)) {
  52. coinVm.OnPropertyChanged(nameof(coinVm.IsSupported));
  53. coinVm.OnPropertyChanged(nameof(coinVm.CoinKernels));
  54. }
  55. }
  56. var kernelVm = entity.Kernel;
  57. kernelVm.OnPropertyChanged(nameof(kernelVm.CoinKernels));
  58. }
  59. });
  60. BuildEventPath<CoinKernelRemovedEvent>("移除了币种内核后刷新VM内存", LogEnum.DevConsole,
  61. action: (message) => {
  62. if (_dicById.TryGetValue(message.Target.GetId(), out CoinKernelViewModel coinKernelVm)) {
  63. _dicById.Remove(message.Target.GetId());
  64. OnPropertyChanged(nameof(AllCoinKernels));
  65. if (AppContext.Instance.CoinVms.TryGetCoinVm((Guid)message.Target.CoinId, out CoinViewModel coinVm)) {
  66. coinVm.OnPropertyChanged(nameof(CoinViewModel.CoinKernel));
  67. coinVm.OnPropertyChanged(nameof(CoinViewModel.CoinKernels));
  68. coinVm.OnPropertyChanged(nameof(NTMiner.Vms.CoinViewModel.IsSupported));
  69. }
  70. var kernelVm = coinKernelVm.Kernel;
  71. kernelVm.OnPropertyChanged(nameof(kernelVm.CoinKernels));
  72. kernelVm.OnPropertyChanged(nameof(kernelVm.CoinVms));
  73. kernelVm.OnPropertyChanged(nameof(kernelVm.SupportedCoinVms));
  74. kernelVm.OnPropertyChanged(nameof(kernelVm.SupportedCoins));
  75. }
  76. });
  77. Init();
  78. #if DEBUG
  79. var elapsedMilliseconds = Write.Stopwatch.Stop();
  80. if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds) {
  81. Write.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.ctor");
  82. }
  83. #endif
  84. }
  85. private void Init() {
  86. foreach (var item in NTMinerRoot.Instance.ServerContext.CoinKernelSet.AsEnumerable()) {
  87. _dicById.Add(item.GetId(), new CoinKernelViewModel(item));
  88. }
  89. }
  90. public bool TryGetCoinKernelVm(Guid id, out CoinKernelViewModel vm) {
  91. return _dicById.TryGetValue(id, out vm);
  92. }
  93. public List<CoinKernelViewModel> AllCoinKernels {
  94. get {
  95. return _dicById.Values.ToList();
  96. }
  97. }
  98. }
  99. }
  100. }