| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- 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<TestItem> TestObj = new List<TestItem> { };
- 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 == "")
- {
- 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("<?xml");
- if (beginIndex < 0)
- {
- ShowMsg(0, $"xml文件没有声明,格式有误,请重新操作");
- return;
- }
- int endIndex = fileContent.LastIndexOf("</map>");
- if (endIndex < 0)
- {
- ShowMsg(0, $"xml文件结束标签,格式有误,请重新操作");
- return;
- }
- string xmlContent = fileContent.Substring(beginIndex, endIndex - beginIndex + 6);
- XmlDocument doc = new XmlDocument();
- try
- {
- Dictionary<string, string> DataList = new Dictionary<string, string>();
- 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 = "否";
- foreach (var testItem in TestObj)
- {
- foreach (var item in DataList)
- {
- if (testItem.Name == item.Key)
- {
- isHave = "是";
- testvValue = item.Value.ToUpper() == "PASS" ? "OK" : "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);
- /* sql.Clear();
- sql.Append($@"INSERT into steptestdetail SELECT steptestdetail_seq.NEXTVAL std_id,{SN} std_sn,{WO} std_makecode,
- '成品产测信息' std_class,SYSDATE std_date,SYSDATE std_indate,{User.UserSourceCode} std_rescode,
- {testItem.ChineseName} std_subclass1,{testvValue} std_value1 FROM dual");
- 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绑定成功,已上传测试结果");
- }
- 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;
- }
- }
- }
- }
|