1
czw
2025-07-05 1c2079be2fae2a811c2afe3682076d3c7a0bac7f
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
{
@@ -76,8 +77,8 @@
            }
            // æ‰§è¡Œå§”托
            if (methodName != "TaskEverythingRun")
                return null;
            //if (methodName != "TaskEverythingRun")
            //    return null;
            return methodDelegate.DynamicInvoke(args);
        }
@@ -254,6 +255,8 @@
            if (VERX != null)
            {
                if (tag.wxr1.R44)
                    LogHelper.Info($"{VERX.deviceName} R44>{tag.wxr1.R44} R44LAST>{tag.wxr1.R44_LAST} R10>{tag.wxr1.R10}");
                if (tag.wxr1.R44 && !tag.wxr1.R44_LAST && tag.wxr1.R10)
                {
                    if (TcpServer.TrayIps.TryGetValue(VERX.deviceNo[0], out string traycode) && !string.IsNullOrEmpty(traycode.Trim()))
@@ -292,6 +295,8 @@
                        if (TcpServer.TrayIps.TryGetValue(VERX.deviceNo[0], out string traycode) && !string.IsNullOrEmpty(traycode.Trim()))
                        {
                            //上报校验。
                            LogHelper.Info($"{VERX.deviceNo[0]} ä¸ŠæŠ¥æ ¡éªŒ>{traycode}");
                            ///var str = Settings.apiHelper.Post(Settings.MESbaseUrl + "mom-basic/dataTransmission/json/service/202", requestJson);
                            var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/202", JsonConvert.SerializeObject(new
                            {
@@ -327,6 +332,8 @@
            if (VERX != null)
            {
                if (tag.wxr1.R46)
                    LogHelper.Info($"{VERX.deviceName} R44>{tag.wxr1.R46} R44LAST>{tag.wxr1.R46_LAST} R10>{tag.wxr1.R11}");
                if (tag.wxr1.R46 && !tag.wxr1.R46_LAST && tag.wxr1.R11)
                {
                    if (TcpServer.TrayIps.TryGetValue(VERX.deviceNo[1], out string traycode) && !string.IsNullOrEmpty(traycode.Trim()))
@@ -364,6 +371,7 @@
                        if (TcpServer.TrayIps.TryGetValue(VERX.deviceNo[1], out string traycode) && !string.IsNullOrEmpty(traycode.Trim()))
                        {
                            //上报校验。
                            LogHelper.Info($"{VERX.deviceNo[1]} ä¸ŠæŠ¥æ ¡éªŒ>{traycode}");
                            ///var str = Settings.apiHelper.Post(Settings.MESbaseUrl + "mom-basic/dataTransmission/json/service/202", requestJson);
                            var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/202", JsonConvert.SerializeObject(new
                            {
@@ -399,6 +407,8 @@
            if (VERX != null)
            {
                if (tag.wxr2.R44)
                    LogHelper.Info($"{VERX.deviceName} R44>{tag.wxr2.R44} R44LAST>{tag.wxr2.R44_LAST} R10>{tag.wxr2.R10}");
                if (tag.wxr2.R44 && !tag.wxr2.R44_LAST && tag.wxr2.R10)
                {
                    if (TcpServer.TrayIps.TryGetValue(VERX.deviceNo[0], out string traycode) && !string.IsNullOrEmpty(traycode.Trim()))
@@ -436,6 +446,7 @@
                        if (TcpServer.TrayIps.TryGetValue(VERX.deviceNo[0], out string traycode) && !string.IsNullOrEmpty(traycode.Trim()))
                        {
                            //上报校验。
                            LogHelper.Info($"{VERX.deviceNo[0]} ä¸ŠæŠ¥æ ¡éªŒ>{traycode}");
                            ///var str = Settings.apiHelper.Post(Settings.MESbaseUrl + "mom-basic/dataTransmission/json/service/202", requestJson);
                            var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/202", JsonConvert.SerializeObject(new
                            {
@@ -472,6 +483,8 @@
            if (VERX != null)
            {
                if (tag.wxr2.R46)
                    LogHelper.Info($"{VERX.deviceName} R44>{tag.wxr2.R46} R44LAST>{tag.wxr2.R46_LAST} R10>{tag.wxr2.R11}");
                if (tag.wxr2.R46 && !tag.wxr2.R46_LAST && tag.wxr2.R11)
                {
                    if (TcpServer.TrayIps.TryGetValue(VERX.deviceNo[1], out string traycode) && !string.IsNullOrEmpty(traycode.Trim()))
@@ -510,7 +523,7 @@
                        else
                        if (TcpServer.TrayIps.TryGetValue(VERX.deviceNo[1], out string traycode) && !string.IsNullOrEmpty(traycode.Trim()))
                        {
                            LogHelper.Info($"Threadwxr2R ä¸ŠæŠ¥æ ¡éªŒ>{traycode}");
                            LogHelper.Info($"{VERX.deviceNo[1]} ä¸ŠæŠ¥æ ¡éªŒ>{traycode}");
                            //上报校验。
                            ///var str = Settings.apiHelper.Post(Settings.MESbaseUrl + "mom-basic/dataTransmission/json/service/202", requestJson);
                            var str = Settings.apiHelper.Post(Settings.WMSbaseUrl + "mom-basic/dataTransmission/json/service/202", JsonConvert.SerializeObject(new
@@ -1584,6 +1597,10 @@
            foreach (var task in tasks)
            {
                LogHelper.Info($"解释MES任务 ->{task.S_TASK_NO} ");
                if (task.S_TYPE?.Trim() == "100")
                {
                    LogHelper.Info($"MES任务 S_TYPE 100->{task.S_TYPE} ");
                }
                var sx = VERXs?.Find(x => x.location.Contains(task.S_END_LOC));
                LogHelper.Info($"解释MES任务 ->{task.S_TASK_NO} {(sx == null ? "非" : "是")}总装机器人任务");
                if (sx == null)
@@ -2008,7 +2025,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();
@@ -2026,12 +2043,168 @@
    }
    public class HttpServer
    {
        System.Net.HttpListener HttpSvcHost = null;
        public static string _listenerPrefix = "";
        public HttpServer(string ip)
        {
            _listenerPrefix = $"http://{ip}:8808/";
        }
        public void HttpServerRun()
        {
            HttpSvcHost = new System.Net.HttpListener();
            HttpSvcHost.AuthenticationSchemes = System.Net.AuthenticationSchemes.Anonymous;
            HttpSvcHost.Prefixes.Add(_listenerPrefix);
            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
@@ -2169,8 +2342,6 @@
        }
    }
    internal class WebSocketClient
    {