SerialPortDeviceViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Aitex.Core.Utilities;
  11. using MECF.Framework.Simulator.Core.Driver;
  12. namespace MECF.Framework.Simulator.Core.Commons
  13. {
  14. public class SerialPortDeviceViewModel : TimerViewModelBase
  15. {
  16. public ObservableCollection<string> PortList { get; set; }
  17. public ICommand ClearLogCommand { get; set; }
  18. public ICommand EnableCommand { get; set; }
  19. public ICommand DisableCommand { get; set; }
  20. public bool IsEnableEnable
  21. {
  22. get { return !_simulator.IsEnabled; }
  23. }
  24. public bool IsEnableDisable
  25. {
  26. get { return _simulator.IsEnabled; }
  27. }
  28. public string RemoteConnection
  29. {
  30. get { return _simulator.RemoteConnection; }
  31. }
  32. public string LocalPort
  33. {
  34. get { return _simulator.PortName; }
  35. }
  36. public bool IsConnected
  37. {
  38. get { return _simulator.IsConnected; }
  39. }
  40. public string ConnectionStatus
  41. {
  42. get
  43. {
  44. if (_simulator.IsEnabled)
  45. return "Receiving";
  46. return "Disable";
  47. }
  48. }
  49. public string LocalPortSetPoint { get; set; }
  50. public ObservableCollection<TransactionLogItem> TransactionLogItems { get; set; }
  51. protected SerialPortDeviceSimulator _simulator;
  52. public SerialPortDeviceViewModel(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 = "COM2";
  59. PortList = new ObservableCollection<string>();
  60. for (int i = 0; i < 200; i++)
  61. {
  62. PortList.Add($"COM{i+1}");
  63. }
  64. }
  65. protected void Init(SerialPortDeviceSimulator sim, bool enable = true)
  66. {
  67. LocalPortSetPoint = sim.PortName;
  68. InvokePropertyChanged("LocalPortSetPoint");
  69. _simulator = sim;
  70. _simulator.MessageOut += _simulator_MessageOut;
  71. _simulator.MessageIn += _simulator_MessageIn;
  72. if (enable)
  73. _simulator.Enable();
  74. }
  75. private void Disable(string obj)
  76. {
  77. _simulator.Disable();
  78. }
  79. private void Enable(string obj)
  80. {
  81. _simulator.PortName = LocalPortSetPoint;
  82. _simulator.Enable();
  83. }
  84. private void ClearLog(string obj)
  85. {
  86. TransactionLogItems.Clear();
  87. }
  88. private void _simulator_MessageIn(string obj)
  89. {
  90. Application.Current.Dispatcher.Invoke(new Action(() =>
  91. {
  92. TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  93. while (TransactionLogItems.Count > 100)
  94. {
  95. TransactionLogItems.RemoveAt(0);
  96. }
  97. }));
  98. }
  99. private void _simulator_MessageOut(string obj)
  100. {
  101. Application.Current.Dispatcher.Invoke(new Action(() =>
  102. {
  103. TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  104. while (TransactionLogItems.Count > 100)
  105. {
  106. TransactionLogItems.RemoveAt(0);
  107. }
  108. }));
  109. }
  110. protected override void Poll()
  111. {
  112. if(Application.Current != null)
  113. {
  114. Application.Current.Dispatcher.Invoke(new Action(() =>
  115. {
  116. InvokePropertyChanged();
  117. }));
  118. }
  119. }
  120. }
  121. }