CommonSocketDeviceSimulator.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using MECF.Framework.Simulator.Core.Commons;
  2. using MECF.Framework.Simulator.Core.Driver;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Timers;
  12. namespace MECF.Framework.Simulator.Core.Commons
  13. {
  14. public class SocketDeviceSimulatoFactory
  15. {
  16. public static CommonSocketDeviceSimulator GetCommonSocketDeviceSimulator(int port, string deviceName)
  17. {
  18. if (deviceName == "Hanbell")
  19. {
  20. return new HanbellPumpSocketSimulator(port, deviceName);
  21. }
  22. else if (deviceName == "SiasunPhoenixB")
  23. {
  24. return new SiasunPhoenixBSocketSimulator(port, deviceName);
  25. }
  26. else if (deviceName == "Siasun1500C800C")
  27. {
  28. return new Siasun1500C800CSocketSimulator(port, deviceName);
  29. }
  30. return null;
  31. }
  32. }
  33. public class CommonSocketDeviceSimulator : SimpleSocketDeviceSimulator
  34. {
  35. public bool Failed { get; set; }
  36. public bool AutoReply { get; set; } = true;
  37. public bool IsAtSpeed { get; set; }
  38. Stopwatch _timer = new Stopwatch();
  39. private System.Timers.Timer _tick;
  40. private object _locker = new object();
  41. public string ResultValue { get; set; }
  42. public List<IOSimulatorItemViewModel> IOSimulatorItemList { get; set; }
  43. public event Action<IOSimulatorItemViewModel> SimulatorItemActived;
  44. string _deviceName;
  45. public CommonSocketDeviceSimulator(int port, string deviceName, bool isAscii = true, string newLine = "\r")
  46. : base(port, -1, newLine, ',', isAscii)
  47. {
  48. _deviceName = deviceName;
  49. ResultValue = "";
  50. _tick = new System.Timers.Timer();
  51. _tick.Interval = 200;
  52. _tick.Elapsed += _tick_Elapsed;
  53. _tick.Start();
  54. IsAtSpeed = true;
  55. }
  56. private void _tick_Elapsed(object sender, ElapsedEventArgs e)
  57. {
  58. lock (_locker)
  59. {
  60. if (_timer.IsRunning && _timer.Elapsed > TimeSpan.FromSeconds(10))
  61. {
  62. _timer.Stop();
  63. IsAtSpeed = true;
  64. }
  65. }
  66. }
  67. protected override void ProcessUnsplitMessage(byte[] binaryMessage)
  68. {
  69. lock (_locker)
  70. {
  71. var activeSimulatorItem = GetActiveIOSimulatorItemViewModel(binaryMessage);
  72. if (activeSimulatorItem == null) return;
  73. activeSimulatorItem.CommandContent = string.Join(",", binaryMessage.Select(bt => bt.ToString("X2")).ToArray());
  74. activeSimulatorItem.CommandRecievedTime = DateTime.Now;
  75. if (SimulatorItemActived != null)
  76. SimulatorItemActived(activeSimulatorItem);
  77. if (AutoReply)
  78. {
  79. OnWriteSimulatorItem(activeSimulatorItem);
  80. }
  81. }
  82. }
  83. protected override void ProcessUnsplitMessage(string msg)
  84. {
  85. lock (_locker)
  86. {
  87. var activeSimulatorItem = GetActiveIOSimulatorItemViewModel(msg);
  88. if (activeSimulatorItem == null) return;
  89. activeSimulatorItem.CommandContent = msg;
  90. activeSimulatorItem.CommandRecievedTime = DateTime.Now;
  91. if (SimulatorItemActived != null)
  92. SimulatorItemActived(activeSimulatorItem);
  93. if (AutoReply)
  94. {
  95. OnWriteSimulatorItem(activeSimulatorItem);
  96. }
  97. }
  98. }
  99. protected virtual IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(string msg)
  100. {
  101. return null;
  102. }
  103. protected virtual IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(byte[] msg)
  104. {
  105. return null;
  106. }
  107. protected virtual void OnWriteSimulatorItem(IOSimulatorItemViewModel activeSimulatorItem)
  108. {
  109. }
  110. public void ManualWriteMessage(IOSimulatorItemViewModel activeSimulatorItem)
  111. {
  112. OnWriteSimulatorItem(activeSimulatorItem);
  113. }
  114. }
  115. public class SiasunPhoenixBSocketSimulator : CommonSocketDeviceSimulator
  116. {
  117. private bool _isWaferPresent = false;
  118. private string _endline = "\r\n";
  119. private readonly Dictionary<string, int> _timeConfigs = new Dictionary<string, int>();
  120. public SiasunPhoenixBSocketSimulator(int port, string deviceName) : base(port, deviceName)
  121. {
  122. //try
  123. //{
  124. // Hashtable timeSim = (Hashtable)ConfigurationManager.GetSection("VacRobotSim");
  125. // _timeConfigs.Add("PICK", int.Parse(timeSim["PICK"].ToString()) * 1000);
  126. // _timeConfigs.Add("PLACE", int.Parse(timeSim["PLACE"].ToString()) * 1000);
  127. // _timeConfigs.Add("GOTO", int.Parse(timeSim["GOTO"].ToString()) * 1000);
  128. // _timeConfigs.Add("RQLOAD", int.Parse(timeSim["RQLOAD"].ToString()) * 1000);
  129. // _timeConfigs.Add("CHECKLOAD", int.Parse(timeSim["CHECKLOAD"].ToString()) * 1000);
  130. // _timeConfigs.Add("HOME", int.Parse(timeSim["HOME"].ToString()) * 1000);
  131. //}
  132. //catch (ConfigurationErrorsException ex)
  133. //{
  134. // throw ex;
  135. //}
  136. }
  137. protected override IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(string msg)
  138. {
  139. if (IOSimulatorItemList == null)
  140. return null;
  141. foreach (var simulatorItem in IOSimulatorItemList)
  142. {
  143. if (msg.Contains(simulatorItem.SourceCommandName))
  144. {
  145. return simulatorItem;
  146. }
  147. }
  148. return null;
  149. }
  150. protected override void OnWriteSimulatorItem(IOSimulatorItemViewModel activeItem)
  151. {
  152. string cmdName = activeItem.SourceCommandName;
  153. if (cmdName.StartsWith("RQ"))
  154. {
  155. if (cmdName.StartsWith("RQ LOAD"))
  156. {
  157. //Thread.Sleep(_timeConfigs["RQLOAD"]);
  158. string response = $"{activeItem.Response} {(activeItem.CommandContent.Contains(" A") ? "A" : "B")} {(_isWaferPresent ? "ON" : "OFF")}";
  159. OnWriteMessage(response + _endline + "_RDY" + _endline);
  160. }
  161. else
  162. {
  163. OnWriteMessage(activeItem.Response + _endline + "_RDY" + _endline);
  164. }
  165. //OnWriteMessage("_RDY" + _endline);
  166. return;
  167. }
  168. else if (cmdName.StartsWith("CHECK LOAD"))
  169. {
  170. Thread.Sleep(1000);
  171. }
  172. else if (cmdName.StartsWith("HOME"))
  173. {
  174. Thread.Sleep(2000);
  175. }
  176. else if (cmdName.StartsWith("RESET"))
  177. {
  178. return;
  179. }
  180. else if (cmdName.StartsWith("PICK"))
  181. {
  182. Thread.Sleep(2000);
  183. }
  184. else if (cmdName.StartsWith("PLACE"))
  185. {
  186. Thread.Sleep(2000);
  187. }
  188. else if (cmdName.StartsWith("GOTO"))
  189. {
  190. Thread.Sleep(1500);
  191. }
  192. OnWriteMessage(activeItem.Response + _endline);
  193. }
  194. }
  195. public class Siasun1500C800CSocketSimulator : CommonSocketDeviceSimulator
  196. {
  197. public Siasun1500C800CSocketSimulator(int port, string deviceName) : base(port, deviceName)
  198. {
  199. }
  200. protected override IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(string msg)
  201. {
  202. if (IOSimulatorItemList == null)
  203. return null;
  204. foreach (var simulatorItem in IOSimulatorItemList)
  205. {
  206. if (msg.Contains(simulatorItem.SourceCommandName))
  207. {
  208. return simulatorItem;
  209. }
  210. }
  211. return null;
  212. }
  213. protected override void OnWriteSimulatorItem(IOSimulatorItemViewModel activeSimulatorItem)
  214. {
  215. if (activeSimulatorItem.SourceCommandName.StartsWith("RQ"))
  216. {
  217. OnWriteMessage(activeSimulatorItem.Response + "\r");
  218. Thread.Sleep(1000);
  219. OnWriteMessage("_RDY" + "\r");
  220. }
  221. else
  222. {
  223. OnWriteMessage(activeSimulatorItem.Response + "\r");
  224. }
  225. }
  226. }
  227. internal class HanbellPumpSocketSimulator : CommonSocketDeviceSimulator
  228. {
  229. private List<byte> _msgBuffer = new List<byte>();
  230. private bool _isPumpOn;
  231. private List<byte> statusArray;
  232. public HanbellPumpSocketSimulator(int port, string deviceName) : base(port, deviceName, false)
  233. {
  234. statusArray = Enumerable.Repeat((byte)0x0,90).ToList();
  235. }
  236. protected override IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(byte[] msg)
  237. {
  238. if (IOSimulatorItemList == null)
  239. return null;
  240. _msgBuffer.AddRange(msg);
  241. if (_msgBuffer.Count < 12)
  242. {
  243. return null;
  244. }
  245. foreach (var simulatorItem in IOSimulatorItemList)
  246. {
  247. if (msg[7] == byte.Parse(simulatorItem.SourceCommand))
  248. {
  249. //action
  250. if (msg[7] == 5)
  251. {
  252. simulatorItem.Response = string.Join(",", msg.Take(12).Select(bt => bt.ToString("X2")).ToArray());
  253. }
  254. _msgBuffer.RemoveRange(0, 12);
  255. return simulatorItem;
  256. }
  257. }
  258. return null;
  259. }
  260. protected override void OnWriteSimulatorItem(IOSimulatorItemViewModel activeSimulatorItem)
  261. {
  262. var responseArray = activeSimulatorItem.Response.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray();
  263. if(activeSimulatorItem.SourceCommandName == "OperatePump")
  264. {
  265. var response = activeSimulatorItem.Response.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray();
  266. _isPumpOn = response[10] == 0xFF;
  267. OnWriteMessage(response);
  268. }
  269. if (activeSimulatorItem.SourceCommandName == "RequestRegisters")
  270. {
  271. List<byte> buffer = new List<byte>();
  272. buffer.AddRange(activeSimulatorItem.Response.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray());
  273. statusArray[61] = (byte)(_isPumpOn ? 0xdc : 0x7f);
  274. buffer.AddRange(statusArray);
  275. OnWriteMessage(buffer.ToArray());
  276. }
  277. }
  278. }
  279. }