GMethod.cs 21 KB

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