using HtmlAgilityPack; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; using System.Windows.Forms; using ZJ_BYD.Untils; using ZJ_BYD.ViewModel; using HtmlDocument = HtmlAgilityPack.HtmlDocument; namespace ZJ_BYD.Common { public class RequestMes { private static object _lock = new object(); private static Encoding RequestEncoding = Encoding.GetEncoding("GB2312"); private static Encoding ResponseEncoding = Encoding.GetEncoding("GB2312"); private static int Timeout = Program.MesPostTimeout; private static string RequestPost(string param) { string result = string.Empty; try { string url = GetBaseUrl(); byte[] byteParam = RequestEncoding.GetBytes(param); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "*/*"; request.UserAgent = "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;SV1;Maxthon;.NET CLR 1.1.4322"; request.Method = "POST"; request.ContentLength = byteParam.Length; request.Timeout = Timeout; request.ServicePoint.Expect100Continue = false; Stream stream = request.GetRequestStream(); stream.Write(byteParam, 0, byteParam.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), ResponseEncoding); result = sr.ReadToEnd(); sr.Close(); return result; } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; var msg = $"RequestPost方法出发异常:{errMsg},请联系管理员!"; LogHelper.WriteLog(msg); return msg; } } /// /// 获取基础url路径 /// /// private static string GetBaseUrl() { string MES_IP = Program.MesIpAddress.Trim(); string MES_PORT = Program.MesPort.Trim(); return "http://" + MES_IP + ":" + MES_PORT + "/manufacturing/IntegrationServlet"; } /// /// MES结果信息验证 /// /// /// private static (bool ok, string msg, string result) CheckMesResult(string html, bool isBindCode = false) { bool result = false; string msg = string.Empty; try { HtmlDocument xmlDoc = new HtmlDocument(); xmlDoc.LoadHtml(html); HtmlNodeCollection xmlNodeList = xmlDoc.DocumentNode.SelectNodes("/html/body/form/table/tr/td/table/tr"); if (xmlNodeList != null && xmlNodeList.Count > 1) { HtmlNode xmlStatus = xmlNodeList.ElementAt(0).SelectSingleNode("td"); HtmlNode xmlText = xmlNodeList.ElementAt(1).SelectSingleNode("td"); msg = HttpUtility.HtmlDecode(xmlText.InnerText); if (!isBindCode) { if (xmlStatus.InnerHtml.Contains("Y")) { result = true; } else { result = false; } } else { if (msg.Contains("ERROR")) { result = false; } else { result = true; } } } else { msg = html; } } catch (Exception ex) { result = false; msg = ex.Message; LogHelper.WriteLog($"CheckMesResult方法发生异常:{ex.Message}"); } return (result, msg, html); } /// /// 通过MES接口校验组件 /// 查询支线上是否存在这个条码的数据 /// /// /// /// /// /// 查询参数 /// /// public static (bool ok, string msg, string result) CheckSubCodeByMes(string site, string resource, string procedure, string subCode, string itemPara, string userName) { lock (_lock) { string title = "组件码校验"; string data = "&message=" + "" + "" + "" + site + "" + "" + userName + "" + "" + subCode + "" + "" + procedure + "" + "" + resource + "" + "PASS" + "" + "" + itemPara + "" + "" + "" + ""; try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } /// /// 输出调试日志 /// /// /// private static void RequestLog(string name, string data, string result) { StringBuilder sb = new StringBuilder(); sb.AppendLine("请求MES: " + name); sb.AppendLine($"Url: {GetBaseUrl()}"); sb.AppendLine($"Method: POST"); sb.AppendLine($"Header: "); sb.AppendLine($"Body: {data}"); sb.AppendLine($"Return: "); sb.AppendLine(result); LogHelper.WriteLog(sb.ToString()); } /// /// 用户验证 /// /// /// /// /// public static (bool ok, string msg, string result) CheckUser(string site, string userName, string userPwd) { lock (_lock) { string title = "用户验证"; string data = $"&message={site}{userName}{userPwd}"; try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } /// /// START /// /// /// /// /// /// /// public static (bool ok, string msg, string result) Start(string site, string productSfcCode, string resource, string procedure, string userName) { lock (_lock) { string title = "START接口"; string data = $"&message={site}XML{productSfcCode}{resource}{procedure}{userName}1Y!erpautogy03!1234567@byd"; try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } /// /// PASS /// /// /// /// /// /// /// /// /// /// public static (bool ok, string msg, string result) Pass(string site, string productSfcCode, string resource, string procedure, string userName, string testFile, string testProgramVersion, List testList) { lock (_lock) { string title = "PASS接口"; string data = "&message=PASS" + site + "XML" + productSfcCode + "" + resource + "" + procedure + "" + userName + "1!erpautogy03!1234567@byd!PASS," + testFile + "," + testProgramVersion; if (testList.Count > 0) { foreach (var item in testList) { data += "!" + item.TestItem + "," + item.TestResult + "," + item.TestVal; } } try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } /// /// ERROR /// /// /// /// /// /// /// /// /// /// /// public static (bool ok, string msg, string result) Error(string site, string productSfcCode, string resource, string procedure, string userName, string testFile, string testProgramVersion, string ngCode, List testList) { lock (_lock) { string title = "ERROR接口"; string data = "&message=ERROR" + "" + "" + site + "" + "" + userName + "" + "" + productSfcCode + "" + "1" + "" + "1" + "" + "" + "" + resource + "" + "" + procedure + "" + "" + "" + ngCode + "" + "XML" + "" + "!erpautogy03!1234567@byd!ERROR," + testFile + "," + testProgramVersion; if (testList.Count > 0) { foreach (var item in testList) { data += "!" + item.TestItem + "," + item.TestResult + "," + item.TestVal; } } try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } /// /// 主条码和组件条码绑定 /// 离散装配标准XML /// /// public static (bool ok, string msg, string result) BindCode(string site, string productSfcCode, string resource, string procedure, string userName, List subInfoList) { try { lock (_lock) { string title = " 主条码和组件条码绑定"; string data = "&message=" + "" + "" + "" + userName + "" + "" + site + "" + "" + productSfcCode + "" + "" + procedure + "" + "" + resource + "" + "True" + "baseFinished:AssemblyPoint" + ""; if (subInfoList != null) { if (subInfoList.Count > 0) { foreach (var item in subInfoList) { var itemTypeDesc = item.ItemType == "EXTERNAL_LOT" ? "外部批次" : "库存批次"; string item_add = "" + "" + item.MaterialNum + "" + "" + item.VersionNo + "" + "1" + "" + "" + "" + item.ItemType + "" + "" + item.SubCode + "" + "" + "" + ""; //(来料类型:" + itemTypeDesc + ") data += item_add; } } } data += "" + "" + "!erpautogy03!1234567@byd"; try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result, true); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } catch (Exception ex) { var errMsg = ex == null ? "未知异常" : ex.Message; MessageBox.Show($"BindCode方法出发异常:{errMsg},请联系管理员!"); return (false, errMsg, errMsg); } } /// /// 获取设备集成信息 /// 查询上工位是否有数据 /// /// 日志标题 /// 站点 /// /// 资源 /// 工序 /// 用户名 /// 测试项 /// public static (bool ok, string msg, string result) GetIntegratedData(string title, string site, string productSfcCode, string resource, string procedure, string userName, List testItems) { lock (_lock) { string data = "&message=" + "" + "" + "" + site + "" + "" + userName + "" + "" + productSfcCode + "" + "" + procedure + "" + "" + resource + "PASS" + ""; if (testItems.Count > 0) { foreach (var item in testItems) { string item_add = "" + item + ""; data += item_add; } } data += ""; try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } /// /// 离散装配前查询已装配组件 /// /// /// /// public static (bool ok, string msg, string result) GetBinded(string site, string productSfcCode) { lock (_lock) { string title = "离散装配前查询"; string data = "&message=" + site + "" + productSfcCode + ""; try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } /// /// 工单绑定 /// /// /// /// /// /// /// public static (bool ok, string msg, string result) BindOrder(string site, string resource, string userName, string userPwd, string orderNo) { lock (_lock) { string title = "工单绑定"; string data = "&message=" + site + "" + userName + "" + userPwd + "" + resource + "" + orderNo + ""; try { string result = RequestPost(data); RequestLog(title, data, result); return CheckMesResult(result); } catch (Exception ex) { RequestLog(title, data, ex.ToString()); return (false, ex.Message, ex.ToString()); } } } } }