RobotViewModel.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.Threading.Tasks;
  4. using System.Windows.Threading;
  5. using ELog;
  6. using Microsoft.Practices.Prism.Events;
  7. using Microsoft.Practices.Prism.ViewModel;
  8. using ENet;
  9. using Infrastructure;
  10. namespace Modules.Robot
  11. {
  12. [Export(contractType: typeof (RobotViewModel)), PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  13. internal class RobotViewModel : NotificationObject
  14. {
  15. private ENetHost host;
  16. private string logText = "";
  17. private IEventAggregator eventAggregator = new EventAggregator();
  18. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  19. {
  20. Interval = new TimeSpan(0, 0, 0, 0, 50)
  21. };
  22. public string LogText
  23. {
  24. get
  25. {
  26. return this.logText;
  27. }
  28. set
  29. {
  30. if (this.logText == value)
  31. {
  32. return;
  33. }
  34. this.logText = value;
  35. this.RaisePropertyChanged("LogText");
  36. }
  37. }
  38. public RobotViewModel()
  39. {
  40. Library.Initialize();
  41. host = new ENetHost(8888, Native.ENET_PROTOCOL_MAXIMUM_PEER_ID);
  42. timer.Tick += delegate { this.host.Run(); };
  43. timer.Start();
  44. }
  45. public async void StartClient()
  46. {
  47. try
  48. {
  49. await host.ConnectAsync(new Address { Host = "192.168.10.246", Port = 8901 }, 2, 0);
  50. }
  51. catch (ENetException e)
  52. {
  53. Log.Debug(e.Message);
  54. return;
  55. }
  56. }
  57. public void Start()
  58. {
  59. for (int i = 0; i < 4095; ++i)
  60. {
  61. StartClient();
  62. }
  63. }
  64. }
  65. }