SerialPortDeviceViewModel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. {
  61. "COM1", "COM2", "COM3", "COM4", "COM5",
  62. "COM6", "COM7", "COM8", "COM9", "COM10",
  63. "COM11", "COM12", "COM13", "COM14", "COM15",
  64. "COM16", "COM17", "COM18", "COM19", "COM20",
  65. "COM21", "COM22", "COM23", "COM24", "COM25",
  66. };
  67. }
  68. protected void Init(SerialPortDeviceSimulator sim, bool enable = true)
  69. {
  70. LocalPortSetPoint = sim.PortName;
  71. InvokePropertyChanged("LocalPortSetPoint");
  72. _simulator = sim;
  73. _simulator.MessageOut += _simulator_MessageOut;
  74. _simulator.MessageIn += _simulator_MessageIn;
  75. if (enable)
  76. _simulator.Enable();
  77. }
  78. private void Disable(string obj)
  79. {
  80. _simulator.Disable();
  81. }
  82. private void Enable(string obj)
  83. {
  84. _simulator.PortName = LocalPortSetPoint;
  85. _simulator.Enable();
  86. }
  87. private void ClearLog(string obj)
  88. {
  89. TransactionLogItems.Clear();
  90. }
  91. private void _simulator_MessageIn(string obj)
  92. {
  93. Application.Current.Dispatcher.Invoke(new Action(() =>
  94. {
  95. TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  96. while (TransactionLogItems.Count > 100)
  97. {
  98. TransactionLogItems.RemoveAt(0);
  99. }
  100. }));
  101. }
  102. private void _simulator_MessageOut(string obj)
  103. {
  104. Application.Current.Dispatcher.Invoke(new Action(() =>
  105. {
  106. TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  107. while (TransactionLogItems.Count > 100)
  108. {
  109. TransactionLogItems.RemoveAt(0);
  110. }
  111. }));
  112. }
  113. protected override void Poll()
  114. {
  115. if(Application.Current != null)
  116. {
  117. Application.Current.Dispatcher.Invoke(new Action(() =>
  118. {
  119. InvokePropertyChanged();
  120. }));
  121. }
  122. }
  123. }
  124. }