MinerClientAddViewModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using NTMiner.Vms;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Windows.Input;
  7. namespace NTMiner.MinerStudio.Vms {
  8. public class MinerClientAddViewModel : ViewModelBase {
  9. public readonly Guid Id = Guid.NewGuid();
  10. private string _leftIp = "192.168.0.100";
  11. private string _rightIp = "192.168.0.200";
  12. private bool _isIpRange;
  13. public ICommand Save { get; private set; }
  14. public MinerClientAddViewModel() {
  15. if (WpfUtil.IsInDesignMode) {
  16. return;
  17. }
  18. this.Save = new DelegateCommand(() => {
  19. if (!IPAddress.TryParse(this.LeftIp, out _)) {
  20. VirtualRoot.Out.ShowError("IP格式不正确", autoHideSeconds: 4);
  21. return;
  22. }
  23. if (this.IsIpRange) {
  24. if (!IPAddress.TryParse(this.RightIp, out _)) {
  25. VirtualRoot.Out.ShowError("IP格式不正确", autoHideSeconds: 4);
  26. return;
  27. }
  28. List<string> clientIps = Net.IpUtil.CreateIpRange(this.LeftIp, this.RightIp);
  29. if (clientIps.Count > 101) {
  30. VirtualRoot.Out.ShowError("最多支持一次添加101个IP", autoHideSeconds: 4);
  31. return;
  32. }
  33. if (clientIps.Count == 0) {
  34. VirtualRoot.Out.ShowError("没有IP", autoHideSeconds: 4);
  35. return;
  36. }
  37. ((ILocalMinerStudioService)MinerStudioRoot.MinerStudioService).AddClientsAsync(clientIps, (response, e) => {
  38. if (!response.IsSuccess()) {
  39. VirtualRoot.Out.ShowError(response.ReadMessage(e));
  40. }
  41. else {
  42. MinerStudioRoot.MinerClientsWindowVm.QueryMinerClients();
  43. VirtualRoot.Execute(new CloseWindowCommand(this.Id));
  44. }
  45. });
  46. }
  47. else {
  48. ((ILocalMinerStudioService)MinerStudioRoot.MinerStudioService).AddClientsAsync(new List<string> { this.LeftIp }, (response, e) => {
  49. if (!response.IsSuccess()) {
  50. VirtualRoot.Out.ShowError(response.ReadMessage(e));
  51. }
  52. else {
  53. MinerStudioRoot.MinerClientsWindowVm.QueryMinerClients();
  54. VirtualRoot.Execute(new CloseWindowCommand(this.Id));
  55. }
  56. });
  57. }
  58. });
  59. var localIp = VirtualRoot.LocalIpSet.AsEnumerable().FirstOrDefault();
  60. if (localIp != null) {
  61. uint left = Net.IpUtil.ConvertToIpNum(localIp.DefaultIPGateway) + 1;
  62. this._leftIp = Net.IpUtil.ConvertToIpString(left);
  63. this._rightIp = Net.IpUtil.ConvertToIpString(left + 100);
  64. }
  65. }
  66. public string LeftIp {
  67. get => _leftIp;
  68. set {
  69. _leftIp = value;
  70. OnPropertyChanged(nameof(LeftIp));
  71. if (!IPAddress.TryParse(value, out _)) {
  72. throw new ValidationException("IP格式不正确");
  73. }
  74. }
  75. }
  76. public bool IsIpRange {
  77. get => _isIpRange;
  78. set {
  79. _isIpRange = value;
  80. OnPropertyChanged(nameof(IsIpRange));
  81. }
  82. }
  83. public string RightIp {
  84. get => _rightIp;
  85. set {
  86. _rightIp = value;
  87. OnPropertyChanged(nameof(RightIp));
  88. if (!IPAddress.TryParse(value, out _)) {
  89. throw new ValidationException("IP格式不正确");
  90. }
  91. }
  92. }
  93. }
  94. }