Make_QueryMac.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using DevExpress.Printing.Core.PdfExport.Metafile;
  2. using DevExpress.Utils.Drawing.Helpers;
  3. using NPOI.SS.Formula.Eval;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Configuration.Assemblies;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Windows.Forms;
  17. using System.Xml;
  18. using UAS_MES_NEW.DataOperate;
  19. using UAS_MES_NEW.Entity;
  20. using UAS_MES_NEW.PublicMethod;
  21. namespace UAS_MES_NEW.Make
  22. {
  23. public partial class Make_QueryMac : Form
  24. {
  25. StringBuilder sql = new StringBuilder();
  26. DataHelper dh = new DataHelper();
  27. DataTable dt = new DataTable();
  28. string assemblyPath, assemblyDirectory;
  29. string apkPath, adbPath;
  30. string outPath, fullPath, macPath;
  31. string output = "", error = "";
  32. string SN,MAC,WO = "";
  33. string resultType = "NG";
  34. List<TestItem> TestObj = new List<TestItem> { };
  35. public Make_QueryMac()
  36. {
  37. InitializeComponent();
  38. }
  39. private void Make_WirelessTest_Load(object sender, EventArgs e)
  40. {
  41. assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
  42. assemblyDirectory = Path.GetDirectoryName(assemblyPath);
  43. apkPath = Path.Combine(assemblyDirectory, "AutoProdTest_1.0.apk");
  44. adbPath = Path.Combine(assemblyDirectory, "adb.exe");
  45. outPath = "C:\\Users\\MI\\Desktop";
  46. TestObj.Add(new TestItem() { ChineseName = "SN信息测试" , Name = "SN_Info_FP" });
  47. TestObj.Add(new TestItem() { ChineseName = "WiFi测试", Name = "WiFi_FP" });
  48. TestObj.Add(new TestItem() { ChineseName = "Type-C网线口测试", Name = "Ethernet_FP" });
  49. TestObj.Add(new TestItem() { ChineseName = "灯带效果测试", Name = "LED_FP" });
  50. TestObj.Add(new TestItem() { ChineseName = "功率测试", Name = "Power_FP" });
  51. TestObj.Add(new TestItem() { ChineseName = "存储测试", Name = "Storage_FP" });
  52. TestObj.Add(new TestItem() { ChineseName = "恢复出厂测试", Name = "FactoryReset_FP" });
  53. TestObj.Add(new TestItem() { ChineseName = "算法测试", Name = "Algorithm_FP" });
  54. TestObj.Add(new TestItem() { ChineseName = "HDMI显示测试", Name = "HDMI_FP" });
  55. TestObj.Add(new TestItem() { ChineseName = "蓝牙测试", Name = "Bluetooth_FP" });
  56. TestObj.Add(new TestItem() { ChineseName = "摄像头成像测试", Name = "Camera_FP" });
  57. TestObj.Add(new TestItem() { ChineseName = "风扇测试", Name = "Fan_FP" });
  58. }
  59. private void GetSNCode_KeyDown(object sender, KeyEventArgs e)
  60. {
  61. if (e.KeyCode == Keys.Enter)
  62. {
  63. if (GetSNCode.Text == "")
  64. {
  65. ShowMsg(0, "序列号不允许为空");
  66. ClearSnDetail();
  67. return;
  68. }
  69. string oStatus, ErrorMessage = "";
  70. if (LogicHandler.GetMakeInfo(GetSNCode.Text.Trim(), out WO, out oStatus, out ErrorMessage))
  71. {
  72. sql.Clear();
  73. sql.Append($@"SELECT ms_sncode,ma_code,ms_prodcode,pr_detail
  74. FROM makeserial,make,product WHERE ms_sncode = '{GetSNCode.Text.Trim()}'
  75. AND ms_makecode = ma_code AND ma_prodcode = pr_code");
  76. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  77. if (dt.Rows.Count > 0)
  78. {
  79. SetSndetail(dt);
  80. }
  81. }
  82. else
  83. {
  84. ShowMsg(0, "序列号不存在");
  85. ClearSnDetail();
  86. return;
  87. }
  88. SN = GetSNCode.Text.Trim();
  89. GetSNCode.Focus();
  90. GetSNCode.SelectAll();
  91. Query_Click(null, null);
  92. }
  93. }
  94. private void Query_Click(object sender, EventArgs e)
  95. {
  96. if (GetSNCode.Text == "" || string.IsNullOrEmpty(SN))
  97. {
  98. ShowMsg(0, "序列号不允许为空");
  99. return;
  100. }
  101. //if (!File.Exists(apkPath))
  102. //{
  103. // ShowMsg(0, $"测试apk文件不存在 - {apkPath}");
  104. // return;
  105. //}
  106. if (!File.Exists(adbPath))
  107. {
  108. ShowMsg(0, $"adb命令文件不存在 - {adbPath}");
  109. return;
  110. }
  111. if (!Directory.Exists(outPath))
  112. {
  113. Directory.CreateDirectory(outPath);
  114. }
  115. string dateFolderName = DateTime.Now.ToString("yyyyMMdd");
  116. fullPath = Path.Combine(outPath, dateFolderName);
  117. if (!Directory.Exists(fullPath))
  118. {
  119. Directory.CreateDirectory(fullPath);
  120. }
  121. if (CheckConnected(adbPath, out string ConnectStr))
  122. {
  123. ShowMsg(0, $"设备连接异常: {ConnectStr}".Replace("\r\n", " - "));
  124. return;
  125. }
  126. string readMac = "shell ip addr show wlan0";
  127. ExecuteAdbCommand(adbPath, readMac, ref output, ref error);
  128. if (!string.IsNullOrEmpty(error))
  129. {
  130. ShowMsg(0, $"获取MAC异常: {error}");
  131. return;
  132. }
  133. if (!string.IsNullOrEmpty(output))
  134. {
  135. Match match = Regex.Match(output, @"link/ether\s+([0-9a-fA-F:]{17})");
  136. if (match.Success)
  137. {
  138. MAC = match.Groups[1].Value.Trim().ToUpper().Replace(":","");
  139. ShowMsg(1, $"解析结果: {MAC}");
  140. }
  141. }
  142. if(MAC == snCollectionBox1.Text)
  143. {
  144. MsgBox.Text = "Pass";
  145. MsgBox.BackColor = Color.LawnGreen;
  146. resultType = "OK";
  147. string omakeCode, oMsid, oErrorMessage;
  148. if (LogicHandler.CheckStepSNAndMacode(WO, User.UserSourceCode, SN, User.UserCode, out omakeCode, out oMsid, out oErrorMessage))
  149. {
  150. if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, SN, "MAC比对", resultType, User.UserCode, out oErrorMessage))
  151. {
  152. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, woVal.Text, User.UserLineCode, User.UserSourceCode, "MAC比对", "", SN, "");
  153. sql.Clear();
  154. sql.Append($@"INSERT INTO steptestdetail (std_id,std_sn,std_makecode,std_date,std_indate,
  155. std_rescode,std_stepcode,std_class,std_value1,STD_VALUE2,STD_TESTRESULT) values
  156. (steptestdetail_seq.NEXTVAL,'{SN}','{woVal.Text}',sysdate,sysdate,
  157. '{User.UserSourceCode}','{User.CurrentStepCode}','MAC比对','{snCollectionBox1.Text}','{MAC}','{resultType}')");
  158. dh.ExecuteSql(sql.ToString(), "insert");
  159. ShowMsg(1, $"MAC核对成功,已处理过站并上传测试结果");
  160. }
  161. else
  162. {
  163. ShowMsg(0, $"过站处理记录NG: {oErrorMessage}");
  164. }
  165. }
  166. else
  167. {
  168. ShowMsg(0, $"过站核对NG: {oErrorMessage}");
  169. }
  170. snCollectionBox1.Focus();
  171. snCollectionBox1.SelectAll();
  172. }
  173. else
  174. {
  175. MsgBox.Text = "Fail";
  176. MsgBox.BackColor = Color.Red;
  177. resultType = "NG";
  178. snCollectionBox1.Focus();
  179. snCollectionBox1.SelectAll();
  180. }
  181. /*res.Items.Clear();
  182. res.Items.Add(output);*/
  183. /*string install = $"install -r {apkPath}";
  184. ExecuteAdbCommand(adbPath, install, ref output, ref error);
  185. if (!output.ToUpper().Contains("SUCCESS"))
  186. {
  187. return;
  188. }*/
  189. /*string readMac = "shell cat /sys/class/net/wlan0/address";
  190. ExecuteAdbCommand(adbPath, readMac, ref output, ref error);
  191. if (!string.IsNullOrEmpty(error))
  192. {
  193. ShowMsg(0, $"获取MAC异常: {error}");
  194. return;
  195. }
  196. MAC = output.Replace("\r\n", "").Replace(":", "").ToUpper();
  197. macPath = Path.Combine(fullPath, MAC);
  198. if (!Directory.Exists(macPath))
  199. {
  200. Directory.CreateDirectory(macPath);
  201. }
  202. string command = $"pull sdcard/Download/FactoryTest_result.xml {macPath}";
  203. ExecuteAdbCommand(adbPath, command, ref output, ref error);
  204. if (!string.IsNullOrEmpty(error))
  205. {
  206. ShowMsg(0, $"提取测试记录异常: {error}");
  207. return;
  208. }
  209. string path = Path.Combine(macPath, "FactoryTest_result.xml");
  210. ParseXml(path);*/
  211. }
  212. private void snCollectionBox1_KeyDown(object sender, KeyEventArgs e)
  213. {
  214. if (e.KeyCode != Keys.Enter) return;
  215. GetSNCode.Focus();
  216. GetSNCode.SelectAll();
  217. MsgBox.Text = "";
  218. MsgBox.BackColor = SystemColors.Control;
  219. }
  220. private void ParseXml(string path)
  221. {
  222. if (!File.Exists(path))
  223. {
  224. ShowMsg(0, $"未产生检测结果,请重新检测");
  225. return;
  226. }
  227. string fileContent = File.ReadAllText(path, Encoding.UTF8);
  228. fileContent = fileContent.Trim();
  229. if (fileContent.Length == 0)
  230. {
  231. ShowMsg(0, $"输出无测试结果,请在AnLink执行测试");
  232. return;
  233. }
  234. int beginIndex = fileContent.IndexOf("<?xml");
  235. if (beginIndex < 0)
  236. {
  237. ShowMsg(0, $"xml文件没有声明,格式有误,请重新操作");
  238. return;
  239. }
  240. int endIndex = fileContent.LastIndexOf("</map>");
  241. if (endIndex < 0)
  242. {
  243. ShowMsg(0, $"xml文件结束标签,格式有误,请重新操作");
  244. return;
  245. }
  246. string xmlContent = fileContent.Substring(beginIndex, endIndex - beginIndex + 6);
  247. XmlDocument doc = new XmlDocument();
  248. try
  249. {
  250. Dictionary<string, string> DataList = new Dictionary<string, string>();
  251. doc.LoadXml(xmlContent);
  252. XmlNodeList stringNodes = doc.SelectNodes("//string");
  253. foreach (XmlNode node in stringNodes)
  254. {
  255. string name = node.Attributes["name"].Value;
  256. string value = node.InnerText;
  257. DataList.Add(name, value);
  258. }
  259. dgv.Rows.Clear();
  260. string testvValue = "",isHave = "否",currentMac = "";
  261. foreach (var testItem in TestObj)
  262. {
  263. foreach (var item in DataList)
  264. {
  265. if (testItem.Name == item.Key)
  266. {
  267. isHave = "是";
  268. testvValue = item.Value.ToUpper() == "PASS" ? "OK" : "NG";
  269. }
  270. }
  271. if(testvValue == "NG")
  272. {
  273. currentMac = "NG";
  274. }
  275. DataGridViewRow dr = new DataGridViewRow();
  276. dr.CreateCells(dgv);
  277. dr.Cells[0].Value = MAC;
  278. dr.Cells[1].Value = testItem.ChineseName;
  279. dr.Cells[2].Value = isHave;
  280. dr.Cells[3].Value = testItem.Name;
  281. dr.Cells[4].Value = testvValue;
  282. dgv.Rows.Add(dr);
  283. string res = currentMac == "NG" ? "NG" : "OK";
  284. string omakeCode, oMsid, oErrorMessage;
  285. if (LogicHandler.CheckStepSNAndMacode(WO, User.UserSourceCode, SN, User.UserCode, out omakeCode, out oMsid, out oErrorMessage))
  286. {
  287. if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, SN, "成品产测信息", res, User.UserCode, out oErrorMessage))
  288. {
  289. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, WO, User.UserLineCode, User.UserSourceCode, "成品产测信息", "", SN, "");
  290. sql.Clear();
  291. sql.Append($@"INSERT INTO steptestdetail (std_id,std_sn,std_makecode,std_date,std_indate,
  292. std_rescode,std_stepcode,
  293. std_class,std_subclass1,std_value1) values
  294. (steptestdetail_seq.NEXTVAL,'{SN}','{WO}',sysdate,sysdate,
  295. '{User.UserSourceCode}','{User.CurrentStepCode}',
  296. '成品产测信息','{testItem.ChineseName}','{testvValue}')");
  297. dh.ExecuteSql(sql.ToString(), "insert");
  298. sql.Clear();
  299. sql.Append($@"UPDATE makeserial SET ms_mac = '{MAC}' WHERE ms_sncode = '{SN}'");
  300. dh.ExecuteSql(sql.ToString(), "update");
  301. ShowMsg(1, $"MAC绑定成功,已上传测试结果");
  302. }
  303. else
  304. {
  305. ShowMsg(0, $"过站处理记录NG: {oErrorMessage}");
  306. }
  307. }
  308. else
  309. {
  310. ShowMsg(0, $"过站核对NG: {oErrorMessage}");
  311. }
  312. }
  313. }
  314. catch (XmlException ex)
  315. {
  316. ShowMsg(0, ex.Message);
  317. }
  318. }
  319. public void ExecuteAdbCommand(string adbPath, string command, ref string output, ref string error)
  320. {
  321. output = string.Empty;
  322. error = string.Empty;
  323. try
  324. {
  325. using (Process process = new Process())
  326. {
  327. process.StartInfo.FileName = adbPath;
  328. process.StartInfo.Arguments = command;
  329. process.StartInfo.UseShellExecute = false;
  330. process.StartInfo.RedirectStandardOutput = true;
  331. process.StartInfo.RedirectStandardError = true;
  332. process.StartInfo.CreateNoWindow = true;
  333. process.Start();
  334. output = process.StandardOutput.ReadToEnd();
  335. error = process.StandardError.ReadToEnd();
  336. process.WaitForExit();
  337. string msg = output ?? error;
  338. ShowMsg(1, $"执行ADB成功: {msg.Replace("\r\n", "")}");
  339. }
  340. }
  341. catch (Exception ex)
  342. {
  343. ShowMsg(0, $"执行ADB时异常: {ex.Message}");
  344. }
  345. }
  346. private void Clean_Click(object sender, EventArgs e)
  347. {
  348. OperatResult.Clear();
  349. }
  350. private void SetSndetail(DataTable dt)
  351. {
  352. snVal.Text = dt.Rows[0]["ms_sncode"].ToString();
  353. woVal.Text = dt.Rows[0]["ma_code"].ToString();
  354. prodVal.Text = dt.Rows[0]["ms_prodcode"].ToString();
  355. prodNameVal.Text = dt.Rows[0]["pr_detail"].ToString();
  356. }
  357. private void ClearSnDetail()
  358. {
  359. snVal.Text = "";
  360. woVal.Text = "";
  361. prodVal.Text = "";
  362. prodNameVal.Text = "";
  363. }
  364. private void ShowMsg(int type, string msg)
  365. {
  366. string fullDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  367. if (type == 0)
  368. {
  369. OperatResult.AppendText($"{fullDateTime}: {msg}\n", Color.Red);
  370. }
  371. else if (type == 1)
  372. {
  373. OperatResult.AppendText($"{fullDateTime}: {msg}\n", Color.Black);
  374. }
  375. }
  376. public class TestItem
  377. {
  378. public string Name { get; set; }
  379. public string ChineseName { get; set; }
  380. }
  381. public bool CheckConnected(string adbPath,out string ConnectStr)
  382. {
  383. try
  384. {
  385. using (Process process = new Process())
  386. {
  387. process.StartInfo.FileName = adbPath;
  388. process.StartInfo.Arguments = "devices";
  389. process.StartInfo.UseShellExecute = false;
  390. process.StartInfo.RedirectStandardOutput = true;
  391. process.StartInfo.CreateNoWindow = true;
  392. process.Start();
  393. ConnectStr = process.StandardOutput.ReadToEnd();
  394. process.WaitForExit();
  395. return output.Contains("device") && !output.Contains("List of devices attached");
  396. }
  397. }
  398. catch(Exception ex)
  399. {
  400. ConnectStr = ex.Message;
  401. return false;
  402. }
  403. }
  404. }
  405. }