GMethod.cs 20 KB

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