CommonSocketDeviceSimulator.cs 12 KB

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