using HalconDotNet;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Serialization;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.Linq;
|
using System.Net;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Web;
|
using System.Xml;
|
|
namespace LB_VisionProcesses.Processes
|
{
|
public class BaseProcess : IProcess
|
{
|
public override bool Run()
|
{
|
DateTime StartTime = DateTime.Now;
|
|
InitRunParams();
|
//HOperatorSet.GenEmptyObj(out HObject EmptyObj);
|
//OutputImage = EmptyObj;
|
|
#region 运行逻辑
|
|
#endregion
|
Result = true;
|
RunTime = (DateTime.Now - StartTime).TotalMilliseconds;
|
return false;
|
}
|
|
|
/// <summary>
|
/// 加载算法
|
/// </summary>
|
/// <param name="fullPath">完整路径带.json</param>
|
/// <returns></returns>
|
public override bool Load(string fullPath = null)
|
{
|
try
|
{
|
if (!fullPath.Contains(".json"))
|
{
|
Debug.WriteLine("文件路径不完整");
|
return false;
|
}
|
|
if (fullPath.StartsWith(".\\"))
|
{
|
// 判断原字符串长度是否大于等于2,避免越界
|
if (fullPath.Length >= 2)
|
{
|
// 替换开头两个字符
|
fullPath = Application.StartupPath + fullPath.Substring(2);
|
Debug.WriteLine($"修改后的字符串: {fullPath}");
|
}
|
}
|
|
if (string.IsNullOrEmpty(fullPath) || fullPath.Trim() == "")
|
{
|
Debug.WriteLine("文件路径不完整");
|
return false;
|
}
|
|
// 获取不带文件名的目录路径
|
string directoryPath = Path.GetDirectoryName(fullPath);
|
strProcessName = Path.GetFileNameWithoutExtension(fullPath);
|
|
if (!File.Exists(fullPath))
|
{
|
Debug.WriteLine("文件不存在创建空文件");
|
Save(directoryPath);
|
return true;
|
}
|
string strJson = string.Empty;
|
using (StreamReader streamReader = new StreamReader(fullPath, Encoding.UTF8))
|
{
|
strJson = streamReader.ReadToEnd();
|
streamReader.Close();
|
}
|
Params = JsonConvert.DeserializeObject<ProcessParams>(strJson);
|
if (Params == null)
|
return false;
|
|
Params.FixDeserializedData();
|
return true;
|
}
|
catch { return false; }
|
}
|
|
/// <summary>
|
/// 保存算法
|
/// </summary>
|
/// <param name="filePath">不带.json</param>
|
/// <returns></returns>
|
public override bool Save(string filePath = null)
|
{
|
try
|
{
|
if (string.IsNullOrEmpty(filePath) || filePath.Trim() == "")
|
{
|
Debug.WriteLine("文件路径不完整");
|
return false;
|
}
|
|
string strJson = string.Empty;
|
|
var settings = new JsonSerializerSettings
|
{
|
Formatting = Newtonsoft.Json.Formatting.Indented,
|
// 自定义缩进(4空格)
|
ContractResolver = new DefaultContractResolver
|
{
|
NamingStrategy = new CamelCaseNamingStrategy()
|
}
|
};
|
|
strJson = JsonConvert.SerializeObject(Params, settings);
|
//判断文件夹是否存在,防呆输入为文件名称
|
if (!Directory.Exists(filePath))
|
{
|
try
|
{
|
Directory.CreateDirectory(filePath);
|
}
|
catch (Exception)
|
{ }
|
}
|
File.WriteAllText(filePath + "//" + strProcessName + ".json", strJson, Encoding.UTF8);
|
return true;
|
}
|
catch { return false; }
|
}
|
|
public override void InitRunParams()
|
{
|
Result = true;
|
Msg = string.Empty;
|
|
if (Record != null)
|
Record.Dispose();
|
}
|
|
public override void Dispose()
|
{
|
return;
|
}
|
|
public override object Clone()
|
{
|
return MemberwiseClone();
|
}
|
}
|
|
public class TVInfo
|
{
|
public string ODF { get; set; }
|
public string ProductName { get; set; }
|
public string ProductCode { get; set; }
|
|
}
|
|
public class RootObject4
|
{
|
public string Guid { get; set; }
|
|
public string IsSuccess { get; set; }
|
|
public string Msg { get; set; }
|
|
public string UpdateUser { get; set; }
|
}
|
|
public class MaInfo
|
{
|
public string ODF { get; set; }
|
public string BOM { get; set; }
|
public string SCREEN { get; set; }
|
public string SHIPPING { get; set; }
|
public string SIZE { get; set; }
|
public string Model { get; set; }
|
}
|
|
public class GetWebServer
|
{
|
public static string QueryGetWebService(String URL, String MethodName, Hashtable Pars)
|
{
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars));
|
request.Method = "GET";
|
request.ContentType = "application/x-www-form-urlencoded";
|
SetWebRequest(request);
|
return ReadXmlResponse(request.GetResponse());
|
}
|
|
/// <summary>
|
/// 获得产品信息
|
/// </summary>
|
/// <param name="URL"></param>
|
/// <param name="MethodName"></param>
|
/// <param name="SN"></param>
|
/// <returns></returns>
|
public static string GetTVInfo(String URL, String MethodName, string SN)
|
{
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + "SN=" + Uri.EscapeDataString(SN));
|
request.Method = "GET";
|
request.ContentType = "application/x-www-form-urlencoded";
|
SetWebRequest(request);
|
return ReadXmlResponse(request.GetResponse());
|
}
|
|
|
/// <summary>
|
/// 获得下一工序代码
|
/// </summary>
|
/// <param name="URL"></param>
|
/// <param name="MethodName"></param>
|
/// <param name="SN"></param>
|
/// <returns></returns>
|
public static string GetNextProcessCode(String URL, String MethodName, string SN)
|
{
|
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + "TVSN=" + Uri.EscapeDataString(SN));
|
|
request.Method = "GET";
|
request.ContentType = "application/x-www-form-urlencoded";
|
SetWebRequest(request);
|
return ReadXmlResponse(request.GetResponse());
|
}
|
|
/// <summary>
|
/// 上传条码扫描路线结果
|
/// </summary>
|
/// <param name="URL"></param>
|
/// <param name="MethodName"></param>
|
/// <param name="TVSN"></param>
|
/// <param name="LineCode"></param>
|
/// <param name="PcProcessCode"></param>
|
/// <param name="Host"></param>
|
/// <param name="IP"></param>
|
/// <param name="User"></param>
|
/// <param name="UserName"></param>
|
/// <returns></returns>
|
public static string Scan(String URL, String MethodName, string SN, string LineCode, string PcProcessCode, string Host, string IP, string User, string UserName)
|
{
|
try
|
{
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + "TVSN=" + Uri.EscapeDataString(SN) + "&" + "LineCode=" + LineCode + "&" + "PcProcessCode=" + PcProcessCode + "&" + "Host=" + Host + "&" + "IP=" + IP + "&" + "User=" + User + "&" + "UserName=" + UserName + "&" + "PartStrs=" + "");
|
request.Method = "GET";
|
request.ContentType = "application/x-www-form-urlencoded";
|
SetWebRequest(request);
|
return ReadXmlResponse(request.GetResponse());
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(ex.ToString());
|
return "";
|
}
|
}
|
|
private static void SetWebRequest(HttpWebRequest request)
|
{
|
request.Credentials = CredentialCache.DefaultCredentials;
|
request.Timeout = 50000;
|
}
|
|
private static string ReadXmlResponse(WebResponse response)
|
{
|
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
|
String retXml = sr.ReadToEnd();
|
sr.Close();
|
XmlDocument doc = new XmlDocument();
|
return retXml;
|
}
|
|
private static String ParsToString(Hashtable Pars)
|
{
|
StringBuilder sb = new StringBuilder();
|
foreach (string k in Pars.Keys)
|
{
|
if (sb.Length > 0)
|
{
|
sb.Append("&");
|
}
|
sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));
|
}
|
return sb.ToString();
|
|
}
|
|
public static string PostXml(string url, string SN)
|
{
|
string text = "";
|
Stream stream = null;
|
string text2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><GetMaInfo xmlns=\"http://tempuri.org/\"><sn>" + SN + "</sn></GetMaInfo></soap:Body></soap:Envelope>";
|
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
|
httpWebRequest.Method = "POST";
|
httpWebRequest.ContentLength = text2.Length;
|
httpWebRequest.ContentType = "text/xml";
|
httpWebRequest.KeepAlive = false;
|
httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
|
httpWebRequest.Timeout = 7000;
|
Encoding encoding = Encoding.GetEncoding("UTF-8");
|
byte[] bytes = encoding.GetBytes(text2);
|
try
|
{
|
stream = httpWebRequest.GetRequestStream();
|
}
|
catch (Exception)
|
{
|
return "INT ERR";
|
}
|
|
stream.Write(bytes, 0, text2.Length);
|
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
|
using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
|
{
|
text = streamReader.ReadToEnd();
|
streamReader.Close();
|
}
|
|
text = text.Substring(0, text.LastIndexOf("}</GetMaInfoResult>") + 1);
|
text = text.Substring(text.LastIndexOf("<GetMaInfoResult>{") + 17);
|
RootObject4 rootObject = JsonConvert.DeserializeObject<RootObject4>(text);
|
if (rootObject.IsSuccess == "true")
|
{
|
return rootObject.Msg;
|
}
|
|
return "error" + rootObject.Msg;
|
}
|
|
}
|
}
|