HttpServer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Dynamic;
  5. using System.IO;
  6. using System.Net.Sockets;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using DevExpress.XtraSpellChecker;
  10. using Newtonsoft.Json;
  11. using UAS_MES_NEW.DataOperate;
  12. namespace UAS_MES_NEW.PublicMethod
  13. {
  14. public class BindItem
  15. {
  16. [JsonProperty("bindKey")]
  17. public string BindKey { get; set; }
  18. [JsonProperty("bindValue")]
  19. public string BindValue { get; set; }
  20. [JsonProperty("keyType")]
  21. public string KeyType { get; set; }
  22. [JsonProperty("keyValue")]
  23. public string KeyValue { get; set; }
  24. [JsonProperty("hashType")]
  25. public string HashType { get; set; }
  26. [JsonProperty("hashKey")]
  27. public string HashKey { get; set; }
  28. }
  29. public class RootObject
  30. {
  31. [JsonProperty("productModuleType")]
  32. public string ProductModuleType { get; set; }
  33. [JsonProperty("productMainClass")]
  34. public string ProductMainClass { get; set; }
  35. [JsonProperty("productMediumClass")]
  36. public string ProductMediumClass { get; set; }
  37. [JsonProperty("productSubClass")]
  38. public string ProductSubClass { get; set; }
  39. [JsonProperty("task")]
  40. public string Task { get; set; }
  41. [JsonProperty("processType")]
  42. public string ProcessType { get; set; }
  43. [JsonProperty("userId")]
  44. public string UserId { get; set; }
  45. [JsonProperty("bindList")]
  46. public List<BindItem> BindList { get; set; }
  47. }
  48. public class ZteData
  49. {
  50. public string tool_name { get; set; }
  51. public string response { get; set; }
  52. public int status { get; set; }
  53. public string product_name { get; set; }
  54. public string product_mode { get; set; }
  55. public string material_code { get; set; }
  56. public string batch_no { get; set; }
  57. public string production_date { get; set; }
  58. public string big_box_no { get; set; }
  59. public string box_no { get; set; }
  60. public string reserve10 { get; set; }
  61. public int packing_num { get; set; }
  62. public string soft_version { get; set; }
  63. public string hd_version { get; set; }
  64. public string voltage { get; set; }
  65. public string power { get; set; }
  66. public string order_no { get; set; }
  67. public string[] d_sn { get; set; }
  68. public string[] mac_start { get; set; }
  69. public string[] mac_end { get; set; }
  70. public string[] DeviceId { get; set; }
  71. public string[] dev_en_no { get; set; }
  72. public string[] reserve3 { get; set; }
  73. public string[] reserve4 { get; set; }
  74. }
  75. class HttpServer
  76. {
  77. static DataHelper dh = new DataHelper();
  78. static string ComputeSha256Hash(string input)
  79. {
  80. using (SHA256 sha256 = SHA256.Create())
  81. {
  82. byte[] bytes = Encoding.UTF8.GetBytes(input);
  83. byte[] hashBytes = sha256.ComputeHash(bytes);
  84. StringBuilder builder = new StringBuilder();
  85. for (int i = 0; i < hashBytes.Length; i++)
  86. {
  87. builder.Append(hashBytes[i].ToString("x2"));
  88. }
  89. return builder.ToString().ToUpper();
  90. }
  91. }
  92. public static void SendBoxData(string iBox)
  93. {
  94. string sn = dh.getFieldDataByCondition("packagedetail", "replace(wm_concat(pd_barcode),',','-')", "pd_outboxcode='" + iBox + "'").ToString();
  95. var root = new RootObject
  96. {
  97. ProductModuleType = "整机",
  98. ProductMainClass = "固网_CPE",
  99. ProductMediumClass = "CPE",
  100. ProductSubClass = "ZXSLC SC50L",
  101. Task = "2025070006",
  102. ProcessType = "生产过程",
  103. UserId = "邓帝森",
  104. BindList = new List<BindItem>()
  105. };
  106. root.BindList.Add(new BindItem
  107. {
  108. BindKey = "cartonSn",
  109. BindValue = iBox,
  110. KeyType = "macbox",
  111. KeyValue = sn,
  112. HashType = "SHA256",
  113. HashKey = ComputeSha256Hash(sn)
  114. });
  115. DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail pd_outboxcode='" + iBox + "'", "select");
  116. for (int i = 0; i < dt.Rows.Count; i++)
  117. {
  118. root.BindList.Add(new BindItem
  119. {
  120. BindKey = "mac",
  121. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  122. KeyType = "EN",
  123. KeyValue = "4Y12R9300001",
  124. HashType = "NON-HASH",
  125. HashKey = "4Y12R9300001"
  126. });
  127. root.BindList.Add(new BindItem
  128. {
  129. BindKey = "mac",
  130. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  131. KeyType = "电源条码",
  132. KeyValue = "1U1Y2505100013640",
  133. HashType = "NON-HASH",
  134. HashKey = "1U1Y2505100013640"
  135. });
  136. root.BindList.Add(new BindItem
  137. {
  138. BindKey = "mac",
  139. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  140. KeyType = "JY-License",
  141. KeyValue = "Qk1K4rmuowTDl+NEtSIeHx1td6Tzzm0wDrw/AMCvJ6mrXQ5wsSgoxia+T5Cj+uH4xrGUrjGrZvh/WUQN88iDrPO7kuTA0mvo/Ay+AsFJYnn00JQYQkN9ul+a+Qh0EPBqCVa4ikllHcD1Pqq4Eubs6XXNFhKOtK7F7FMotfGzzAVg7RhKVoVRVFcK6ubSVWXW+SnGx5muH+WZPaiHLjt+kxBR8tZJuBl1LQvMWP74JSWKkcFWwcWlARnCJePvRTvKTDd5e7q9rpBpAc7Z79XYQ6Fs4eXM44O6/hA88YW/BK0TZq3AaaMxH8BpJnwZt4lIyuqXispeaB2eieFBR0JwWg==",
  142. HashType = "SHA256",
  143. HashKey = ComputeSha256Hash(sn),
  144. }); ;
  145. }
  146. string json = JsonConvert.SerializeObject(root, Formatting.Indented);
  147. string returnstr = SendData(json);
  148. Console.WriteLine(json);
  149. }
  150. //获取SN的EN号,需要SN单个获取
  151. public static void GetEN(string iSN)
  152. {
  153. DataTable dt = (DataTable)dh.ExecuteSql("select pr_prefix from makeserial left join product on ms_prodcode=pr_code where ms_sncode ='"+iSN+"' order by ms_id desc", "select");
  154. string prefix = "";
  155. if (dt.Rows.Count > 0)
  156. {
  157. prefix = dt.Rows[0]["pr_prefix"].ToString();
  158. }
  159. dynamic obj = new ExpandoObject();
  160. obj.tool_name = "zte";
  161. obj.request = "Storage2";
  162. obj.User = "mes";
  163. obj.password = "258456";
  164. obj.po_sn = "";
  165. obj.wholeDeviceCode = "";
  166. obj.reg_code = "6400-6134-3346-8166";
  167. obj.prefix_en_no = "4N6FH4T";
  168. obj.prefix_big_box = "";
  169. obj.packing_num = "";
  170. obj.isn = iSN;
  171. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  172. string returnstr = SendData(json);
  173. Console.WriteLine(json);
  174. }
  175. //获取外箱号信息
  176. public static void GetOutBoxInfo(string iBox)
  177. {
  178. dynamic obj = new ExpandoObject();
  179. obj.request = "Q_wai_xiang";
  180. obj.packing_num = 20;
  181. obj.User = "mes";
  182. obj.po_sn = "250908001001";
  183. obj.password = "258456";
  184. obj.tool_name = "zte";
  185. obj.noType = 0;
  186. var expandoDict = obj as IDictionary<string, object>;
  187. DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail where pd_outboxcode='" + iBox + "'", "select");
  188. for (int i = 0; i < dt.Rows.Count; i++)
  189. {
  190. string enNoKey = $"en_no{i}";
  191. string enNoValue = dt.Rows[i]["pd_barcode"].ToString();
  192. expandoDict[enNoKey] = enNoValue;
  193. }
  194. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  195. string returnstr = SendData(json);
  196. ZteData zteData = JsonConvert.DeserializeObject<ZteData>(returnstr);
  197. //将返回的信息存入数据库
  198. for (int i = 0; i < zteData.d_sn.Length; i++)
  199. {
  200. 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, " +
  201. "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)" +
  202. "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 + "'," +
  203. "'" + zteData.voltage + "','" + zteData.power + "','" + zteData.order_no + "','" + zteData.big_box_no + "','" + zteData.d_sn[i] + "'," +
  204. "'" + zteData.reserve3[i] + "','" + zteData.DeviceId[i] + "','" + zteData.mac_start[i] + "','" + zteData.mac_end[i] + "','" + zteData.production_date + "',sysdate)", "insert");
  205. }
  206. Console.WriteLine(json);
  207. }
  208. //获取栈板信息
  209. public static void GetPalletInfo(string iBox)
  210. {
  211. dynamic obj = new ExpandoObject();
  212. obj.request = "pallet";
  213. obj.packing_num = 20;
  214. obj.User = "mes";
  215. obj.po_sn = "250908001001";
  216. obj.password = "258456";
  217. obj.tool_name = "zte";
  218. var expandoDict = obj as IDictionary<string, object>;
  219. DataTable dt = (DataTable)dh.ExecuteSql("select pa_outboxcode from package where pa_mothercode='" + iBox + "'", "select");
  220. for (int i = 0; i < dt.Rows.Count; i++)
  221. {
  222. string enNoKey = $"big_box_no{i}";
  223. string enNoValue = dt.Rows[i]["pa_outboxcode"].ToString();
  224. expandoDict[enNoKey] = enNoValue;
  225. }
  226. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  227. string returnstr = SendData(json);
  228. Console.WriteLine(json);
  229. }
  230. //发送数据
  231. public static string SendData(string json)
  232. {
  233. string serverIP = "192.168.1.160";
  234. int port = 21610;
  235. try
  236. {
  237. // 创建TCP客户端并连接到服务器
  238. TcpClient client = new TcpClient();
  239. client.Connect(serverIP, port);
  240. // 获取网络流
  241. NetworkStream stream = client.GetStream();
  242. // 要发送的请求报文(根据实际需求修改)
  243. byte[] packet = BuildPacket(json);
  244. // 发送请求
  245. stream.Write(packet, 0, packet.Length);
  246. Console.WriteLine($"已发送请求: {json}");
  247. // 接收响应
  248. byte[] buffer = new byte[1024];
  249. int bytesRead = stream.Read(buffer, 0, buffer.Length);
  250. string responseMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
  251. Console.WriteLine($"收到响应: {responseMessage}");
  252. // 关闭流和连接
  253. stream.Close();
  254. client.Close();
  255. return responseMessage;
  256. }
  257. catch (SocketException ex)
  258. {
  259. Console.WriteLine($"网络错误: {ex.Message}");
  260. }
  261. catch (Exception ex)
  262. {
  263. Console.WriteLine($"错误: {ex.Message}");
  264. }
  265. return "";
  266. }
  267. public static readonly uint PROTOCOL_FLAG = 0x4C4F4F54;
  268. public static byte[] BuildPacket(string jsonData)
  269. {
  270. if (string.IsNullOrEmpty(jsonData))
  271. {
  272. throw new ArgumentException("JSON数据不能为空");
  273. }
  274. using (MemoryStream ms = new MemoryStream())
  275. {
  276. // 1. 写入flag (小端序)
  277. byte[] flagBytes = BitConverter.GetBytes(PROTOCOL_FLAG);
  278. ms.Write(flagBytes, 0, 4);
  279. // 2. 将JSON字符串转换为UTF8字节数组
  280. byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonData);
  281. // 3. 写入data_len (小端序)
  282. byte[] lengthBytes = BitConverter.GetBytes(jsonBytes.Length);
  283. ms.Write(lengthBytes, 0, 4);
  284. // 4. 写入data
  285. ms.Write(jsonBytes, 0, jsonBytes.Length);
  286. return ms.ToArray();
  287. }
  288. }
  289. }
  290. }