From 0d746f4f5032d7bd7e86649e22a3227bd6087cc6 Mon Sep 17 00:00:00 2001 From: czw <selecti@yeah.net> Date: 星期四, 03 七月 2025 16:23:20 +0800 Subject: [PATCH] 11 --- 2025年6月12日/AuxAllWCS/Build/Project/代码/VS自定义类/AutoThread.cs | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 159 insertions(+), 1 deletions(-) diff --git "a/2025\345\271\2646\346\234\21012\346\227\245/AuxAllWCS/Build/Project/\344\273\243\347\240\201/VS\350\207\252\345\256\232\344\271\211\347\261\273/AutoThread.cs" "b/2025\345\271\2646\346\234\21012\346\227\245/AuxAllWCS/Build/Project/\344\273\243\347\240\201/VS\350\207\252\345\256\232\344\271\211\347\261\273/AutoThread.cs" index 4abdad7..6cf597f 100644 --- "a/2025\345\271\2646\346\234\21012\346\227\245/AuxAllWCS/Build/Project/\344\273\243\347\240\201/VS\350\207\252\345\256\232\344\271\211\347\261\273/AutoThread.cs" +++ "b/2025\345\271\2646\346\234\21012\346\227\245/AuxAllWCS/Build/Project/\344\273\243\347\240\201/VS\350\207\252\345\256\232\344\271\211\347\261\273/AutoThread.cs" @@ -25,6 +25,7 @@ using System.Net.WebSockets; using System.Net; using System.Threading; +using System.IO; namespace GZ.Projects.AuxAllWCS { @@ -2008,7 +2009,7 @@ if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { Console.WriteLine($"ip= {ip.ToString()}"); - + new HttpServer(ip.ToString()).HttpServerRun(); new TcpServer(ip.ToString()); var server = new EnhancedWebSocketServer($"http://{ip.ToString()}:8809/").StartAsync(); //var client = new WebSocketClient($"http://{ip.ToString()}:8809/").ConnectAsync(); @@ -2028,10 +2029,167 @@ + } + + public class HttpServer + { + System.Net.HttpListener HttpSvcHost = null; + + private readonly string _listenerPrefix = ""; + public HttpServer(string ip) + { + _listenerPrefix = ip; + } + public void HttpServerRun() + { + HttpSvcHost = new System.Net.HttpListener(); + HttpSvcHost.AuthenticationSchemes = System.Net.AuthenticationSchemes.Anonymous; + HttpSvcHost.Prefixes.Add($"http://{_listenerPrefix}:8808/"); + HttpSvcHost.Start(); + HttpSvcHost.BeginGetContext(HttpSvcListenerCallback, null); + } + private async void HttpSvcListenerCallback(IAsyncResult ar) + { + System.Net.HttpListenerContext context = null; + var data = DateTime.Now; + string apth = ""; + try + { + HttpSvcHost.BeginGetContext(HttpSvcListenerCallback, null); + context = HttpSvcHost.EndGetContext(ar); + System.Net.HttpListenerRequest request = context.Request; + System.Net.HttpListenerResponse response = context.Response; + using (var reader = new System.IO.StreamReader(request.InputStream, System.Text.Encoding.UTF8)) + { + string requestJson = reader.ReadToEnd(); + System.Net.HttpStatusCode statusCode = 0; + apth = request.Url.AbsolutePath; + string respstr = HttpSvcListenerCallback_he(request.HttpMethod, request.Url.AbsolutePath, requestJson, out statusCode); + string logContent = ""; + logContent += $"\r\n[{request.HttpMethod}]{request.Url.AbsolutePath}"; + logContent += $"\r\n[request]{requestJson}"; + logContent += $"\r\n[response]{respstr}"; + _ = Task.Run(() => + { + Conn.榛樿鏃ュ織?.Info(logContent); + }); + byte[] bytstr = Encoding.UTF8.GetBytes(respstr); + response.StatusCode = (int)statusCode; + response.SendChunked = false; + response.ContentLength64 = bytstr.Length; + //response.OutputStream.Write(bytstr, 0, bytstr.Length); + if (request.Url.AbsolutePath.ToLower().Contains(".js")) + response.ContentType = "application/javascript"; + else if (request.Url.AbsolutePath.ToLower().Contains(".svg")) + response.ContentType = "image/svg+xml"; + // 寮傛鍐欏叆鍝嶅簲 + await response.OutputStream.WriteAsync(bytstr, 0, bytstr.Length); + } + } + catch (Exception ex) + { + _ = Task.Run(() => + { + Conn.榛樿鏃ュ織.Error(ex.ToString()); + }); + } + finally + { + context?.Response.Close(); + Console.WriteLine(apth + "<<>>" + DateTime.Now.Subtract(data).TotalMilliseconds); + } + } + private System.String HttpSvcListenerCallback_he(System.String method, System.String path, System.String requestJson, out System.Net.HttpStatusCode statusCode) + { + try + { + switch (method) + { + case "POST": + { + switch (path) + { + case "/agv/agvCallbackService/agvCallback": + { + statusCode = System.Net.HttpStatusCode.OK; + LogHelper.Info("ExecuteState Request-haikang锛� + requestJson); + var model = JsonConvert.DeserializeObject<HaiKangOrderInfo>(requestJson); + AutoThread.Instance.OperateHKTaskStatus(model); + return JsonConvert.SerializeObject(new HkReturnResult { reqCode = model.reqCode }); + } + case "/api/Wcs/CreateTask": + { + statusCode = System.Net.HttpStatusCode.OK; + + LogHelper.Info("/api/Wcs/CreateTask锛� + requestJson); + var model = JsonConvert.DeserializeObject<ToWMSMES.CreateTask>(requestJson); + var res = ToWMSMES.CreateTask.CreatemesTask(model); + return JsonConvert.SerializeObject(res); + } + case "/api/Wcs/TestRequestTask": + { + statusCode = System.Net.HttpStatusCode.OK; + LogHelper.Info("/api/Wcs/TestRequestTask锛� + requestJson); + var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/200", requestJson); + return str; + } + case "/api/Wcs/updateStatus": + { + statusCode = System.Net.HttpStatusCode.OK; + LogHelper.Info("/api/Wcs/updateStatus锛� + requestJson); + var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/201", requestJson); + return str; + } + case "/api/Wcs/toMes": + { + statusCode = System.Net.HttpStatusCode.OK; + + LogHelper.Info("/api/Wcs/toMes锛� + requestJson); + var str = Settings.apiHelper.Post(Settings.MESbaseUrl + "mom-basic/dataTransmission/json/service/202", requestJson); + return str; + } + case "/api/Wcs/CreatePointTask": + { + statusCode = System.Net.HttpStatusCode.OK; + + LogHelper.Info("/api/Wcs/CreatepointTask锛� + requestJson); + var model = JsonConvert.DeserializeObject<ToWMSMES.CreateTask>(requestJson); + var res = ToWMSMES.CreateTask.CreatePointTask(model); + return JsonConvert.SerializeObject(res); + } + } + break; + } + case "GET": + { + switch (path) + { + case var _ when System.Text.RegularExpressions.Regex.IsMatch(path, @"\.(html|ico|js|css)(\?.*)?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase): + { + statusCode = System.Net.HttpStatusCode.OK; + // 澶嶅埗鍒癱ase 涓�+ //var _ when System.Text.RegularExpressions.Regex.IsMatch(path, @"\.(html|ico|js|css)(\?.*)?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase) + var filePath = /*Directory.GetCurrentDirectory() + "\\Static" + "\\" + path.Substring(1);*/System.IO.Path.Combine(Directory.GetCurrentDirectory() + "\\Static", path.Substring(1)); + return File.ReadAllText(filePath); + } + } + break; + } + } + statusCode = System.Net.HttpStatusCode.NotFound; + return ""; + } + catch (Exception ex) + { + Conn.榛樿鏃ュ織.Error(ex.ToString()); + statusCode = System.Net.HttpStatusCode.InternalServerError; + return ""; + } + } } class EnhancedWebSocketServer -- Gitblit v1.9.1