HttpServer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. //获取外箱号信息
  149. public static void GetOutBoxInfo(string iBox)
  150. {
  151. dynamic obj = new ExpandoObject();
  152. obj.request = "Q_wai_xiang";
  153. obj.packing_num = 20;
  154. obj.User = "mes";
  155. obj.po_sn = "250908001001";
  156. obj.password = "258456";
  157. obj.tool_name = "zte";
  158. obj.noType = 0;
  159. var expandoDict = obj as IDictionary<string, object>;
  160. DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail where pd_outboxcode='" + iBox + "'", "select");
  161. for (int i = 0; i < dt.Rows.Count; i++)
  162. {
  163. string enNoKey = $"en_no{i}";
  164. string enNoValue = dt.Rows[i]["pd_barcode"].ToString();
  165. expandoDict[enNoKey] = enNoValue;
  166. }
  167. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  168. string returnstr = SendData(json);
  169. ZteData zteData = JsonConvert.DeserializeObject<ZteData>(returnstr);
  170. //将返回的信息存入数据库
  171. for (int i = 0; i < zteData.d_sn.Length; i++)
  172. {
  173. 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, " +
  174. "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)" +
  175. "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 + "'," +
  176. "'" + zteData.voltage + "','" + zteData.power + "','" + zteData.order_no + "','" + zteData.big_box_no + "','" + zteData.d_sn[i] + "'," +
  177. "'" + zteData.reserve3[i] + "','" + zteData.DeviceId[i] + "','" + zteData.mac_start[i] + "','" + zteData.mac_end[i] + "','" + zteData.production_date + "',sysdate)", "insert");
  178. }
  179. Console.WriteLine(json);
  180. }
  181. //获取栈板信息
  182. public static void GetPalletInfo(string iBox)
  183. {
  184. dynamic obj = new ExpandoObject();
  185. obj.request = "pallet";
  186. obj.packing_num = 20;
  187. obj.User = "mes";
  188. obj.po_sn = "250908001001";
  189. obj.password = "258456";
  190. obj.tool_name = "zte";
  191. var expandoDict = obj as IDictionary<string, object>;
  192. DataTable dt = (DataTable)dh.ExecuteSql("select pa_outboxcode from package where pa_mothercode='" + iBox + "'", "select");
  193. for (int i = 0; i < dt.Rows.Count; i++)
  194. {
  195. string enNoKey = $"big_box_no{i}";
  196. string enNoValue = dt.Rows[i]["pa_outboxcode"].ToString();
  197. expandoDict[enNoKey] = enNoValue;
  198. }
  199. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  200. string returnstr = SendData(json);
  201. Console.WriteLine(json);
  202. }
  203. //获取SN的EN号,需要SN单个获取
  204. public static void GetEN(string iSN)
  205. {
  206. dynamic obj = new ExpandoObject();
  207. obj.tool_name = "zte";
  208. obj.request = "Storage2";
  209. obj.User = "mes";
  210. obj.password = "258456";
  211. obj.po_sn = "";
  212. obj.wholeDeviceCode = "";
  213. obj.reg_code = "6400-6134-3346-8166";
  214. obj.prefix_en_no = "4N6FH4T";
  215. obj.prefix_big_box = "";
  216. obj.packing_num = "";
  217. obj.isn = iSN;
  218. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  219. string returnstr = SendData(json);
  220. Console.WriteLine(json);
  221. }
  222. //发送数据
  223. public static string SendData(string json)
  224. {
  225. string serverIP = "192.168.1.160";
  226. int port = 21610;
  227. try
  228. {
  229. // 创建TCP客户端并连接到服务器
  230. TcpClient client = new TcpClient();
  231. client.Connect(serverIP, port);
  232. // 获取网络流
  233. NetworkStream stream = client.GetStream();
  234. // 要发送的请求报文(根据实际需求修改)
  235. string requestMessage = "你的请求报文内容";
  236. byte[] requestData = Encoding.UTF8.GetBytes(requestMessage);
  237. // 发送请求
  238. stream.Write(requestData, 0, requestData.Length);
  239. Console.WriteLine($"已发送请求: {requestMessage}");
  240. // 接收响应
  241. byte[] buffer = new byte[1024];
  242. int bytesRead = stream.Read(buffer, 0, buffer.Length);
  243. string responseMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
  244. Console.WriteLine($"收到响应: {responseMessage}");
  245. // 关闭流和连接
  246. stream.Close();
  247. client.Close();
  248. return responseMessage;
  249. }
  250. catch (SocketException ex)
  251. {
  252. Console.WriteLine($"网络错误: {ex.Message}");
  253. }
  254. catch (Exception ex)
  255. {
  256. Console.WriteLine($"错误: {ex.Message}");
  257. }
  258. return "";
  259. }
  260. }
  261. }