RobotViewModel.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Windows.Threading;
  4. using Microsoft.Practices.Prism.ViewModel;
  5. namespace Modules.Robot
  6. {
  7. [Export(contractType: typeof (RobotViewModel)),
  8. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  9. internal sealed class RobotViewModel: NotificationObject, IDisposable
  10. {
  11. private string loginIP = "192.168.11.95";
  12. private ushort loginPort = 8888;
  13. private string account = "egametang@163.com";
  14. private string password = "163bio1";
  15. private string command = "";
  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 string Command
  84. {
  85. get
  86. {
  87. return this.command;
  88. }
  89. set
  90. {
  91. if (this.command == value)
  92. {
  93. return;
  94. }
  95. this.command = value;
  96. this.RaisePropertyChanged("Command");
  97. }
  98. }
  99. public RobotViewModel()
  100. {
  101. this.timer.Tick += delegate { this.loginClient.RunOnce(); };
  102. this.timer.Start();
  103. }
  104. ~RobotViewModel()
  105. {
  106. this.Disposing(false);
  107. }
  108. public void Dispose()
  109. {
  110. this.Disposing(true);
  111. GC.SuppressFinalize(this);
  112. }
  113. private void Disposing(bool disposing)
  114. {
  115. this.loginClient.Dispose();
  116. }
  117. public void Login()
  118. {
  119. this.loginClient.Login(
  120. this.LoginIP, this.LoginPort, this.Account, this.Password);
  121. }
  122. public void SendCommand()
  123. {
  124. this.loginClient.SendCommand(this.Command);
  125. }
  126. }
  127. }