SocketDeviceViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Input;
  9. using Aitex.Core.UI.MVVM;
  10. using MECF.Framework.Simulator.Core.Driver;
  11. namespace MECF.Framework.Simulator.Core.Commons
  12. {
  13. public class SocketDeviceViewModel:TimerViewModelBase
  14. {
  15. public ICommand ClearLogCommand { get; set; }
  16. public ICommand EnableCommand { get; set; }
  17. public ICommand DisableCommand { get; set; }
  18. public bool IsEnableEnable
  19. {
  20. get { return !_simulator.IsEnabled; }
  21. }
  22. public bool IsEnableDisable
  23. {
  24. get { return _simulator.IsEnabled; }
  25. }
  26. public bool IsConnected
  27. {
  28. get { return _simulator.IsConnected; }
  29. }
  30. public int LocalPort
  31. {
  32. get { return _simulator.LocalPort; }
  33. }
  34. public string RemoteConnection
  35. {
  36. get { return _simulator.RemoteConnection; }
  37. }
  38. public string ConnectionStatus
  39. {
  40. get
  41. {
  42. if (_simulator.IsConnected)
  43. return "Connected";
  44. if (_simulator.IsEnabled)
  45. return "Listening";
  46. return "Disable";
  47. }
  48. }
  49. private int maxItemNumber = 200;
  50. public string LocalPortSetPoint { get; set; }
  51. public ObservableCollection<TransactionLogItem> TransactionLogItems { get; set; }
  52. protected SocketDeviceSimulator _simulator;
  53. public SocketDeviceViewModel(string name) : base(name)
  54. {
  55. ClearLogCommand = new DelegateCommand<string>(ClearLog);
  56. EnableCommand = new DelegateCommand<string>(Enable);
  57. DisableCommand = new DelegateCommand<string>(Disable);
  58. TransactionLogItems = new ObservableCollection<TransactionLogItem>();
  59. LocalPortSetPoint = "";
  60. }
  61. protected void Init(SocketDeviceSimulator sim, bool enable=true)
  62. {
  63. LocalPortSetPoint = sim.LocalPort.ToString();
  64. _simulator = sim;
  65. _simulator.MessageOut += _simulator_MessageOut;
  66. _simulator.MessageIn += _simulator_MessageIn;
  67. if (enable)
  68. _simulator.Enable();
  69. }
  70. private void Disable(string obj)
  71. {
  72. _simulator.Disable();
  73. }
  74. private void Enable(string obj)
  75. {
  76. int port;
  77. if (int.TryParse(LocalPortSetPoint, out port))
  78. _simulator.LocalPort = port;
  79. _simulator.Enable();
  80. }
  81. private void ClearLog(string obj)
  82. {
  83. TransactionLogItems.Clear();
  84. }
  85. private void _simulator_MessageIn(string obj)
  86. {
  87. Application.Current.Dispatcher.Invoke(new Action(() =>
  88. {
  89. TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  90. if (TransactionLogItems.Count > maxItemNumber)
  91. TransactionLogItems = new ObservableCollection<TransactionLogItem>(TransactionLogItems.Skip(1).Take(maxItemNumber).ToList());
  92. Poll();
  93. }));
  94. }
  95. private void _simulator_MessageOut(string obj)
  96. {
  97. Application.Current.Dispatcher.Invoke(new Action(() =>
  98. {
  99. TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  100. if (TransactionLogItems.Count > maxItemNumber)
  101. TransactionLogItems = new ObservableCollection<TransactionLogItem>(TransactionLogItems.Skip(1).Take(maxItemNumber).ToList());
  102. Poll();
  103. }));
  104. }
  105. protected override void Poll()
  106. {
  107. Application.Current.Dispatcher.Invoke(new Action(() =>
  108. {
  109. InvokeAllPropertyChanged();
  110. }));
  111. }
  112. }
  113. }