YZQZFabAuto.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Diagnostics.Eventing.Reader;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Security.Policy;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using Aitex.Core.Backend;
  12. using Aitex.Core.MES.JC;
  13. using Aitex.Core.RT.IOCore;
  14. using Aitex.Core.RT.Log;
  15. using Aitex.Core.RT.SCCore;
  16. using Aitex.Core.Util;
  17. using Newtonsoft.Json;
  18. using Newtonsoft.Json.Linq;
  19. using TwinCAT.Ads.Internal;
  20. namespace Aitex.Core.MES.YZQZ
  21. {
  22. public class YZQZFabAuto : FabAutoComm
  23. {
  24. private static bool jobStart = false;
  25. private static PeriodicJob _thread;
  26. private static string m_LotId;
  27. private static string m_ReceipeName;
  28. private static string m_machineNumber;
  29. private static string m_machineType;
  30. private static string m_wanPanPath;
  31. private static string m_operation;
  32. private static string m_operatorId;
  33. private static List<JobRecord> records;
  34. /// <summary>
  35. /// 根据扫码信息获取Recipe
  36. /// </summary>
  37. /// <returns>Lot--operation </returns>
  38. public override ResponseResult GetRecipeByMultipleLot(List<string> list ,string machineNumber,string machineType,string webApi)
  39. {
  40. List<RequestRecipe> requstParams = new List<RequestRecipe>();
  41. foreach (var lot in list)
  42. {
  43. RequestRecipe rrp= new RequestRecipe();
  44. rrp.LotId = lot;
  45. rrp.MachineNumber = machineNumber;
  46. rrp.MachineType = machineType;
  47. requstParams.Add(rrp);
  48. }
  49. string url = webApi + "/api/mes/GetRecipeByMultipleLot?strQuery={0}";
  50. string jsonParms = JsonConvert.SerializeObject(requstParams);
  51. LOG.Info("GetRecipeByMultipleLot|"+string.Format(url, jsonParms));
  52. string responseResult = HttpClient.HttpGet(string.Format(url, jsonParms));
  53. LOG.Info("GetRecipeByMultipleLot|"+responseResult);
  54. if (!string.IsNullOrEmpty(responseResult))
  55. {
  56. try
  57. {
  58. ResponseResult response = JsonConvert.DeserializeObject<ResponseResult>(responseResult);
  59. //if (response.Status ==200)
  60. //{
  61. // resResult = new KeyValuePair<ResultStatus, List<ResponseRecipe>>((ResultStatus)response.Status, response.Data);
  62. // cacheResponseReceip.Clear();
  63. // cacheResponseReceip.AddRange(response.Data);
  64. // m_operation = response.Data[0].Operation;
  65. // // Operation = response.Data[0].Operation;
  66. //}
  67. //else
  68. //{
  69. // resResult = new KeyValuePair<ResultStatus, List<ResponseRecipe>>((ResultStatus)response.Status, null);
  70. //}
  71. return response;
  72. }
  73. catch (Exception)
  74. {
  75. return null;
  76. }
  77. }
  78. return null;
  79. }
  80. public override bool SaveMachineErrorRecord(List<RequestMachineErrorRecord> requestRecords) {
  81. try
  82. {
  83. var postdata = JsonConvert.SerializeObject(requestRecords);
  84. string webapi = SC.GetSCString(SCName.System_YZQZWebapi).Value + "/api/mes/SaveMachineErrorRecord";
  85. string status;
  86. string result = HttpClient.PostResponse(webapi, postdata,out status);
  87. LOG.Info("MES|ErrorResponse|" + result);
  88. return true;
  89. }
  90. catch (Exception ex)
  91. {
  92. LOG.Info("MES|ErrorResponse|"+ex.Message);
  93. return false;
  94. }
  95. }
  96. public override bool SaveMachineStatusRecord(int State)
  97. {
  98. List<RequestMachineStatusRecord> requestRecords=new List<RequestMachineStatusRecord>();
  99. RequestMachineStatusRecord record= new RequestMachineStatusRecord();
  100. PState Pstate = (PState)State;
  101. string stateStr,color;
  102. switch (Pstate)
  103. {
  104. case PState.Idle:
  105. stateStr = "空闲";
  106. color = "黄色";
  107. break;
  108. case PState.Error:
  109. stateStr = "报警";
  110. color = "红色";
  111. break;
  112. default:
  113. stateStr = "运行";
  114. color = "绿色";
  115. break;
  116. }
  117. record.Status = stateStr;
  118. record.MachineNumber = SC.GetSCString(SCName.System_YZQZMachineNumber).Value;
  119. record.ColorName = color;
  120. record.MachineType = SC.GetSCString(SCName.System_YZQZMachineType).Value;
  121. record.UploadTime = DateTime.Now.ToString();
  122. requestRecords.Add(record);
  123. try
  124. {
  125. var postdata = JsonConvert.SerializeObject(requestRecords);
  126. string webapi = SC.GetSCString(SCName.System_YZQZWebapi).Value + "/api/mes/SaveMachineStatusRecord";
  127. LOG.Info("SaveMachineStatusRecord|" + Pstate.ToString());
  128. string status;
  129. string result = HttpClient.PostResponse(webapi, postdata, out status);
  130. return true;
  131. }
  132. catch (Exception ex)
  133. {
  134. LOG.Info("MES|ErrorResponse|" + ex.Message);
  135. return false;
  136. }
  137. }
  138. public override bool StartJobRecord(string lotId = "", string receipeName = "", string operation = "", string operatorId = "")
  139. {
  140. LOG.Info("StartJobRecord|" + "start|"+lotId+"|"+ receipeName);
  141. if (jobStart) return false;
  142. else jobStart = true;
  143. m_LotId = lotId;
  144. m_ReceipeName = receipeName;
  145. m_operation=operation;
  146. m_operatorId=operatorId;
  147. if (records == null)
  148. {
  149. records = new List<JobRecord>();
  150. }
  151. else records.Clear();
  152. work(true, false);
  153. _thread = new PeriodicJob(30000, this.OnTimer, "DBJob", true);
  154. _thread.Start();
  155. return true;
  156. }
  157. private bool OnTimer()
  158. {
  159. return work();
  160. }
  161. private bool work(bool start=false,bool end=false)
  162. {
  163. int i = 0;
  164. LOG.Info("work |"+i++);
  165. if (string.IsNullOrEmpty(m_machineNumber)) m_machineNumber = SC.GetSCString(SCName.System_YZQZMachineNumber).Value;
  166. if (string.IsNullOrEmpty(m_machineType)) m_machineType = SC.GetSCString(SCName.System_YZQZMachineType).Value;
  167. JobRecord oneRecord = new JobRecord();
  168. if(start)
  169. oneRecord.StartTime = DateTime.Now.ToString();
  170. if(end)
  171. oneRecord.EndtTime = DateTime.Now.ToString();
  172. oneRecord.CharmberPressure = IO.AI["AI_Chamber_real_pressure_fb_"].Value.ToString();
  173. oneRecord.OxygenConcentration = IO.AI["AI_MFC1_real_flow__fb"].Value.ToString();//???? how to make sure O2.
  174. oneRecord.Power = IO.AI["AI_RF_forward_power_fb"].Value.ToString();
  175. oneRecord.LotId =m_LotId;
  176. oneRecord.MachineNumber = m_machineNumber;
  177. oneRecord.MachineType = m_machineType;
  178. oneRecord.ReceipeName = m_ReceipeName;
  179. oneRecord.Operation = m_operation;
  180. oneRecord.OperatorId = m_operatorId;
  181. records.Add(oneRecord);
  182. return true;
  183. }
  184. public override void EndJobRecord()
  185. {
  186. LOG.Info("EndJobRecord|");
  187. if (!jobStart) return;
  188. if(jobStart)jobStart = false;
  189. work(false,true);
  190. if (_thread != null)
  191. _thread.Stop();
  192. saveToWanpan();
  193. }
  194. private static string dateTimeStr;
  195. private async void saveToWanpan()
  196. {
  197. await Task.Run(() =>
  198. {
  199. dateTimeStr = DateTime.Now.ToString("yyyyMMddHHmm");
  200. Export2CsvLocalFile();
  201. Export2CsvWanPanFile();
  202. records.Clear();
  203. });
  204. }
  205. private void Export2CsvLocalFile()
  206. {
  207. try
  208. {
  209. string path = string.Format(@"{0}", "Logs");
  210. DirectoryInfo curFolderInfo = new DirectoryInfo(path);
  211. string csvFileName = m_LotId.Replace("/","-").TrimEnd('-') +".csv";
  212. string filePath = Path.Combine(curFolderInfo.FullName, csvFileName);
  213. LOG.Info("Export2CsvLocalFile" + filePath);
  214. using (System.IO.StreamWriter fs = new System.IO.StreamWriter(filePath, false, Encoding.UTF8))
  215. {
  216. fs.Write("【批号】,【工序】,【作业员ID】,【机台号】,【机台类型】,【程序名称】,【开始时间】,【结束时间】,【氧气浓度】,【腔体压力】,【功率】");
  217. fs.WriteLine();
  218. foreach (var item in records)
  219. {
  220. fs.Write(item.LotId.TrimEnd('/'));
  221. fs.Write(",");
  222. fs.Write(item.Operation);
  223. fs.Write(",");
  224. fs.Write(item.OperatorId);
  225. fs.Write(",");
  226. fs.Write(item.MachineNumber);
  227. fs.Write(",");
  228. fs.Write(item.MachineType);
  229. fs.Write(",");
  230. fs.Write(item.ReceipeName);
  231. fs.Write(",");
  232. fs.Write(item.StartTime);
  233. fs.Write(",");
  234. fs.Write(item.EndtTime);
  235. fs.Write(",");
  236. fs.Write(item.OxygenConcentration);
  237. fs.Write(",");
  238. fs.Write(item.CharmberPressure);
  239. fs.Write(",");
  240. fs.Write(item.Power);
  241. fs.Write("\n");
  242. }
  243. fs.Close();
  244. }
  245. }
  246. catch (Exception ex)
  247. {
  248. LOG.Info(ex.Message);
  249. return;
  250. }
  251. }
  252. private void Export2CsvWanPanFile()
  253. {
  254. try
  255. {
  256. if (string.IsNullOrEmpty(m_wanPanPath))
  257. m_wanPanPath = SC.GetSCString(SCName.System_YZQZWanPan).Value;
  258. string csvFileName = m_LotId.Replace("/", "-").TrimEnd('-') + ".csv";
  259. string filePath = Path.Combine(m_wanPanPath, csvFileName);
  260. LOG.Info("Export2CsvWanPanFile" + filePath);
  261. using (System.IO.StreamWriter fs = new System.IO.StreamWriter(filePath, false, Encoding.UTF8))
  262. {
  263. fs.Write("【批号】,【工序】,【作业员ID】,【机台号】,【机台类型】,【程序名称】,【开始时间】,【结束时间】,【氧气浓度】,【腔体压力】,【功率】");
  264. fs.WriteLine();
  265. foreach (var item in records)
  266. {
  267. fs.Write(item.LotId.TrimEnd('/'));
  268. fs.Write(",");
  269. fs.Write(item.Operation);
  270. fs.Write(",");
  271. fs.Write(item.OperatorId);
  272. fs.Write(",");
  273. fs.Write(item.MachineNumber);
  274. fs.Write(",");
  275. fs.Write(item.MachineType);
  276. fs.Write(",");
  277. fs.Write(item.ReceipeName);
  278. fs.Write(",");
  279. fs.Write(item.StartTime);
  280. fs.Write(",");
  281. fs.Write(item.EndtTime);
  282. fs.Write(",");
  283. fs.Write(item.OxygenConcentration);
  284. fs.Write(",");
  285. fs.Write(item.CharmberPressure);
  286. fs.Write(",");
  287. fs.Write(item.Power);
  288. fs.Write("\n");
  289. }
  290. fs.Close();
  291. }
  292. }
  293. catch (Exception ex)
  294. {
  295. LOG.Info(ex.Message);
  296. return;
  297. }
  298. }
  299. }
  300. public enum PState
  301. {
  302. Init,
  303. Idle,
  304. Vent,
  305. Feed,
  306. Pumping,
  307. StopPumping,
  308. GasFlowing,
  309. StopGasFlowing,
  310. RfPowering,
  311. CyclePurge,
  312. LinePump,
  313. TankPump,
  314. ContainerPump,
  315. LoadProcessRecipe,
  316. ProcessHighVac,
  317. PreProcess,
  318. Processing,
  319. PostProcess,
  320. LeakCheck,
  321. Error,
  322. }
  323. }