HttpServer.cs 15 KB

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