You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

519 lines
21 KiB
C#

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;
}
}
/// <summary>
/// 获取基础url路径
/// </summary>
/// <returns></returns>
private static string GetBaseUrl()
{
string MES_IP = Program.MesIpAddress.Trim();
string MES_PORT = Program.MesPort.Trim();
return "http://" + MES_IP + ":" + MES_PORT + "/manufacturing/IntegrationServlet";
}
/// <summary>
/// MES结果信息验证
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
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("</b>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);
}
/// <summary>
/// 通过MES接口校验组件
/// 查询支线上是否存在这个条码的数据
/// </summary>
/// <param name="site"></param>
/// <param name="resource"></param>
/// <param name="procedure"></param>
/// <param name="subCode"></param>
/// <param name="itemPara">查询参数</param>
/// <param name="userName"></param>
/// <returns></returns>
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=" +
"<PRODUCTION_REQUEST>" +
"<SIDE>" +
"<SITE>" + site + "</SITE>" +
"<USER>" + userName + "</USER>" +
"<SFC>" + subCode + "</SFC>" +
"<OPERATION>" + procedure + "</OPERATION>" +
"<RESOURCE>" + resource + "</RESOURCE>" +
"<ISOK>PASS</ISOK>" +
"<LIST>" +
"<ITEM>" + itemPara + "</ITEM>" +
"</LIST>" +
"</SIDE>" +
"</PRODUCTION_REQUEST>";
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());
}
}
}
/// <summary>
/// 输出调试日志
/// </summary>
/// <param name="webClient"></param>
/// <param name="result"></param>
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());
}
/// <summary>
/// 用户验证
/// </summary>
/// <param name="site"></param>
/// <param name="userName"></param>
/// <param name="userPwd"></param>
/// <returns></returns>
public static (bool ok, string msg, string result) CheckUser(string site, string userName, string userPwd)
{
lock (_lock)
{
string title = "用户验证";
string data = $"&message=<PRODUCTION_REQUEST><USER><SITE>{site}</SITE><NAME>{userName}</NAME><PWD>{userPwd}</PWD></USER></PRODUCTION_REQUEST>";
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());
}
}
}
/// <summary>
/// START
/// </summary>
/// <param name="site"></param>
/// <param name="productSfcCode"></param>
/// <param name="resource"></param>
/// <param name="procedure"></param>
/// <param name="userName"></param>
/// <returns></returns>
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=<PRODUCTION_REQUEST><START><SFC_LIST><SFC><SITE>{site}</SITE><ACTIVITY>XML</ACTIVITY><ID>{productSfcCode}</ID><RESOURCE>{resource}</RESOURCE><OPERATION>{procedure}</OPERATION><USER>{userName}</USER><QTY>1</QTY><DATE_TIME></DATE_TIME><COMPLEX>Y</COMPLEX></SFC></SFC_LIST></START></PRODUCTION_REQUEST>!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());
}
}
}
/// <summary>
/// PASS
/// </summary>
/// <param name="site"></param>
/// <param name="productSfcCode"></param>
/// <param name="resource"></param>
/// <param name="procedure"></param>
/// <param name="userName"></param>
/// <param name="testFile"></param>
/// <param name="testProgram"></param>
/// <param name="testList"></param>
/// <returns></returns>
public static (bool ok, string msg, string result) Pass(string site, string productSfcCode, string resource, string procedure, string userName, string testFile, string testProgramVersion, List<PostMesTestItemVM> testList)
{
lock (_lock)
{
string title = "PASS接口";
string data = "&message=PASS<PRODUCTION_REQUEST><COMPLETE><SFC_LIST><SFC><SITE>" + site + "</SITE><ACTIVITY>XML</ACTIVITY><ID>" + productSfcCode + "</ID><RESOURCE>" + resource + "</RESOURCE><OPERATION>" + procedure + "</OPERATION><USER>" + userName + "</USER><QTY>1</QTY><DATE_TIME></DATE_TIME><DATE_STARTED></DATE_STARTED></SFC></SFC_LIST></COMPLETE></PRODUCTION_REQUEST>!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());
}
}
}
/// <summary>
/// ERROR
/// </summary>
/// <param name="site"></param>
/// <param name="productSfcCode"></param>
/// <param name="resource"></param>
/// <param name="procedure"></param>
/// <param name="userName"></param>
/// <param name="testFile"></param>
/// <param name="testProgramVersion"></param>
/// <param name="ngCode"></param>
/// <param name="testList"></param>
/// <returns></returns>
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<PostMesTestItemVM> testList)
{
lock (_lock)
{
string title = "ERROR接口";
string data = "&message=ERROR" +
"<PRODUCTION_REQUEST" +
"><NC_LOG_COMPLETE>" +
"<SITE>" + site + "</SITE>" +
"<OWNER TYPE =\"USER\">" + userName + "</OWNER>" +
"<NC_CONTEXT>" + productSfcCode + "</NC_CONTEXT>" +
"<QTY>1</QTY" +
"><IDENTIFIER></IDENTIFIER>" +
"<FAILURE_ID></FAILURE_ID>" +
"<DEFECT_COUNT>1</DEFECT_COUNT>" +
"<COMMENTS></COMMENTS>" +
"<DATE_TIME></DATE_TIME>" +
"<RESOURCE>" + resource + "</RESOURCE>" +
"<OPERATION>" + procedure + "</OPERATION>" +
"<ROOT_CAUSE_OPER></ROOT_CAUSE_OPER>" +
"<NC_CODE>" + ngCode + "</NC_CODE>" +
"<ACTIVITY>XML</ACTIVITY>" +
"</NC_LOG_COMPLETE>" +
"</PRODUCTION_REQUEST>!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());
}
}
}
/// <summary>
/// 主条码和组件条码绑定
/// 离散装配标准XML
/// </summary>
/// <returns></returns>
public static (bool ok, string msg, string result) BindCode(string site, string productSfcCode, string resource, string procedure, string userName, List<PostMesSubInfo> subInfoList)
{
try
{
lock (_lock)
{
string title = " 主条码和组件条码绑定";
string data = "&message=" +
"<PRODUCTION_REQUEST>" +
"<ASSEMBLE_COMPONENTS>" +
"<USER>" + userName + "</USER>" +
"<SITE>" + site + "</SITE>" +
"<PARENT_SFC>" + productSfcCode + "</PARENT_SFC>" +
"<OPERATION>" + procedure + "</OPERATION>" +
"<RESOURCE>" + resource + "</RESOURCE>" +
"<CHECK_OPER>True</CHECK_OPER>" +
"<EVENT>baseFinished:AssemblyPoint</EVENT>" +
"<IDENTIFIER_LIST>";
if (subInfoList != null)
{
if (subInfoList.Count > 0)
{
foreach (var item in subInfoList)
{
var itemTypeDesc = item.ItemType == "EXTERNAL_LOT" ? "外部批次" : "库存批次";
string item_add = "<IDENTIFIER_OBJECT>" +
"<IDENTIFIER>" + item.MaterialNum + "</IDENTIFIER>" +
"<REVISION>" + item.VersionNo + "</REVISION>" +
"<QTY>1</QTY>" +
"<ASSY_DATA_VALUES>" +
"<ASSY_DATA>" +
"<DATA_FIELD>" + item.ItemType + "</DATA_FIELD>" +
"<DATA_ATTR>" + item.SubCode + "</DATA_ATTR>" +
"</ASSY_DATA>" +
"</ASSY_DATA_VALUES>" +
"</IDENTIFIER_OBJECT>"; //(来料类型:" + itemTypeDesc + ")
data += item_add;
}
}
}
data += "</IDENTIFIER_LIST>" +
"</ASSEMBLE_COMPONENTS>" +
"</PRODUCTION_REQUEST>!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);
}
}
/// <summary>
/// 获取设备集成信息
/// 查询上工位是否有数据
/// </summary>
/// <param name="title">日志标题</param>
/// <param name="site">站点</param>
/// <param name="productSfcCode"></param>
/// <param name="resource">资源</param>
/// <param name="procedure">工序</param>
/// <param name="userName">用户名</param>
/// <param name="testItems">测试项</param>
/// <returns></returns>
public static (bool ok, string msg, string result) GetIntegratedData(string title, string site, string productSfcCode, string resource, string procedure, string userName, List<string> testItems)
{
lock (_lock)
{
string data = "&message=" +
"<PRODUCTION_REQUEST>" +
"<SIDE>" +
"<SITE>" + site + "</SITE>" +
"<USER>" + userName + "</USER>" +
"<SFC>" + productSfcCode + "</SFC>" +
"<OPERATION>" + procedure + "</OPERATION>" +
"<RESOURCE>" + resource + "</RESOURCE" +
"><ISOK>PASS</ISOK>" +
"<LIST>";
if (testItems.Count > 0)
{
foreach (var item in testItems)
{
string item_add = "<ITEM>" + item + "</ITEM>";
data += item_add;
}
}
data += "</LIST></SIDE></PRODUCTION_REQUEST>";
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());
}
}
}
/// <summary>
/// 离散装配前查询已装配组件
/// </summary>
/// <param name="site"></param>
/// <param name="productSfcCode"></param>
/// <returns></returns>
public static (bool ok, string msg, string result) GetBinded(string site, string productSfcCode)
{
lock (_lock)
{
string title = "离散装配前查询";
string data = "&message=<PRODUCTION_REQUEST><ASSEMBLE_SFC><SITE>" + site + "</SITE><SFC>" + productSfcCode + "</SFC></ASSEMBLE_SFC></PRODUCTION_REQUEST>";
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());
}
}
}
/// <summary>
/// 工单绑定
/// </summary>
/// <param name="site"></param>
/// <param name="resource"></param>
/// <param name="userName"></param>
/// <param name="userPwd"></param>
/// <param name="orderNo"></param>
/// <returns></returns>
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=<PRODUCTION_REQUEST><RESOURCE_BANDING_SHOPORDER><SITE>" + site + "</SITE><NAME>" + userName + "</NAME><PWD>" + userPwd + "</PWD><RESOURCE>" + resource + "</RESOURCE><SHOPORDER>" + orderNo + "</SHOPORDER></RESOURCE_BANDING_SHOPORDER></PRODUCTION_REQUEST>";
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());
}
}
}
}
}