RobotViewModel.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Windows.Threading;
  4. using Log;
  5. using Microsoft.Practices.Prism.ViewModel;
  6. namespace Modules.Robot
  7. {
  8. [Export(contractType: typeof (RobotViewModel)),
  9. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  10. internal sealed class RobotViewModel: NotificationObject, IDisposable
  11. {
  12. private string loginIP = "192.168.11.95";
  13. private ushort loginPort = 8888;
  14. private string account = "egametang@163.com";
  15. private string password = "163bio1";
  16. private readonly LoginClient.LoginClient loginClient = new LoginClient.LoginClient();
  17. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  18. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  19. public string LoginIP
  20. {
  21. get
  22. {
  23. return this.loginIP;
  24. }
  25. set
  26. {
  27. if (this.loginIP == value)
  28. {
  29. return;
  30. }
  31. this.loginIP = value;
  32. this.RaisePropertyChanged("LoginIP");
  33. }
  34. }
  35. public ushort LoginPort
  36. {
  37. get
  38. {
  39. return this.loginPort;
  40. }
  41. set
  42. {
  43. if (this.loginPort == value)
  44. {
  45. return;
  46. }
  47. this.loginPort = value;
  48. this.RaisePropertyChanged("LoginPort");
  49. }
  50. }
  51. public string Account
  52. {
  53. get
  54. {
  55. return this.account;
  56. }
  57. set
  58. {
  59. if (this.account == value)
  60. {
  61. return;
  62. }
  63. this.account = value;
  64. this.RaisePropertyChanged("Account");
  65. }
  66. }
  67. public string Password
  68. {
  69. get
  70. {
  71. return this.password;
  72. }
  73. set
  74. {
  75. if (this.password == value)
  76. {
  77. return;
  78. }
  79. this.password = value;
  80. this.RaisePropertyChanged("Password");
  81. }
  82. }
  83. public RobotViewModel()
  84. {
  85. this.timer.Tick += delegate { this.loginClient.RunOnce(); };
  86. this.timer.Start();
  87. }
  88. ~RobotViewModel()
  89. {
  90. this.Disposing(false);
  91. }
  92. public void Dispose()
  93. {
  94. this.Disposing(true);
  95. GC.SuppressFinalize(this);
  96. }
  97. private void Disposing(bool disposing)
  98. {
  99. this.loginClient.Dispose();
  100. }
  101. public void Login()
  102. {
  103. try
  104. {
  105. // 登录
  106. this.loginClient.Login(
  107. this.LoginIP, this.LoginPort, this.Account, this.Password);
  108. }
  109. catch (Exception e)
  110. {
  111. Logger.Trace("realm exception: {0}, {1}", e.Message, e.StackTrace);
  112. }
  113. }
  114. }
  115. }