Make_ProductTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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_ProductTest : 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_ProductTest()
  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 == "")
  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 install = $"install -r {apkPath}";
  123. ExecuteAdbCommand(adbPath, install, ref output, ref error);
  124. if (!output.ToUpper().Contains("SUCCESS"))
  125. {
  126. return;
  127. }*/
  128. string readMac = "shell cat /sys/class/net/wlan0/address";
  129. ExecuteAdbCommand(adbPath, readMac, ref output, ref error);
  130. if (!string.IsNullOrEmpty(error))
  131. {
  132. ShowMsg(0, $"获取MAC异常: {error}");
  133. return;
  134. }
  135. MAC = output.Replace("\r\n", "").Replace(":", "").ToUpper();
  136. macPath = Path.Combine(fullPath, MAC);
  137. if (!Directory.Exists(macPath))
  138. {
  139. Directory.CreateDirectory(macPath);
  140. }
  141. string command = $"pull sdcard/Download/FactoryTest_result.xml {macPath}";
  142. ExecuteAdbCommand(adbPath, command, ref output, ref error);
  143. if (!string.IsNullOrEmpty(error))
  144. {
  145. ShowMsg(0, $"提取测试记录异常: {error}");
  146. return;
  147. }
  148. string path = Path.Combine(macPath, "FactoryTest_result.xml");
  149. ParseXml(path);
  150. }
  151. private void ParseXml(string path)
  152. {
  153. if (!File.Exists(path))
  154. {
  155. ShowMsg(0, $"未产生检测结果,请重新检测");
  156. return;
  157. }
  158. string fileContent = File.ReadAllText(path, Encoding.UTF8);
  159. fileContent = fileContent.Trim();
  160. if (fileContent.Length == 0)
  161. {
  162. ShowMsg(0, $"输出无测试结果,请在AnLink执行测试");
  163. return;
  164. }
  165. int beginIndex = fileContent.IndexOf("<?xml");
  166. if (beginIndex < 0)
  167. {
  168. ShowMsg(0, $"xml文件没有声明,格式有误,请重新操作");
  169. return;
  170. }
  171. int endIndex = fileContent.LastIndexOf("</map>");
  172. if (endIndex < 0)
  173. {
  174. ShowMsg(0, $"xml文件结束标签,格式有误,请重新操作");
  175. return;
  176. }
  177. string xmlContent = fileContent.Substring(beginIndex, endIndex - beginIndex + 6);
  178. XmlDocument doc = new XmlDocument();
  179. try
  180. {
  181. Dictionary<string, string> DataList = new Dictionary<string, string>();
  182. doc.LoadXml(xmlContent);
  183. XmlNodeList stringNodes = doc.SelectNodes("//string");
  184. foreach (XmlNode node in stringNodes)
  185. {
  186. string name = node.Attributes["name"].Value;
  187. string value = node.InnerText;
  188. DataList.Add(name, value);
  189. }
  190. dgv.Rows.Clear();
  191. string testvValue = "",isHave = "否";
  192. foreach (var testItem in TestObj)
  193. {
  194. foreach (var item in DataList)
  195. {
  196. if (testItem.Name == item.Key)
  197. {
  198. isHave = "是";
  199. testvValue = item.Value.ToUpper() == "PASS" ? "OK" : "NG";
  200. }
  201. }
  202. DataGridViewRow dr = new DataGridViewRow();
  203. dr.CreateCells(dgv);
  204. dr.Cells[0].Value = MAC;
  205. dr.Cells[1].Value = testItem.ChineseName;
  206. dr.Cells[2].Value = isHave;
  207. dr.Cells[3].Value = testItem.Name;
  208. dr.Cells[4].Value = testvValue;
  209. dgv.Rows.Add(dr);
  210. /* sql.Clear();
  211. sql.Append($@"INSERT into steptestdetail SELECT steptestdetail_seq.NEXTVAL std_id,{SN} std_sn,{WO} std_makecode,
  212. '成品产测信息' std_class,SYSDATE std_date,SYSDATE std_indate,{User.UserSourceCode} std_rescode,
  213. {testItem.ChineseName} std_subclass1,{testvValue} std_value1 FROM dual");
  214. dh.ExecuteSql(sql.ToString(), "insert");*/
  215. }
  216. //sql.Clear();
  217. //sql.Append($@"UPDATE makeserial SET ms_mac = '{MAC}' WHERE ms_sncode = '{SN}'");
  218. //dh.ExecuteSql(sql.ToString(), "update");
  219. ShowMsg(1, $"MAC绑定成功,已上传测试结果");
  220. }
  221. catch (XmlException ex)
  222. {
  223. ShowMsg(0, ex.Message);
  224. }
  225. }
  226. public void ExecuteAdbCommand(string adbPath, string command, ref string output, ref string error)
  227. {
  228. output = string.Empty;
  229. error = string.Empty;
  230. try
  231. {
  232. using (Process process = new Process())
  233. {
  234. process.StartInfo.FileName = adbPath;
  235. process.StartInfo.Arguments = command;
  236. process.StartInfo.UseShellExecute = false;
  237. process.StartInfo.RedirectStandardOutput = true;
  238. process.StartInfo.RedirectStandardError = true;
  239. process.StartInfo.CreateNoWindow = true;
  240. process.Start();
  241. output = process.StandardOutput.ReadToEnd();
  242. error = process.StandardError.ReadToEnd();
  243. process.WaitForExit();
  244. string msg = output ?? error;
  245. ShowMsg(1, $"执行ADB成功: {msg.Replace("\r\n", "")}");
  246. }
  247. }
  248. catch (Exception ex)
  249. {
  250. ShowMsg(0, $"执行ADB时异常: {ex.Message}");
  251. }
  252. }
  253. private void Clean_Click(object sender, EventArgs e)
  254. {
  255. OperatResult.Clear();
  256. }
  257. private void SetSndetail(DataTable dt)
  258. {
  259. snVal.Text = dt.Rows[0]["ms_sncode"].ToString();
  260. woVal.Text = dt.Rows[0]["ma_code"].ToString();
  261. prodVal.Text = dt.Rows[0]["ms_prodcode"].ToString();
  262. prodNameVal.Text = dt.Rows[0]["pr_detail"].ToString();
  263. }
  264. private void ClearSnDetail()
  265. {
  266. snVal.Text = "";
  267. woVal.Text = "";
  268. prodVal.Text = "";
  269. prodNameVal.Text = "";
  270. }
  271. private void ShowMsg(int type, string msg)
  272. {
  273. string fullDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  274. if (type == 0)
  275. {
  276. OperatResult.AppendText($"{fullDateTime}: {msg}\n", Color.Red);
  277. }
  278. else if (type == 1)
  279. {
  280. OperatResult.AppendText($"{fullDateTime}: {msg}\n", Color.Black);
  281. }
  282. }
  283. public class TestItem
  284. {
  285. public string Name { get; set; }
  286. public string ChineseName { get; set; }
  287. }
  288. public bool CheckConnected(string adbPath,out string ConnectStr)
  289. {
  290. try
  291. {
  292. using (Process process = new Process())
  293. {
  294. process.StartInfo.FileName = adbPath;
  295. process.StartInfo.Arguments = "devices";
  296. process.StartInfo.UseShellExecute = false;
  297. process.StartInfo.RedirectStandardOutput = true;
  298. process.StartInfo.CreateNoWindow = true;
  299. process.Start();
  300. ConnectStr = process.StandardOutput.ReadToEnd();
  301. process.WaitForExit();
  302. return output.Contains("device") && !output.Contains("List of devices attached");
  303. }
  304. }
  305. catch(Exception ex)
  306. {
  307. ConnectStr = ex.Message;
  308. return false;
  309. }
  310. }
  311. }
  312. }