using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.Net; using System.Text; using Curtain.DataAccess; using Curtain.Extension.ExObjectConvert; using Curtain.Extension.ExSystemData; using Curtain.Framework.Json; using Curtain.Helpers; using Curtain.Log; using Curtain.Net.Sockets.PLC; using Curtain.Net.Sockets.PLC.Model; using Curtain.Net.Sockets.PLC.Model.Siemens; using PLC_S.Proxy; namespace PLC_S.ServerModel { /// /// 自动调用API接口 xuwei add 2023-07-24 /// public class ApiAuto : SocketServer, IShowFormLog, IWindowsServer { public Dictionary PLC_FLAGS = new Dictionary(); public const string M_NAME = "AA"; private MES_S _mes_s = null; private readonly Logger logger = Logger.CreateLogger(M_NAME); private readonly LogInfo logger_t = new LogInfo(); private readonly LogInfo logger_e = new LogInfo(); private bool _S_STOP = false; public int Interval { get; set; } public string EPTS_CODE { get; set; } public ApiAuto() { logger.FileExistDays = 30; logger.FilePrefix = ""; logger.FileSuffix = M_NAME; logger.LevelOneFile = true; logger.FileNameWithoutDate = true; logger_t.SubFolderFormat = ""; logger_t.FileExistDays = 30; logger_t.LevelOneFile = true; logger_e.FileExistDays = 10; this.Model.FormatType = CommandFormatType.StartStopChar; this.ServerStarting += ServerSocket_ServerStarting; this.ServerStarted += ServerSocket_ServerStarted; this.ServerStoping += ServerSocket_ServerStoping; this.ServerStoped += ServerSocket_ServerStoped; this.ServerMessage += ServerSocket_ServerMessage; this.Received += ServerSocket_Received; _mes_s = MES_S.Get(); } #region Server public IFormLogShow FormLogShow { get; set; } private void ServerSocket_ServerStarting(object sender, CancelEventArgs e) { try { _S_STOP = false; logger.OutputTrace($"{M_NAME}_ServerStarting"); } catch { } } private void ServerSocket_ServerStarted(object sender, EventArgs e) { try { _S_STOP = false; logger.OutputTrace($"{M_NAME}_ServerStarted"); } catch { } } private void ServerSocket_ServerStoping(object sender, CancelEventArgs e) { try { _S_STOP = false; logger.OutputTrace($"{M_NAME}_ServerStoping"); } catch { } } private void ServerSocket_ServerStoped(object sender, EventArgs e) { try { _S_STOP = false; logger.OutputTrace($"{M_NAME}_ServerStoped"); } catch { } } private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e) { try { _S_STOP = false; string message = e.ToString(); string cip = e?.Client?.IP; if (e.Type == ServerMessageType.Debug) { Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t); } if (e.Type == ServerMessageType.Error) { Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e); } if (e.Type == ServerMessageType.Trace) { Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t); } if (e.Type == ServerMessageType.Warning) { Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e); } FormLogShow?.ShowLog("ServerMessage=" + message); } catch { } } #endregion /// /// 返回产品型号 /// /// /// private void ServerSocket_Received(object sender, ReceiveSession e) { PLC_S_AA plc_s = null; string cIP = e?.Client?.IP; string logKeyT = $"{M_NAME}-T[{cIP}]"; string logKeyE = $"{M_NAME}-E[{cIP}]"; _S_STOP = false; SocketClient plc = null; Logger LoggerT = null; try { LoggerT = Logger.CreateLogger(logKeyT, logger_t); lock (LoggerT) { LoggerT.BeginTracking(); string outputMessage = e.ToString(); Logger.Trace(outputMessage, logKeyT, logger_t); FormLogShow?.ShowLog(outputMessage); #region 【必须】判断接收数据是否为空 if (string.IsNullOrWhiteSpace(e.Content)) { outputMessage = "接收数据为空"; Logger.Warn(outputMessage, logKeyT, logger_t); Logger.Warn(outputMessage, logKeyE, logger_e); FormLogShow?.ShowLog(outputMessage); return; } #endregion #region 【必须】判断接收设备代码是否为空 outputMessage = e.Content; Logger.Trace(outputMessage, logKeyT, logger_t); FormLogShow?.ShowLog(outputMessage); string[] c = e.Content.Split('#'); string flag = null; if (c.Length > 0) { flag = c[0]; } string barCode = null; if (c.Length > 1) { barCode = c[1]; } if (string.IsNullOrWhiteSpace(flag)) { outputMessage = $"[{e.Content}]接收设备代码为空"; Logger.Warn(outputMessage, logKeyT, logger_t); Logger.Warn(outputMessage, logKeyE, logger_e); FormLogShow?.ShowLog(outputMessage); return; } #endregion #region 【必须】读取INI配置文件 plc_s = GetPLC_S_AA(flag, e, logKeyE); outputMessage = $"[{e.Content}]INI配置={JsonHelper.FromObject(plc_s)}"; Logger.Trace(outputMessage, logKeyT, logger_t); if (plc_s == null) { outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误"; Logger.Error(null, outputMessage, logKeyT, logger_t); Logger.Error(null, outputMessage, logKeyE, logger_e); FormLogShow?.ShowLog(outputMessage); return; } #endregion #region 【必须】调用API接口 if (!string.IsNullOrEmpty(plc_s.API_URL)) { outputMessage = $"[{e.Content}]接口地址={plc_s.API_URL.Replace("@barcode", barCode)}"; Logger.Trace(outputMessage, logKeyT, logger_t); outputMessage = $"[{e.Content}]执行结果={GetApi(plc_s.API_URL.Replace("@barcode", barCode))}"; Logger.Trace(outputMessage, logKeyT, logger_t); } else { outputMessage = $"[{e.Content}]API_URL未设置,请检查确认!"; Logger.Trace(outputMessage, logKeyT, logger_t); FormLogShow?.ShowLog(outputMessage); } #endregion } } catch (Exception ex) { FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}"); Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t); Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e); } finally { LoggerT?.EndTracking(); plc?.Disconnect(); plc?.Close(); } } public static string GetApi(string url) { //拼合URL参数 string webUrl = url; //创建Web访问对象 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(webUrl); //通过Web访问对象获取响应内容 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法 string returnXml = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾 reader.Close(); myResponse.Close(); return returnXml; } private PLC_S_AA GetPLC_S_AA(string flag, ReceiveSession e, string loggerName) { try { flag = M_NAME + flag; if (PLC_FLAGS.ContainsKey(flag)) { return PLC_FLAGS[flag]; } else { PLC_S_AA plc_s = new PLC_S_AA(); INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini"); plc_s.API_URL = ini.Read(flag, "API_URL"); PLC_FLAGS.Add(flag, plc_s); return plc_s; } } catch (Exception ex) { Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_GA", loggerName, logger_e); FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_GA={ex.Message}"); return null; } } } }