Make_QueryMac.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. snCollectionBox1.Focus();
  148. snCollectionBox1.SelectAll();
  149. }
  150. else
  151. {
  152. MsgBox.Text = "Fail";
  153. MsgBox.BackColor = Color.Red;
  154. resultType = "NG";
  155. snCollectionBox1.Focus();
  156. snCollectionBox1.SelectAll();
  157. }
  158. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, woVal.Text, User.UserLineCode, User.UserSourceCode, "MAC比对", "", SN, "");
  159. sql.Clear();
  160. sql.Append($@"INSERT INTO steptestdetail (std_id,std_sn,std_makecode,std_date,std_indate,
  161. std_rescode,std_stepcode,std_class,std_value1,STD_VALUE2,STD_TESTRESULT) values
  162. (steptestdetail_seq.NEXTVAL,'{SN}','{woVal.Text}',sysdate,sysdate,
  163. '{User.UserSourceCode}','{User.CurrentStepCode}','MAC比对','{snCollectionBox1.Text}','{MAC}','{resultType}')");
  164. dh.ExecuteSql(sql.ToString(), "insert");
  165. ShowMsg(1, $"MAC核对成功,已上传测试结果");
  166. /*res.Items.Clear();
  167. res.Items.Add(output);*/
  168. /*string install = $"install -r {apkPath}";
  169. ExecuteAdbCommand(adbPath, install, ref output, ref error);
  170. if (!output.ToUpper().Contains("SUCCESS"))
  171. {
  172. return;
  173. }*/
  174. /*string readMac = "shell cat /sys/class/net/wlan0/address";
  175. ExecuteAdbCommand(adbPath, readMac, ref output, ref error);
  176. if (!string.IsNullOrEmpty(error))
  177. {
  178. ShowMsg(0, $"获取MAC异常: {error}");
  179. return;
  180. }
  181. MAC = output.Replace("\r\n", "").Replace(":", "").ToUpper();
  182. macPath = Path.Combine(fullPath, MAC);
  183. if (!Directory.Exists(macPath))
  184. {
  185. Directory.CreateDirectory(macPath);
  186. }
  187. string command = $"pull sdcard/Download/FactoryTest_result.xml {macPath}";
  188. ExecuteAdbCommand(adbPath, command, ref output, ref error);
  189. if (!string.IsNullOrEmpty(error))
  190. {
  191. ShowMsg(0, $"提取测试记录异常: {error}");
  192. return;
  193. }
  194. string path = Path.Combine(macPath, "FactoryTest_result.xml");
  195. ParseXml(path);*/
  196. }
  197. private void snCollectionBox1_KeyDown(object sender, KeyEventArgs e)
  198. {
  199. if (e.KeyCode != Keys.Enter) return;
  200. GetSNCode.Focus();
  201. GetSNCode.SelectAll();
  202. MsgBox.Text = "Pass";
  203. MsgBox.BackColor = Color.LawnGreen;
  204. }
  205. private void ParseXml(string path)
  206. {
  207. if (!File.Exists(path))
  208. {
  209. ShowMsg(0, $"未产生检测结果,请重新检测");
  210. return;
  211. }
  212. string fileContent = File.ReadAllText(path, Encoding.UTF8);
  213. fileContent = fileContent.Trim();
  214. if (fileContent.Length == 0)
  215. {
  216. ShowMsg(0, $"输出无测试结果,请在AnLink执行测试");
  217. return;
  218. }
  219. int beginIndex = fileContent.IndexOf("<?xml");
  220. if (beginIndex < 0)
  221. {
  222. ShowMsg(0, $"xml文件没有声明,格式有误,请重新操作");
  223. return;
  224. }
  225. int endIndex = fileContent.LastIndexOf("</map>");
  226. if (endIndex < 0)
  227. {
  228. ShowMsg(0, $"xml文件结束标签,格式有误,请重新操作");
  229. return;
  230. }
  231. string xmlContent = fileContent.Substring(beginIndex, endIndex - beginIndex + 6);
  232. XmlDocument doc = new XmlDocument();
  233. try
  234. {
  235. Dictionary<string, string> DataList = new Dictionary<string, string>();
  236. doc.LoadXml(xmlContent);
  237. XmlNodeList stringNodes = doc.SelectNodes("//string");
  238. foreach (XmlNode node in stringNodes)
  239. {
  240. string name = node.Attributes["name"].Value;
  241. string value = node.InnerText;
  242. DataList.Add(name, value);
  243. }
  244. dgv.Rows.Clear();
  245. string testvValue = "",isHave = "否",currentMac = "";
  246. foreach (var testItem in TestObj)
  247. {
  248. foreach (var item in DataList)
  249. {
  250. if (testItem.Name == item.Key)
  251. {
  252. isHave = "是";
  253. testvValue = item.Value.ToUpper() == "PASS" ? "OK" : "NG";
  254. }
  255. }
  256. if(testvValue == "NG")
  257. {
  258. currentMac = "NG";
  259. }
  260. DataGridViewRow dr = new DataGridViewRow();
  261. dr.CreateCells(dgv);
  262. dr.Cells[0].Value = MAC;
  263. dr.Cells[1].Value = testItem.ChineseName;
  264. dr.Cells[2].Value = isHave;
  265. dr.Cells[3].Value = testItem.Name;
  266. dr.Cells[4].Value = testvValue;
  267. dgv.Rows.Add(dr);
  268. string res = currentMac == "NG" ? "NG" : "OK";
  269. string omakeCode, oMsid, oErrorMessage;
  270. if (LogicHandler.CheckStepSNAndMacode(WO, User.UserSourceCode, SN, User.UserCode, out omakeCode, out oMsid, out oErrorMessage))
  271. {
  272. if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, SN, "成品产测信息", res, User.UserCode, out oErrorMessage))
  273. {
  274. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, WO, User.UserLineCode, User.UserSourceCode, "成品产测信息", "", SN, "");
  275. sql.Clear();
  276. sql.Append($@"INSERT INTO steptestdetail (std_id,std_sn,std_makecode,std_date,std_indate,
  277. std_rescode,std_stepcode,
  278. std_class,std_subclass1,std_value1) values
  279. (steptestdetail_seq.NEXTVAL,'{SN}','{WO}',sysdate,sysdate,
  280. '{User.UserSourceCode}','{User.CurrentStepCode}',
  281. '成品产测信息','{testItem.ChineseName}','{testvValue}')");
  282. dh.ExecuteSql(sql.ToString(), "insert");
  283. sql.Clear();
  284. sql.Append($@"UPDATE makeserial SET ms_mac = '{MAC}' WHERE ms_sncode = '{SN}'");
  285. dh.ExecuteSql(sql.ToString(), "update");
  286. ShowMsg(1, $"MAC绑定成功,已上传测试结果");
  287. }
  288. else
  289. {
  290. ShowMsg(0, $"过站处理记录NG: {oErrorMessage}");
  291. }
  292. }
  293. else
  294. {
  295. ShowMsg(0, $"过站核对NG: {oErrorMessage}");
  296. }
  297. }
  298. }
  299. catch (XmlException ex)
  300. {
  301. ShowMsg(0, ex.Message);
  302. }
  303. }
  304. public void ExecuteAdbCommand(string adbPath, string command, ref string output, ref string error)
  305. {
  306. output = string.Empty;
  307. error = string.Empty;
  308. try
  309. {
  310. using (Process process = new Process())
  311. {
  312. process.StartInfo.FileName = adbPath;
  313. process.StartInfo.Arguments = command;
  314. process.StartInfo.UseShellExecute = false;
  315. process.StartInfo.RedirectStandardOutput = true;
  316. process.StartInfo.RedirectStandardError = true;
  317. process.StartInfo.CreateNoWindow = true;
  318. process.Start();
  319. output = process.StandardOutput.ReadToEnd();
  320. error = process.StandardError.ReadToEnd();
  321. process.WaitForExit();
  322. string msg = output ?? error;
  323. ShowMsg(1, $"执行ADB成功: {msg.Replace("\r\n", "")}");
  324. }
  325. }
  326. catch (Exception ex)
  327. {
  328. ShowMsg(0, $"执行ADB时异常: {ex.Message}");
  329. }
  330. }
  331. private void Clean_Click(object sender, EventArgs e)
  332. {
  333. OperatResult.Clear();
  334. }
  335. private void SetSndetail(DataTable dt)
  336. {
  337. snVal.Text = dt.Rows[0]["ms_sncode"].ToString();
  338. woVal.Text = dt.Rows[0]["ma_code"].ToString();
  339. prodVal.Text = dt.Rows[0]["ms_prodcode"].ToString();
  340. prodNameVal.Text = dt.Rows[0]["pr_detail"].ToString();
  341. }
  342. private void ClearSnDetail()
  343. {
  344. snVal.Text = "";
  345. woVal.Text = "";
  346. prodVal.Text = "";
  347. prodNameVal.Text = "";
  348. }
  349. private void ShowMsg(int type, string msg)
  350. {
  351. string fullDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  352. if (type == 0)
  353. {
  354. OperatResult.AppendText($"{fullDateTime}: {msg}\n", Color.Red);
  355. }
  356. else if (type == 1)
  357. {
  358. OperatResult.AppendText($"{fullDateTime}: {msg}\n", Color.Black);
  359. }
  360. }
  361. public class TestItem
  362. {
  363. public string Name { get; set; }
  364. public string ChineseName { get; set; }
  365. }
  366. public bool CheckConnected(string adbPath,out string ConnectStr)
  367. {
  368. try
  369. {
  370. using (Process process = new Process())
  371. {
  372. process.StartInfo.FileName = adbPath;
  373. process.StartInfo.Arguments = "devices";
  374. process.StartInfo.UseShellExecute = false;
  375. process.StartInfo.RedirectStandardOutput = true;
  376. process.StartInfo.CreateNoWindow = true;
  377. process.Start();
  378. ConnectStr = process.StandardOutput.ReadToEnd();
  379. process.WaitForExit();
  380. return output.Contains("device") && !output.Contains("List of devices attached");
  381. }
  382. }
  383. catch(Exception ex)
  384. {
  385. ConnectStr = ex.Message;
  386. return false;
  387. }
  388. }
  389. }
  390. }