SocketDeviceViewModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public string LocalPortSetPoint { get; set; }
  50. public ObservableCollection<TransactionLogItem> TransactionLogItems { get; set; }
  51. protected SocketDeviceSimulator _simulator;
  52. public SocketDeviceViewModel(string name) : base(name)
  53. {
  54. ClearLogCommand = new DelegateCommand<string>(ClearLog);
  55. EnableCommand = new DelegateCommand<string>(Enable);
  56. DisableCommand = new DelegateCommand<string>(Disable);
  57. TransactionLogItems = new ObservableCollection<TransactionLogItem>();
  58. LocalPortSetPoint = "";
  59. }
  60. protected void Init(SocketDeviceSimulator sim, bool enable=true)
  61. {
  62. LocalPortSetPoint = sim.LocalPort.ToString();
  63. _simulator = sim;
  64. _simulator.MessageOut += _simulator_MessageOut;
  65. _simulator.MessageIn += _simulator_MessageIn;
  66. if (enable)
  67. _simulator.Enable();
  68. }
  69. private void Disable(string obj)
  70. {
  71. _simulator.Disable();
  72. }
  73. private void Enable(string obj)
  74. {
  75. int port;
  76. if (int.TryParse(LocalPortSetPoint, out port))
  77. _simulator.LocalPort = port;
  78. _simulator.Enable();
  79. }
  80. private void ClearLog(string obj)
  81. {
  82. TransactionLogItems.Clear();
  83. }
  84. private void _simulator_MessageIn(string obj)
  85. {
  86. Application.Current.Dispatcher.Invoke(new Action(() =>
  87. {
  88. TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  89. }));
  90. }
  91. private void _simulator_MessageOut(string obj)
  92. {
  93. Application.Current.Dispatcher.Invoke(new Action(() =>
  94. {
  95. TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  96. }));
  97. }
  98. protected override void Poll()
  99. {
  100. Application.Current.Dispatcher.Invoke(new Action(() =>
  101. {
  102. InvokeAllPropertyChanged();
  103. }));
  104. }
  105. }
  106. }