| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Dynamic;
- using System.IO;
- using System.Net.Sockets;
- using System.Security.Cryptography;
- using System.Text;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using UAS_MES_NEW.DataOperate;
- using UAS_MES_NEW.Entity;
- 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<BindItem> 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();
- }
- }
- //获取SN的EN号,彩盒标机身标一个工站一起打印,需要SN单个获取
- public static void GetEN(string iSN)
- {
- DataTable dt = (DataTable)dh.ExecuteSql("select ms_makecode,pr_prefix,pr_prebigxbox,pr_regcode from makeserial left join product on ms_prodcode=pr_code where ms_sncode ='" + iSN + "' order by ms_id desc", "select");
- string prefix = "";
- string regcode = "";
- string makecode = "";
- if (dt.Rows.Count > 0)
- {
- prefix = dt.Rows[0]["pr_prefix"].ToString();
- regcode = dt.Rows[0]["pr_regcode"].ToString();
- makecode = dt.Rows[0]["ms_makecode"].ToString();
- }
- dynamic obj = new ExpandoObject();
- obj.tool_name = "zte";
- obj.request = "Storage2";
- obj.User = "mes";
- obj.password = "258456";
- obj.po_sn = iSN;
- obj.wholeDeviceCode = "";
- obj.reg_code = regcode;
- obj.prefix_en_no = prefix;
- obj.prefix_big_box = "";
- obj.packing_num = "";
- obj.isn = prefix;
- string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
- string returnstr = SendData(json);
- JObject deviceObj = JObject.Parse(returnstr);
- string WholeDeviceCode = deviceObj["WholeDeviceCode"].ToString();
- string SN = deviceObj["SN"].ToString();
- string dev_en_no = deviceObj["dev_en_no"].ToString();
- string mac = deviceObj["mac"].ToString();
- string mac_start = deviceObj["mac_start"].ToString();
- string mac_end = deviceObj["mac_end"].ToString();
- string reg_code = deviceObj["reg_code"].ToString();
- string reserve3 = deviceObj["reserve3"].ToString();
- 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");
- Console.WriteLine(json);
- }
- //发送外箱号数据,打印前获取
- public static void SendBoxData(string iBox)
- {
- string sn = dh.getFieldDataByCondition("packagedetail", "replace(wm_concat(pd_barcode),',','-')", "pd_outboxcode='" + iBox + "'").ToString();
- DataTable dt = (DataTable)dh.ExecuteSql("select pr_custmachinetype,ms_makecode from makeserial left join product on ms_prodcode=pr_code where ms_outboxcode ='" + iBox + "' order by ms_id desc", "select");
- string pr_custmachinetype = "";
- string makecode = "";
- if (dt.Rows.Count > 0)
- {
- pr_custmachinetype = dt.Rows[0]["pr_custmachinetype"].ToString();
- makecode = dt.Rows[0]["ms_makecode"].ToString();
- }
- var root = new RootObject
- {
- ProductModuleType = "整机",
- ProductMainClass = "固网_CPE",
- ProductMediumClass = "CPE",
- ProductSubClass = pr_custmachinetype,
- Task = makecode,
- ProcessType = "生产过程",
- UserId = User.UserName,
- BindList = new List<BindItem>()
- };
- root.BindList.Add(new BindItem
- {
- BindKey = "cartonSn",
- BindValue = iBox,
- KeyType = "macbox",
- KeyValue = sn,
- HashType = "SHA256",
- HashKey = ComputeSha256Hash(sn)
- });
- 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();
- var expandoDict = obj as IDictionary<string, object>;
- DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode,pd_makecode from packagedetail where pd_outboxcode='" + iBox + "'", "select");
- string makecode = "";
- 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;
- makecode = dt.Rows[i]["pd_makecode"].ToString();
- }
- obj.request = "Q_wai_xiang";
- obj.packing_num = 20;
- obj.User = "mes";
- obj.po_sn = makecode;
- obj.password = "258456";
- obj.tool_name = "zte";
- obj.noType = 0;
- string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
- string returnstr = SendData(json);
- ZteData zteData = JsonConvert.DeserializeObject<ZteData>(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)
- {
- DataTable dt = (DataTable)dh.ExecuteSql("select pa_outboxcode,pa_makecode from package where pa_mothercode='" + iBox + "'", "select");
- dynamic obj = new ExpandoObject();
- var expandoDict = obj as IDictionary<string, object>;
- string makecode = "";
- 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;
- makecode = dt.Rows[i]["pa_makecode"].ToString();
- }
- obj.request = "pallet";
- obj.packing_num = dt.Rows.Count;
- obj.User = "mes";
- obj.po_sn = makecode;
- obj.password = "258456";
- obj.tool_name = "zte";
- string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
- string returnstr = SendData(json);
- Console.WriteLine(returnstr);
- 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();
- // 要发送的请求报文(根据实际需求修改)
- byte[] packet = BuildPacket(json);
- // 发送请求
- 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);
- Console.WriteLine($"收到响应: {responseMessage}");
- // 关闭流和连接
- stream.Close();
- client.Close();
- return responseMessage;
- }
- catch (SocketException ex)
- {
- Console.WriteLine($"网络错误: {ex.Message}");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"错误: {ex.Message}");
- }
- return "";
- }
- public static readonly uint PROTOCOL_FLAG = 0x4C4F4F54;
- public static byte[] BuildPacket(string jsonData)
- {
- if (string.IsNullOrEmpty(jsonData))
- {
- throw new ArgumentException("JSON数据不能为空");
- }
- using (MemoryStream ms = new MemoryStream())
- {
- // 1. 写入flag (小端序)
- byte[] flagBytes = BitConverter.GetBytes(PROTOCOL_FLAG);
- ms.Write(flagBytes, 0, 4);
- // 2. 将JSON字符串转换为UTF8字节数组
- byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonData);
- // 3. 写入data_len (小端序)
- byte[] lengthBytes = BitConverter.GetBytes(jsonBytes.Length);
- ms.Write(lengthBytes, 0, 4);
- // 4. 写入data
- ms.Write(jsonBytes, 0, jsonBytes.Length);
- return ms.ToArray();
- }
- }
- }
- }
|