MinerStudioRoot.partials.MinerClientConsoleViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using NTMiner.Core;
  2. using NTMiner.MinerStudio.Vms;
  3. using NTMiner.Vms;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace NTMiner.MinerStudio {
  8. public static partial class MinerStudioRoot {
  9. public class MinerClientConsoleViewModel : ViewModelBase {
  10. private readonly List<ConsoleOutLine> _outLines = new List<ConsoleOutLine>();
  11. private readonly object _locker = new object();
  12. private MinerClientViewModel _minerClientVm;
  13. private DateTime _latestTimestamp = Timestamp.UnixBaseTime;
  14. public DateTime LatestTimestamp {
  15. get { return _latestTimestamp; }
  16. set {
  17. if (_latestTimestamp != value) {
  18. _latestTimestamp = value;
  19. OnPropertyChanged(nameof(LatestTimestamp));
  20. OnPropertyChanged(nameof(LatestTimeSpanText));
  21. }
  22. }
  23. }
  24. public string LatestTimeSpanText {
  25. get {
  26. if (LatestTimestamp == Timestamp.UnixBaseTime) {
  27. return string.Empty;
  28. }
  29. return Timestamp.GetTimeSpanBeforeText(LatestTimestamp);
  30. }
  31. }
  32. public MinerClientConsoleViewModel() {
  33. if (ClientAppType.IsMinerStudio) {
  34. VirtualRoot.BuildEventPath<MinerClientSelectionChangedEvent>("刷新矿机控制台输出", LogEnum.DevConsole, this.GetType(), PathPriority.Normal, path: message => {
  35. bool isChanged = true;
  36. if (message.MinerClientVm != null && this._minerClientVm != null && this._minerClientVm.ClientId == message.MinerClientVm.ClientId) {
  37. isChanged = false;
  38. }
  39. LatestTimestamp = Timestamp.UnixBaseTime;
  40. if (isChanged) {
  41. lock (_locker) {
  42. _outLines.Clear();
  43. try {
  44. Console.Clear();
  45. }
  46. catch {
  47. }
  48. this._minerClientVm = message.MinerClientVm;
  49. }
  50. SendGetConsoleOutLinesMqMessage(isFast: true);
  51. }
  52. });
  53. VirtualRoot.BuildEventPath<ClientConsoleOutLinesEvent>("将收到的挖矿端控制台消息输出到输出窗口", LogEnum.DevConsole, this.GetType(), PathPriority.Normal, path: message => {
  54. if (this._minerClientVm == null
  55. || this._minerClientVm.ClientId != message.ClientId
  56. || message.Data == null
  57. || message.Data.Count == 0) {
  58. return;
  59. }
  60. lock (_locker) {
  61. foreach (var item in message.Data) {
  62. _outLines.Add(item);
  63. NTMinerConsole.UserLine(item.Line, ConsoleColor.White, withPrefix: false);
  64. }
  65. // 因为客户端的时间可能不准所以不能使用客户端的时间
  66. LatestTimestamp = DateTime.Now;
  67. }
  68. });
  69. VirtualRoot.BuildEventPath<Per5SecondEvent>("周期获取当前选中的那台矿机的控制台输出", LogEnum.DevConsole, this.GetType(), PathPriority.Normal, path: message => {
  70. SendGetConsoleOutLinesMqMessage(isFast: false);
  71. });
  72. VirtualRoot.BuildEventPath<Per1SecondEvent>("客户端控制台输出倒计时秒表", LogEnum.None, this.GetType(), PathPriority.Normal, path: message => {
  73. if (this._minerClientVm == null || this._latestTimestamp == Timestamp.UnixBaseTime) {
  74. return;
  75. }
  76. OnPropertyChanged(nameof(LatestTimeSpanText));
  77. });
  78. }
  79. }
  80. private void SendGetConsoleOutLinesMqMessage(bool isFast) {
  81. if (this._minerClientVm == null) {
  82. return;
  83. }
  84. long afterTime = 0;
  85. var minerClientVm = this._minerClientVm;
  86. lock (_locker) {
  87. var item = _outLines.LastOrDefault();
  88. if (item != null) {
  89. afterTime = item.Timestamp;
  90. }
  91. }
  92. if (isFast) {
  93. MinerStudioService.FastGetConsoleOutLinesAsync(minerClientVm, afterTime);
  94. }
  95. else {
  96. MinerStudioService.GetConsoleOutLinesAsync(minerClientVm, afterTime);
  97. }
  98. }
  99. }
  100. }
  101. }