callm 1 сар өмнө
parent
commit
e89638dbc5

+ 1 - 0
UAS_MES_HYSX/FunctionCode/Packing/Packing_PackageCollectionZX.cs

@@ -633,6 +633,7 @@ namespace UAS_MES_NEW.Packing
                 string Seal = MessageBox.Show(this.ParentForm, "是否确认封箱?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
                 if (Seal == "Yes")
                 {
+                    HttpServer.GetOutBoxInfo(pa_outboxcode.Text);
                     OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "采集完成,手动封箱成功\n", Color.Green);
                     dh.UpdateByCondition("package", "pa_status=1,pa_totalqty=pa_currentqty,pa_packageqty=pa_currentqty", "pa_outboxcode='" + pa_outboxcode.Text + "'");
                     pa_status.Text = "1";

+ 62 - 0
UAS_MES_HYSX/PublicMethod/FACT_DATA_HEAD.cs

@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace UAS_MES_NEW.PublicMethod
+{
+    internal class FACT_DATA_HEAD
+    {
+        public const int MY_LEN = 8;
+        public static int PROTOCOL_FLAG = 0x4C4F4F54;
+
+
+        private UInt32 nFlag { get; set; }
+        private UInt32 nDataLen { get; set; }
+
+        public FACT_DATA_HEAD(byte[] arrData)
+        {
+            if (arrData == null)
+            {
+                nFlag = (UInt32)PROTOCOL_FLAG;
+                nDataLen = 0;
+            }
+            else
+            {
+                if (arrData.Length < MY_LEN)
+                {
+                    nFlag = (UInt32)PROTOCOL_FLAG;
+                    nDataLen = 0;
+                }
+                else
+                {
+                    nFlag = (UInt32)BitConverter.ToInt32(arrData, 0);
+                    nDataLen = (UInt32)BitConverter.ToInt32(arrData, 4);
+                }
+            }
+        }
+
+        public void setDataLen(int nLen)
+        {
+            nDataLen = (UInt32)nLen;
+        }
+        public int getDatalen()
+        {
+            return (int)nDataLen;
+        }
+        public int getFlag()
+        {
+            return (int)nFlag;
+        }
+        public byte[] getBytes()
+        {
+            byte[] bytHead = new byte[MY_LEN];
+            byte[] bytSrc = BitConverter.GetBytes(nFlag);
+            Array.Copy(bytSrc, 0, bytHead, 0, bytSrc.Length);
+
+            bytSrc = BitConverter.GetBytes(nDataLen);
+            Array.Copy(bytSrc, 0, bytHead, 4, bytSrc.Length);
+            return bytHead;
+        }
+    }
+}

+ 66 - 0
UAS_MES_HYSX/PublicMethod/FACT_DATA_PROTOCOL.cs

@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace UAS_MES_NEW.PublicMethod
+{
+    internal class FACT_DATA_PROTOCOL
+    {
+        public static string TOOL_VERSION_STR = "2.1.35";
+        public static string TOOL_BUILD_DATE = "_2025-06-18";
+
+        public static string KEY_JSON_tool_name = "tool_name";
+        public static string KEY_JSON_request = "request";
+        public static string KEY_JSON_User = "User";
+        public static string KEY_JSON_Password = "Password";
+        public static string KEY_JSON_SN = "SN";
+        public static string KEY_JSON_status = "status";
+        public static string KEY_JSON_tool_version_min = "tool_version_min";
+
+        public static string VALUE_JSON_instar = "instar";	//tool_name=instar
+        public static string VALUE_JSON_zte = "ajy_label";		//tool_name=zte
+        public static string VALUE_JSON_hy = "hy";		//tool_name=hy
+        public static string VALUE_JSON_cocoon = "cocoon";  //tool_name=cocoon
+
+        public static string VALUE_JSON_Login = "Login";
+        public static string VALUE_JSON_user_role = "user_role";
+        public static string VALUE_JSON_Query = "Query";
+        public static string VALUE_JSON_PreSubmit = "PreSubmit";
+        public static string VALUE_JSON_Submit = "Submit";
+        public static string VALUE_JSON_storage = "Storage";
+        public static string VALUE_JSON_storage2 = "Storage2";
+        public static string VALUE_JSON_Re_en_no = "Re_en_no";
+        public static string VALUE_JSON_Q_cai_he = "Q_cai_he";
+        public static string VALUE_JSON_Q_wai_xiang = "Q_wai_xiang";
+        public static string VALUE_JSON_AJC = "AJC";
+        public static string VALUE_JSON_AJC_Mingpai = "ajc_mingpai";
+        public static string VALUE_JSON_AJC_caihe = "ajc_cai_he";
+        public static string VALUE_JSON_AJC_waixiang = "ajc_wai_xiang";
+        public static string VALUE_JSON_AJC_SN = "sn";
+        public static string VALUE_JSON_AJC_Product_id = "Product_id";
+
+        public static string VALUE_JSON_storager = "storager";
+        public static string VALUE_JSON_storage_time = "storage_time";
+        public static string VALUE_JSON_logined_user = "logined_user";
+        public static string VALUE_JSON_logined_time = "logined_time";
+
+        public static string KEY_JSON_label_templ = "label_templ";
+        public static string KEY_JSON_product_name = "product_name";
+        public static string KEY_JSON_product_mode = "product_mode";
+        public static string KEY_JSON_prefix_en_no = "prefix_en_no";
+        public static string KEY_JSON_prefix_big_box = "prefix_big_box";
+        public static string KEY_JSON_packing_num = "packing_num";
+
+        public static string KEY_JSON_dev_en_no = "dev_en_no";
+        public static string KEY_JSON_dev_isn = "isn";
+        public static string KEY_JSON_mac = "mac";
+        public static string KEY_JSON_dev_sn = "dev_sn";
+        public static string KEY_JSON_dev_id = "dev_id";
+        public static string KEY_JSON_reg_code = "reg_code";
+        public static string KEY_JSON_device_type = "device_type";
+
+        public static string KEY_JSON_reserve2 = "reserve2";
+        public static string KEY_JSON_reserve3 = "reserve3";
+    }
+}

+ 111 - 15
UAS_MES_HYSX/PublicMethod/HttpServer.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Data;
 using System.Dynamic;
 using System.IO;
+using System.Net;
 using System.Net.Sockets;
 using System.Security.Cryptography;
 using System.Text;
@@ -14,6 +15,7 @@ using UAS_MES_NEW.Entity;
 namespace UAS_MES_NEW.PublicMethod
 {
 
+
     public class BindItem
     {
         [JsonProperty("bindKey")]
@@ -92,6 +94,8 @@ namespace UAS_MES_NEW.PublicMethod
     class HttpServer
     {
         static DataHelper dh = new DataHelper();
+        public static int SN_SERVICE_PORT = 8234;
+        public static int MAX_BUF_SIZE = 4096;
         static string ComputeSha256Hash(string input)
         {
             using (SHA256 sha256 = SHA256.Create())
@@ -150,7 +154,7 @@ namespace UAS_MES_NEW.PublicMethod
             string device_type = deviceObj["device_type"].ToString();
             string en_no = deviceObj["en_no"].ToString();
             dh.ExecuteSql("insert into ZTEDATA(ZD_ID,ZD_D_SN,zd_WholeDeviceCode,ZD_DEV_EN_NO,ZD_SN, ZD_MAKECODE, ZD_TYPE, ZD_VALUE,zd_mac,ZD_MAC_START, ZD_MAC_END,ZD_RESERVE3,zd_enno,zd_devicetype,zd_regcode)" +
-                "values(ZTEDATA_seq.nextval,'" + SN + "','" + WholeDeviceCode + "','" + dev_en_no + "','" + iSN + "','" + makecode + "','彩盒机身标','" + mac + "','" + mac_start + "','" + mac_end + "','" + reserve3 + "','" + en_no + "','" + device_type + "','" + reg_code + "')", "insert");
+                "values(ZTEDATA_seq.nextval,'" + SN + "','" + WholeDeviceCode + "','" + dev_en_no + "','" + iSN + "','" + makecode + "','彩盒机身标','','" + mac + "','" + mac_start + "','" + mac_end + "','" + reserve3 + "','" + en_no + "','" + device_type + "','" + reg_code + "')", "insert");
             Console.WriteLine(json);
         }
 
@@ -290,33 +294,51 @@ namespace UAS_MES_NEW.PublicMethod
         //发送数据
         public static string SendData(string json)
         {
-            string serverIP = "192.168.1.160";
-            int port = 21610;
+            string serverIP = "10.1.162.175";
+            int port = 8234;
 
             try
             {
-                // 创建TCP客户端并连接到服务器
                 TcpClient client = new TcpClient();
                 client.Connect(serverIP, port);
-                // 获取网络流
                 NetworkStream stream = client.GetStream();
 
-                // 要发送的请求报文(根据实际需求修改)
-                byte[] packet = BuildPacket(json);
-                // 发送请求
+                // 发送数据
+                byte[] data = Encoding.UTF8.GetBytes(json);
+                byte[] lengthPrefix = BitConverter.GetBytes(data.Length);
+                byte[] packet = new byte[lengthPrefix.Length + data.Length];
+
+                Buffer.BlockCopy(lengthPrefix, 0, packet, 0, lengthPrefix.Length);
+                Buffer.BlockCopy(data, 0, packet, lengthPrefix.Length, data.Length);
+
                 stream.Write(packet, 0, packet.Length);
                 Console.WriteLine($"已发送请求: {json}");
 
-                // 接收响应
-                byte[] buffer = new byte[1024];
-                int bytesRead = stream.Read(buffer, 0, buffer.Length);
-                string responseMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
+                // 接收数据 - 先读取长度
+                byte[] lengthBuffer = new byte[4];
+                int totalBytesRead = 0;
+                while (totalBytesRead < lengthBuffer.Length)
+                {
+                    int bytesRead = stream.Read(lengthBuffer, totalBytesRead, lengthBuffer.Length - totalBytesRead);
+                    if (bytesRead == 0) break;
+                    totalBytesRead += bytesRead;
+                }
+
+                int messageLength = BitConverter.ToInt32(lengthBuffer, 0);
+
+                // 根据长度读取完整数据
+                byte[] messageBuffer = new byte[messageLength];
+                totalBytesRead = 0;
+                while (totalBytesRead < messageLength)
+                {
+                    int bytesRead = stream.Read(messageBuffer, totalBytesRead, messageLength - totalBytesRead);
+                    if (bytesRead == 0) break;
+                    totalBytesRead += bytesRead;
+                }
 
+                string responseMessage = Encoding.UTF8.GetString(messageBuffer, 0, totalBytesRead);
                 Console.WriteLine($"收到响应: {responseMessage}");
 
-                // 关闭流和连接
-                stream.Close();
-                client.Close();
                 return responseMessage;
             }
             catch (SocketException ex)
@@ -357,5 +379,79 @@ namespace UAS_MES_NEW.PublicMethod
                 return ms.ToArray();
             }
         }
+
+        public static string ToServerReq(string strJSON)
+        {
+            int nRet = 0;
+            string strServerIP = "10.1.162.175";
+            string strResp = "";
+            IPAddress ip = IPAddress.Parse(strServerIP);
+            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+            while (true)
+            {
+                try
+                {
+                    clientSocket.Connect(new IPEndPoint(ip, SN_SERVICE_PORT));
+                }
+                catch
+                {
+                    nRet = -13;
+                    break;
+                }
+                try
+                {
+                    FACT_DATA_HEAD stHead = new FACT_DATA_HEAD(null);
+                    byte[] bytJson = Encoding.UTF8.GetBytes(strJSON); //Encoding.ASCII.GetBytes(strJSON);
+                    stHead.setDataLen(bytJson.Length);
+                    byte[] bytBuf = new byte[FACT_DATA_HEAD.MY_LEN + bytJson.Length];
+                    byte[] bytSrcHead = stHead.getBytes();
+                    Array.Copy(bytSrcHead, 0, bytBuf, 0, bytSrcHead.Length);
+                    Array.Copy(bytJson, 0, bytBuf, FACT_DATA_HEAD.MY_LEN, bytJson.Length);
+                    clientSocket.Send(bytBuf);
+                }
+                catch
+                {
+                    nRet = -12;
+                    break;
+                }
+
+                byte[] bytRecv = new byte[MAX_BUF_SIZE];
+                int nRecvSize = 0, nSize = 0;
+                nRecvSize = clientSocket.Receive(bytRecv, 0, FACT_DATA_HEAD.MY_LEN, SocketFlags.None);
+
+                //                 string strTmp;
+                //                 strTmp = string.Format("clientSocket.Receive={0}", nRecvSize);
+                //                 Trace.WriteLine(strTmp);
+
+                if (nRecvSize > 0)
+                {
+                    FACT_DATA_HEAD stHead = new FACT_DATA_HEAD(bytRecv);
+                    Console.WriteLine(stHead.getFlag());
+                    Console.WriteLine(FACT_DATA_HEAD.PROTOCOL_FLAG);
+                    //if (stHead.getFlag() == FACT_DATA_HEAD.PROTOCOL_FLAG)
+                    {
+                        int nPos = 0;
+                        nSize = stHead.getDatalen();
+                        while (nPos < nSize)
+                        {
+                            nRecvSize = clientSocket.Receive(bytRecv, nPos, 1869881979 - nPos, SocketFlags.None);
+                            if (nRecvSize < 0)
+                            {
+                                nRet = -14;
+                                break;
+                            }
+                            nPos += nRecvSize;
+                        }
+                        strResp = Encoding.Default.GetString(bytRecv, 0, nSize);
+                    }
+                    //else nRet = -11;
+                }
+                else nRet = -10;
+                break;
+            }
+            if (nRet > -13) clientSocket.Shutdown(SocketShutdown.Both);
+            clientSocket.Close();
+            return strResp;
+        }
     }
 }

+ 2 - 0
UAS_MES_HYSX/UAS_MES_HYSX.csproj

@@ -1459,6 +1459,8 @@
     <Compile Include="PublicMethod\DrawHelper.cs" />
     <Compile Include="PublicMethod\Encryption.cs" />
     <Compile Include="PublicMethod\ExceptionHandler.cs" />
+    <Compile Include="PublicMethod\FACT_DATA_HEAD.cs" />
+    <Compile Include="PublicMethod\FACT_DATA_PROTOCOL.cs" />
     <Compile Include="PublicMethod\ftpOperater.cs" />
     <Compile Include="PublicMethod\GlobalEventsHandler.cs" />
     <Compile Include="PublicMethod\HttpServer.cs" />