DeviceSimulator.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Aitex.Core.RT.DataCenter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MECF.Framework.Simulator.Core.Driver
  8. {
  9. public class DeviceSimulator
  10. {
  11. public event Action<string> MessageIn;
  12. public event Action<string> MessageOut;
  13. public event Action<string> ErrorOccur;
  14. public virtual bool IsEnabled
  15. {
  16. get { return false; }
  17. }
  18. public virtual bool IsConnected
  19. {
  20. get { return false; }
  21. }
  22. protected SortedList<string, Action<string>> commandList = new SortedList<string, Action<string>>();
  23. protected string _lineDelimiter;
  24. protected char _msgDelimiter;
  25. protected int _commandIndex;
  26. public DeviceSimulator(int commandIndex, string lindDelimiter, char msgDelimiter)
  27. {
  28. _lineDelimiter = lindDelimiter;
  29. _msgDelimiter = msgDelimiter;
  30. _commandIndex = commandIndex;
  31. }
  32. protected virtual void AddCommandHandler(string command, Action<string> handler)
  33. {
  34. commandList.Add(command, handler);
  35. }
  36. protected virtual void ProcessUnsplitMessage(string message)
  37. {
  38. }
  39. protected virtual void ProcessUnsplitMessage(byte[] data)
  40. {
  41. }
  42. protected virtual void OnErrorMessage(string message)
  43. {
  44. if (ErrorOccur != null)
  45. {
  46. ErrorOccur(message);
  47. }
  48. }
  49. protected void OnReadMessage(byte[] data)
  50. {
  51. if(MessageIn!=null)
  52. {
  53. MessageIn(string.Join(" ", Array.ConvertAll(data, x => x.ToString("X2"))));
  54. }
  55. if(_commandIndex<0)
  56. {
  57. ProcessUnsplitMessage(data);
  58. return;
  59. }
  60. }
  61. protected void OnReadMessage(string message)
  62. {
  63. //if (message.Contains("FETCH"))
  64. //{
  65. //}
  66. if (MessageIn != null)
  67. {
  68. MessageIn(message);
  69. }
  70. if (_commandIndex < 0)
  71. {
  72. ProcessUnsplitMessage(message);
  73. return;
  74. }
  75. if (_msgDelimiter.Equals(' '))
  76. {
  77. string cmd = message.Substring(_commandIndex, 4);
  78. if (cmd.Contains(_msgDelimiter))
  79. cmd = cmd.Substring(0, cmd.IndexOf(_msgDelimiter));
  80. if (!commandList.ContainsKey(cmd))
  81. {
  82. CommandNotRecognized(message);
  83. }
  84. else
  85. {
  86. //Log.WriteIfEnabled( LogCategory.Debug, source, DeviceId + ":ProcessMessages: '" + msg.Message );
  87. var handler = commandList[cmd];
  88. if (handler == null)
  89. {
  90. //Log.WriteIfEnabled( LogCategory.Error, source, DeviceId + ":ProcessMessages: CANNOT FIND MESSAGE HANDLER '" + msg.Message );
  91. }
  92. else
  93. {
  94. if (!message.Contains("stat_pdo") && !message.Contains("statfx"))
  95. {
  96. //Log.WriteIfEnabled(LogCategory.Debug, source, "Received command " + message);
  97. }
  98. handler(message);
  99. }
  100. }
  101. }
  102. else
  103. {
  104. string[] msgComponents = message.Split(_msgDelimiter);
  105. int index = msgComponents[0] == "$$$" ? 1 : _commandIndex;
  106. if (msgComponents.Length <= index)
  107. {
  108. CommandNotRecognized(message);
  109. return;
  110. }
  111. // find the message handler in the dictionary
  112. string cmd = msgComponents[index];
  113. if (!commandList.ContainsKey(cmd))
  114. {
  115. CommandNotRecognized(message);
  116. }
  117. else
  118. {
  119. //Log.WriteIfEnabled( LogCategory.Debug, source, DeviceId + ":ProcessMessages: '" + msg.Message );
  120. var handler = commandList[cmd];
  121. if (handler == null)
  122. {
  123. //Log.WriteIfEnabled( LogCategory.Error, source, DeviceId + ":ProcessMessages: CANNOT FIND MESSAGE HANDLER '" + msg.Message );
  124. }
  125. else
  126. {
  127. if (!message.Contains("stat_pdo") && !message.Contains("statfx"))
  128. {
  129. //Log.WriteIfEnabled(LogCategory.Debug, source, "Received command " + message);
  130. }
  131. handler(message);
  132. }
  133. }
  134. }
  135. }
  136. protected virtual void CommandNotRecognized(string msg)
  137. {
  138. if (commandList.ContainsKey("Unknown") && commandList["Unknown"] != null)
  139. {
  140. commandList["Unknown"](msg);
  141. }
  142. else
  143. {
  144. ProcessWriteMessage("_ERR Unrecognized command");
  145. }
  146. }
  147. protected void OnWriteMessage(string msg)
  148. {
  149. if (MessageOut != null)
  150. MessageOut(msg);
  151. ProcessWriteMessage(msg);
  152. }
  153. protected void OnWriteMessage(byte[] bytes)
  154. {
  155. if (MessageOut != null)
  156. {
  157. MessageOut(string.Join(" ", Array.ConvertAll(bytes, x => x.ToString("X2"))));
  158. }
  159. ProcessWriteMessage(bytes);
  160. }
  161. protected virtual void ProcessWriteMessage(string msg)
  162. {
  163. }
  164. protected virtual void ProcessWriteMessage(byte[] bytes)
  165. {
  166. }
  167. }
  168. }