Make_QueryMac.cs 14 KB

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