RobotViewModel.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 string command = "";
  17. private readonly LoginClient.LoginClient loginClient = new LoginClient.LoginClient();
  18. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  19. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  20. public string LoginIP
  21. {
  22. get
  23. {
  24. return this.loginIP;
  25. }
  26. set
  27. {
  28. if (this.loginIP == value)
  29. {
  30. return;
  31. }
  32. this.loginIP = value;
  33. this.RaisePropertyChanged("LoginIP");
  34. }
  35. }
  36. public ushort LoginPort
  37. {
  38. get
  39. {
  40. return this.loginPort;
  41. }
  42. set
  43. {
  44. if (this.loginPort == value)
  45. {
  46. return;
  47. }
  48. this.loginPort = value;
  49. this.RaisePropertyChanged("LoginPort");
  50. }
  51. }
  52. public string Account
  53. {
  54. get
  55. {
  56. return this.account;
  57. }
  58. set
  59. {
  60. if (this.account == value)
  61. {
  62. return;
  63. }
  64. this.account = value;
  65. this.RaisePropertyChanged("Account");
  66. }
  67. }
  68. public string Password
  69. {
  70. get
  71. {
  72. return this.password;
  73. }
  74. set
  75. {
  76. if (this.password == value)
  77. {
  78. return;
  79. }
  80. this.password = value;
  81. this.RaisePropertyChanged("Password");
  82. }
  83. }
  84. public string Command
  85. {
  86. get
  87. {
  88. return this.command;
  89. }
  90. set
  91. {
  92. if (this.command == value)
  93. {
  94. return;
  95. }
  96. this.command = value;
  97. this.RaisePropertyChanged("Command");
  98. }
  99. }
  100. public RobotViewModel()
  101. {
  102. this.timer.Tick += delegate { this.loginClient.RunOnce(); };
  103. this.timer.Start();
  104. }
  105. ~RobotViewModel()
  106. {
  107. this.Disposing(false);
  108. }
  109. public void Dispose()
  110. {
  111. this.Disposing(true);
  112. GC.SuppressFinalize(this);
  113. }
  114. private void Disposing(bool disposing)
  115. {
  116. this.loginClient.Dispose();
  117. }
  118. public void Login()
  119. {
  120. try
  121. {
  122. this.loginClient.Login(
  123. this.LoginIP, this.LoginPort, this.Account, this.Password);
  124. }
  125. catch (Exception e)
  126. {
  127. Logger.Trace("realm exception: {0}, {1}", e.Message, e.StackTrace);
  128. }
  129. }
  130. public void SendCommand()
  131. {
  132. this.loginClient.SendCommand(this.Command);
  133. }
  134. }
  135. }