using System; using System.Collections.Generic; using System.Data; using System.Dynamic; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; using UAS_MES_NEW.DataOperate; namespace UAS_MES_NEW.PublicMethod { public class BindItem { [JsonProperty("bindKey")] public string BindKey { get; set; } [JsonProperty("bindValue")] public string BindValue { get; set; } [JsonProperty("keyType")] public string KeyType { get; set; } [JsonProperty("keyValue")] public string KeyValue { get; set; } [JsonProperty("hashType")] public string HashType { get; set; } [JsonProperty("hashKey")] public string HashKey { get; set; } } public class RootObject { [JsonProperty("productModuleType")] public string ProductModuleType { get; set; } [JsonProperty("productMainClass")] public string ProductMainClass { get; set; } [JsonProperty("productMediumClass")] public string ProductMediumClass { get; set; } [JsonProperty("productSubClass")] public string ProductSubClass { get; set; } [JsonProperty("task")] public string Task { get; set; } [JsonProperty("processType")] public string ProcessType { get; set; } [JsonProperty("userId")] public string UserId { get; set; } [JsonProperty("bindList")] public List BindList { get; set; } } public class ZteData { public string tool_name { get; set; } public string response { get; set; } public int status { get; set; } public string product_name { get; set; } public string product_mode { get; set; } public string material_code { get; set; } public string batch_no { get; set; } public string production_date { get; set; } public string big_box_no { get; set; } public string box_no { get; set; } public string reserve10 { get; set; } public int packing_num { get; set; } public string soft_version { get; set; } public string hd_version { get; set; } public string voltage { get; set; } public string power { get; set; } public string order_no { get; set; } public string[] d_sn { get; set; } public string[] mac_start { get; set; } public string[] mac_end { get; set; } public string[] DeviceId { get; set; } public string[] dev_en_no { get; set; } public string[] reserve3 { get; set; } public string[] reserve4 { get; set; } } class HttpServer { static DataHelper dh = new DataHelper(); static string ComputeSha256Hash(string input) { using (SHA256 sha256 = SHA256.Create()) { byte[] bytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = sha256.ComputeHash(bytes); StringBuilder builder = new StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { builder.Append(hashBytes[i].ToString("x2")); } return builder.ToString().ToUpper(); } } public static void SendBoxData(string iBox) { string sn = dh.getFieldDataByCondition("packagedetail", "replace(wm_concat(pd_barcode),',','-')", "pd_outboxcode='" + iBox + "'").ToString(); var root = new RootObject { ProductModuleType = "整机", ProductMainClass = "固网_CPE", ProductMediumClass = "CPE", ProductSubClass = "ZXSLC SC50L", Task = "2025070006", ProcessType = "生产过程", UserId = "邓帝森", BindList = new List() }; root.BindList.Add(new BindItem { BindKey = "cartonSn", BindValue = iBox, KeyType = "macbox", KeyValue = sn, HashType = "SHA256", HashKey = ComputeSha256Hash(sn) }); DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail pd_outboxcode='" + iBox + "'", "select"); for (int i = 0; i < dt.Rows.Count; i++) { root.BindList.Add(new BindItem { BindKey = "mac", BindValue = dt.Rows[i]["pd_barcode"].ToString(), KeyType = "EN", KeyValue = "4Y12R9300001", HashType = "NON-HASH", HashKey = "4Y12R9300001" }); root.BindList.Add(new BindItem { BindKey = "mac", BindValue = dt.Rows[i]["pd_barcode"].ToString(), KeyType = "电源条码", KeyValue = "1U1Y2505100013640", HashType = "NON-HASH", HashKey = "1U1Y2505100013640" }); root.BindList.Add(new BindItem { BindKey = "mac", BindValue = dt.Rows[i]["pd_barcode"].ToString(), KeyType = "JY-License", KeyValue = "Qk1K4rmuowTDl+NEtSIeHx1td6Tzzm0wDrw/AMCvJ6mrXQ5wsSgoxia+T5Cj+uH4xrGUrjGrZvh/WUQN88iDrPO7kuTA0mvo/Ay+AsFJYnn00JQYQkN9ul+a+Qh0EPBqCVa4ikllHcD1Pqq4Eubs6XXNFhKOtK7F7FMotfGzzAVg7RhKVoVRVFcK6ubSVWXW+SnGx5muH+WZPaiHLjt+kxBR8tZJuBl1LQvMWP74JSWKkcFWwcWlARnCJePvRTvKTDd5e7q9rpBpAc7Z79XYQ6Fs4eXM44O6/hA88YW/BK0TZq3AaaMxH8BpJnwZt4lIyuqXispeaB2eieFBR0JwWg==", HashType = "SHA256", HashKey = ComputeSha256Hash(sn), }); ; } string json = JsonConvert.SerializeObject(root, Formatting.Indented); string returnstr = SendData(json); Console.WriteLine(json); } //获取外箱号信息 public static void GetOutBoxInfo(string iBox) { dynamic obj = new ExpandoObject(); obj.request = "Q_wai_xiang"; obj.packing_num = 20; obj.User = "mes"; obj.po_sn = "250908001001"; obj.password = "258456"; obj.tool_name = "zte"; obj.noType = 0; var expandoDict = obj as IDictionary; DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail where pd_outboxcode='" + iBox + "'", "select"); for (int i = 0; i < dt.Rows.Count; i++) { string enNoKey = $"en_no{i}"; string enNoValue = dt.Rows[i]["pd_barcode"].ToString(); expandoDict[enNoKey] = enNoValue; } string json = JsonConvert.SerializeObject(obj, Formatting.Indented); string returnstr = SendData(json); ZteData zteData = JsonConvert.DeserializeObject(returnstr); //将返回的信息存入数据库 for (int i = 0; i < zteData.d_sn.Length; i++) { dh.ExecuteSql("insert into ZTEDATA(ZD_ID,ZD_SN, ZD_MAKECODE, ZD_TYPE, ZD_VALUE,ZD_product_mode, ZD_PRODUCTNAME, ZD_MATERIAL_CODE, ZD_BATCH_NO, ZD_SOFT_VERSION, ZD_HD_VERSION, " + "ZD_VOLTAGE, ZD_POWER, ZD_ORDER_NO, ZD_BIG_BOX_NO, ZD_D_SN, ZD_DEV_EN_NO, ZD_RESERVE3, ZD_DEVICEID, ZD_MAC_START, ZD_MAC_END,zd_production_date, ZD_INDATE)" + "values(ZTEDATA_seq.nextval,'" + zteData.d_sn[i] + "','','BOX','','" + zteData.product_mode + "','" + zteData.product_name + "','" + zteData.material_code + "','" + zteData.batch_no + "','" + zteData.soft_version + "','" + zteData.hd_version + "'," + "'" + zteData.voltage + "','" + zteData.power + "','" + zteData.order_no + "','" + zteData.big_box_no + "','" + zteData.d_sn[i] + "'," + "'" + zteData.reserve3[i] + "','" + zteData.DeviceId[i] + "','" + zteData.mac_start[i] + "','" + zteData.mac_end[i] + "','" + zteData.production_date + "',sysdate)", "insert"); } Console.WriteLine(json); } //获取栈板信息 public static void GetPalletInfo(string iBox) { dynamic obj = new ExpandoObject(); obj.request = "pallet"; obj.packing_num = 20; obj.User = "mes"; obj.po_sn = "250908001001"; obj.password = "258456"; obj.tool_name = "zte"; var expandoDict = obj as IDictionary; DataTable dt = (DataTable)dh.ExecuteSql("select pa_outboxcode from package where pa_mothercode='" + iBox + "'", "select"); for (int i = 0; i < dt.Rows.Count; i++) { string enNoKey = $"big_box_no{i}"; string enNoValue = dt.Rows[i]["pa_outboxcode"].ToString(); expandoDict[enNoKey] = enNoValue; } string json = JsonConvert.SerializeObject(obj, Formatting.Indented); string returnstr = SendData(json); Console.WriteLine(json); } //获取SN的EN号,需要SN单个获取 public static void GetEN(string iSN) { dynamic obj = new ExpandoObject(); obj.tool_name = "zte"; obj.request = "Storage2"; obj.User = "mes"; obj.password = "258456"; obj.po_sn = ""; obj.wholeDeviceCode = ""; obj.reg_code = "6400-6134-3346-8166"; obj.prefix_en_no = "4N6FH4T"; obj.prefix_big_box = ""; obj.packing_num = ""; obj.isn = iSN; string json = JsonConvert.SerializeObject(obj, Formatting.Indented); string returnstr = SendData(json); Console.WriteLine(json); } //发送数据 public static string SendData(string json) { string serverIP = "192.168.1.160"; int port = 21610; try { // 创建TCP客户端并连接到服务器 TcpClient client = new TcpClient(); client.Connect(serverIP, port); // 获取网络流 NetworkStream stream = client.GetStream(); // 要发送的请求报文(根据实际需求修改) string requestMessage = "你的请求报文内容"; byte[] requestData = Encoding.UTF8.GetBytes(requestMessage); // 发送请求 stream.Write(requestData, 0, requestData.Length); Console.WriteLine($"已发送请求: {requestMessage}"); // 接收响应 byte[] buffer = new byte[1024]; int bytesRead = stream.Read(buffer, 0, buffer.Length); string responseMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead); Console.WriteLine($"收到响应: {responseMessage}"); // 关闭流和连接 stream.Close(); client.Close(); return responseMessage; } catch (SocketException ex) { Console.WriteLine($"网络错误: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"错误: {ex.Message}"); } return ""; } } }