Make_ProductTest.cs 14 KB

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