SocketDeviceViewModel.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. if(!string.IsNullOrEmpty(obj))
  87. {
  88. Application.Current.Dispatcher.Invoke(new Action(() =>
  89. {
  90. TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  91. }));
  92. }
  93. }
  94. private void _simulator_MessageOut(string obj)
  95. {
  96. if (!string.IsNullOrEmpty(obj))
  97. {
  98. Application.Current.Dispatcher.Invoke(new Action(() =>
  99. {
  100. TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  101. }));
  102. }
  103. }
  104. protected override void Poll()
  105. {
  106. if (Application.Current != null)
  107. {
  108. Application.Current.Dispatcher.Invoke(new Action(() =>
  109. {
  110. InvokeAllPropertyChanged();
  111. }));
  112. }
  113. }
  114. }
  115. public class SimpleSocketDeviceViewModel : TimerViewModelBase
  116. {
  117. public ICommand ClearLogCommand { get; set; }
  118. public ICommand EnableCommand { get; set; }
  119. public ICommand DisableCommand { get; set; }
  120. public bool IsEnableEnable
  121. {
  122. get { return !_simulator.IsEnabled; }
  123. }
  124. public bool IsEnableDisable
  125. {
  126. get { return _simulator.IsEnabled; }
  127. }
  128. public bool IsConnected
  129. {
  130. get { return _simulator.IsConnected; }
  131. }
  132. public int LocalPort
  133. {
  134. get { return _simulator.LocalPort; }
  135. }
  136. //public string RemoteConnection
  137. //{
  138. // get { return _simulator.RemoteConnection; }
  139. //}
  140. public string ConnectionStatus
  141. {
  142. get
  143. {
  144. if (_simulator.IsConnected)
  145. return "Connected";
  146. if (_simulator.IsEnabled)
  147. return "Listening";
  148. return "Disable";
  149. }
  150. }
  151. public string LocalPortSetPoint { get; set; }
  152. public ObservableCollection<TransactionLogItem> TransactionLogItems { get; set; }
  153. protected SimpleSocketDeviceSimulator _simulator;
  154. public SimpleSocketDeviceViewModel(string name) : base(name)
  155. {
  156. ClearLogCommand = new DelegateCommand<string>(ClearLog);
  157. EnableCommand = new DelegateCommand<string>(Enable);
  158. DisableCommand = new DelegateCommand<string>(Disable);
  159. TransactionLogItems = new ObservableCollection<TransactionLogItem>();
  160. LocalPortSetPoint = "";
  161. }
  162. protected void Init(SimpleSocketDeviceSimulator sim, bool enable = true)
  163. {
  164. LocalPortSetPoint = sim.LocalPort.ToString();
  165. _simulator = sim;
  166. _simulator.MessageOut += _simulator_MessageOut;
  167. _simulator.MessageIn += _simulator_MessageIn;
  168. if (enable)
  169. _simulator.Enable();
  170. }
  171. private void Disable(string obj)
  172. {
  173. _simulator.Disable();
  174. }
  175. private void Enable(string obj)
  176. {
  177. int port;
  178. if (int.TryParse(LocalPortSetPoint, out port))
  179. _simulator.LocalPort = port;
  180. _simulator.Enable();
  181. }
  182. private void ClearLog(string obj)
  183. {
  184. TransactionLogItems.Clear();
  185. }
  186. private void _simulator_MessageIn(string obj)
  187. {
  188. if(!string.IsNullOrEmpty(obj))
  189. {
  190. Application.Current.Dispatcher.Invoke(new Action(() =>
  191. {
  192. TransactionLogItems.Add(new TransactionLogItem() { Incoming = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  193. }));
  194. }
  195. }
  196. private void _simulator_MessageOut(string obj)
  197. {
  198. if (!string.IsNullOrEmpty(obj))
  199. {
  200. Application.Current.Dispatcher.Invoke(new Action(() =>
  201. {
  202. TransactionLogItems.Add(new TransactionLogItem() { Outgoing = obj, OccurTime = DateTime.Now.ToString("HH:mm:ss.fff") });
  203. }));
  204. }
  205. }
  206. protected override void Poll()
  207. {
  208. if (Application.Current != null)
  209. {
  210. Application.Current.Dispatcher.Invoke(new Action(() =>
  211. {
  212. InvokeAllPropertyChanged();
  213. }));
  214. }
  215. }
  216. }
  217. }