GMethod.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using SufeiUtil;
  4. using SXLibrary;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data.SQLite;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace MOKA_Factory_Tools
  13. {
  14. internal class GMethod
  15. {
  16. private static MaInfo s_maInfo = null;
  17. private static string strLastOrder = "";
  18. private static string strOrderKind = ""; // 试产 or 量产;
  19. private static Dictionary<string, string> LastKaylaOrderInfo = new Dictionary<string, string>();
  20. private static string url_MaInfo = "http://smes-prd-app01.tclking.com:9002/SAPService.asmx";
  21. private static string url_ReportKey = "http://smes-app-prod.tclking.com/FireTVApi/api/SMES/RecordKey";
  22. private static string url_getOrderProductStatus = "http://smes-app-prod.tclking.com/FireTVApi/api/SMES/getOrderProductStatus?sn=";
  23. public static void SetMexicanConfig(bool bbMexican=false)
  24. {
  25. if (bbMexican)
  26. {
  27. url_MaInfo = "http://SMES.TMSA.MX:9002/SAPService.asmx";
  28. url_ReportKey = "http://SMES.TMSA.MX/FireTVApi/api/SMESMX/RecordKey";
  29. url_getOrderProductStatus = "http://SMES.TMSA.MX/FireTVApi/api/SMESMX/getOrderProductStatus?sn=";
  30. }
  31. }
  32. public static bool GetMaInfo(string sn, string order, out MaInfo maInfo, SQLiteConnection errorDBNow)
  33. {
  34. string desc;
  35. maInfo = null;
  36. if (s_maInfo != null && s_maInfo.ODF.Equals(order, StringComparison.InvariantCultureIgnoreCase) )
  37. {
  38. maInfo = s_maInfo;
  39. return true;
  40. }
  41. string strPost = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  42. "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
  43. "<soap:Body>" +
  44. "<GetMaInfo xmlns=\"http://tempuri.org/\">" +
  45. "<sn>" + sn + "</sn>" +
  46. "</GetMaInfo>" +
  47. "</soap:Body>" +
  48. "</soap:Envelope>";
  49. HttpHelper http = new HttpHelper();
  50. HttpItem item = new HttpItem()
  51. {
  52. Encoding = Encoding.UTF8,
  53. Method = "post",
  54. ContentType = "text/xml",
  55. KeepAlive = false
  56. };
  57. item.URL = url_MaInfo;
  58. item.Postdata = strPost;
  59. HttpResult result = http.GetHtml(item);
  60. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  61. {
  62. Log.WriteGetKeyLog("\r\nGetMaInfo :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  63. if (!Xmlconfig.GetMaInfoXml(result.Html, out maInfo, out desc))
  64. {
  65. Log.WriteGetKeyLog("GetMaInfo error:" + desc + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  66. return false;
  67. }
  68. }
  69. else
  70. {
  71. Log.WriteGetKeyLog("GetMaInfo error:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  72. return false;
  73. }
  74. s_maInfo = maInfo;
  75. return true;
  76. }
  77. /// <summary>
  78. /// 上报数据抄写结果到MES系统中;
  79. /// </summary>
  80. /// <param name="fsn"></param>
  81. /// <param name="dsn"></param>
  82. /// <param name="dateTime">抄写时间</param>
  83. /// <param name="result">抄写结果</param>
  84. /// <param name="msg">如果上报失败,此参数为失败的描述信息</param>
  85. /// <returns></returns>
  86. public static bool ReportDataBurningResultToMES(string fsn, string dsn, string result, string dateTime, SQLiteConnection errorDBNow, out string msg, out string returnObject)
  87. {
  88. msg = "";
  89. returnObject = "";
  90. string strPost = string.Format("{{\"FSN\":\"{0}\",\"DSN\":\"{1}\",\"DataCopy\":\"{2}\",\"UPDATETIME\":\"{3}\"}}", fsn, dsn, result, dateTime);
  91. HttpHelper http = new HttpHelper();
  92. HttpItem item = new HttpItem()
  93. {
  94. Encoding = Encoding.UTF8,
  95. Method = "post",
  96. ContentType = "application/json",
  97. KeepAlive = false
  98. };
  99. // 测试地址;
  100. item.URL = url_ReportKey;
  101. item.Postdata = strPost;
  102. HttpResult hResult = http.GetHtml(item);
  103. if (hResult.StatusCode == System.Net.HttpStatusCode.OK)
  104. {
  105. try
  106. {
  107. using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html)))
  108. {
  109. JObject jObject = (JObject)JToken.ReadFrom(reader);
  110. reader.Close();
  111. bool bResult = jObject["Result"].Value<bool>();
  112. msg = jObject["EroMsg"].Value<string>();
  113. if (jObject["returnObject"] != null)
  114. returnObject = jObject["returnObject"].ToString();
  115. Log.WriteInfoLog("nReportDataBurningResultToMES:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  116. return bResult;
  117. }
  118. }
  119. catch (Exception e)
  120. {
  121. msg = e.Message;
  122. Log.WriteErrorLog("ReportDataBurningResultToMES Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  123. }
  124. }
  125. else
  126. {
  127. msg = hResult.StatusDescription;
  128. Log.WriteInfoLog("ReportDataBurningResultToMES Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  129. }
  130. // 没有进行数据库查重:惹数据已添加过,同一次FSN抄写多次失败后导致多条记录。
  131. SQLiteHelper.InsertDelayErrorReport(errorDBNow, new object[] { item.URL, item.Postdata });
  132. return false;
  133. }
  134. /// <summary>
  135. /// 上传本地数据库失败的日志,重新上传;
  136. /// </summary>
  137. /// <param name="url"></param>
  138. /// <param name="strPost"></param>
  139. /// <param name="mark"></param>
  140. /// <returns></returns>
  141. public static bool ReportDataBurningResultToMES(string url, string strPost, out bool mark)
  142. {
  143. // 表示是否上传成功;
  144. mark = false;
  145. HttpHelper http = new HttpHelper();
  146. HttpItem item = new HttpItem()
  147. {
  148. Encoding = Encoding.UTF8,
  149. Method = "post",
  150. ContentType = "application/json",
  151. KeepAlive = false
  152. };
  153. item.URL = url;
  154. item.Postdata = strPost;
  155. HttpResult hResult = http.GetHtml(item);
  156. if (hResult.StatusCode == System.Net.HttpStatusCode.OK)
  157. {
  158. try
  159. {
  160. using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html)))
  161. {
  162. JObject jObject = (JObject)JToken.ReadFrom(reader);
  163. reader.Close();
  164. mark = jObject["Result"].Value<bool>();
  165. Log.WriteInfoLog("nReportDataBurningResultToMES:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  166. return true;
  167. }
  168. }
  169. catch (Exception)
  170. {
  171. Log.WriteErrorLog("ReportDataBurningResultToMES error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  172. throw;
  173. }
  174. }
  175. else
  176. {
  177. Log.WriteInfoLog("ReportDataBurningResultToMES error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  178. }
  179. return false;
  180. }
  181. /// <summary>
  182. /// 通过SN获取订单类型
  183. /// </summary>
  184. /// <param name="sn"></param>
  185. /// <returns></returns>
  186. public static bool GetOrderTypeFromSN(string sn, string order, out string result, out string msg)
  187. {
  188. msg = "";
  189. result = "";
  190. if (order.Equals(strLastOrder, StringComparison.OrdinalIgnoreCase))
  191. {
  192. if (strOrderKind.Equals("量产") || strOrderKind.Equals("试产"))
  193. {
  194. result = strOrderKind;
  195. return true;
  196. }
  197. }
  198. HttpHelper http = new HttpHelper();
  199. HttpItem item = new HttpItem()
  200. {
  201. Encoding = Encoding.UTF8,
  202. Method = "post",
  203. ContentType = "application/x-www-form-urlencoded",
  204. KeepAlive = false
  205. };
  206. // 测试地址;
  207. item.URL = url_getOrderProductStatus + sn;
  208. HttpResult hResult = http.GetHtml(item);
  209. if (hResult.StatusCode == System.Net.HttpStatusCode.OK)
  210. {
  211. try
  212. {
  213. using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html)))
  214. {
  215. JObject jObject = (JObject)JToken.ReadFrom(reader);
  216. reader.Close();
  217. bool bResult = jObject["Result"].Value<bool>();
  218. msg = jObject["EroMsg"].Value<string>();
  219. string returnObject = jObject["returnObject"].ToString();
  220. if (bResult)
  221. {
  222. if (returnObject.Contains("量产"))
  223. result = "量产";
  224. else if (returnObject.Contains("试产"))
  225. result = "试产";
  226. else
  227. result = "错误返回";
  228. strOrderKind = result;
  229. Log.WriteInfoLog("\r\nIsCuringBOM :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + hResult.Html);
  230. return true;
  231. }
  232. Log.WriteInfoLog("IsCuringBOM error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  233. }
  234. }
  235. catch (Exception e)
  236. {
  237. msg = e.Message;
  238. Log.WriteErrorLog("IsCuringBOM Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  239. }
  240. }
  241. else
  242. {
  243. msg = hResult.StatusDescription;
  244. Log.WriteInfoLog("IsCuringBOM Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  245. }
  246. strOrderKind = "";
  247. return false;
  248. }
  249. /// <summary>
  250. // Section1:本机信息;
  251. // Section2:SMES信息;
  252. // Section3:实际抄写Key信息;
  253. /// </summary>
  254. /// <param name="Line"></param>
  255. /// <param name="Station"></param>
  256. /// <param name="order"></param>
  257. /// <param name="sn"></param>
  258. /// <param name="StartTime"></param>
  259. /// <param name="EndOfTime"></param>
  260. /// <param name="returnObject"></param>
  261. /// <param name="key"></param>
  262. /// <param name="result"></param>
  263. /// <returns></returns>
  264. public static bool GeneratKaylaData(string Line, string Station, string order, string sn, DateTime StartTime, DateTime EndOfTime, string returnObject, KeyInfo key, bool result)
  265. {
  266. Dictionary<string, string> localInfo = new Dictionary<string, string>();
  267. // 本机信息;
  268. localInfo.Add("Item", "Value");
  269. localInfo.Add("factory_id_I", "TCL");
  270. localInfo.Add("operator_I", Line);
  271. localInfo.Add("station_I", Station);
  272. localInfo.Add("fixture_id_I", "1_1");
  273. localInfo.Add("gui_ver_I", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
  274. localInfo.Add("route_check_I", "1");
  275. // SMES信息;
  276. string strLastOrder = CommonMethod.ReadProfileString("MOKAFactoryTools", "LastOrder", "");
  277. if (!strLastOrder.Equals(order, StringComparison.OrdinalIgnoreCase))
  278. {
  279. // 请求新的订单信息;
  280. MaInfo maInfo;
  281. if (!GetMaInfo(sn, order, out maInfo, null))
  282. {
  283. Log.WriteErrorLog("Kayla GetMaInfo失败");
  284. return false;
  285. }
  286. LastKaylaOrderInfo = new Dictionary<string, string>();
  287. if (maInfo != null)
  288. {
  289. LastKaylaOrderInfo.Add("project_name_I", maInfo.project_name_I);
  290. LastKaylaOrderInfo.Add("build_I", maInfo.build_I);
  291. LastKaylaOrderInfo.Add("build_config_I", maInfo.build_config_I);
  292. LastKaylaOrderInfo.Add("diag_ver_I", maInfo.diag_ver_I);
  293. LastKaylaOrderInfo.Add("image_name_ver_I", maInfo.image_name_ver_I);
  294. LastKaylaOrderInfo.Add("soc_type", maInfo.soc_type);
  295. LastKaylaOrderInfo.Add("speaker_vender", maInfo.speaker_vender);
  296. LastKaylaOrderInfo.Add("mic_vender", maInfo.mic_vender);
  297. }
  298. }
  299. Dictionary<string, string> dic = localInfo.Union(LastKaylaOrderInfo).ToDictionary(k => k.Key, v => v.Value);
  300. dic.Add("start_time_I", StartTime.ToString());
  301. dic.Add("serial_id_I", key != null ? key.DSN : ""); // DSN;
  302. dic.Add("FSN", sn);
  303. // 耐压值;
  304. if (returnObject != null && returnObject.Length > 0 && returnObject.Contains("Hightpot"))
  305. {
  306. Log.WriteInfoLog(String.Format("returnObjec={0}", returnObject));
  307. // 删除头尾[];
  308. returnObject = returnObject.Remove(0, 1);
  309. returnObject = returnObject.Remove(returnObject.Length - 1, 1);
  310. JObject jObject = JObject.Parse(returnObject);
  311. string hightpot = CommonMethod.JSON_SeleteNode(jObject, "Hightpot");
  312. int pos = hightpot.LastIndexOf('/');
  313. if (pos != -1)
  314. {
  315. hightpot = hightpot.Substring(pos + 1);
  316. pos = hightpot.IndexOf("mA");
  317. // 使用正规去除数字+小数点以外的字符;
  318. string AC = hightpot.Substring(0, pos + 2);
  319. string DC = hightpot.Substring(pos + 2);
  320. AC = System.Text.RegularExpressions.Regex.Replace(AC, @"[^\d.]*", "");
  321. DC = System.Text.RegularExpressions.Regex.Replace(DC, @"[^\d.]*", "");
  322. dic.Add("AC_I", AC);
  323. dic.Add("DC_R", DC);
  324. }
  325. else
  326. {
  327. dic.Add("AC_I", "");
  328. dic.Add("DC_R", "");
  329. }
  330. }
  331. else
  332. {
  333. dic.Add("AC_I", "");
  334. dic.Add("DC_R", "");
  335. Log.WriteErrorLog("获取耐压值结果为空");
  336. }
  337. if (key != null)
  338. {
  339. if (key.DID.enable)
  340. {
  341. dic.Add("DID", key.DID); // 本身就是字符串;
  342. dic.Add("DID_check", key.DID.result ? "PASS" : "FAIL");
  343. }
  344. if (key.Mac.enable)
  345. {
  346. dic.Add("Mac", key.Mac);//keyInfo.Mac.data.Replace("-", ":");
  347. dic.Add("Mac_check", key.Mac.result ? "PASS" : "FAIL");
  348. }
  349. if (key.HDCP.enable)
  350. {
  351. dic.Add("HDCP", key.HDCP);
  352. dic.Add("HDCP_check", key.HDCP.result ? "PASS" : "FAIL");
  353. }
  354. if (key.HDCP22.enable)
  355. {
  356. dic.Add("HDCP22", key.HDCP22);
  357. dic.Add("HDCP22_check", key.HDCP22.result ? "PASS" : "FAIL");
  358. }
  359. if (key.ESN.enable)
  360. {
  361. dic.Add("ESN", key.ESN);
  362. dic.Add("ESN_check", key.ESN.result ? "PASS" : "FAIL");
  363. }
  364. if (key.Widevine.enable)
  365. {
  366. dic.Add("Widevine", key.Widevine);
  367. dic.Add("Widevine_check", key.Widevine.result ? "PASS" : "FAIL");
  368. }
  369. if (key.CI_plus.enable)
  370. {
  371. dic.Add("CI_plus", key.CI_plus);
  372. dic.Add("CI_plus_check", key.CI_plus.result ? "PASS" : "FAIL");
  373. }
  374. if (key.Attestation.enable)
  375. {
  376. dic.Add("Attestation", key.Attestation);
  377. dic.Add("Attestation_check", key.Attestation.result ? "PASS" : "FAIL");
  378. }
  379. if (key.MGK.enable)
  380. {
  381. dic.Add("MGK", key.MGK);
  382. dic.Add("MGK_check", key.MGK.result ? "PASS" : "FAIL");
  383. }
  384. if (key.Fairplay.enable)
  385. {
  386. dic.Add("Fairplay", key.Fairplay);
  387. dic.Add("Fairplay_check", key.Fairplay.result ? "PASS" : "FAIL");
  388. }
  389. if (key.DSN.enable)
  390. {
  391. dic.Add("DSN", key.DSN);
  392. dic.Add("DSN_check", key.DSN.result ? "PASS" : "FAIL");
  393. }
  394. if (key.WiFi_MAC.enable)
  395. {
  396. dic.Add("WiFi_MAC", key.WiFi_MAC);
  397. dic.Add("WiFi_MAC_check", key.WiFi_MAC.result ? "PASS" : "FAIL");
  398. }
  399. if (key.BT_MAC.enable)
  400. {
  401. dic.Add("BT_MAC", key.BT_MAC);
  402. dic.Add("BT_MAC_check", key.BT_MAC.result ? "PASS" : "FAIL");
  403. }
  404. if (key.LEK.enable)
  405. {
  406. dic.Add("LEK", key.LEK);
  407. dic.Add("LEK_check", key.LEK.result ? "PASS" : "FAIL");
  408. }
  409. if (key.PEK.enable)
  410. {
  411. dic.Add("PEK", key.PEK);
  412. dic.Add("PEK_check", key.PEK.result ? "PASS" : "FAIL");
  413. }
  414. if (key.Playready.enable)
  415. {
  416. dic.Add("Playready", key.Playready);
  417. dic.Add("Playready_check", key.Playready.result ? "PASS" : "FAIL");
  418. }
  419. if (key.Hashkey.enable)
  420. {
  421. dic.Add("Hashkey", key.Hashkey);
  422. dic.Add("Hashkey_check", key.Hashkey.result ? "PASS" : "FAIL");
  423. }
  424. if (key.ECP.enable)
  425. {
  426. dic.Add("ECP", key.ECP);
  427. dic.Add("ECP_check", key.ECP.result ? "PASS" : "FAIL");
  428. }
  429. if (key.YouTube_KEY.enable)
  430. {
  431. dic.Add("YouTube_KEY", key.YouTube_KEY);
  432. dic.Add("YouTube_KEY_check", key.YouTube_KEY.result ? "PASS" : "FAIL");
  433. }
  434. }
  435. dic.Add("cycle_time_I", EndOfTime.Subtract(StartTime).Seconds.ToString());
  436. dic.Add("test_result_I", result ? "PASS" : "FAIL");
  437. try
  438. {
  439. // 以DSN命名文件;
  440. if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Kayla"))
  441. {
  442. Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Kayla");
  443. }
  444. string savefile = string.Format("{0}Kayla\\{1}.csv", AppDomain.CurrentDomain.BaseDirectory, sn);
  445. using (System.IO.StreamWriter file = new System.IO.StreamWriter(savefile, false))
  446. {
  447. foreach (KeyValuePair<string, string> kvp in dic)
  448. {
  449. file.WriteLine(string.Format("{0},{1}", kvp.Key, kvp.Value));
  450. }
  451. }
  452. }
  453. catch (Exception ex)
  454. {
  455. Log.WriteErrorLog(string.Format("保存csv文件失败:{0}", ex.ToString()));
  456. return false;
  457. }
  458. CommonMethod.StartProcess(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "tcp_send.exe", sn);
  459. return true;
  460. }
  461. }
  462. }