using Community;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;
using System.Xml;
namespace Model
{
public class ERP_ParameterClass : NotificationObject
{
public int ID { get; set; }
///
/// 对应的软件ID
///
public int SoftID { get; set; }
#region ERP验证用户名
private string eRP_UserName;
///
/// ERP验证用户名
///
public string ERP_UserName
{
get { return eRP_UserName; }
set
{
eRP_UserName = value;
RaisePropertyChanged("ERP_UserName");
}
}
#endregion
#region ERP验证密码
private string eRP_PassWord;
///
/// ERP验证密码
///
public string ERP_PassWord
{
get { return eRP_PassWord; }
set
{
eRP_PassWord = value;
RaisePropertyChanged("ERP_PassWord");
}
}
#endregion
#region 连接测试地址
private string eRP_ConnTest;
///
/// 连接测试地址
///
public string ERP_ConnTest
{
get { return eRP_ConnTest; }
set
{
eRP_ConnTest = value;
RaisePropertyChanged("ERP_ConnTest");
}
}
#endregion
#region 数据上传地址
private string eRP_ConnUpload;
///
/// 数据上传地址
///
public string ERP_ConnUpload
{
get { return eRP_ConnUpload; }
set
{
eRP_ConnUpload = value;
RaisePropertyChanged("ERP_ConnUpload");
}
}
#endregion
#region 是否需要登入验证
private bool login_Val;
///
/// 是否需要登入验证
///
public bool Login_Val
{
get { return login_Val; }
set
{
login_Val = value;
RaisePropertyChanged("Login_Val");
}
}
#endregion
#region 是否是WebServic
private bool webServicIn;
///
/// 是否是WebServic
///
public bool WebServicIn
{
get { return webServicIn; }
set
{
webServicIn = value;
RaisePropertyChanged("WebServicIn");
}
}
#endregion
#region SN
private string snCode;
///
/// SN
///
public string SnCode
{
get { return snCode; }
set
{
snCode = value;
RaisePropertyChanged("SnCode");
}
}
#endregion
#region 站别
private string stationCode;
///
/// 站别
///
public string StationCode
{
get { return stationCode; }
set
{
stationCode = value;
RaisePropertyChanged("StationCode");
}
}
#endregion
#region HTTP请求的返回值
private string jsonStr;
///
/// HTTP请求的返回值
///
public string JsonStr
{
get { return jsonStr; }
set
{
jsonStr = value;
RaisePropertyChanged("JsonStr");
}
}
#endregion
#region 连接状态
private ShowInfo state;
///
/// 连接状态
///
public ShowInfo State
{
get { return state; }
set
{
state = value;
this.RaisePropertyChanged("State");
}
}
#endregion
#region 启用MES
private bool enableing;
///
/// 不启用MES
///
public bool Enableing
{
get { return enableing; }
set
{
enableing = value;
this.RaisePropertyChanged("Enableing");
}
}
#endregion
#region Get请求
///
/// Get请求
///
/// URL连接地址
/// HTTP请求响应状态,成功时为“OK”
public string GetResponse(string url)
{
try
{
JsonStr = "";
if (url.StartsWith("https"))
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
}
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (Login_Val)
{
string bstr = Convert.ToBase64String(Encoding.Default.GetBytes(ERP_UserName + ":" + ERP_PassWord));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", bstr);
}
HttpResponseMessage response = httpClient.GetAsync(url).Result;
string statusCode = response.StatusCode.GetHashCode() + ":" + response.StatusCode.ToString();
if (response.IsSuccessStatusCode)
{
JsonStr = response.Content.ReadAsStringAsync().Result;
}
else
{
State.StatuInfo = statusCode;
}
httpClient.Dispose();
response.Dispose();
}
catch (Exception ex)
{
State.StatuInfo = ex.Message;
}
return JsonStr;
}
#endregion
#region Post请求
///
/// Post请求
///
///
/// post数据
/// HTTP请求响应状态,成功时为“OK”
public string PostResponse(string url, string postData)
{
try
{
JsonStr = "";
if (url.StartsWith("https"))
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
}
HttpContent httpContent = new StringContent(postData);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
httpContent.Headers.ContentType.CharSet = "utf-8";
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (Login_Val)
{
string bstr = Convert.ToBase64String(Encoding.Default.GetBytes(ERP_UserName + ":" + ERP_PassWord));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", bstr);
//httpClient..setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
}
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
string statusCode = response.StatusCode.ToString();
if (response.IsSuccessStatusCode)
{
JsonStr = response.Content.ReadAsStringAsync().Result;
}
httpClient.Dispose();
httpContent.Dispose();
response.Dispose();
}
catch (Exception ex)
{
State.StatuInfo = ex.Message;
}
return JsonStr;
}
#endregion
///
/// 构造函数
///
public ERP_ParameterClass()
{
State = new ShowInfo() { StatuInfo = "", StatusBackground = ColorStr.Red, StatusForeground = ColorStr.White };
}
///
/// MES连接测试
///
///
public MesReturnValue ConnTest()
{
JsonStr = GetResponse(ERP_ConnTest);
if (!string.IsNullOrEmpty(JsonStr))
{
MesReturnValue mv = JsonToObj(JsonStr);
if (mv != null)
{
State.StatuInfo = "连接成功";
State.StatusBackground = ColorStr.Green;
}
else
{
State.StatuInfo = "连接失败或URL错误";
State.StatusBackground = ColorStr.Red;
}
return mv;
}
else
{
return new MesReturnValue();
}
}
///
/// 上传数据
///
///
///
public string DataUoload(DataUpload UpdataInfo)
{
//ERP_Ajxa aa = new ERP_Ajxa();
//aa.data = list;
string jsona = ObjToJson(UpdataInfo);
RecordRemoteSensingLog("Upload=>" + jsona);//记录上传值
string a = PostResponse(ERP_ConnUpload, jsona);
RecordRemoteSensingLog("ReturnPer=>" + a); //记录返回值
//return jss.Deserialize(a);
return a;
}
///
/// js 序列化器
///
static JavaScriptSerializer jss = new JavaScriptSerializer();
#region 1、将对象转换成Json字符创
///
/// 将对象转换成Json字符创(泛型)
///
///
///
public string ObjToJson(object obj)
{
return jss.Serialize(obj);
}
#endregion
#region 2、将Json还原成对象(泛型)
///
/// 将Json还原成对象(泛型)
///
///
///
///
public T JsonToObj(string json)
{
try
{
//var deserializedResult = jss.Deserialize>(json);
var deserializedResult = jss.Deserialize(json);
return deserializedResult;
}
catch (Exception)
{
return default(T);
}
}
#endregion
#region 记录远程交互信息
static string path;
static string paths;
///
/// 打开的文件流对象
///
static FileStream FS { get; set; }
static byte[] data { get; set; }
///
/// 记录远程交互信息
///
///
private static void RecordRemoteSensingLog(string json)
{
try
{
path = @"D:\\MesUploadLog";
paths = @"D:\\MesUploadLog\" + DateTime.Now.ToString("D") + ".txt";
//创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//打开或创建文件
FS = File.Open(paths, FileMode.Append);
//获得字节数组
data = System.Text.Encoding.Default.GetBytes(json + "|" + DateTime.Now.ToString() + "\r\n");
//开始写入
FS.Write(data, 0, data.Length);
//清空缓冲区、关闭流
FS.Flush();
FS.Close();
}
catch (Exception)
{
throw;
}
}
#endregion
#region 对象转键值对
///
/// 对象转键值对
///
///
///
public Dictionary ObjTodic(object Coperobj)
{
Dictionary par = new Dictionary();
try
{
var Types = Coperobj.GetType();//获得类型
foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段
{
par.Add(key: sp.Name, value: sp.GetValue(Coperobj).ToString());
}
}
catch (Exception)
{
return null;
}
return par;
}
#endregion
}
///
/// 上传数据Ajxa格式
///
///
internal class ERP_Ajxa
{
public List data { get; set; }
}
}