StartStopMineButtonViewModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using NTMiner.Bus;
  2. using System.Windows.Input;
  3. namespace NTMiner.Vms {
  4. public class StartStopMineButtonViewModel : ViewModelBase {
  5. public static readonly StartStopMineButtonViewModel Instance = new StartStopMineButtonViewModel();
  6. public ICommand StartMine { get; private set; }
  7. public ICommand StopMine { get; private set; }
  8. private StartStopMineButtonViewModel() {
  9. if (Design.IsInDesignMode) {
  10. return;
  11. }
  12. #if DEBUG
  13. Write.Stopwatch.Restart();
  14. #endif
  15. this.StartMine = new DelegateCommand(() => {
  16. VirtualRoot.ThisWorkerMessage(nameof(StartStopMineButtonViewModel), WorkerMessageType.Info, $"手动开始挖矿", toConsole: true);
  17. this.MinerProfile.IsMining = true;
  18. NTMinerRoot.Instance.StartMine();
  19. BtnStopText = "正在挖矿";
  20. });
  21. this.StopMine = new DelegateCommand(() => {
  22. VirtualRoot.ThisWorkerMessage(nameof(StartStopMineButtonViewModel), WorkerMessageType.Info, $"手动停止挖矿", toConsole: true);
  23. if (!NTMinerRoot.Instance.IsMining) {
  24. this.MinerProfile.IsMining = false;
  25. }
  26. NTMinerRoot.IsAutoStartCanceled = true;
  27. NTMinerRoot.Instance.StopMineAsync(StopMineReason.LocalUserAction, () => {
  28. if (!NTMinerRoot.Instance.IsMining) {
  29. this.MinerProfile.IsMining = false;
  30. }
  31. });
  32. });
  33. #if DEBUG
  34. Write.DevTimeSpan($"耗时{Write.Stopwatch.ElapsedMilliseconds}毫秒 {this.GetType().Name}.ctor");
  35. #endif
  36. }
  37. public void AutoStart() {
  38. bool IsAutoStart = (MinerProfile.IsAutoStart || CommandLineArgs.IsAutoStart);
  39. if (IsAutoStart && !this.MinerProfile.IsMining) {
  40. this.MinerProfile.IsMining = true;
  41. int n = MinerProfile.AutoStartDelaySeconds;
  42. IMessagePathId handler = null;
  43. handler = VirtualRoot.BuildEventPath<Per1SecondEvent>("挖矿倒计时", LogEnum.None,
  44. action: message => {
  45. if (NTMinerRoot.IsAutoStartCanceled) {
  46. BtnStopText = $"尚未开始";
  47. n = 0;
  48. }
  49. else {
  50. BtnStopText = $"倒计时{--n}";
  51. }
  52. if (n <= 0) {
  53. VirtualRoot.DeletePath(handler);
  54. if (!NTMinerRoot.IsAutoStartCanceled) {
  55. BtnStopText = "正在挖矿";
  56. MinerProfile.IsMining = true;
  57. VirtualRoot.ThisWorkerMessage(nameof(StartStopMineButtonViewModel), WorkerMessageType.Info, $"自动开始挖矿", toConsole: true);
  58. NTMinerRoot.Instance.StartMine();
  59. }
  60. }
  61. });
  62. }
  63. }
  64. private string _btnStopText = "正在挖矿";
  65. public string BtnStopText {
  66. get => _btnStopText;
  67. set {
  68. _btnStopText = value;
  69. OnPropertyChanged(nameof(BtnStopText));
  70. }
  71. }
  72. public MinerProfileViewModel MinerProfile {
  73. get {
  74. return AppContext.Instance.MinerProfileVm;
  75. }
  76. }
  77. }
  78. }