HttpServer.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Dynamic;
  5. using System.Net.Sockets;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using HslCommunication;
  9. using System.Threading;
  10. using Newtonsoft.Json;
  11. using UAS_MES_NEW.DataOperate;
  12. using System.IO;
  13. namespace UAS_MES_NEW.PublicMethod
  14. {
  15. public class BindItem
  16. {
  17. [JsonProperty("bindKey")]
  18. public string BindKey { get; set; }
  19. [JsonProperty("bindValue")]
  20. public string BindValue { get; set; }
  21. [JsonProperty("keyType")]
  22. public string KeyType { get; set; }
  23. [JsonProperty("keyValue")]
  24. public string KeyValue { get; set; }
  25. [JsonProperty("hashType")]
  26. public string HashType { get; set; }
  27. [JsonProperty("hashKey")]
  28. public string HashKey { get; set; }
  29. }
  30. public class RootObject
  31. {
  32. [JsonProperty("productModuleType")]
  33. public string ProductModuleType { get; set; }
  34. [JsonProperty("productMainClass")]
  35. public string ProductMainClass { get; set; }
  36. [JsonProperty("productMediumClass")]
  37. public string ProductMediumClass { get; set; }
  38. [JsonProperty("productSubClass")]
  39. public string ProductSubClass { get; set; }
  40. [JsonProperty("task")]
  41. public string Task { get; set; }
  42. [JsonProperty("processType")]
  43. public string ProcessType { get; set; }
  44. [JsonProperty("userId")]
  45. public string UserId { get; set; }
  46. [JsonProperty("bindList")]
  47. public List<BindItem> BindList { get; set; }
  48. }
  49. class HttpServer
  50. {
  51. static DataHelper dh = new DataHelper();
  52. static string ComputeSha256Hash(string input)
  53. {
  54. using (SHA256 sha256 = SHA256.Create())
  55. {
  56. byte[] bytes = Encoding.UTF8.GetBytes(input);
  57. byte[] hashBytes = sha256.ComputeHash(bytes);
  58. StringBuilder builder = new StringBuilder();
  59. for (int i = 0; i < hashBytes.Length; i++)
  60. {
  61. builder.Append(hashBytes[i].ToString("x2"));
  62. }
  63. return builder.ToString().ToUpper();
  64. }
  65. }
  66. public static void SendBoxData(string iBox)
  67. {
  68. string sn = dh.getFieldDataByCondition("packagedetail", "replace(wm_concat(pd_barcode),',','-')", "pd_outboxcode='" + iBox + "'").ToString();
  69. var root = new RootObject
  70. {
  71. ProductModuleType = "整机",
  72. ProductMainClass = "固网_CPE",
  73. ProductMediumClass = "CPE",
  74. ProductSubClass = "ZXSLC SC50L",
  75. Task = "2025070006",
  76. ProcessType = "生产过程",
  77. UserId = "邓帝森",
  78. BindList = new List<BindItem>()
  79. };
  80. root.BindList.Add(new BindItem
  81. {
  82. BindKey = "cartonSn",
  83. BindValue = iBox,
  84. KeyType = "macbox",
  85. KeyValue = sn,
  86. HashType = "SHA256",
  87. HashKey = ComputeSha256Hash(sn)
  88. });
  89. DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail pd_outboxcode='" + iBox + "'", "select");
  90. for (int i = 0; i < dt.Rows.Count; i++)
  91. {
  92. root.BindList.Add(new BindItem
  93. {
  94. BindKey = "mac",
  95. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  96. KeyType = "EN",
  97. KeyValue = "4Y12R9300001",
  98. HashType = "NON-HASH",
  99. HashKey = "4Y12R9300001"
  100. });
  101. root.BindList.Add(new BindItem
  102. {
  103. BindKey = "mac",
  104. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  105. KeyType = "电源条码",
  106. KeyValue = "1U1Y2505100013640",
  107. HashType = "NON-HASH",
  108. HashKey = "1U1Y2505100013640"
  109. });
  110. root.BindList.Add(new BindItem
  111. {
  112. BindKey = "mac",
  113. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  114. KeyType = "JY-License",
  115. KeyValue = "Qk1K4rmuowTDl+NEtSIeHx1td6Tzzm0wDrw/AMCvJ6mrXQ5wsSgoxia+T5Cj+uH4xrGUrjGrZvh/WUQN88iDrPO7kuTA0mvo/Ay+AsFJYnn00JQYQkN9ul+a+Qh0EPBqCVa4ikllHcD1Pqq4Eubs6XXNFhKOtK7F7FMotfGzzAVg7RhKVoVRVFcK6ubSVWXW+SnGx5muH+WZPaiHLjt+kxBR8tZJuBl1LQvMWP74JSWKkcFWwcWlARnCJePvRTvKTDd5e7q9rpBpAc7Z79XYQ6Fs4eXM44O6/hA88YW/BK0TZq3AaaMxH8BpJnwZt4lIyuqXispeaB2eieFBR0JwWg==",
  116. HashType = "SHA256",
  117. HashKey = ComputeSha256Hash(sn),
  118. }); ;
  119. }
  120. string json = JsonConvert.SerializeObject(root, Formatting.Indented);
  121. Console.WriteLine(json);
  122. }
  123. public static void GetOutBoxInfo(string iBox)
  124. {
  125. dynamic obj = new ExpandoObject();
  126. obj.request = "Q_wai_xiang";
  127. obj.packing_num = 20;
  128. obj.User = "mes";
  129. obj.po_sn = "250908001001";
  130. obj.password = "258456";
  131. obj.tool_name = "zte";
  132. obj.noType = 0;
  133. var expandoDict = obj as IDictionary<string, object>;
  134. DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail where pd_outboxcode='" + iBox + "'", "select");
  135. for (int i = 0; i < dt.Rows.Count; i++)
  136. {
  137. string enNoKey = $"en_no{i}";
  138. string enNoValue = dt.Rows[i]["pd_barcode"].ToString();
  139. expandoDict[enNoKey] = enNoValue;
  140. }
  141. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  142. Console.WriteLine(json);
  143. }
  144. public static void GetPalletInfo(string iBox)
  145. {
  146. dynamic obj = new ExpandoObject();
  147. obj.request = "pallet";
  148. obj.packing_num = 20;
  149. obj.User = "mes";
  150. obj.po_sn = "250908001001";
  151. obj.password = "258456";
  152. obj.tool_name = "zte";
  153. var expandoDict = obj as IDictionary<string, object>;
  154. DataTable dt = (DataTable)dh.ExecuteSql("select pa_outboxcode from package where pa_mothercode='" + iBox + "'", "select");
  155. for (int i = 0; i < dt.Rows.Count; i++)
  156. {
  157. string enNoKey = $"big_box_no{i}";
  158. string enNoValue = dt.Rows[i]["pa_outboxcode"].ToString();
  159. expandoDict[enNoKey] = enNoValue;
  160. }
  161. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  162. Console.WriteLine(json);
  163. }
  164. public static void GetEN(string iSN)
  165. {
  166. dynamic obj = new ExpandoObject();
  167. obj.tool_name = "zte";
  168. obj.request = "Storage2";
  169. obj.User = "mes";
  170. obj.password = "258456";
  171. obj.po_sn = "";
  172. obj.wholeDeviceCode = "";
  173. obj.reg_code = "6400-6134-3346-8166";
  174. obj.prefix_en_no = "4N6FH4T";
  175. obj.prefix_big_box = "";
  176. obj.packing_num = "";
  177. obj.isn = iSN;
  178. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  179. Console.WriteLine(json);
  180. }
  181. public static void SendData(string json)
  182. {
  183. string serverIP = "192.168.1.160";
  184. int port = 21610;
  185. try
  186. {
  187. // 创建TCP客户端并连接到服务器
  188. TcpClient client = new TcpClient();
  189. client.Connect(serverIP, port);
  190. // 获取网络流
  191. NetworkStream stream = client.GetStream();
  192. // 要发送的请求报文(根据实际需求修改)
  193. string requestMessage = "你的请求报文内容";
  194. byte[] requestData = Encoding.UTF8.GetBytes(requestMessage);
  195. // 发送请求
  196. stream.Write(requestData, 0, requestData.Length);
  197. Console.WriteLine($"已发送请求: {requestMessage}");
  198. // 接收响应
  199. byte[] buffer = new byte[1024];
  200. int bytesRead = stream.Read(buffer, 0, buffer.Length);
  201. string responseMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
  202. Console.WriteLine($"收到响应: {responseMessage}");
  203. // 关闭流和连接
  204. stream.Close();
  205. client.Close();
  206. }
  207. catch (SocketException ex)
  208. {
  209. Console.WriteLine($"网络错误: {ex.Message}");
  210. }
  211. catch (Exception ex)
  212. {
  213. Console.WriteLine($"错误: {ex.Message}");
  214. }
  215. }
  216. }
  217. }