CommonSocketDeviceSimulator.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using MECF.Framework.Simulator.Core.Commons;
  2. using MECF.Framework.Simulator.Core.Driver;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Timers;
  13. namespace MECF.Framework.Simulator.Core.Commons
  14. {
  15. public class SocketDeviceSimulatoFactory
  16. {
  17. public static CommonSocketDeviceSimulator GetCommonSocketDeviceSimulator(int port, string deviceName)
  18. {
  19. if (deviceName == "Hanbell")
  20. {
  21. return new HanbellPumpSocketSimulator(port, deviceName);
  22. }
  23. else if (deviceName == "SiasunPhoenixB")
  24. {
  25. return new SiasunPhoenixBSocketSimulator(port, deviceName);
  26. }
  27. else if (deviceName == "Siasun1500C800C")
  28. {
  29. return new Siasun1500C800CSocketSimulator(port, deviceName);
  30. }
  31. else if (deviceName == "TruPlasmaRF1000")
  32. {
  33. return new TruPlasmaRF1000Simulator(port, deviceName);
  34. }
  35. else if (deviceName == "HiwinRobot")
  36. {
  37. return new HiwinRobotSocketSimulator(port, deviceName);
  38. }
  39. return null;
  40. }
  41. }
  42. public class CommonSocketDeviceSimulator : SimpleSocketDeviceSimulator
  43. {
  44. public bool Failed { get; set; }
  45. public bool AutoReply { get; set; } = true;
  46. public bool IsAtSpeed { get; set; }
  47. Stopwatch _timer = new Stopwatch();
  48. private System.Timers.Timer _tick;
  49. private object _locker = new object();
  50. public string ResultValue { get; set; }
  51. public List<IOSimulatorItemViewModel> IOSimulatorItemList { get; set; }
  52. public event Action<IOSimulatorItemViewModel> SimulatorItemActived;
  53. string _deviceName;
  54. public CommonSocketDeviceSimulator(int port, string deviceName, bool isAscii = true, string newLine = "\r")
  55. : base(port, -1, newLine, ',', isAscii)
  56. {
  57. _deviceName = deviceName;
  58. ResultValue = "";
  59. _tick = new System.Timers.Timer();
  60. _tick.Interval = 200;
  61. _tick.Elapsed += _tick_Elapsed;
  62. _tick.Start();
  63. IsAtSpeed = true;
  64. }
  65. private void _tick_Elapsed(object sender, ElapsedEventArgs e)
  66. {
  67. lock (_locker)
  68. {
  69. if (_timer.IsRunning && _timer.Elapsed > TimeSpan.FromSeconds(10))
  70. {
  71. _timer.Stop();
  72. IsAtSpeed = true;
  73. }
  74. }
  75. }
  76. protected override void ProcessUnsplitMessage(byte[] binaryMessage)
  77. {
  78. lock (_locker)
  79. {
  80. var activeSimulatorItem = GetActiveIOSimulatorItemViewModel(binaryMessage);
  81. if (activeSimulatorItem == null) return;
  82. activeSimulatorItem.CommandContent = string.Join(",", binaryMessage.Select(bt => bt.ToString("X2")).ToArray());
  83. activeSimulatorItem.CommandRecievedTime = DateTime.Now;
  84. if (SimulatorItemActived != null)
  85. SimulatorItemActived(activeSimulatorItem);
  86. if (AutoReply)
  87. {
  88. OnWriteSimulatorItem(activeSimulatorItem);
  89. }
  90. }
  91. }
  92. protected override void ProcessUnsplitMessage(string msg)
  93. {
  94. lock (_locker)
  95. {
  96. var activeSimulatorItem = GetActiveIOSimulatorItemViewModel(msg);
  97. if (activeSimulatorItem == null) return;
  98. activeSimulatorItem.CommandContent = msg;
  99. activeSimulatorItem.CommandRecievedTime = DateTime.Now;
  100. if (SimulatorItemActived != null)
  101. SimulatorItemActived(activeSimulatorItem);
  102. if (AutoReply)
  103. {
  104. OnWriteSimulatorItem(activeSimulatorItem);
  105. }
  106. }
  107. }
  108. protected virtual IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(string msg)
  109. {
  110. return null;
  111. }
  112. protected virtual IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(byte[] msg)
  113. {
  114. return null;
  115. }
  116. protected virtual void OnWriteSimulatorItem(IOSimulatorItemViewModel activeSimulatorItem)
  117. {
  118. }
  119. public void ManualWriteMessage(IOSimulatorItemViewModel activeSimulatorItem)
  120. {
  121. OnWriteSimulatorItem(activeSimulatorItem);
  122. }
  123. }
  124. internal class TruPlasmaRF1000Simulator : CommonSocketDeviceSimulator
  125. {
  126. private List<byte> _forwardPower = new List<byte>() { 0x00, 0x00, 0x00, 0x00 };
  127. private List<byte> _reflectedPower = new List<byte>() { 0x00, 0x00, 0x00, 0x00 };
  128. private List<byte> _onOff = new List<byte>() { 0x00, 0x00, 0x00, 0x00 };
  129. private List<byte> _pulseMode = new List<byte>() { 0x00, 0x00, 0x00, 0x00 };
  130. private List<byte> _clockMode = new List<byte>() { 0x00 };
  131. private List<byte> _freq = new List<byte>() { 0x00, 0x00, 0x00, 0x00 };
  132. private List<byte> _processStatus = new List<byte>() { 0x00, 0x00, 0x00, 0x00 };
  133. private List<byte> _msgBuffer = new List<byte>();
  134. private object _locker = new object();
  135. private int _workFrequency = 60000;//60MHz
  136. public TruPlasmaRF1000Simulator(int port, string deviceName) : base(port, deviceName, false)
  137. {
  138. }
  139. protected override IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(byte[] msg)
  140. {
  141. if (IOSimulatorItemList == null)
  142. return null;
  143. if (msg == null || msg.Length == 0)
  144. return null;
  145. string[] msgArray;
  146. lock (_locker)
  147. {
  148. _msgBuffer.AddRange(msg);
  149. if (_msgBuffer.Count < _msgBuffer[0] + 1)
  150. {
  151. return null;
  152. }
  153. msgArray = _msgBuffer.Select(bt => bt.ToString("X2")).Take(_msgBuffer[0] + 1).ToArray();
  154. _msgBuffer.RemoveRange(0, _msgBuffer[0] + 1);
  155. }
  156. string msgCommand = string.Join(",", msgArray, 2, 4);
  157. foreach (var simulatorItem in IOSimulatorItemList)
  158. {
  159. if (msgCommand == simulatorItem.SourceCommand)
  160. {
  161. return simulatorItem;
  162. }
  163. }
  164. return IOSimulatorItemList.Find(item => item.SourceCommandName == "ExecuteAnyCommand");
  165. }
  166. protected override void OnWriteSimulatorItem(IOSimulatorItemViewModel activeSimulatorItem)
  167. {
  168. OnWriteMessage(BuildMessage(activeSimulatorItem));
  169. }
  170. private byte[] BuildMessage(IOSimulatorItemViewModel activeSimulatorItem)
  171. {
  172. TruPlasmaRFResponse rfResponse = JsonConvert.DeserializeObject<TruPlasmaRFResponse>(activeSimulatorItem.Response);
  173. if (activeSimulatorItem.SourceCommandName == "SetPiValue")
  174. {
  175. _forwardPower.Clear();
  176. var powerArry = activeSimulatorItem.CommandContent.Split(',').Skip(8).Take(2).Select(s => Convert.ToByte(s, 16)).ToArray();
  177. if (powerArry.Length == 2)
  178. {
  179. var power = BitConverter.ToUInt16(powerArry, 0);
  180. _forwardPower.AddRange(BitConverter.GetBytes((UInt32)power));//set value type = UINT16, return value type = UINT32
  181. }
  182. }
  183. else if (activeSimulatorItem.SourceCommandName == "SetPowerOnOff")
  184. {
  185. _onOff.Clear();
  186. _onOff.AddRange(activeSimulatorItem.CommandContent.Split(',').Skip(8).Take(4).Select(s => Convert.ToByte(s, 16)).ToArray());
  187. if (_onOff.Count == 4)
  188. {
  189. var isOn = BitConverter.ToInt32(_onOff.ToArray(), 0) > 0;
  190. _processStatus.Clear();
  191. if (isOn)
  192. _processStatus.AddRange(BitConverter.GetBytes((UInt32)0x00000010));//0x00000010 = Power output on
  193. else
  194. _processStatus.AddRange(BitConverter.GetBytes((UInt32)0x00000000));
  195. }
  196. }
  197. else if (activeSimulatorItem.SourceCommandName == "SetPulseMode")
  198. {
  199. _pulseMode.Clear();
  200. _pulseMode.AddRange(activeSimulatorItem.CommandContent.Split(',').Skip(8).Take(4).Select(s => Convert.ToByte(s, 16)).ToArray());
  201. }
  202. else if (activeSimulatorItem.SourceCommandName == "SetFrequency")
  203. {
  204. _freq.Clear();
  205. var tmp = activeSimulatorItem.CommandContent.Split(',').Skip(8).Take(4).Select(s => Convert.ToByte(s, 16)).ToArray();
  206. if (tmp.Length == 4)
  207. {
  208. int freq = BitConverter.ToInt32(tmp, 0) + _workFrequency * 1000;
  209. _freq.AddRange(BitConverter.GetBytes(freq));
  210. }
  211. }
  212. else if (activeSimulatorItem.SourceCommandName == "SetClockMode")
  213. {
  214. _clockMode.Clear();
  215. _clockMode.AddRange(activeSimulatorItem.CommandContent.Split(',').Skip(8).Take(1).Select(s => Convert.ToByte(s, 16)).ToArray());
  216. }
  217. List<byte> buffer = new List<byte>();
  218. int length = 5;
  219. if (rfResponse.Status != null)
  220. length++;
  221. if (rfResponse.Type != null)
  222. length++;
  223. if (rfResponse.Data != null)
  224. {
  225. length += rfResponse.Data.Split(',').Length;
  226. }
  227. buffer.Add((byte)length);
  228. buffer.Add(Convert.ToByte(rfResponse.GS, 16));
  229. buffer.AddRange(activeSimulatorItem.SourceCommand.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray());
  230. if (rfResponse.Status != null)
  231. buffer.Add(Convert.ToByte(rfResponse.Status, 16));
  232. if (rfResponse.Type != null)
  233. buffer.Add(Convert.ToByte(rfResponse.Type, 16));
  234. if (activeSimulatorItem.SourceCommandName == "ReadPiValue")
  235. {
  236. buffer.AddRange(_forwardPower.ToArray());
  237. }
  238. else if (activeSimulatorItem.SourceCommandName == "ReadPrValue")
  239. {
  240. buffer.AddRange(_reflectedPower.ToArray());
  241. }
  242. else if (activeSimulatorItem.SourceCommandName == "ReadProcessStatus")
  243. {
  244. buffer.AddRange(_processStatus.ToArray());
  245. }
  246. else if (activeSimulatorItem.SourceCommandName == "GetPowerOnOff")
  247. {
  248. buffer.AddRange(_onOff.ToArray());
  249. }
  250. else if (activeSimulatorItem.SourceCommandName == "GetPulseMode")
  251. {
  252. buffer.AddRange(_pulseMode.ToArray());
  253. }
  254. else if (activeSimulatorItem.SourceCommandName == "GetFrequency")
  255. {
  256. buffer.AddRange(_freq.ToArray());
  257. }
  258. else if (activeSimulatorItem.SourceCommandName == "GetClockMode")
  259. {
  260. buffer.AddRange(_clockMode.ToArray());
  261. }
  262. else
  263. {
  264. if (rfResponse.Data != null)
  265. {
  266. buffer.AddRange(rfResponse.Data.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray());
  267. }
  268. }
  269. var contentBuffer = buffer.Take(buffer.Count).ToArray();
  270. return buffer.ToArray();
  271. }
  272. }
  273. public class HiwinRobotSocketSimulator : CommonSocketDeviceSimulator
  274. {
  275. private bool _isWaferPresent = false;
  276. public HiwinRobotSocketSimulator(int port, string deviceName) : base(port, deviceName)
  277. {
  278. }
  279. protected override IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(string msg)
  280. {
  281. if (IOSimulatorItemList == null)
  282. return null;
  283. foreach (var simulatorItem in IOSimulatorItemList)
  284. {
  285. if (msg.Contains(simulatorItem.SourceCommandName))
  286. {
  287. return simulatorItem;
  288. }
  289. }
  290. return null;
  291. }
  292. protected override void OnWriteSimulatorItem(IOSimulatorItemViewModel activeSimulatorItem)
  293. {
  294. Thread.Sleep(100);
  295. if (activeSimulatorItem.SourceCommandName == "GET")
  296. {
  297. _isWaferPresent = true;
  298. }
  299. if (activeSimulatorItem.SourceCommandName == "PUT")
  300. {
  301. _isWaferPresent = false;
  302. }
  303. if (activeSimulatorItem.SourceCommandName == "RSR")
  304. {
  305. OnWriteMessage(activeSimulatorItem.Response + "\r\n");
  306. }
  307. else if (activeSimulatorItem.SourceCommandName == "INPUT")
  308. {
  309. OnWriteMessage($"{(_isWaferPresent ? "1" : "0")}" + "\r\n");
  310. }
  311. else
  312. OnWriteMessage(activeSimulatorItem.Response + "\r\n");
  313. }
  314. }
  315. public class SiasunPhoenixBSocketSimulator : CommonSocketDeviceSimulator
  316. {
  317. private bool _isWaferPresent = false;
  318. private string _endline = "\r\n";
  319. private readonly Dictionary<string, int> _timeConfigs = new Dictionary<string, int>();
  320. public SiasunPhoenixBSocketSimulator(int port, string deviceName) : base(port, deviceName)
  321. {
  322. //try
  323. //{
  324. // Hashtable timeSim = (Hashtable)ConfigurationManager.GetSection("VacRobotSim");
  325. // _timeConfigs.Add("PICK", int.Parse(timeSim["PICK"].ToString()) * 1000);
  326. // _timeConfigs.Add("PLACE", int.Parse(timeSim["PLACE"].ToString()) * 1000);
  327. // _timeConfigs.Add("GOTO", int.Parse(timeSim["GOTO"].ToString()) * 1000);
  328. // _timeConfigs.Add("RQLOAD", int.Parse(timeSim["RQLOAD"].ToString()) * 1000);
  329. // _timeConfigs.Add("CHECKLOAD", int.Parse(timeSim["CHECKLOAD"].ToString()) * 1000);
  330. // _timeConfigs.Add("HOME", int.Parse(timeSim["HOME"].ToString()) * 1000);
  331. //}
  332. //catch (ConfigurationErrorsException ex)
  333. //{
  334. // throw ex;
  335. //}
  336. }
  337. protected override IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(string msg)
  338. {
  339. if (IOSimulatorItemList == null)
  340. return null;
  341. foreach (var simulatorItem in IOSimulatorItemList)
  342. {
  343. if (msg.Contains(simulatorItem.SourceCommandName))
  344. {
  345. return simulatorItem;
  346. }
  347. }
  348. return null;
  349. }
  350. protected override void OnWriteSimulatorItem(IOSimulatorItemViewModel activeItem)
  351. {
  352. string cmdName = activeItem.SourceCommandName;
  353. if (cmdName.StartsWith("RQ"))
  354. {
  355. if (cmdName.StartsWith("RQ LOAD"))
  356. {
  357. //Thread.Sleep(_timeConfigs["RQLOAD"]);
  358. string response = $"{activeItem.Response} {(activeItem.CommandContent.Contains(" A") ? "A" : "B")} {(_isWaferPresent ? "ON" : "OFF")}";
  359. OnWriteMessage(response + _endline + "_RDY" + _endline);
  360. }
  361. else
  362. {
  363. OnWriteMessage(activeItem.Response + _endline + "_RDY" + _endline);
  364. }
  365. //OnWriteMessage("_RDY" + _endline);
  366. return;
  367. }
  368. else if (cmdName.StartsWith("CHECK LOAD"))
  369. {
  370. Thread.Sleep(1000);
  371. }
  372. else if (cmdName.StartsWith("HOME"))
  373. {
  374. Thread.Sleep(2000);
  375. }
  376. else if (cmdName.StartsWith("RESET"))
  377. {
  378. return;
  379. }
  380. else if (cmdName.StartsWith("PICK"))
  381. {
  382. Thread.Sleep(2000);
  383. }
  384. else if (cmdName.StartsWith("PLACE"))
  385. {
  386. Thread.Sleep(2000);
  387. }
  388. else if (cmdName.StartsWith("GOTO"))
  389. {
  390. Thread.Sleep(1500);
  391. }
  392. OnWriteMessage(activeItem.Response + _endline);
  393. }
  394. }
  395. public class Siasun1500C800CSocketSimulator : CommonSocketDeviceSimulator
  396. {
  397. public Siasun1500C800CSocketSimulator(int port, string deviceName) : base(port, deviceName)
  398. {
  399. }
  400. protected override IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(string msg)
  401. {
  402. if (IOSimulatorItemList == null)
  403. return null;
  404. foreach (var simulatorItem in IOSimulatorItemList)
  405. {
  406. if (msg.Contains(simulatorItem.SourceCommandName))
  407. {
  408. return simulatorItem;
  409. }
  410. }
  411. return null;
  412. }
  413. protected override void OnWriteSimulatorItem(IOSimulatorItemViewModel activeSimulatorItem)
  414. {
  415. if (activeSimulatorItem.SourceCommandName.StartsWith("RQ"))
  416. {
  417. OnWriteMessage(activeSimulatorItem.Response + "\r");
  418. Thread.Sleep(1000);
  419. OnWriteMessage("_RDY" + "\r");
  420. }
  421. else
  422. {
  423. OnWriteMessage(activeSimulatorItem.Response + "\r");
  424. }
  425. }
  426. }
  427. internal class HanbellPumpSocketSimulator : CommonSocketDeviceSimulator
  428. {
  429. private List<byte> _msgBuffer = new List<byte>();
  430. private bool _isPumpOn;
  431. private List<byte> statusArray;
  432. public HanbellPumpSocketSimulator(int port, string deviceName) : base(port, deviceName, false)
  433. {
  434. statusArray = Enumerable.Repeat((byte)0x0,90).ToList();
  435. }
  436. protected override IOSimulatorItemViewModel GetActiveIOSimulatorItemViewModel(byte[] msg)
  437. {
  438. if (IOSimulatorItemList == null)
  439. return null;
  440. _msgBuffer.AddRange(msg);
  441. if (_msgBuffer.Count < 12)
  442. {
  443. return null;
  444. }
  445. foreach (var simulatorItem in IOSimulatorItemList)
  446. {
  447. if (msg[7] == byte.Parse(simulatorItem.SourceCommand))
  448. {
  449. //action
  450. if (msg[7] == 5)
  451. {
  452. simulatorItem.Response = string.Join(",", msg.Take(12).Select(bt => bt.ToString("X2")).ToArray());
  453. }
  454. _msgBuffer.RemoveRange(0, 12);
  455. return simulatorItem;
  456. }
  457. }
  458. return null;
  459. }
  460. protected override void OnWriteSimulatorItem(IOSimulatorItemViewModel activeSimulatorItem)
  461. {
  462. var responseArray = activeSimulatorItem.Response.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray();
  463. if(activeSimulatorItem.SourceCommandName == "OperatePump")
  464. {
  465. var response = activeSimulatorItem.Response.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray();
  466. _isPumpOn = response[10] == 0xFF;
  467. OnWriteMessage(response);
  468. }
  469. if (activeSimulatorItem.SourceCommandName == "RequestRegisters")
  470. {
  471. List<byte> buffer = new List<byte>();
  472. buffer.AddRange(activeSimulatorItem.Response.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray());
  473. statusArray[61] = (byte)(_isPumpOn ? 0xdc : 0x7f);
  474. buffer.AddRange(statusArray);
  475. OnWriteMessage(buffer.ToArray());
  476. }
  477. }
  478. }
  479. }