EAP
This commit is contained in:
76
Utilities/HttpUtils.cs
Normal file
76
Utilities/HttpUtils.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
#region << 版 本 注 释 >>
|
||||
/*----------------------------------------------------------------
|
||||
* 创建者:13118
|
||||
* 创建时间:2022/3/17 14:52:03
|
||||
* 版本:V1.0.0
|
||||
* 描述:
|
||||
*
|
||||
* ----------------------------------------------------------------
|
||||
* 修改人:
|
||||
* 时间:
|
||||
* 修改说明:
|
||||
*
|
||||
* 版本:V1.0.1
|
||||
*----------------------------------------------------------------*/
|
||||
#endregion << 版 本 注 释 >>
|
||||
|
||||
using ARI.EAP.HOST.SRD;
|
||||
using Glorysoft.SECS.EQP.Utilities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace ARI.EAP.HOST.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// HttpUtils 的摘要说明
|
||||
/// 调用http接口工具类
|
||||
/// </summary>
|
||||
public static class HttpUtils
|
||||
{
|
||||
public static HttpWebResponse sentPOST(string url,string content)
|
||||
{
|
||||
HttpWebRequest postRequest=(HttpWebRequest)WebRequest.Create(url);
|
||||
postRequest.Method = "POST";
|
||||
postRequest.ContentType = Configuration.conf.httpConfiguration.contentType;
|
||||
postRequest.Timeout = 10000;
|
||||
try
|
||||
{
|
||||
using (StreamWriter dataStream = new StreamWriter(postRequest.GetRequestStream()))
|
||||
{
|
||||
dataStream.Write(content);
|
||||
dataStream.Close();
|
||||
}
|
||||
HttpWebResponse response = (HttpWebResponse)postRequest.GetResponse();
|
||||
return response;
|
||||
}catch(Exception e)
|
||||
{
|
||||
LoggerService.SYSLogger.Error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpWebResponse sentGET(string url,string param)
|
||||
{
|
||||
url = url + param;
|
||||
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url);
|
||||
getRequest.Method = "GET";
|
||||
getRequest.ContentType = Configuration.conf.httpConfiguration.contentType;
|
||||
try
|
||||
{
|
||||
HttpWebResponse response = (HttpWebResponse)getRequest.GetResponse();
|
||||
return response;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LoggerService.SYSLogger.Error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Utilities/LoggerService.cs
Normal file
38
Utilities/LoggerService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
// ********************************************************************************
|
||||
// 文件名字: LoggerService
|
||||
// 文件描述: LoggerService
|
||||
// 开发人员: Michael
|
||||
// 创建时间: 2019/11/8 0:08
|
||||
//
|
||||
// 更新历史:
|
||||
// + 创建 LoggerService.cs 文件. by Michael @2019/11/8 0:08
|
||||
// ********************************************************************************
|
||||
using log4net;
|
||||
using System;
|
||||
|
||||
namespace Glorysoft.SECS.EQP.Utilities
|
||||
{
|
||||
public class LoggerService
|
||||
{
|
||||
public static ILog SECSLogger { get; private set; }
|
||||
public static ILog SYSLogger { get; private set; }
|
||||
public static ILog MQLogger { get; private set; }
|
||||
|
||||
public const string HtoE = "H=>E";
|
||||
public const string EtoH = "E=>H";
|
||||
public const string Receive = "Receive";
|
||||
public const string Send = "Send";
|
||||
|
||||
|
||||
static LoggerService()
|
||||
{
|
||||
var sPath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, @"\logs");
|
||||
LogManager.ConfigureLogger(nameof(SYSLogger), sPath, eLogFilter.DEBUG, eLogFilter.DEBUG | eLogFilter.INFO | eLogFilter.ERROR | eLogFilter.WARN, eFileFomart.Date, null);
|
||||
LogManager.ConfigureLogger(nameof(SECSLogger), sPath, eLogFilter.DEBUG, eLogFilter.DEBUG | eLogFilter.INFO | eLogFilter.ERROR | eLogFilter.WARN, eFileFomart.Date, null);
|
||||
LogManager.ConfigureLogger(nameof(MQLogger), sPath, eLogFilter.DEBUG, eLogFilter.DEBUG | eLogFilter.INFO | eLogFilter.ERROR | eLogFilter.WARN, eFileFomart.Date, null);
|
||||
SYSLogger = LogManager.GetConfigureLogger(nameof(SYSLogger));
|
||||
SECSLogger = LogManager.GetConfigureLogger(nameof(SECSLogger));
|
||||
MQLogger = LogManager.GetConfigureLogger(nameof(MQLogger));
|
||||
}
|
||||
}
|
||||
}
|
||||
175
Utilities/SECSUtil.cs
Normal file
175
Utilities/SECSUtil.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
// ********************************************************************************
|
||||
// 文件名字: SECSUtil
|
||||
// 文件描述: SECSUtil
|
||||
// 开发人员: Michael
|
||||
// 创建时间: 2019/11/8 0:08
|
||||
//
|
||||
// 更新历史:
|
||||
// + 创建 SECSUtil.cs 文件. by Michael @2019/11/8 0:08
|
||||
// ********************************************************************************
|
||||
using Glorysoft.SECSwell;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Glorysoft.SECS.EQP.Utilities
|
||||
{
|
||||
public static class SECSUtil
|
||||
{
|
||||
public static SECSItem CreateSECSItem(string name, eSECS_FORMAT type, object value = null)
|
||||
{
|
||||
return new SECSItem(type, name)
|
||||
{
|
||||
Value = value
|
||||
};
|
||||
}
|
||||
|
||||
public static eSECS_FORMAT GetSECS_FORMAT(this object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return eSECS_FORMAT.ASCII;
|
||||
}
|
||||
switch (value.GetType().Name)
|
||||
{
|
||||
case nameof(Byte): return eSECS_FORMAT.U1;
|
||||
case nameof(SByte): return eSECS_FORMAT.I1;
|
||||
case nameof(UInt16): return eSECS_FORMAT.U2;
|
||||
case nameof(Int16): return eSECS_FORMAT.I2;
|
||||
case nameof(UInt32): return eSECS_FORMAT.U4;
|
||||
case nameof(Int32): return eSECS_FORMAT.I4;
|
||||
case nameof(UInt64): return eSECS_FORMAT.U8;
|
||||
case nameof(Int64): return eSECS_FORMAT.I8;
|
||||
case nameof(Single): return eSECS_FORMAT.F4;
|
||||
case nameof(Double): return eSECS_FORMAT.F8;
|
||||
case nameof(String): return eSECS_FORMAT.ASCII;
|
||||
case nameof(Boolean): return eSECS_FORMAT.BOOLEAN;
|
||||
default: return eSECS_FORMAT.ASCII;
|
||||
}
|
||||
}
|
||||
|
||||
public static TObject To<TObject>(this object input)
|
||||
{
|
||||
if (input == null)
|
||||
{
|
||||
return default(TObject);
|
||||
}
|
||||
var targetType = typeof(TObject);
|
||||
if (targetType.IsEnum)
|
||||
{
|
||||
return (TObject)Enum.Parse(targetType, input.ToString());
|
||||
}
|
||||
if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
|
||||
{
|
||||
if (input == null || input.ToString().Length == 0)
|
||||
{
|
||||
return default(TObject);
|
||||
}
|
||||
NullableConverter nullableConverter = new NullableConverter(targetType);
|
||||
targetType = nullableConverter.UnderlyingType;
|
||||
}
|
||||
return (TObject)Convert.ChangeType(input, targetType);
|
||||
}
|
||||
|
||||
public static TObject To<TObject>(this object input, TObject defaultValue = default(TObject))
|
||||
{
|
||||
try
|
||||
{
|
||||
return input.To<TObject>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
#region Xml序列化
|
||||
|
||||
/// <summary>
|
||||
/// 将对象序列化为Xml字符串.
|
||||
/// </summary>
|
||||
/// <typeparam name="TObject">对象的类型.</typeparam>
|
||||
/// <param name="this">序列化的对象.</param>
|
||||
/// <param name="encoding">字符编码.</param>
|
||||
/// <returns>成功返回Xml字符串, 失败返回 null.</returns>
|
||||
public static string SerializeXml<TObject>(this TObject @this, Encoding encoding = null)
|
||||
{
|
||||
XmlSerializer serializer = new XmlSerializer(@this.GetType());
|
||||
using (MemoryStream memoryStream = new MemoryStream())
|
||||
{
|
||||
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
|
||||
ns.Add("", "");
|
||||
using (XmlWriter writer = XmlWriter.Create(memoryStream, new XmlWriterSettings
|
||||
{
|
||||
Indent = true,
|
||||
Encoding = Encoding.UTF8,
|
||||
OmitXmlDeclaration = true
|
||||
}))
|
||||
{
|
||||
serializer.Serialize(writer, @this, ns);
|
||||
}
|
||||
memoryStream.Position = 0;
|
||||
using (StreamReader reader = new StreamReader(memoryStream, Encoding.UTF8))
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将Xml符串反序列化为对象.
|
||||
/// </summary>
|
||||
/// <typeparam name="TObject">对象的类型.</typeparam>
|
||||
/// <param name="this">反序列化的Xml字符串.</param>
|
||||
/// <param name="encoding">字符编码.</param>
|
||||
/// <returns>成功返回 TObject 对象, 失败返回 null.</returns>
|
||||
public static TObject DeserializeXml<TObject>(this string @this, Encoding encoding = null)
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream((encoding == null ? Encoding.Default.GetBytes(@this) : encoding.GetBytes(@this))))
|
||||
{
|
||||
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TObject));
|
||||
return (TObject)xmlSerializer.Deserialize(memoryStream);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Xml序列化
|
||||
|
||||
public static string ToJson<T>(this T input, bool indented = true)
|
||||
{
|
||||
return JsonConvert.SerializeObject(input, indented ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None);
|
||||
}
|
||||
|
||||
public static T FromJson<T>(this string input)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对象转Get请求参数
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string ObjectToGetParam(this object obj)
|
||||
{
|
||||
StringBuilder strBui = new StringBuilder();
|
||||
|
||||
System.Reflection.PropertyInfo[] proArray = obj.GetType().GetProperties();
|
||||
foreach (System.Reflection.PropertyInfo pro in proArray)
|
||||
{
|
||||
if (strBui.Length < 1)
|
||||
{
|
||||
strBui.Append("?");
|
||||
}
|
||||
else
|
||||
{
|
||||
strBui.Append("&");
|
||||
}
|
||||
strBui.Append(string.Format("{0}={1}", pro.Name, pro.GetValue(obj, null)));
|
||||
}
|
||||
return strBui.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
91
Utilities/XmlSerializeUtil.cs
Normal file
91
Utilities/XmlSerializeUtil.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
#region << 版 本 注 释 >>
|
||||
/*----------------------------------------------------------------
|
||||
* 创建者:Hupe
|
||||
* 创建时间:2021/11/15 14:40:30
|
||||
* 版本:V1.0.0
|
||||
* 描述:
|
||||
*
|
||||
* ----------------------------------------------------------------
|
||||
* 修改人:
|
||||
* 时间:
|
||||
* 修改说明:
|
||||
*
|
||||
* 版本:V1.0.1
|
||||
*----------------------------------------------------------------*/
|
||||
#endregion << 版 本 注 释 >>
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using ARI.EAP.HOST.Common;
|
||||
using System.Windows.Forms;
|
||||
using Glorysoft.SECS.EQP.Utilities;
|
||||
|
||||
namespace ARI.EAP.HOST.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// <remarks>Xml序列化与反序列化</remarks>
|
||||
/// <creator>Hupe</creator>
|
||||
/// </summary>
|
||||
public class XmlSerializeUtil
|
||||
{
|
||||
#region 反序列化
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// </summary>
|
||||
/// <param name="xml">XML字符串</param>
|
||||
/// <returns></returns>
|
||||
public static T Deserialize<T>(string xml)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (StringReader sr = new StringReader(xml))
|
||||
{
|
||||
XmlSerializer xmldes = new XmlSerializer(typeof(T));
|
||||
return (T)xmldes.Deserialize(sr);
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
MessageBox.Show(e.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
LoggerService.SYSLogger.Error(e);
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 序列化为xml文件并保存到本地
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// </summary>
|
||||
/// <param name="obj">对象</param>
|
||||
/// <returns></returns>
|
||||
public static void Serializer<T>(T obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (StreamWriter sw = new StreamWriter(Constants.configuerPath, false, new UTF8Encoding(false)))
|
||||
{
|
||||
Type t = obj.GetType();
|
||||
XmlSerializer xml = new XmlSerializer(t);
|
||||
//序列化对象
|
||||
xml.Serialize(sw, obj);
|
||||
sw.Close();
|
||||
XmlDocument xmlDoc = new XmlDocument();
|
||||
xmlDoc.Save(sw);
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
MessageBox.Show(e.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
LoggerService.SYSLogger.Error(e);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user