RobotViewModel.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Threading.Tasks;
  4. using System.Windows.Threading;
  5. using BossClient;
  6. using Microsoft.Practices.Prism.ViewModel;
  7. namespace Modules.Robot
  8. {
  9. [Export(contractType: typeof (RobotViewModel)),
  10. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  11. internal sealed class RobotViewModel: NotificationObject, IDisposable
  12. {
  13. private string loginIP = "192.168.11.95";
  14. private ushort loginPort = 8888;
  15. private string account = "egametang@163.com";
  16. private string password = "163bio1";
  17. private string command = "";
  18. private bool isEnableSendCommandButton;
  19. private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
  20. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  21. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  22. public string LoginIP
  23. {
  24. get
  25. {
  26. return this.loginIP;
  27. }
  28. set
  29. {
  30. if (this.loginIP == value)
  31. {
  32. return;
  33. }
  34. this.loginIP = value;
  35. this.RaisePropertyChanged("LoginIP");
  36. }
  37. }
  38. public ushort LoginPort
  39. {
  40. get
  41. {
  42. return this.loginPort;
  43. }
  44. set
  45. {
  46. if (this.loginPort == value)
  47. {
  48. return;
  49. }
  50. this.loginPort = value;
  51. this.RaisePropertyChanged("LoginPort");
  52. }
  53. }
  54. public string Account
  55. {
  56. get
  57. {
  58. return this.account;
  59. }
  60. set
  61. {
  62. if (this.account == value)
  63. {
  64. return;
  65. }
  66. this.account = value;
  67. this.RaisePropertyChanged("Account");
  68. }
  69. }
  70. public string Password
  71. {
  72. get
  73. {
  74. return this.password;
  75. }
  76. set
  77. {
  78. if (this.password == value)
  79. {
  80. return;
  81. }
  82. this.password = value;
  83. this.RaisePropertyChanged("Password");
  84. }
  85. }
  86. public string Command
  87. {
  88. get
  89. {
  90. return this.command;
  91. }
  92. set
  93. {
  94. if (this.command == value)
  95. {
  96. return;
  97. }
  98. this.command = value;
  99. this.RaisePropertyChanged("Command");
  100. }
  101. }
  102. public bool IsEnableSendCommandButton
  103. {
  104. get
  105. {
  106. return this.isEnableSendCommandButton;
  107. }
  108. set
  109. {
  110. if (this.isEnableSendCommandButton == value)
  111. {
  112. return;
  113. }
  114. this.isEnableSendCommandButton = value;
  115. this.RaisePropertyChanged("IsEnableSendCommandButton");
  116. }
  117. }
  118. public RobotViewModel()
  119. {
  120. this.timer.Tick += delegate { this.bossClient.RunOnce(); };
  121. this.timer.Start();
  122. }
  123. ~RobotViewModel()
  124. {
  125. this.Disposing(false);
  126. }
  127. public void Dispose()
  128. {
  129. this.Disposing(true);
  130. GC.SuppressFinalize(this);
  131. }
  132. private void Disposing(bool disposing)
  133. {
  134. this.bossClient.Dispose();
  135. }
  136. public async Task Login()
  137. {
  138. await this.bossClient.Login(
  139. this.LoginIP, this.LoginPort, this.Account, this.Password);
  140. this.IsEnableSendCommandButton = true;
  141. this.bossClient.HandleMessages();
  142. }
  143. public void SendCommand()
  144. {
  145. this.bossClient.SendCommand(this.Command);
  146. }
  147. }
  148. }