HttpServer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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,ms_makecode 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. string makecode = "";
  142. if (dt.Rows.Count > 0)
  143. {
  144. pr_custmachinetype = dt.Rows[0]["pr_custmachinetype"].ToString();
  145. makecode = dt.Rows[0]["ms_makecode"].ToString();
  146. }
  147. var root = new RootObject
  148. {
  149. ProductModuleType = "整机",
  150. ProductMainClass = "固网_CPE",
  151. ProductMediumClass = "CPE",
  152. ProductSubClass = pr_custmachinetype,
  153. Task = makecode,
  154. ProcessType = "生产过程",
  155. UserId = User.UserName,
  156. BindList = new List<BindItem>()
  157. };
  158. root.BindList.Add(new BindItem
  159. {
  160. BindKey = "cartonSn",
  161. BindValue = iBox,
  162. KeyType = "macbox",
  163. KeyValue = sn,
  164. HashType = "SHA256",
  165. HashKey = ComputeSha256Hash(sn)
  166. });
  167. dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail pd_outboxcode='" + iBox + "'", "select");
  168. for (int i = 0; i < dt.Rows.Count; i++)
  169. {
  170. root.BindList.Add(new BindItem
  171. {
  172. BindKey = "mac",
  173. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  174. KeyType = "EN",
  175. KeyValue = "4Y12R9300001",
  176. HashType = "NON-HASH",
  177. HashKey = "4Y12R9300001"
  178. });
  179. root.BindList.Add(new BindItem
  180. {
  181. BindKey = "mac",
  182. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  183. KeyType = "电源条码",
  184. KeyValue = "1U1Y2505100013640",
  185. HashType = "NON-HASH",
  186. HashKey = "1U1Y2505100013640"
  187. });
  188. root.BindList.Add(new BindItem
  189. {
  190. BindKey = "mac",
  191. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  192. KeyType = "JY-License",
  193. KeyValue = "Qk1K4rmuowTDl+NEtSIeHx1td6Tzzm0wDrw/AMCvJ6mrXQ5wsSgoxia+T5Cj+uH4xrGUrjGrZvh/WUQN88iDrPO7kuTA0mvo/Ay+AsFJYnn00JQYQkN9ul+a+Qh0EPBqCVa4ikllHcD1Pqq4Eubs6XXNFhKOtK7F7FMotfGzzAVg7RhKVoVRVFcK6ubSVWXW+SnGx5muH+WZPaiHLjt+kxBR8tZJuBl1LQvMWP74JSWKkcFWwcWlARnCJePvRTvKTDd5e7q9rpBpAc7Z79XYQ6Fs4eXM44O6/hA88YW/BK0TZq3AaaMxH8BpJnwZt4lIyuqXispeaB2eieFBR0JwWg==",
  194. HashType = "SHA256",
  195. HashKey = ComputeSha256Hash(sn),
  196. }); ;
  197. }
  198. string json = JsonConvert.SerializeObject(root, Formatting.Indented);
  199. string returnstr = SendData(json);
  200. Console.WriteLine(json);
  201. }
  202. //获取外箱号信息
  203. public static void GetOutBoxInfo(string iBox)
  204. {
  205. dynamic obj = new ExpandoObject();
  206. var expandoDict = obj as IDictionary<string, object>;
  207. DataTable dt = (DataTable)dh.ExecuteSql("select pd_barcode,pd_makecode from packagedetail where pd_outboxcode='" + iBox + "'", "select");
  208. string makecode = "";
  209. for (int i = 0; i < dt.Rows.Count; i++)
  210. {
  211. string enNoKey = $"en_no{i}";
  212. string enNoValue = dt.Rows[i]["pd_barcode"].ToString();
  213. expandoDict[enNoKey] = enNoValue;
  214. makecode = dt.Rows[i]["pd_makecode"].ToString();
  215. }
  216. obj.request = "Q_wai_xiang";
  217. obj.packing_num = 20;
  218. obj.User = "mes";
  219. obj.po_sn = makecode;
  220. obj.password = "258456";
  221. obj.tool_name = "zte";
  222. obj.noType = 0;
  223. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  224. string returnstr = SendData(json);
  225. ZteData zteData = JsonConvert.DeserializeObject<ZteData>(returnstr);
  226. //将返回的信息存入数据库
  227. for (int i = 0; i < zteData.d_sn.Length; i++)
  228. {
  229. 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, " +
  230. "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)" +
  231. "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 + "'," +
  232. "'" + zteData.voltage + "','" + zteData.power + "','" + zteData.order_no + "','" + zteData.big_box_no + "','" + zteData.d_sn[i] + "'," +
  233. "'" + zteData.reserve3[i] + "','" + zteData.DeviceId[i] + "','" + zteData.mac_start[i] + "','" + zteData.mac_end[i] + "','" + zteData.production_date + "',sysdate)", "insert");
  234. }
  235. Console.WriteLine(json);
  236. }
  237. //获取栈板信息
  238. public static void GetPalletInfo(string iBox)
  239. {
  240. DataTable dt = (DataTable)dh.ExecuteSql("select pa_outboxcode,pa_makecode from package where pa_mothercode='" + iBox + "'", "select");
  241. dynamic obj = new ExpandoObject();
  242. var expandoDict = obj as IDictionary<string, object>;
  243. string makecode = "";
  244. for (int i = 0; i < dt.Rows.Count; i++)
  245. {
  246. string enNoKey = $"big_box_no{i}";
  247. string enNoValue = dt.Rows[i]["pa_outboxcode"].ToString();
  248. expandoDict[enNoKey] = enNoValue;
  249. makecode = dt.Rows[i]["pa_makecode"].ToString();
  250. }
  251. obj.request = "pallet";
  252. obj.packing_num = dt.Rows.Count;
  253. obj.User = "mes";
  254. obj.po_sn = makecode;
  255. obj.password = "258456";
  256. obj.tool_name = "zte";
  257. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  258. string returnstr = SendData(json);
  259. Console.WriteLine(returnstr);
  260. Console.WriteLine(json);
  261. }
  262. //发送数据
  263. public static string SendData(string json)
  264. {
  265. string serverIP = "192.168.1.160";
  266. int port = 21610;
  267. try
  268. {
  269. // 创建TCP客户端并连接到服务器
  270. TcpClient client = new TcpClient();
  271. client.Connect(serverIP, port);
  272. // 获取网络流
  273. NetworkStream stream = client.GetStream();
  274. // 要发送的请求报文(根据实际需求修改)
  275. byte[] packet = BuildPacket(json);
  276. // 发送请求
  277. stream.Write(packet, 0, packet.Length);
  278. Console.WriteLine($"已发送请求: {json}");
  279. // 接收响应
  280. byte[] buffer = new byte[1024];
  281. int bytesRead = stream.Read(buffer, 0, buffer.Length);
  282. string responseMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
  283. Console.WriteLine($"收到响应: {responseMessage}");
  284. // 关闭流和连接
  285. stream.Close();
  286. client.Close();
  287. return responseMessage;
  288. }
  289. catch (SocketException ex)
  290. {
  291. Console.WriteLine($"网络错误: {ex.Message}");
  292. }
  293. catch (Exception ex)
  294. {
  295. Console.WriteLine($"错误: {ex.Message}");
  296. }
  297. return "";
  298. }
  299. public static readonly uint PROTOCOL_FLAG = 0x4C4F4F54;
  300. public static byte[] BuildPacket(string jsonData)
  301. {
  302. if (string.IsNullOrEmpty(jsonData))
  303. {
  304. throw new ArgumentException("JSON数据不能为空");
  305. }
  306. using (MemoryStream ms = new MemoryStream())
  307. {
  308. // 1. 写入flag (小端序)
  309. byte[] flagBytes = BitConverter.GetBytes(PROTOCOL_FLAG);
  310. ms.Write(flagBytes, 0, 4);
  311. // 2. 将JSON字符串转换为UTF8字节数组
  312. byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonData);
  313. // 3. 写入data_len (小端序)
  314. byte[] lengthBytes = BitConverter.GetBytes(jsonBytes.Length);
  315. ms.Write(lengthBytes, 0, 4);
  316. // 4. 写入data
  317. ms.Write(jsonBytes, 0, jsonBytes.Length);
  318. return ms.ToArray();
  319. }
  320. }
  321. }
  322. }