11
czw
2025-07-03 0d746f4f5032d7bd7e86649e22a3227bd6087cc6
2025Äê6ÔÂ12ÈÕ/AuxAllWCS/Build/Project/´úÂë/VS×Ô¶¨ÒåÀà/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;
                                        // å¤åˆ¶åˆ°case ä¸Š
                                        //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