using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SufeiUtil; using SXLibrary; using System; using System.Collections.Generic; using System.Data.SQLite; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Security.Policy; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MOKA_Factory_Tools { internal class GMethod { private static MaInfo s_maInfo = null; private static string strLastOrder = ""; private static string strOrderKind = ""; // 试产 or 量产; private static Dictionary LastKaylaOrderInfo = new Dictionary(); private static string url_MaInfo = "http://smes-prd-app01.tclking.com:9002/SAPService.asmx"; private static string url_ReportKey = "http://smes-app-prod.tclking.com/FireTVApi/api/SMES/RecordKey"; private static string url_getOrderProductStatus = "http://smes-app-prod.tclking.com/FireTVApi/api/SMES/getOrderProductStatus?sn="; private static string url_getMainBoardPSNWithSN = "http://smes-app-prod.tclking.com:9006/api/xm/GetXMTVParts?TVSN="; private static string url_getTVInfo = "http://smes-app-prod.tclking.com:9006/api/PE/GetTVInfo?sn="; public static void SetPolandConfig(bool bPoland = false) { if(bPoland) { url_MaInfo = "http://10.146.0.56:8084/?wsdl?op=GetMaInfo"; url_ReportKey = "http://10.146.0.56/FireTV/RecordKey"; url_getOrderProductStatus = "http://10.146.0.56/FireTV/GetOrderProductStatus?SN="; url_getTVInfo = "http://10.146.0.56/FireTV/GetTVinfo?SN="; } } public static void SetMexicanConfig(bool bMexican=false) { if (bMexican) { url_MaInfo = "http://SMES.TMSA.MX:9002/SAPService.asmx"; url_ReportKey = "http://SMES.TMSA.MX/FireTVApi/api/SMESMX/RecordKey"; url_getOrderProductStatus = "http://SMES.TMSA.MX/FireTVApi/api/SMESMX/getOrderProductStatus?sn="; url_getTVInfo = "http://SMES.TMSA.MX:9006/api/PE/GetTVInfo?sn="; } } public static bool GetMaInfo(string sn, string order, out MaInfo maInfo, SQLiteConnection errorDBNow) { if (GetMaInfoBySAP(sn, order, out maInfo, errorDBNow)) { return true; } return GetMaInfoByMES(sn, order, out maInfo, errorDBNow); } public static bool GetMaInfoByMES(string sn, string order, out MaInfo maInfo, SQLiteConnection errorDBNow) { maInfo = null; if (s_maInfo != null && s_maInfo.ODF.Equals(order, StringComparison.InvariantCultureIgnoreCase)) { maInfo = s_maInfo; return true; } HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = url_getTVInfo + sn, Encoding = Encoding.UTF8, Method = "get", PostEncoding = Encoding.UTF8, KeepAlive = false }; item.ContentType = "application/x-www-form-urlencoded"; HttpResult hResult = http.GetHtml(item); if (hResult.StatusCode == System.Net.HttpStatusCode.OK) { try { using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html))) { JObject jObject = (JObject)JToken.ReadFrom(reader); reader.Close(); maInfo.ODF = jObject["ODF"].Value(); maInfo.BOM = jObject["ProductCode"].Value(); maInfo.Model = jObject["ProductName"].Value().Split('/')[1]; maInfo.SIZE = maInfo.Model.Substring(0, 3); Log.WriteInfoLog("GetMaInfoByMES:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); s_maInfo = maInfo; return true; } } catch (Exception) { Log.WriteErrorLog("GetMaInfoByMES error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } } else { Log.WriteInfoLog("GetMaInfoByMES error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } return false; } public static bool GetMaInfoBySAP(string sn, string order, out MaInfo maInfo, SQLiteConnection errorDBNow) { string desc; maInfo = null; if (s_maInfo != null && s_maInfo.ODF.Equals(order, StringComparison.InvariantCultureIgnoreCase) ) { maInfo = s_maInfo; return true; } string strPost = "" + "" + "" + "" + "" + sn + "" + "" + "" + ""; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { Encoding = Encoding.UTF8, Method = "post", ContentType = "text/xml", KeepAlive = false }; item.URL = url_MaInfo; item.Postdata = strPost; HttpResult result = http.GetHtml(item); if (result.StatusCode == System.Net.HttpStatusCode.OK) { Log.WriteGetKeyLog("\r\nGetMaInfo :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html); if (!Xmlconfig.GetMaInfoXml(result.Html, out maInfo, out desc)) { Log.WriteGetKeyLog("GetMaInfo error:" + desc + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); return false; } } else { Log.WriteGetKeyLog("GetMaInfo error:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); return false; } s_maInfo = maInfo; return true; } /// /// 上报数据抄写结果到MES系统中; /// /// /// /// 抄写时间 /// 抄写结果 /// 如果上报失败,此参数为失败的描述信息 /// public static bool ReportDataBurningResultToMES(string fsn, string dsn, string result, string dateTime, SQLiteConnection errorDBNow, out string msg, out string returnObject) { msg = ""; returnObject = ""; string strPost = string.Format("{{\"FSN\":\"{0}\",\"DSN\":\"{1}\",\"DataCopy\":\"{2}\",\"UPDATETIME\":\"{3}\"}}", fsn, dsn, result, dateTime); HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { Encoding = Encoding.UTF8, Method = "post", ContentType = "application/json", KeepAlive = false }; // 测试地址; item.URL = url_ReportKey; item.Postdata = strPost; HttpResult hResult = http.GetHtml(item); if (hResult.StatusCode == System.Net.HttpStatusCode.OK) { try { using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html))) { JObject jObject = (JObject)JToken.ReadFrom(reader); reader.Close(); bool bResult = jObject["Result"].Value(); msg = jObject["EroMsg"].Value(); if (jObject["returnObject"] != null) returnObject = jObject["returnObject"].ToString(); Log.WriteInfoLog("nReportDataBurningResultToMES:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); return bResult; } } catch (Exception e) { msg = e.Message; Log.WriteErrorLog("ReportDataBurningResultToMES Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } } else { msg = hResult.StatusDescription; Log.WriteInfoLog("ReportDataBurningResultToMES Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } // 没有进行数据库查重:惹数据已添加过,同一次FSN抄写多次失败后导致多条记录。 SQLiteHelper.InsertDelayErrorReport(errorDBNow, new object[] { item.URL, item.Postdata }); return false; } /// /// 上传本地数据库失败的日志,重新上传; /// /// /// /// /// public static bool ReportDataBurningResultToMES(string url, string strPost, out bool mark) { // 表示是否上传成功; mark = false; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { Encoding = Encoding.UTF8, Method = "post", ContentType = "application/json", KeepAlive = false }; item.URL = url; item.Postdata = strPost; HttpResult hResult = http.GetHtml(item); if (hResult.StatusCode == System.Net.HttpStatusCode.OK) { try { using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html))) { JObject jObject = (JObject)JToken.ReadFrom(reader); reader.Close(); mark = jObject["Result"].Value(); Log.WriteInfoLog("nReportDataBurningResultToMES:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); return true; } } catch (Exception) { Log.WriteErrorLog("ReportDataBurningResultToMES error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); return false; } } else { Log.WriteInfoLog("ReportDataBurningResultToMES error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } return false; } /// /// 通过SN获取订单类型 /// /// /// public static bool GetOrderTypeFromSN(string sn, string order, out string result, out string msg) { msg = ""; result = ""; if (order.Equals(strLastOrder, StringComparison.OrdinalIgnoreCase)) { if (strOrderKind.Equals("量产") || strOrderKind.Equals("试产")) { result = strOrderKind; return true; } } HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { Encoding = Encoding.UTF8, Method = "post", ContentType = "application/x-www-form-urlencoded", KeepAlive = false }; // 测试地址; item.URL = url_getOrderProductStatus + sn; HttpResult hResult = http.GetHtml(item); if (hResult.StatusCode == System.Net.HttpStatusCode.OK) { try { using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html))) { JObject jObject = (JObject)JToken.ReadFrom(reader); reader.Close(); bool bResult = jObject["Result"].Value(); msg = jObject["EroMsg"].Value(); string returnObject = jObject["returnObject"].ToString(); if (bResult) { if (returnObject.Contains("量产")) result = "量产"; else if (returnObject.Contains("试产")) result = "试产"; else result = "错误返回"; strOrderKind = result; Log.WriteInfoLog("\r\nIsCuringBOM :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + hResult.Html); return true; } Log.WriteInfoLog("IsCuringBOM error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } } catch (Exception e) { msg = e.Message; Log.WriteErrorLog("IsCuringBOM Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } } else { msg = hResult.StatusDescription; Log.WriteInfoLog("IsCuringBOM Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } strOrderKind = ""; return false; } /// // Section1:本机信息; // Section2:SMES信息; // Section3:实际抄写Key信息; /// /// /// /// /// /// /// /// /// /// /// public static bool GeneratKaylaData(string Line, string Station, string order, string sn, DateTime StartTime, DateTime EndOfTime, string returnObject, KeyInfo key, bool result) { Dictionary localInfo = new Dictionary(); // 本机信息; localInfo.Add("Item", "Value"); localInfo.Add("factory_id_I", "TCL"); localInfo.Add("operator_I", Line); localInfo.Add("station_I", Station); localInfo.Add("fixture_id_I", "1_1"); localInfo.Add("gui_ver_I", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()); localInfo.Add("route_check_I", "1"); // SMES信息; string strLastOrder = CommonMethod.ReadProfileString("MOKAFactoryTools", "LastOrder", ""); if (!strLastOrder.Equals(order, StringComparison.OrdinalIgnoreCase)) { // 请求新的订单信息; MaInfo maInfo; if (!GetMaInfo(sn, order, out maInfo, null)) { Log.WriteErrorLog("Kayla GetMaInfo失败"); return false; } LastKaylaOrderInfo = new Dictionary(); if (maInfo != null) { LastKaylaOrderInfo.Add("project_name_I", maInfo.project_name_I); LastKaylaOrderInfo.Add("build_I", maInfo.build_I); LastKaylaOrderInfo.Add("build_config_I", maInfo.build_config_I); LastKaylaOrderInfo.Add("diag_ver_I", maInfo.diag_ver_I); LastKaylaOrderInfo.Add("image_name_ver_I", maInfo.image_name_ver_I); LastKaylaOrderInfo.Add("soc_type", maInfo.soc_type); LastKaylaOrderInfo.Add("speaker_vender", maInfo.speaker_vender); LastKaylaOrderInfo.Add("mic_vender", maInfo.mic_vender); } } Dictionary dic = localInfo.Union(LastKaylaOrderInfo).ToDictionary(k => k.Key, v => v.Value); dic.Add("start_time_I", StartTime.ToString()); dic.Add("serial_id_I", key != null ? key.DSN : ""); // DSN; dic.Add("FSN", sn); // 耐压值; if (returnObject != null && returnObject.Length > 0 && returnObject.Contains("Hightpot")) { Log.WriteInfoLog(String.Format("returnObjec={0}", returnObject)); // 删除头尾[]; returnObject = returnObject.Remove(0, 1); returnObject = returnObject.Remove(returnObject.Length - 1, 1); JObject jObject = JObject.Parse(returnObject); string hightpot = CommonMethod.JSON_SeleteNode(jObject, "Hightpot"); int pos = hightpot.LastIndexOf('/'); if (pos != -1) { hightpot = hightpot.Substring(pos + 1); pos = hightpot.IndexOf("mA"); // 使用正规去除数字+小数点以外的字符; string AC = hightpot.Substring(0, pos + 2); string DC = hightpot.Substring(pos + 2); AC = System.Text.RegularExpressions.Regex.Replace(AC, @"[^\d.]*", ""); DC = System.Text.RegularExpressions.Regex.Replace(DC, @"[^\d.]*", ""); dic.Add("AC_I", AC); dic.Add("DC_R", DC); } else { dic.Add("AC_I", ""); dic.Add("DC_R", ""); } } else { dic.Add("AC_I", ""); dic.Add("DC_R", ""); Log.WriteErrorLog("获取耐压值结果为空"); } if (key != null) { if (key.DID.enable) { dic.Add("DID", key.DID); // 本身就是字符串; dic.Add("DID_check", key.DID.result ? "PASS" : "FAIL"); } if (key.Mac.enable) { dic.Add("Mac", key.Mac);//keyInfo.Mac.data.Replace("-", ":"); dic.Add("Mac_check", key.Mac.result ? "PASS" : "FAIL"); } if (key.HDCP.enable) { dic.Add("HDCP", key.HDCP); dic.Add("HDCP_check", key.HDCP.result ? "PASS" : "FAIL"); } if (key.HDCP22.enable) { dic.Add("HDCP22", key.HDCP22); dic.Add("HDCP22_check", key.HDCP22.result ? "PASS" : "FAIL"); } if (key.ESN.enable) { dic.Add("ESN", key.ESN); dic.Add("ESN_check", key.ESN.result ? "PASS" : "FAIL"); } if (key.Widevine.enable) { dic.Add("Widevine", key.Widevine); dic.Add("Widevine_check", key.Widevine.result ? "PASS" : "FAIL"); } if (key.CI_plus.enable) { dic.Add("CI_plus", key.CI_plus); dic.Add("CI_plus_check", key.CI_plus.result ? "PASS" : "FAIL"); } if (key.Attestation.enable) { dic.Add("Attestation", key.Attestation); dic.Add("Attestation_check", key.Attestation.result ? "PASS" : "FAIL"); } if (key.MGK.enable) { dic.Add("MGK", key.MGK); dic.Add("MGK_check", key.MGK.result ? "PASS" : "FAIL"); } if (key.Fairplay.enable) { dic.Add("Fairplay", key.Fairplay); dic.Add("Fairplay_check", key.Fairplay.result ? "PASS" : "FAIL"); } if (key.DSN.enable) { dic.Add("DSN", key.DSN); dic.Add("DSN_check", key.DSN.result ? "PASS" : "FAIL"); } if (key.WiFi_MAC.enable) { dic.Add("WiFi_MAC", key.WiFi_MAC); dic.Add("WiFi_MAC_check", key.WiFi_MAC.result ? "PASS" : "FAIL"); } if (key.BT_MAC.enable) { dic.Add("BT_MAC", key.BT_MAC); dic.Add("BT_MAC_check", key.BT_MAC.result ? "PASS" : "FAIL"); } if (key.LEK.enable) { dic.Add("LEK", key.LEK); dic.Add("LEK_check", key.LEK.result ? "PASS" : "FAIL"); } if (key.PEK.enable) { dic.Add("PEK", key.PEK); dic.Add("PEK_check", key.PEK.result ? "PASS" : "FAIL"); } if (key.Playready.enable) { dic.Add("Playready", key.Playready); dic.Add("Playready_check", key.Playready.result ? "PASS" : "FAIL"); } if (key.Hashkey.enable) { dic.Add("Hashkey", key.Hashkey); dic.Add("Hashkey_check", key.Hashkey.result ? "PASS" : "FAIL"); } if (key.ECP.enable) { dic.Add("ECP", key.ECP); dic.Add("ECP_check", key.ECP.result ? "PASS" : "FAIL"); } if (key.YouTube_KEY.enable) { dic.Add("YouTube_KEY", key.YouTube_KEY); dic.Add("YouTube_KEY_check", key.YouTube_KEY.result ? "PASS" : "FAIL"); } if ( key.FVP.enable ) { dic.Add("FVP_set", key.FVP.data); dic.Add("FVP_check", key.FVP.result ? "PASS" : "FAIL"); } } dic.Add("cycle_time_I", EndOfTime.Subtract(StartTime).Seconds.ToString()); dic.Add("test_result_I", result ? "PASS" : "FAIL"); try { // 以DSN命名文件; if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Kayla")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Kayla"); } string savefile = string.Format("{0}Kayla\\{1}.csv", AppDomain.CurrentDomain.BaseDirectory, sn); Log.WriteInfoLog(string.Format("Kayla: sn={0}, savefile={1}", sn, savefile)); using (System.IO.StreamWriter file = new System.IO.StreamWriter(savefile, false)) { foreach (KeyValuePair kvp in dic) { file.WriteLine(string.Format("{0},{1}", kvp.Key, kvp.Value)); } } } catch (Exception ex) { Log.WriteErrorLog(string.Format("保存csv文件失败:{0}", ex.ToString())); return false; } CommonMethod.StartProcess(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "tcp_send.exe", sn); return true; } // 根据FSN获取服务器上与之绑定的PSN值; // 这个函数在哪里调用,被谁调用; // 成功怎么处理;失败怎么处理; public static bool GetMainBoardPSNWithSN(string FSN, out string PSN) { string msg; PSN = ""; HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { Encoding = Encoding.UTF8, Method = "post", ContentType = "application/x-www-form-urlencoded", KeepAlive = false }; item.URL = url_getMainBoardPSNWithSN + FSN; HttpResult hResult = http.GetHtml(item); if (hResult.StatusCode == System.Net.HttpStatusCode.OK) { try { using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html))) { JObject jObject = (JObject)JToken.ReadFrom(reader); reader.Close(); foreach (var ss in jObject["Tvparts"]) //查找某个字段与值 { //如果里面的TypeCode为MA(主板),读出他的PSN if (ss["PartTypeCode"].Value().Equals("MA", StringComparison.OrdinalIgnoreCase)) { PSN = ss["PartSN"].Value(); } } if (PSN == "" || PSN == null) { MessageBox.Show(LResource.FindPSNError); return false; } Log.WriteInfoLog("PartSN" + PSN); return true; } } catch (Exception e) { msg = e.Message; Log.WriteErrorLog("IsCuringBOM Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } } else { msg = hResult.StatusDescription; Log.WriteInfoLog("IsCuringBOM Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata); } return false; } } }