using DevExpress.Printing.Core.PdfExport.Metafile; using NPOI.SS.Formula.Eval; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Assemblies; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicMethod; namespace UAS_MES_NEW.Make { public partial class Make_ProductTest : Form { StringBuilder sql = new StringBuilder(); DataHelper dh = new DataHelper(); DataTable dt = new DataTable(); string assemblyPath, assemblyDirectory; string apkPath, adbPath; string outPath, fullPath, macPath; string output = "", error = ""; string SN,MAC,WO = ""; List TestObj = new List { }; public Make_ProductTest() { InitializeComponent(); } private void Make_WirelessTest_Load(object sender, EventArgs e) { assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location; assemblyDirectory = Path.GetDirectoryName(assemblyPath); apkPath = Path.Combine(assemblyDirectory, "AutoProdTest_1.0.apk"); adbPath = Path.Combine(assemblyDirectory, "adb.exe"); outPath = "C:\\Users\\MI\\Desktop"; TestObj.Add(new TestItem() { ChineseName = "SN信息测试" , Name = "SN_Info_FP" }); TestObj.Add(new TestItem() { ChineseName = "WiFi测试", Name = "WiFi_FP" }); TestObj.Add(new TestItem() { ChineseName = "Type-C网线口测试", Name = "Ethernet_FP" }); TestObj.Add(new TestItem() { ChineseName = "灯带效果测试", Name = "LED_FP" }); TestObj.Add(new TestItem() { ChineseName = "功率测试", Name = "Power_FP" }); TestObj.Add(new TestItem() { ChineseName = "存储测试", Name = "Storage_FP" }); TestObj.Add(new TestItem() { ChineseName = "恢复出厂测试", Name = "FactoryReset_FP" }); TestObj.Add(new TestItem() { ChineseName = "算法测试", Name = "Algorithm_FP" }); TestObj.Add(new TestItem() { ChineseName = "HDMI显示测试", Name = "HDMI_FP" }); TestObj.Add(new TestItem() { ChineseName = "蓝牙测试", Name = "Bluetooth_FP" }); TestObj.Add(new TestItem() { ChineseName = "摄像头成像测试", Name = "Camera_FP" }); TestObj.Add(new TestItem() { ChineseName = "风扇测试", Name = "Fan_FP" }); } private void GetSNCode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (GetSNCode.Text == "") { ShowMsg(0, "序列号不允许为空"); ClearSnDetail(); return; } string oStatus, ErrorMessage = ""; if (LogicHandler.GetMakeInfo(GetSNCode.Text.Trim(), out WO, out oStatus, out ErrorMessage)) { sql.Clear(); sql.Append($@"SELECT ms_sncode,ma_code,ms_prodcode,pr_detail FROM makeserial,make,product WHERE ms_sncode = '{GetSNCode.Text.Trim()}' AND ms_makecode = ma_code AND ma_prodcode = pr_code"); dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select"); if (dt.Rows.Count > 0) { SetSndetail(dt); } } else { ShowMsg(0, "序列号不存在"); ClearSnDetail(); return; } SN = GetSNCode.Text.Trim(); GetSNCode.Focus(); GetSNCode.SelectAll(); } } private void Query_Click(object sender, EventArgs e) { if (GetSNCode.Text == "" || string.IsNullOrEmpty(SN)) { ShowMsg(0, "序列号不允许为空"); return; } //if (!File.Exists(apkPath)) //{ // ShowMsg(0, $"测试apk文件不存在 - {apkPath}"); // return; //} if (!File.Exists(adbPath)) { ShowMsg(0, $"adb命令文件不存在 - {adbPath}"); return; } if (!Directory.Exists(outPath)) { Directory.CreateDirectory(outPath); } string dateFolderName = DateTime.Now.ToString("yyyyMMdd"); fullPath = Path.Combine(outPath, dateFolderName); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } if (CheckConnected(adbPath, out string ConnectStr)) { ShowMsg(0, $"设备连接异常: {ConnectStr}".Replace("\r\n", " - ")); return; } /*string install = $"install -r {apkPath}"; ExecuteAdbCommand(adbPath, install, ref output, ref error); if (!output.ToUpper().Contains("SUCCESS")) { return; }*/ string readMac = "shell cat /sys/class/net/wlan0/address"; ExecuteAdbCommand(adbPath, readMac, ref output, ref error); if (!string.IsNullOrEmpty(error)) { ShowMsg(0, $"获取MAC异常: {error}"); return; } MAC = output.Replace("\r\n", "").Replace(":", "").ToUpper(); macPath = Path.Combine(fullPath, MAC); if (!Directory.Exists(macPath)) { Directory.CreateDirectory(macPath); } string command = $"pull sdcard/Download/FactoryTest_result.xml {macPath}"; ExecuteAdbCommand(adbPath, command, ref output, ref error); if (!string.IsNullOrEmpty(error)) { ShowMsg(0, $"提取测试记录异常: {error}"); return; } string path = Path.Combine(macPath, "FactoryTest_result.xml"); ParseXml(path); } private void ParseXml(string path) { if (!File.Exists(path)) { ShowMsg(0, $"未产生检测结果,请重新检测"); return; } string fileContent = File.ReadAllText(path, Encoding.UTF8); fileContent = fileContent.Trim(); if (fileContent.Length == 0) { ShowMsg(0, $"输出无测试结果,请在AnLink执行测试"); return; } int beginIndex = fileContent.IndexOf(""); if (endIndex < 0) { ShowMsg(0, $"xml文件结束标签,格式有误,请重新操作"); return; } string xmlContent = fileContent.Substring(beginIndex, endIndex - beginIndex + 6); XmlDocument doc = new XmlDocument(); try { Dictionary DataList = new Dictionary(); doc.LoadXml(xmlContent); XmlNodeList stringNodes = doc.SelectNodes("//string"); foreach (XmlNode node in stringNodes) { string name = node.Attributes["name"].Value; string value = node.InnerText; DataList.Add(name, value); } dgv.Rows.Clear(); string testvValue = "",isHave = "否",currentMac = ""; foreach (var testItem in TestObj) { foreach (var item in DataList) { if (testItem.Name == item.Key) { isHave = "是"; testvValue = item.Value.ToUpper() == "PASS" ? "OK" : "NG"; } } if(testvValue == "NG") { currentMac = "NG"; } DataGridViewRow dr = new DataGridViewRow(); dr.CreateCells(dgv); dr.Cells[0].Value = MAC; dr.Cells[1].Value = testItem.ChineseName; dr.Cells[2].Value = isHave; dr.Cells[3].Value = testItem.Name; dr.Cells[4].Value = testvValue; dgv.Rows.Add(dr); string res = currentMac == "NG" ? "NG" : "OK"; string omakeCode, oMsid, oErrorMessage; if (LogicHandler.CheckStepSNAndMacode(WO, User.UserSourceCode, SN, User.UserCode, out omakeCode, out oMsid, out oErrorMessage)) { if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, SN, "成品产测信息", res, User.UserCode, out oErrorMessage)) { LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, WO, User.UserLineCode, User.UserSourceCode, "成品产测信息", "", SN, ""); sql.Clear(); sql.Append($@"INSERT INTO steptestdetail (std_id,std_sn,std_makecode,std_date,std_indate, std_rescode,std_stepcode, std_class,std_subclass1,std_value1) values (steptestdetail_seq.NEXTVAL,'{SN}','{WO}',sysdate,sysdate, '{User.UserSourceCode}','{User.CurrentStepCode}', '成品产测信息','{testItem.ChineseName}','{testvValue}')"); dh.ExecuteSql(sql.ToString(), "insert"); sql.Clear(); sql.Append($@"UPDATE makeserial SET ms_mac = '{MAC}' WHERE ms_sncode = '{SN}'"); dh.ExecuteSql(sql.ToString(), "update"); ShowMsg(1, $"MAC绑定成功,已上传测试结果"); } else { ShowMsg(0, $"过站处理记录NG: {oErrorMessage}"); } } else { ShowMsg(0, $"过站核对NG: {oErrorMessage}"); } } } catch (XmlException ex) { ShowMsg(0, ex.Message); } } public void ExecuteAdbCommand(string adbPath, string command, ref string output, ref string error) { output = string.Empty; error = string.Empty; try { using (Process process = new Process()) { process.StartInfo.FileName = adbPath; process.StartInfo.Arguments = command; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.Start(); output = process.StandardOutput.ReadToEnd(); error = process.StandardError.ReadToEnd(); process.WaitForExit(); string msg = output ?? error; ShowMsg(1, $"执行ADB成功: {msg.Replace("\r\n", "")}"); } } catch (Exception ex) { ShowMsg(0, $"执行ADB时异常: {ex.Message}"); } } private void Clean_Click(object sender, EventArgs e) { OperatResult.Clear(); } private void SetSndetail(DataTable dt) { snVal.Text = dt.Rows[0]["ms_sncode"].ToString(); woVal.Text = dt.Rows[0]["ma_code"].ToString(); prodVal.Text = dt.Rows[0]["ms_prodcode"].ToString(); prodNameVal.Text = dt.Rows[0]["pr_detail"].ToString(); } private void ClearSnDetail() { snVal.Text = ""; woVal.Text = ""; prodVal.Text = ""; prodNameVal.Text = ""; } private void ShowMsg(int type, string msg) { string fullDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if (type == 0) { OperatResult.AppendText($"{fullDateTime}: {msg}\n", Color.Red); } else if (type == 1) { OperatResult.AppendText($"{fullDateTime}: {msg}\n", Color.Black); } } public class TestItem { public string Name { get; set; } public string ChineseName { get; set; } } public bool CheckConnected(string adbPath,out string ConnectStr) { try { using (Process process = new Process()) { process.StartInfo.FileName = adbPath; process.StartInfo.Arguments = "devices"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.CreateNoWindow = true; process.Start(); ConnectStr = process.StandardOutput.ReadToEnd(); process.WaitForExit(); return output.Contains("device") && !output.Contains("List of devices attached"); } } catch(Exception ex) { ConnectStr = ex.Message; return false; } } } }