HttpServer.cs 10 KB

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