GMethod.cs 20 KB

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