HttpServer.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Dynamic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Net.Sockets;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using Newtonsoft.Json;
  13. using Newtonsoft.Json.Linq;
  14. using UAS_MES_NEW.DataOperate;
  15. using UAS_MES_NEW.Entity;
  16. namespace UAS_MES_NEW.PublicMethod
  17. {
  18. public class BindItem
  19. {
  20. [JsonProperty("bindKey")]
  21. public string BindKey { get; set; }
  22. [JsonProperty("bindValue")]
  23. public string BindValue { get; set; }
  24. [JsonProperty("keyType")]
  25. public string KeyType { get; set; }
  26. [JsonProperty("keyValue")]
  27. public string KeyValue { get; set; }
  28. [JsonProperty("hashType")]
  29. public string HashType { get; set; }
  30. [JsonProperty("hashKey")]
  31. public string HashKey { get; set; }
  32. }
  33. public class RootObject
  34. {
  35. [JsonProperty("productModuleType")]
  36. public string ProductModuleType { get; set; }
  37. [JsonProperty("productMainClass")]
  38. public string ProductMainClass { get; set; }
  39. [JsonProperty("productMediumClass")]
  40. public string ProductMediumClass { get; set; }
  41. [JsonProperty("productSubClass")]
  42. public string ProductSubClass { get; set; }
  43. [JsonProperty("task")]
  44. public string Task { get; set; }
  45. [JsonProperty("processType")]
  46. public string ProcessType { get; set; }
  47. [JsonProperty("userId")]
  48. public string UserId { get; set; }
  49. [JsonProperty("bindList")]
  50. public List<BindItem> BindList { get; set; }
  51. }
  52. public class DeviceInfo
  53. {
  54. [JsonProperty("tool_name")]
  55. public string ToolName { get; set; }
  56. [JsonProperty("response")]
  57. public string Response { get; set; }
  58. [JsonProperty("status")]
  59. public int Status { get; set; }
  60. [JsonProperty("product_name")]
  61. public string ProductName { get; set; }
  62. [JsonProperty("product_mode")]
  63. public string ProductMode { get; set; }
  64. [JsonProperty("material_code")]
  65. public string MaterialCode { get; set; }
  66. [JsonProperty("batch_no")]
  67. public string BatchNo { get; set; }
  68. [JsonProperty("production_date")]
  69. public string ProductionDate { get; set; }
  70. [JsonProperty("big_box_no")]
  71. public string BigBoxNo { get; set; }
  72. [JsonProperty("box_no")]
  73. public string BoxNo { get; set; }
  74. [JsonProperty("reserve10")]
  75. public string Reserve10 { get; set; }
  76. [JsonProperty("packing_num")]
  77. public int PackingNum { get; set; }
  78. [JsonProperty("soft_version")]
  79. public string SoftVersion { get; set; }
  80. [JsonProperty("hd_version")]
  81. public string HdVersion { get; set; }
  82. [JsonProperty("voltage")]
  83. public string Voltage { get; set; }
  84. [JsonProperty("power")]
  85. public string Power { get; set; }
  86. [JsonProperty("order_no")]
  87. public string OrderNo { get; set; }
  88. // 使用字典或列表来存储序列化的数组数据
  89. public List<string> DSN { get; set; } = new List<string>();
  90. public List<string> MacStart { get; set; } = new List<string>();
  91. public List<string> MacEnd { get; set; } = new List<string>();
  92. public List<string> DeviceId { get; set; } = new List<string>();
  93. public List<string> DevEnNo { get; set; } = new List<string>();
  94. public List<string> Reserve3 { get; set; } = new List<string>();
  95. public List<string> Reserve4 { get; set; } = new List<string>();
  96. }
  97. public class DeviceInfoConverter : JsonConverter<DeviceInfo>
  98. {
  99. public override DeviceInfo ReadJson(JsonReader reader, System.Type objectType, DeviceInfo existingValue, bool hasExistingValue, JsonSerializer serializer)
  100. {
  101. JObject jsonObject = JObject.Load(reader);
  102. DeviceInfo deviceInfo = new DeviceInfo();
  103. // 解析基本字段
  104. serializer.Populate(jsonObject.CreateReader(), deviceInfo);
  105. // 解析数组字段
  106. ParseArrayFields(jsonObject, deviceInfo);
  107. return deviceInfo;
  108. }
  109. public override void WriteJson(JsonWriter writer, DeviceInfo value, JsonSerializer serializer)
  110. {
  111. throw new NotImplementedException();
  112. }
  113. private void ParseArrayFields(JObject jsonObject, DeviceInfo deviceInfo)
  114. {
  115. int index = 0;
  116. // 解析 d_sn 数组
  117. while (true)
  118. {
  119. string key = $"d_sn{index}";
  120. if (jsonObject.TryGetValue(key, out JToken value))
  121. {
  122. deviceInfo.DSN.Add(value.ToString());
  123. index++;
  124. }
  125. else
  126. {
  127. break;
  128. }
  129. }
  130. // 解析 mac_start 数组
  131. index = 0;
  132. while (true)
  133. {
  134. string key = $"mac_start{index}";
  135. if (jsonObject.TryGetValue(key, out JToken value))
  136. {
  137. deviceInfo.MacStart.Add(value.ToString());
  138. index++;
  139. }
  140. else
  141. {
  142. break;
  143. }
  144. }
  145. // 解析 mac_end 数组
  146. index = 0;
  147. while (true)
  148. {
  149. string key = $"mac_end{index}";
  150. if (jsonObject.TryGetValue(key, out JToken value))
  151. {
  152. deviceInfo.MacEnd.Add(value.ToString());
  153. index++;
  154. }
  155. else
  156. {
  157. break;
  158. }
  159. }
  160. // 解析 dev_en_no 数组
  161. index = 0;
  162. while (true)
  163. {
  164. string key = $"dev_en_no{index}";
  165. if (jsonObject.TryGetValue(key, out JToken value))
  166. {
  167. deviceInfo.DevEnNo.Add(value.ToString());
  168. index++;
  169. }
  170. else
  171. {
  172. break;
  173. }
  174. }
  175. // 解析 reserve3 数组
  176. index = 0;
  177. while (true)
  178. {
  179. string key = $"reserve3_{index}";
  180. if (jsonObject.TryGetValue(key, out JToken value))
  181. {
  182. deviceInfo.Reserve3.Add(value.ToString());
  183. index++;
  184. }
  185. else
  186. {
  187. break;
  188. }
  189. }
  190. // 解析 reserve4 数组
  191. index = 0;
  192. while (true)
  193. {
  194. string key = $"reserve4_{index}";
  195. if (jsonObject.TryGetValue(key, out JToken value))
  196. {
  197. deviceInfo.Reserve4.Add(value.ToString());
  198. index++;
  199. }
  200. else
  201. {
  202. break;
  203. }
  204. }
  205. // 解析 DeviceId 数组
  206. index = 0;
  207. while (true)
  208. {
  209. string key = $"DeviceId{index}";
  210. if (jsonObject.TryGetValue(key, out JToken value))
  211. {
  212. deviceInfo.DeviceId.Add(value.ToString());
  213. index++;
  214. }
  215. else
  216. {
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. public class HttpServer
  223. {
  224. static DataHelper dh = new DataHelper();
  225. public static int SN_SERVICE_PORT = 21610;
  226. public static int MAX_BUF_SIZE = 8192;
  227. static string ComputeSha256Hash(string input)
  228. {
  229. using (SHA256 sha256 = SHA256.Create())
  230. {
  231. byte[] bytes = Encoding.UTF8.GetBytes(input);
  232. byte[] hashBytes = sha256.ComputeHash(bytes);
  233. StringBuilder builder = new StringBuilder();
  234. for (int i = 0; i < hashBytes.Length; i++)
  235. {
  236. builder.Append(hashBytes[i].ToString("x2"));
  237. }
  238. return builder.ToString().ToUpper();
  239. }
  240. }
  241. //获取SN的EN号,彩盒标机身标一个工站一起打印,需要SN单个获取
  242. public static string GetEN(string iSN)
  243. {
  244. DataTable dt = (DataTable)dh.ExecuteSql("select ms_id,ma_custpo,ms_makecode,pr_prefix,PREFIX_EN_NO,PREFIX_BIG_BOX,pr_prebigxbox,pr_regcode from makeserial left join product on ms_prodcode=pr_code left join make on ma_code=ms_makecode left join ZTE_ORDER on ma_custpo=po_sn where ms_sncode ='" + iSN + "' or ms_firstsn='" + iSN + "' order by ms_id desc", "select");
  245. string prefix = "";
  246. string regcode = "";
  247. string makecode = "";
  248. string prebigxbox = "";
  249. string po = "";
  250. string ms_id = "";
  251. if (dt.Rows.Count > 0)
  252. {
  253. prefix = dt.Rows[0]["PREFIX_EN_NO"].ToString();
  254. regcode = dt.Rows[0]["pr_regcode"].ToString();
  255. makecode = dt.Rows[0]["ms_makecode"].ToString();
  256. prebigxbox = dt.Rows[0]["PREFIX_BIG_BOX"].ToString();
  257. po = dt.Rows[0]["ma_custpo"].ToString();
  258. ms_id = dt.Rows[0]["ms_id"].ToString();
  259. }
  260. dynamic obj = new ExpandoObject();
  261. obj.tool_name = "zte";
  262. obj.request = "Storage2";
  263. obj.User = "mes";
  264. obj.password = "258456";
  265. obj.po_sn = po;
  266. obj.wholeDeviceCode = iSN;
  267. obj.reg_code = regcode;
  268. obj.prefix_en_no = prefix;
  269. obj.prefix_big_box = prebigxbox;
  270. obj.packing_num = 6;
  271. obj.isn = iSN;
  272. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  273. string returnstr = ToServerReq(json);
  274. dh.ExecuteSql("insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE)values(EISDATA_seq.nextval,'" + json + "',sysdate,'GETEN')", "insert");
  275. dh.ExecuteSql("insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE)values(EISDATA_seq.nextval,'" + returnstr + "',sysdate,'GETEN')", "insert");
  276. JObject deviceObj = JObject.Parse(returnstr);
  277. string WholeDeviceCode = deviceObj["WholeDeviceCode"].ToString();
  278. string SN = deviceObj["SN"].ToString();
  279. string dev_en_no = deviceObj["dev_en_no"].ToString();
  280. string mac = deviceObj["mac"].ToString();
  281. string mac_start = deviceObj["mac_start"].ToString();
  282. string mac_end = deviceObj["mac_end"].ToString();
  283. string reg_code = deviceObj["reg_code"].ToString();
  284. string reserve3 = deviceObj["reserve3"].ToString();
  285. string status = deviceObj["status"].ToString();
  286. string device_type = deviceObj["device_type"].ToString();
  287. string en_no = deviceObj["en_no"].ToString();
  288. if (dh.CheckExist("ZTEDATA", "zd_sn='" + iSN + "' and zd_makecode='" + makecode + "' and zd_type='彩盒机身标'"))
  289. {
  290. dh.ExecuteSql("delete from ZTEDATA where zd_sn='" + iSN + "' and zd_makecode='" + makecode + "' and zd_type='彩盒机身标'", "delete");
  291. }
  292. dh.ExecuteSql("update makeserial set ms_sncode='" + dev_en_no + "' where ms_id='" + ms_id + "'", "update");
  293. 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)" +
  294. "values(ZTEDATA_seq.nextval,'" + SN + "','" + WholeDeviceCode + "','" + dev_en_no + "','" + iSN + "','" + makecode + "','彩盒机身标','','" + mac + "','" + mac_start + "','" + mac_end + "','" + reserve3 + "','" + en_no + "','" + device_type + "','" + reg_code + "')", "insert");
  295. return dev_en_no;
  296. }
  297. private string HandleWebException(WebException ex)
  298. {
  299. if (ex.Response != null)
  300. {
  301. using (HttpWebResponse response = (HttpWebResponse)ex.Response)
  302. using (Stream stream = response.GetResponseStream())
  303. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  304. {
  305. string errorResponse = reader.ReadToEnd();
  306. return errorResponse;
  307. }
  308. }
  309. else
  310. {
  311. string errorMsg = $"网络错误: {ex.Message}";
  312. return errorMsg;
  313. }
  314. }
  315. //发送外箱号数据,打印前获取
  316. public static async void SendBoxData(string iBox)
  317. {
  318. string sn = dh.getFieldDataByCondition("packagedetail", "replace(wm_concat(pd_barcode),',','-')", "pd_outboxcode='" + iBox + "'").ToString();
  319. DataTable dt = (DataTable)dh.ExecuteSql("select pr_code,pr_custmachinetype,ms_makecode from makeserial left join product on ms_prodcode=pr_code where ms_outboxcode ='" + iBox + "' order by ms_id desc", "select");
  320. string pr_custmachinetype = "";
  321. string makecode = "";
  322. string pr_code = "";
  323. if (dt.Rows.Count > 0)
  324. {
  325. pr_custmachinetype = dt.Rows[0]["pr_custmachinetype"].ToString();
  326. makecode = dt.Rows[0]["ms_makecode"].ToString();
  327. pr_code = dt.Rows[0]["pr_code"].ToString();
  328. }
  329. var root = new RootObject
  330. {
  331. ProductModuleType = "整机",
  332. ProductMainClass = "固网_CPE",
  333. ProductMediumClass = "CPE",
  334. ProductSubClass = pr_custmachinetype,
  335. Task = makecode,
  336. ProcessType = "生产过程",
  337. UserId = User.UserName,
  338. BindList = new List<BindItem>()
  339. };
  340. root.BindList.Add(new BindItem
  341. {
  342. BindKey = "cartonSn",
  343. BindValue = iBox,
  344. KeyType = "macbox",
  345. KeyValue = sn,
  346. HashType = "SHA256",
  347. HashKey = ComputeSha256Hash(sn)
  348. });
  349. dt = (DataTable)dh.ExecuteSql("select pd_barcode from packagedetail where pd_outboxcode='" + iBox + "'", "select");
  350. DataTable dt1 = (DataTable)dh.ExecuteSql("select pid_sql,pid_item,pid_value,pid_valuetype from productitem left join productitemdetail on pid_piid=pi_id where pi_prodcode='" + pr_code + "'", "select");
  351. for (int i = 0; i < dt.Rows.Count; i++)
  352. {
  353. for (int j = 0; j < dt1.Rows.Count; j++)
  354. {
  355. if (dt1.Rows[j]["pid_valuetype"].ToString() == "SHA256")
  356. {
  357. root.BindList.Add(new BindItem
  358. {
  359. BindKey = "mac",
  360. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  361. KeyType = dt1.Rows[j]["pid_item"].ToString(),
  362. KeyValue = dt1.Rows[j]["pid_item"].ToString(),
  363. HashType = "SHA256",
  364. HashKey = ComputeSha256Hash(sn),
  365. }); ;
  366. }
  367. else
  368. {
  369. string sql = dt1.Rows[j]["pid_sql"].ToString();
  370. string value = "";
  371. if (sql != "")
  372. {
  373. Regex ConnoteA = new Regex("{\\w+}");
  374. foreach (Match mch in ConnoteA.Matches(sql))
  375. {
  376. string x = mch.Value.Trim();
  377. sql = sql.Replace(x, "'" + dt.Rows[i]["pd_barcode"].ToString() + "'");
  378. }
  379. DataTable Param = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  380. if (Param.Rows.Count > 0)
  381. {
  382. value = Param.Rows[0][0].ToString();
  383. }
  384. }
  385. else
  386. {
  387. value = dt1.Rows[j]["pid_value"].ToString();
  388. }
  389. root.BindList.Add(new BindItem
  390. {
  391. BindKey = "mac",
  392. BindValue = dt.Rows[i]["pd_barcode"].ToString(),
  393. KeyType = dt1.Rows[j]["pid_item"].ToString(),
  394. KeyValue = value,
  395. HashType = "NON-HASH",
  396. HashKey = dt1.Rows[j]["pid_item"].ToString(),
  397. });
  398. }
  399. }
  400. }
  401. var httpClientHandler = new HttpClientHandler();
  402. httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
  403. using (var client = new HttpClient(httpClientHandler))
  404. {
  405. client.DefaultRequestHeaders.Add("apikey", "60e7107a86284a6d842e551704e69253");
  406. client.DefaultRequestHeaders.Add("AppCode", "91678314583d4c2bbf185bc5573ac73d");
  407. StringBuilder sbText = new StringBuilder();
  408. string json = JsonConvert.SerializeObject(root, Formatting.Indented);
  409. var content = new StringContent(json, Encoding.UTF8, "application/json");
  410. var response = await client.PostAsync("https://172.30.30.10:29001/api/mds-web-outservice/v1/api/rest/mds/saveFmKeyHashcodeInfoBatch", content);
  411. if (response.IsSuccessStatusCode)
  412. {
  413. }
  414. else
  415. {
  416. BaseUtil.ShowError(response.StatusCode.ToString());
  417. }
  418. }
  419. }
  420. //获取外箱号信息
  421. public static void GetOutBoxInfo(string iBox)
  422. {
  423. dynamic obj = new ExpandoObject();
  424. var expandoDict = obj as IDictionary<string, object>;
  425. DataTable dt = (DataTable)dh.ExecuteSql("select ma_custpo,pd_barcode,pd_makecode,ZD_DEV_EN_NO from packagedetail left join ZTEDATA on zd_makecode=pd_makecode and pd_barcode=ZD_DEV_EN_NO left join make on ma_code=pd_makecode where pd_outboxcode='" + iBox + "'", "select");
  426. string makecode = "";
  427. string po = "";
  428. for (int i = 0; i < dt.Rows.Count; i++)
  429. {
  430. string enNoKey = $"en_no{i}";
  431. string enNoValue = dt.Rows[i]["ZD_DEV_EN_NO"].ToString();
  432. expandoDict[enNoKey] = enNoValue;
  433. makecode = dt.Rows[i]["pd_makecode"].ToString();
  434. po = dt.Rows[i]["ma_custpo"].ToString();
  435. }
  436. obj.request = "Q_wai_xiang";
  437. obj.packing_num = dt.Rows.Count;
  438. obj.User = "mes";
  439. obj.po_sn = po;
  440. obj.password = "258456";
  441. obj.tool_name = "zte";
  442. obj.noType = 0;
  443. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  444. string returnstr = ToServerReq(json);
  445. dh.ExecuteSql("insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE)values(EISDATA_seq.nextval,'" + json + "',sysdate,'GetOutBoxInfo')", "insert");
  446. var settings = new JsonSerializerSettings
  447. {
  448. Converters = new List<JsonConverter> { new DeviceInfoConverter() }
  449. };
  450. DeviceInfo deviceInfo = JsonConvert.DeserializeObject<DeviceInfo>(returnstr, settings);
  451. //将返回的信息存入数据库
  452. for (int i = 0; i < deviceInfo.DSN.Count; i++)
  453. {
  454. if (dh.CheckExist("ZTEDATA", "zd_sn='" + deviceInfo.DSN[i] + "' and zd_makecode='" + makecode + "' and zd_type='BOX'"))
  455. {
  456. dh.ExecuteSql("delete from ZTEDATA where zd_sn='" + deviceInfo.DSN[i] + "' and zd_makecode='" + makecode + "' and zd_type='BOX'", "delete");
  457. }
  458. 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, " +
  459. "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)" +
  460. "values(ZTEDATA_seq.nextval,'" + deviceInfo.DSN[i] + "','" + makecode + "','BOX','" + deviceInfo.DSN[i] + "','" + deviceInfo.ProductMode + "','" + deviceInfo.ProductName + "','" + deviceInfo.MaterialCode + "','" + deviceInfo.BatchNo + "','" + deviceInfo.SoftVersion + "','" + deviceInfo.HdVersion + "'," +
  461. "'" + deviceInfo.Voltage + "','" + deviceInfo.Power + "','" + deviceInfo.OrderNo + "','" + deviceInfo.BigBoxNo + "','" + deviceInfo.DSN[i] + "'," +
  462. "'" + deviceInfo.DevEnNo[i] + "', '" + deviceInfo.Reserve3[i] + "','" + deviceInfo.DeviceId[i] + "','" + deviceInfo.MacStart[i] + "','" + deviceInfo.MacEnd[i] + "','" + deviceInfo.ProductionDate + "',sysdate)", "insert");
  463. }
  464. if (returnstr.Length > 4000)
  465. {
  466. returnstr = returnstr.Substring(0, 3999);
  467. }
  468. dh.ExecuteSql("insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE)values(EISDATA_seq.nextval,'" + returnstr + "',sysdate,'GetOutBoxInfo')", "insert");
  469. }
  470. //获取栈板信息
  471. public static void GetPalletInfo(string iBox)
  472. {
  473. DataTable dt = (DataTable)dh.ExecuteSql("select pa_outboxcode,pa_makecode from package where pa_mothercode='" + iBox + "'", "select");
  474. dynamic obj = new ExpandoObject();
  475. var expandoDict = obj as IDictionary<string, object>;
  476. string makecode = "";
  477. for (int i = 0; i < dt.Rows.Count; i++)
  478. {
  479. string enNoKey = $"big_box_no{i}";
  480. string enNoValue = dt.Rows[i]["pa_outboxcode"].ToString();
  481. expandoDict[enNoKey] = enNoValue;
  482. makecode = dt.Rows[i]["pa_makecode"].ToString();
  483. }
  484. obj.request = "pallet";
  485. obj.packing_num = dt.Rows.Count;
  486. obj.User = "mes";
  487. obj.po_sn = makecode;
  488. obj.password = "258456";
  489. obj.tool_name = "zte";
  490. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  491. dh.ExecuteSql("insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE)values(EISDATA_seq.nextval,'" + json + "',sysdate,'GetPalletInfo')", "insert");
  492. string returnstr = ToServerReq(json);
  493. if (returnstr.Length > 4000)
  494. {
  495. returnstr = returnstr.Substring(0, 3999);
  496. }
  497. dh.ExecuteSql("insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE)values(EISDATA_seq.nextval,'" + returnstr + "',sysdate,'GetPalletInfo')", "insert");
  498. }
  499. public static void GetMingPai_Thailand(string iSN)
  500. {
  501. DataTable dt = (DataTable)dh.ExecuteSql("select ms_id,ma_custpo,ms_makecode,pr_prefix,PREFIX_EN_NO,PREFIX_BIG_BOX,pr_prebigxbox,pr_regcode from makeserial left join product on ms_prodcode=pr_code left join make on ma_code=ms_makecode left join ZTE_ORDER on ma_custpo=po_sn where ms_sncode ='" + iSN + "' or ms_firstsn='" + iSN + "' order by ms_id desc", "select");
  502. string po = "";
  503. string makecode = "";
  504. string ms_id = "";
  505. if (dt.Rows.Count > 0)
  506. {
  507. po = dt.Rows[0]["ma_custpo"].ToString();
  508. makecode = dt.Rows[0]["ms_makecode"].ToString();
  509. ms_id = dt.Rows[0]["ms_id"].ToString();
  510. }
  511. dynamic obj = new ExpandoObject();
  512. obj.tool_name = "zte";
  513. obj.request = "ajc_mingpai";
  514. obj.User = "1453";
  515. obj.password = "12345678";
  516. obj.po_sn = po;
  517. obj.product_id = iSN;
  518. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  519. string returnstr = ToServerReq(json);
  520. JObject deviceObj = JObject.Parse(returnstr);
  521. string tool_name = deviceObj["tool_name"].ToString();
  522. string response = deviceObj["response"].ToString();
  523. string sn = deviceObj["sn"].ToString();
  524. string status = deviceObj["status"].ToString();
  525. string product_id = deviceObj["product_id"].ToString();
  526. string dev_mac = deviceObj["dev_mac"].ToString();
  527. string wifi_mac = deviceObj["wifi_mac"].ToString();
  528. string en = deviceObj["en"].ToString();
  529. string en_prefix = deviceObj["en_prefix"].ToString();
  530. string product_name = deviceObj["product_name"].ToString();
  531. string vendor = deviceObj["vendor"].ToString();
  532. string esim_iccid = deviceObj["esim_iccid"].ToString();
  533. string exsim_iccid = deviceObj["exsim_iccid"].ToString();
  534. string fg_ver = deviceObj["4g_ver"].ToString();
  535. string fg_type = deviceObj["4g_type"].ToString();
  536. if (dh.CheckExist("ZTEDATA", "zd_sn='" + iSN + "' and zd_makecode='" + makecode + "' and zd_type='铭牌标'"))
  537. {
  538. dh.ExecuteSql("delete from ZTEDATA where zd_sn='" + iSN + "' and zd_makecode='" + makecode + "' and zd_type='铭牌标'", "delete");
  539. }
  540. dh.ExecuteSql("update makeserial set ms_sncode='" + sn + "' where ms_id='" + ms_id + "'", "update");
  541. dh.ExecuteSql("insert into ZTEDATA(ZD_ID,zd_vendor,ZD_D_SN,ZD_DEV_EN_NO,ZD_SN, ZD_MAKECODE, ZD_TYPE, ZD_VALUE,zd_mac,zd_enno,ZD_PRODUCTNAME)" +
  542. "values(ZTEDATA_seq.nextval,'" + vendor + "','" + sn + "','" + sn + "','" + iSN + "','" + makecode + "','铭牌标','','" + wifi_mac + "'" +
  543. ",'" + en + "','" + product_name + "')", "insert");
  544. }
  545. public static void GetCaiHe_Thailand(string iSN)
  546. {
  547. DataTable dt = (DataTable)dh.ExecuteSql("select ms_id,ma_custpo,ms_makecode,pr_prefix,PREFIX_EN_NO,PREFIX_BIG_BOX,pr_prebigxbox,pr_regcode from makeserial left join product on ms_prodcode=pr_code left join make on ma_code=ms_makecode left join ZTE_ORDER on ma_custpo=po_sn where ms_sncode ='" + iSN + "' or ms_firstsn='" + iSN + "' order by ms_id desc", "select");
  548. string po = "";
  549. string makecode = "";
  550. string ms_id = "";
  551. if (dt.Rows.Count > 0)
  552. {
  553. po = dt.Rows[0]["ma_custpo"].ToString();
  554. makecode = dt.Rows[0]["ms_makecode"].ToString();
  555. ms_id = dt.Rows[0]["ms_id"].ToString();
  556. }
  557. dynamic obj = new ExpandoObject();
  558. obj.tool_name = "zte";
  559. obj.request = "ajc_cai_he";
  560. obj.User = "1453";
  561. obj.password = "12345678";
  562. obj.po_sn = po;
  563. obj.product_id = iSN;
  564. string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
  565. string returnstr = ToServerReq(json);
  566. JObject deviceObj = JObject.Parse(returnstr);
  567. string tool_name = deviceObj["tool_name"].ToString();
  568. string response = deviceObj["response"].ToString();
  569. string sn = deviceObj["sn"].ToString();
  570. string status = deviceObj["status"].ToString();
  571. string product_id = deviceObj["product_id"].ToString();
  572. string dev_mac = deviceObj["dev_mac"].ToString();
  573. string wifi_mac = deviceObj["wifi_mac"].ToString();
  574. string en = deviceObj["en"].ToString();
  575. if (dh.CheckExist("ZTEDATA", "zd_sn='" + iSN + "' and zd_makecode='" + makecode + "' and zd_type='彩盒标'"))
  576. {
  577. dh.ExecuteSql("delete from ZTEDATA where zd_sn='" + iSN + "' and zd_makecode='" + makecode + "' and zd_type='彩盒标'", "delete");
  578. }
  579. dh.ExecuteSql("update makeserial set ms_sncode='" + sn + "' where ms_id='" + ms_id + "'", "update");
  580. dh.ExecuteSql("insert into ZTEDATA(ZD_ID,ZD_D_SN,ZD_DEV_EN_NO,ZD_SN, ZD_MAKECODE, ZD_TYPE, ZD_VALUE,zd_mac,zd_enno,ZD_PRODUCTNAME)" +
  581. "values(ZTEDATA_seq.nextval,'" + sn + "','" + sn + "','" + iSN + "','" + makecode + "','彩盒标','','" + wifi_mac + "'" +
  582. ",'" + en + "')", "insert");
  583. }
  584. public static void GetOutBoxInf_Thailand(string iBox)
  585. {
  586. }
  587. public static readonly uint PROTOCOL_FLAG = 0x4C4F4F54;
  588. public static string ToServerReq(string strJSON)
  589. {
  590. int nRet = 0;
  591. string strServerIP = "192.168.1.160";
  592. string strResp = "";
  593. IPAddress ip = IPAddress.Parse(strServerIP);
  594. Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  595. while (true)
  596. {
  597. try
  598. {
  599. clientSocket.Connect(new IPEndPoint(ip, SN_SERVICE_PORT));
  600. }
  601. catch
  602. {
  603. nRet = -13;
  604. break;
  605. }
  606. try
  607. {
  608. FACT_DATA_HEAD stHead = new FACT_DATA_HEAD(null);
  609. byte[] bytJson = Encoding.UTF8.GetBytes(strJSON); //Encoding.ASCII.GetBytes(strJSON);
  610. stHead.setDataLen(bytJson.Length);
  611. byte[] bytBuf = new byte[FACT_DATA_HEAD.MY_LEN + bytJson.Length];
  612. byte[] bytSrcHead = stHead.getBytes();
  613. Array.Copy(bytSrcHead, 0, bytBuf, 0, bytSrcHead.Length);
  614. Array.Copy(bytJson, 0, bytBuf, FACT_DATA_HEAD.MY_LEN, bytJson.Length);
  615. clientSocket.Send(bytBuf);
  616. }
  617. catch
  618. {
  619. nRet = -12;
  620. break;
  621. }
  622. //判定接口回传的flag和本地传过去的是否一致
  623. byte[] bytRecv = new byte[MAX_BUF_SIZE];
  624. int nRecvSize = 0, nSize = 0;
  625. nRecvSize = clientSocket.Receive(bytRecv, 0, FACT_DATA_HEAD.MY_LEN, SocketFlags.None);
  626. if (nRecvSize > 0)
  627. {
  628. FACT_DATA_HEAD stHead = new FACT_DATA_HEAD(bytRecv);
  629. if (stHead.getFlag() == FACT_DATA_HEAD.PROTOCOL_FLAG)
  630. {
  631. int nPos = 0;
  632. nSize = stHead.getDatalen();
  633. while (nPos < nSize)
  634. {
  635. nRecvSize = clientSocket.Receive(bytRecv, nPos, nSize - nPos, SocketFlags.None);
  636. if (nRecvSize < 0)
  637. {
  638. nRet = -14;
  639. break;
  640. }
  641. nPos += nRecvSize;
  642. }
  643. strResp = Encoding.Default.GetString(bytRecv, 0, nSize);
  644. }
  645. else nRet = -11;
  646. }
  647. else nRet = -10;
  648. break;
  649. }
  650. if (nRet > -13) clientSocket.Shutdown(SocketShutdown.Both);
  651. clientSocket.Close();
  652. return strResp;
  653. }
  654. }
  655. }