MinerStudioRoot.partials.MinerClientConsoleViewModel.cs 4.7 KB

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