CommonMethod.cs 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using SufeiUtil;
  4. using SXLibrary;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Data.SQLite;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Management;
  13. using System.Net;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Windows.Forms;
  17. using System.Xml;
  18. using WHC.OrderWater.Commons;
  19. using HttpHelper = SufeiUtil.HttpHelper;
  20. using System.Web;
  21. using System.Security.Cryptography.X509Certificates;
  22. using System.Net.Security;
  23. using System.Runtime.InteropServices;
  24. using System.Diagnostics;
  25. using System.Linq;
  26. namespace MOKA_Factory_Tools
  27. {
  28. class CommonMethod
  29. {
  30. [DllImport("kernel32")]
  31. public static extern bool GetProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize);
  32. [DllImport("kernel32")]
  33. public static extern bool WriteProfileString(string lpApplicationName, string lpKeyName, string lpString);
  34. public static string FactoryName = "Unknown";
  35. private static string lastOrder = "";
  36. private static List<string> ListACASSN = new List<string>();
  37. private static LoginInput loginInput = new LoginInput();
  38. private static Dictionary<string,string> LastKaylaOrderInfo = new Dictionary<string,string>();
  39. /// <summary>
  40. /// 保存登录信息;
  41. /// </summary>
  42. /// <param name="strAccount"></param>
  43. /// <param name="strPssword"></param>
  44. public static void SaveLoginInfo(string strAccount, string strPssword)
  45. {
  46. loginInput.account = strAccount;
  47. loginInput.password = strPssword;
  48. }
  49. /// <summary>
  50. /// 获取登录账号;
  51. /// </summary>
  52. /// <returns></returns>
  53. public static string GetLoginAccout()
  54. {
  55. return loginInput.account;
  56. }
  57. public static string ReadProfileString(string lpApplicationName, string lpKeyName, string lpDefault)
  58. {
  59. StringBuilder MyString = new StringBuilder(256);
  60. GetProfileString(lpApplicationName, lpKeyName, lpDefault, MyString, 256);
  61. if ( MyString.Length > 0)
  62. return MyString.ToString();
  63. return lpDefault;
  64. }
  65. /// <summary>
  66. /// 更新G客户抄写状态;
  67. /// </summary>
  68. /// <param name="SN">当前SN</param>
  69. /// <param name="bResult">当前SN的抄写结果,true=pass, false=fail</param>
  70. public static void UpdateGSNProfile( string SN, bool bResult = false )
  71. {
  72. string strLastSN = ReadProfileString("MOKAFactoryTools", "LastGSN", "");
  73. if (!strLastSN.Equals(SN, StringComparison.OrdinalIgnoreCase))
  74. {
  75. WriteProfileString("MOKAFactoryTools", "LastGSN", SN);
  76. // 清空连续次数值;
  77. WriteProfileString("MOKAFactoryTools", "GFailCount", "0");
  78. // 清空上次测试状态;
  79. WriteProfileString("MOKAFactoryTools", "LastGResult", "0");
  80. }
  81. else
  82. {
  83. if (bResult == false)
  84. {
  85. int nGSNCount = Convert.ToInt32(ReadProfileString("MOKAFactoryTools", "GFailCount", "0")) + 1;
  86. // 更新连续测试中累计失败的次数;
  87. WriteProfileString("MOKAFactoryTools", "GFailCount", nGSNCount.ToString());
  88. }
  89. // 更新抄写状态;
  90. WriteProfileString("MOKAFactoryTools", "LastGResult", bResult ? "1" : "0");
  91. }
  92. }
  93. //public static string LocalMacAddress = GetMacAddress();
  94. /// <summary>
  95. /// 获取活动网卡硬件地址
  96. /// </summary>
  97. /// <returns></returns>
  98. public static string GetMacAddress()
  99. {
  100. string mac = "";
  101. using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
  102. {
  103. ManagementObjectCollection moc = mc.GetInstances();
  104. int i = 0;
  105. int j = 0;
  106. foreach (ManagementObject mo in moc)
  107. {
  108. if (mo["MacAddress"] != null)
  109. {
  110. if (Convert.ToBoolean(mo["IPEnabled"]))
  111. {
  112. if (j < 2)
  113. {
  114. mac = mo["MacAddress"].ToString() + "&" + mac;
  115. j++;
  116. }
  117. }
  118. else
  119. {
  120. if (i < 1)
  121. {
  122. mac += mo["MacAddress"].ToString() + "&";
  123. i++;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. return mac.Replace(":", "-");
  130. //return "C0-B6-F9-44-D7-BB";
  131. }
  132. /// <summary>
  133. ///
  134. /// </summary>
  135. /// <param name="runFilePath"></param>
  136. /// <param name="args"></param>
  137. /// <returns></returns>
  138. public static bool StartProcess(string runFilePath, string args)
  139. {
  140. try
  141. {
  142. Process process = new Process();//创建进程对象
  143. ProcessStartInfo startInfo = new ProcessStartInfo(runFilePath, args);
  144. startInfo.UseShellExecute = false;
  145. startInfo.CreateNoWindow = true;
  146. process.StartInfo = startInfo;
  147. return process.Start();
  148. //process.WaitForExit();
  149. }
  150. catch (Exception ex)
  151. {
  152. Log.WriteErrorLog(string.Format("打开进程{0}失败:{1}", runFilePath, ex.Message));
  153. }
  154. return false;
  155. }
  156. public static bool StartProcess(string runFilePath, string args, out string outPutMsg)
  157. {
  158. try
  159. {
  160. outPutMsg = null;
  161. Process process = new Process();
  162. ProcessStartInfo startInfo = new ProcessStartInfo(runFilePath, args);
  163. startInfo.UseShellExecute = false;
  164. process.StartInfo = startInfo;
  165. process.StartInfo.RedirectStandardError = true;
  166. process.StartInfo.RedirectStandardInput = true;
  167. process.StartInfo.RedirectStandardOutput = true;
  168. process.StartInfo.CreateNoWindow= true;
  169. process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "replace_tv_projects";
  170. process.Start();
  171. outPutMsg = process.StandardOutput.ReadToEnd();
  172. process.WaitForExit();
  173. process.Close();
  174. }
  175. catch(Exception ex)
  176. {
  177. outPutMsg = null;
  178. Log.WriteErrorLog(string.Format("打开进程{0}失败:{1}", runFilePath, ex.Message));
  179. return false;
  180. }
  181. Log.WriteInfoLog(string.Format("adb push结果={0}",outPutMsg));
  182. return true;
  183. }
  184. /// <summary>
  185. /// 获取订单信息
  186. /// </summary>
  187. /// <param name="url"></param>
  188. /// <param name="order"></param>
  189. /// <param name="pcmac"></param>
  190. /// <param name="iptvip"></param>
  191. /// <param name="version"></param>
  192. /// <param name="sqliteConn"></param>
  193. /// <param name="mid"></param>
  194. /// <returns></returns>
  195. public static bool GetMidInfo(string url, string order, string pcmac, string iptvip, string version, SQLiteConnection sqliteConn, out MidList mid)
  196. {
  197. mid = new MidList();
  198. OrderInput orderInput1 = new OrderInput { bid = order, mac = pcmac, version = "", clientType = "" };//"74-27-EA-6E-DA-9F"
  199. string jsonorder = JsonConvert.SerializeObject(orderInput1, Newtonsoft.Json.Formatting.Indented);
  200. HttpHelper http = new HttpHelper();
  201. HttpItem item = new HttpItem()
  202. {
  203. URL = url,//URL这里都是测试URl   必需项
  204. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  205. //Encoding = Encoding.Default,
  206. Method = "post",//URL     可选项 默认为Get
  207. Postdata = jsonorder,
  208. PostEncoding = Encoding.UTF8,
  209. KeepAlive = false
  210. };
  211. item.ContentType = "application/json;charset=utf-8";
  212. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  213. stopwatch.Start();
  214. HttpResult result = http.GetHtml(item);
  215. stopwatch.Stop();
  216. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  217. stopwatch.Reset();
  218. string ParseText = result.Html;
  219. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  220. {
  221. Log.WriteGetKeyLog("\r\nGet MID info\r\nUsing time:" + usingtime + "ms\r\n" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  222. }
  223. else
  224. {
  225. Log.WriteErrorLog("\r\nFail to GetMidInfo:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  226. ReportErrormsg("Fail to GetMidInfo", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  227. return false;
  228. }
  229. try
  230. {
  231. JObject jObject = JObject.Parse(ParseText);
  232. mid.message = jObject["message"].Value<string>();
  233. mid.code = jObject["code"].Value<string>();
  234. mid.factoryname = jObject["factoryName"].Value<string>();
  235. mid.factoryNum = jObject["factoryNum"].Value<string>();
  236. mid.version = jObject["version"].Value<string>();
  237. mid.quantity = jObject["quantity"].Value<string>();
  238. mid.projectid = jObject["projectId"].Value<string>();
  239. mid.clienttype = jObject["clientType"].Value<string>();
  240. mid.host = jObject["host"].Value<string>();
  241. mid.whiteType = jObject["whiteType"].Value<string>();
  242. object a = jObject["rokuCustomer"];
  243. if (jObject["rokuCustomer"].ToString().Length > 0)
  244. mid.rokuCustomer = new RokuCustomer()
  245. {
  246. id = JSON_SeleteNode(jObject["rokuCustomer"], "id").Trim(),
  247. ordernum = JSON_SeleteNode(jObject["rokuCustomer"], "ordernum").Trim(),
  248. brand = JSON_SeleteNode(jObject["rokuCustomer"], "brand").Trim(),
  249. region = JSON_SeleteNode(jObject["rokuCustomer"], "region").Trim(),
  250. oemmodel = JSON_SeleteNode(jObject["rokuCustomer"], "oemmodel").Trim(),
  251. supporturl = JSON_SeleteNode(jObject["rokuCustomer"], "supporturl").Trim(),
  252. supportphone = JSON_SeleteNode(jObject["rokuCustomer"], "supportphone").Trim(),
  253. productiondate = JSON_SeleteNode(jObject["rokuCustomer"], "productiondate").Trim(),
  254. remotetype = JSON_SeleteNode(jObject["rokuCustomer"], "remotetype").Trim(),
  255. updatetime = JSON_SeleteNode(jObject["rokuCustomer"], "updatetime").Trim(),
  256. createtime = JSON_SeleteNode(jObject["rokuCustomer"], "createtime").Trim(),
  257. isdelete = JSON_SeleteNode(jObject["rokuCustomer"], "isdelete").Trim()
  258. };
  259. mid.keytype = new Dictionary<string, string>();
  260. foreach (var ss in jObject["obj"]) //查找某个字段与值
  261. {
  262. mid.keytype.Add(ss["name"].Value<string>(), ss["type"].Value<string>());
  263. }
  264. if (mid.code == "1000")
  265. return true;
  266. else
  267. return false;
  268. }
  269. catch (Exception ex)
  270. {
  271. Log.WriteErrorLog("\r\nFail to parse MID info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  272. ReportErrormsg("Fail to parse MID info", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  273. MessageBox.Show("Fail to parse MID info\r\n " + ex.Message);
  274. return false;
  275. }
  276. }
  277. public static bool CheckKeyType(Dictionary<string, string> keyTypes, Config_WriteDone cfg_write, out List<KeyType> resultKeyType)
  278. {
  279. resultKeyType = new List<KeyType>();
  280. Func<string, List <KeyType> ,bool > check_key_type = (keyname, listType) =>
  281. {
  282. if (!keyTypes.ContainsKey(keyname)) {
  283. Log.WriteErrorLog(string.Format("{0} is not included in the available key type of the order", keyname));
  284. return false;
  285. }
  286. KeyType keyType = new KeyType();
  287. keyType.name = keyname;
  288. keyType.type = keyTypes[keyname];
  289. listType.Add(keyType);
  290. return true;
  291. };
  292. if (cfg_write.DIDWrite_YesNo)
  293. {
  294. if (!check_key_type("DeviceID", resultKeyType))
  295. {
  296. return false;
  297. }
  298. }
  299. if (cfg_write.MacWrite_YesNo)
  300. {
  301. if (!check_key_type("MAC", resultKeyType))
  302. {
  303. return false;
  304. }
  305. }
  306. if (cfg_write.HDCPWrite_YesNo)
  307. {
  308. if (!check_key_type("HDCP_KEY", resultKeyType))
  309. {
  310. return false;
  311. }
  312. }
  313. if (cfg_write.HDCP22Write_YesNo)
  314. {
  315. if (!check_key_type("HDCP2.2_KEY", resultKeyType))
  316. {
  317. return false;
  318. }
  319. }
  320. if (cfg_write.ESNWrite_YesNo)
  321. {
  322. if (!check_key_type("NETFILX_ESN", resultKeyType))
  323. {
  324. return false;
  325. }
  326. }
  327. if (cfg_write.WidevineWrite_YesNo)
  328. {
  329. if (!check_key_type("Widevine_KEY", resultKeyType))
  330. {
  331. return false;
  332. }
  333. }
  334. if (cfg_write.CI_plusWrite_YesNo)
  335. {
  336. if (!check_key_type("CI_PLUS_KEY", resultKeyType))
  337. {
  338. return false;
  339. }
  340. }
  341. if (cfg_write.AttestationWrite_YesNo)
  342. {
  343. if (!check_key_type("Attestation_key", resultKeyType))
  344. {
  345. return false;
  346. }
  347. }
  348. if (cfg_write.MGKWrite_YesNo)
  349. {
  350. if (!check_key_type("MGK_KEY", resultKeyType))
  351. {
  352. return false;
  353. }
  354. }
  355. if (cfg_write.FairplayWrite_YesNo)
  356. {
  357. if (!check_key_type("Fairplay_KEY", resultKeyType))
  358. {
  359. return false;
  360. }
  361. }
  362. if (cfg_write.ECPWrite_YesNo)
  363. {
  364. if (!check_key_type("ECP_KEY", resultKeyType))
  365. {
  366. return false;
  367. }
  368. }
  369. if (cfg_write.WifiMacWrite_YesNo)
  370. {
  371. if (!check_key_type("WiFi_MAC", resultKeyType))
  372. {
  373. return false;
  374. }
  375. }
  376. if (cfg_write.BTMacWrite_YesNo)
  377. {
  378. if (!check_key_type("BT_MAC", resultKeyType))
  379. {
  380. return false;
  381. }
  382. }
  383. if (cfg_write.LEKWrite_YesNo)
  384. {
  385. if (!check_key_type("LEK", resultKeyType))
  386. {
  387. return false;
  388. }
  389. }
  390. if (cfg_write.PEKWrite_YesNo)
  391. {
  392. if (!check_key_type("PEK", resultKeyType))
  393. {
  394. return false;
  395. }
  396. }
  397. if (cfg_write.PlayreadyWrite_YesNo)
  398. {
  399. if (!check_key_type("PlayReady_key", resultKeyType))
  400. {
  401. return false;
  402. }
  403. }
  404. if (cfg_write.HashWrite_YesNo)
  405. {
  406. if (!check_key_type("Hashkey", resultKeyType))
  407. {
  408. return false;
  409. }
  410. }
  411. if (cfg_write.DSNWrite_YesNo)
  412. {
  413. if (!check_key_type("DSN", resultKeyType))
  414. {
  415. return false;
  416. }
  417. }
  418. if (cfg_write.YouTubeWrite_YesNo)
  419. {
  420. if (!check_key_type("YouTube_KEY", resultKeyType))
  421. {
  422. return false;
  423. }
  424. }
  425. if (cfg_write.ACASKeyWrite_YesNo)
  426. {
  427. if (!check_key_type("ACAS_KEY", resultKeyType))
  428. {
  429. return false;
  430. }
  431. }
  432. if (cfg_write.DAKWrite_YesNo)
  433. {
  434. if (!check_key_type("DAK", resultKeyType))
  435. {
  436. return false;
  437. }
  438. }
  439. Log.WriteInfoLog("key type of order need to copy="+resultKeyType.ToString());
  440. return true;
  441. }
  442. /// <summary>
  443. /// 获取预绑定模式下的key包地址
  444. /// </summary>
  445. /// <param name="midInfo"></param>
  446. /// <param name="order"></param>
  447. /// <param name="sqliteConn"></param>
  448. /// <param name="midAddress"></param>
  449. /// <returns></returns>
  450. public static bool GetMidAddress(MidList midInfo, string order, SQLiteConnection sqliteConn, out MidAddress midAddress)
  451. {
  452. midAddress = new MidAddress();
  453. HttpHelper http = new HttpHelper();
  454. HttpItem item = new HttpItem()
  455. {
  456. Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  457. //Encoding = Encoding.Default,
  458. Method = "post",//URL     可选项 默认为Get
  459. Postdata = ("orderNumber=" + order + "&mac=" + HttpUtility.UrlEncode(TestMode.MAC)),
  460. KeepAlive = false
  461. };
  462. #region 新接口统一返回带/v1,getofflinelist.do依旧采用旧host地址;
  463. string url = midInfo.host;
  464. if (url.EndsWith("/v1"))
  465. {
  466. url = url.Replace("/v1", "");
  467. }
  468. item.URL = url + "/getofflinelist.do?";
  469. #endregion
  470. item.ContentType = "application/x-www-form-urlencoded";
  471. HttpResult result = http.GetHtml(item);
  472. string ParseText = result.Html;
  473. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  474. {
  475. }
  476. else
  477. {
  478. Log.WriteErrorLog("\r\nFail to get Mid address:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  479. CommonMethod.ReportErrormsg("Fail to get Mid address", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  480. return false;
  481. }
  482. try
  483. {
  484. JObject jObject = JObject.Parse(ParseText);
  485. midAddress.des = JSON_SeleteNode(jObject, "des");
  486. midAddress.code = JSON_SeleteNode(jObject, "code");
  487. midAddress.order = JSON_SeleteNode(jObject, "order_number");
  488. midAddress.number = JSON_SeleteNode(jObject, "order_quantity");
  489. midAddress.pid = JSON_SeleteNode(jObject, "project_id");
  490. midAddress.ctype = JSON_SeleteNode(jObject, "client_type");
  491. midAddress.purl = JSON_SeleteNode(jObject, "packet_url");
  492. midAddress.psize = JSON_SeleteNode(jObject, "packet_size");
  493. midAddress.pmd5 = JSON_SeleteNode(jObject, "packet_md5");
  494. midAddress.version = JSON_SeleteNode(jObject, "soft_version");
  495. midAddress.host = midInfo.host;
  496. Log.WriteGetKeyLog("\r\nGet Mid address success:" + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  497. return true;
  498. }
  499. catch (Exception ex)
  500. {
  501. Log.WriteErrorLog("\r\nFail to parse MID address," + ex.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  502. CommonMethod.ReportErrormsg("Fail to parse MID address", ex.Message + "\r\n" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  503. MessageBox.Show("Fail to parse MIDAddress info\r\n " + ex.Message);
  504. return false;
  505. }
  506. }
  507. /// <summary>
  508. /// 获取预绑定模式下的key包地址
  509. /// </summary>
  510. /// <param name="midInfo"></param>
  511. /// <param name="order"></param>
  512. /// <param name="sqliteConn"></param>
  513. /// <param name="midAddress"></param>
  514. /// <returns></returns>
  515. public static bool GetMidAddress2(MidList midInfo, string order, SQLiteConnection sqliteConn, out List<MidAddress> midAddressList)
  516. {
  517. midAddressList = new List<MidAddress>();
  518. HttpHelper http = new HttpHelper();
  519. HttpItem item = new HttpItem()
  520. {
  521. Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  522. //Encoding = Encoding.Default,
  523. Method = "post",//URL     可选项 默认为Get
  524. Postdata = ("orderNumber=" + order + "&mac=" + HttpUtility.UrlEncode(TestMode.MAC)),
  525. KeepAlive = false
  526. };
  527. #region 新接口统一返回带/v1,getofflinelist.do依旧采用旧host地址;
  528. string url = midInfo.host;
  529. if (url.EndsWith("/v1"))
  530. {
  531. url = url.Replace("/v1", "");
  532. }
  533. item.URL = url + "/getofflinelist.do?";
  534. #endregion
  535. item.ContentType = "application/x-www-form-urlencoded";
  536. HttpResult result = http.GetHtml(item);
  537. string ParseText = result.Html;
  538. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  539. {
  540. }
  541. else
  542. {
  543. Log.WriteErrorLog("\r\nFail to get Mid address:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  544. CommonMethod.ReportErrormsg("Fail to get Mid address", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  545. return false;
  546. }
  547. try
  548. {
  549. JObject jObject = JObject.Parse(ParseText);
  550. if (jObject["data"] != null)
  551. {
  552. foreach (var data in jObject["data"])
  553. {
  554. midAddressList.Add(new MidAddress()
  555. {
  556. des = JSON_SeleteNode(jObject, "des"),
  557. code = JSON_SeleteNode(jObject, "code"),
  558. order = data["order_number"].ToObject<string>(),
  559. number = data["order_quantity"].ToObject<string>(),
  560. pid = data["project_id"].ToObject<string>(),
  561. ctype = data["client_type"].ToObject<string>(),
  562. purl = data["packet_url"].ToObject<string>(),
  563. psize = data["packet_size"].ToObject<string>(),
  564. pmd5 = data["packet_md5"].ToObject<string>(),
  565. version = data["soft_version"].ToObject<string>(),
  566. host = midInfo.host
  567. });
  568. }
  569. }
  570. Log.WriteGetKeyLog("\r\nGet Mid address success:" + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  571. return true;
  572. }
  573. catch (Exception ex)
  574. {
  575. Log.WriteErrorLog("\r\nFail to parse MID address," + ex.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  576. CommonMethod.ReportErrormsg("Fail to parse MID address", ex.Message + "\r\n" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  577. MessageBox.Show("Fail to parse MIDAddress info\r\n " + ex.Message);
  578. return false;
  579. }
  580. }
  581. /// <summary>
  582. /// 解析XML格式的DID
  583. /// </summary>
  584. /// <param name="str"></param>
  585. /// <param name="node"></param>
  586. /// <param name="node2"></param>
  587. /// <param name="code"></param>
  588. /// <param name="desc"></param>
  589. /// <param name="value"></param>
  590. /// <returns></returns>
  591. public static bool GetDIDKeyXml(string str, string node, string node2, out string code, out string desc, out string value)
  592. {
  593. code = "";
  594. value = "";
  595. try
  596. {
  597. XmlDocument xml = new XmlDocument();
  598. //xml.Load(path);
  599. xml.LoadXml(str);
  600. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  601. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  602. if (code == "200")
  603. {
  604. value = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("deviceid").InnerText;
  605. string CRC32 = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("crc32").InnerText;
  606. if (CrcUtils.CRC32(value) == Convert.ToUInt32(CRC32))
  607. {
  608. return true;
  609. }
  610. else
  611. {
  612. desc = "CRC32 error";
  613. return false;
  614. }
  615. }
  616. return false;
  617. }
  618. catch (Exception ex)
  619. {
  620. desc = ex.Message;
  621. return false;
  622. }
  623. }
  624. /// <summary>
  625. /// 在线获取Key:最新接口;
  626. /// </summary>
  627. /// <param name="url"></param>
  628. /// <param name="sn"></param>
  629. /// <param name="order"></param>
  630. /// <param name="psn"></param>
  631. /// <param name="firetv_device_code"></param>
  632. /// <param name="connection"></param>
  633. /// <param name="key"></param>
  634. /// <param name="error"></param>
  635. /// <param name="md5"></param>
  636. /// <returns></returns>
  637. public static bool GetIDMKeys(string url, string sn, string order, string psn, string firetv_device_code, List<KeyType> key2Write, SQLiteConnection connection, out KeyInfo keys, out string msg)
  638. {
  639. msg = "";
  640. keys = new KeyInfo();
  641. string code;
  642. bool result = false;
  643. HttpHelper http = new HttpHelper();
  644. HttpItem item = new HttpItem()
  645. {
  646. Encoding = Encoding.UTF8,
  647. Method = "post",
  648. ContentType = "application/json",
  649. KeepAlive = false
  650. };
  651. // 计算函数耗时;
  652. Stopwatch watch = new Stopwatch();
  653. watch.Start();
  654. item.URL = url + "/bind/order";
  655. item.Postdata = string.Format("{{\"orderNum\":\"{0}\",\"sn\":\"{1}\",\"psn\":\"{2}\",\"orderCode\":\"{3}\",\"skipKey\":{4}}}", order, sn, psn, firetv_device_code, key2Write == null ? "[]" : JsonConvert.SerializeObject(key2Write).ToString());
  656. HttpResult httpResult = http.GetHtml(item);
  657. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK)
  658. {
  659. try {
  660. JObject jObject = JObject.Parse(httpResult.Html);
  661. msg = JSON_SeleteNode(jObject, "msg");
  662. code = JSON_SeleteNode(jObject, "code");
  663. if (code == "0")
  664. {
  665. Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject["data"].ToString());
  666. if (data != null)
  667. {
  668. Func<string, string, bool> CheckMD5 = (data, md5) =>
  669. {
  670. if (data == null)
  671. return true;
  672. if (!GetMD5.GetStrMd5(data).Equals(md5, StringComparison.OrdinalIgnoreCase))
  673. return false;
  674. return true;
  675. };
  676. if (data.ContainsKey("mac") && data.ContainsKey("mac_md5"))
  677. {
  678. keys.Mac.data = data["mac"];
  679. keys.Mac.md5 = data["mac_md5"];
  680. if (!CheckMD5(keys.Mac.data, keys.Mac.md5))
  681. {
  682. msg = string.Format("Mac Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Mac.data, keys.Mac.md5);
  683. goto end;
  684. }
  685. }
  686. if (data.ContainsKey("btMac") && data.ContainsKey("btMac_md5"))
  687. {
  688. keys.BT_MAC.data = data["btMac"];
  689. keys.BT_MAC.md5 = data["btMac_md5"];
  690. if (!CheckMD5(keys.BT_MAC.data, keys.BT_MAC.md5))
  691. {
  692. msg = string.Format("BT_MAC Key Value md5 Is Incorrect: data={0}, md5={1}", keys.BT_MAC.data, keys.BT_MAC.md5);
  693. goto end;
  694. }
  695. }
  696. if (data.ContainsKey("wifiMac") && data.ContainsKey("wifiMac_md5"))
  697. {
  698. keys.WiFi_MAC.data = data["wifiMac"];
  699. keys.WiFi_MAC.md5 = data["wifiMac_md5"];
  700. if (!CheckMD5(keys.WiFi_MAC.data, keys.WiFi_MAC.md5))
  701. {
  702. msg = string.Format("WiFi_MAC Key Value md5 Is Incorrect: data={0}, md5={1}", keys.WiFi_MAC.data, keys.WiFi_MAC.md5);
  703. goto end;
  704. }
  705. }
  706. if (data.ContainsKey("deviceid") && data.ContainsKey("deviceid_md5"))
  707. {
  708. keys.DID.data = data["deviceid"];
  709. keys.DID.md5 = data["deviceid_md5"];
  710. if (!CheckMD5(keys.DID.data, keys.DID.md5))
  711. {
  712. msg = string.Format("DID Key Value md5 Is Incorrect: data={0}, md5={1}", keys.DID.data, keys.DID.md5);
  713. goto end;
  714. }
  715. }
  716. if (data.ContainsKey("hdcp") && data.ContainsKey("hdcp_md5"))
  717. {
  718. keys.HDCP.data = data["hdcp"];
  719. keys.HDCP.md5 = data["hdcp_md5"];
  720. if (!CheckMD5(keys.HDCP.data, keys.HDCP.md5))
  721. {
  722. msg = string.Format("HDCP Key Value md5 Is Incorrect: data={0}, md5={1}", keys.HDCP.data, keys.HDCP.md5);
  723. goto end;
  724. }
  725. }
  726. if (data.ContainsKey("hdcp2") && data.ContainsKey("hdcp2_md5"))
  727. {
  728. keys.HDCP22.data = data["hdcp2"];
  729. keys.HDCP22.md5 = data["hdcp2_md5"];
  730. if (!CheckMD5(keys.HDCP22.data, keys.HDCP22.md5))
  731. {
  732. msg = string.Format("HDCP22 Key Value md5 Is Incorrect: data={0}, md5={1}", keys.HDCP22.data, keys.HDCP22.md5);
  733. goto end;
  734. }
  735. }
  736. if (data.ContainsKey("ci") && data.ContainsKey("ci_md5"))
  737. {
  738. keys.CI_plus.data = data["ci"];
  739. keys.CI_plus.md5 = data["ci_md5"];
  740. if (!CheckMD5(keys.CI_plus.data, keys.CI_plus.md5))
  741. {
  742. msg = string.Format("CI_plus Key Value md5 Is Incorrect: data={0}, md5={1}", keys.CI_plus.data, keys.CI_plus.md5);
  743. goto end;
  744. }
  745. }
  746. if (data.ContainsKey("ecp") && data.ContainsKey("ecp_md5"))
  747. {
  748. keys.ECP.data = data["ecp"];
  749. keys.ECP.md5 = data["ecp_md5"];
  750. if (!CheckMD5(keys.ECP.data, keys.ECP.md5))
  751. {
  752. msg = string.Format("ECP Key Value md5 Is Incorrect: data={0}, md5={1}", keys.ECP.data, keys.ECP.md5);
  753. goto end;
  754. }
  755. }
  756. if (data.ContainsKey("acas") && data.ContainsKey("acas_md5"))
  757. {
  758. keys.ACASKey_Data.data = data["acas"];
  759. keys.ACASKey_Data.md5 = data["acas_md5"];
  760. if (!CheckMD5(keys.ACASKey_Data.data, keys.ACASKey_Data.md5))
  761. {
  762. msg = string.Format("ACASKey_Data Key Value md5 Is Incorrect: data={0}, md5={1}", keys.ACASKey_Data.data, keys.ACASKey_Data.md5);
  763. goto end;
  764. }
  765. }
  766. if (data.ContainsKey("lek") && data.ContainsKey("lek_md5"))
  767. {
  768. keys.LEK.data = data["lek"];
  769. keys.LEK.md5 = data["lek_md5"];
  770. if (!CheckMD5(keys.LEK.data, keys.LEK.md5))
  771. {
  772. msg = string.Format("LEK Key Value md5 Is Incorrect: data={0}, md5={1}", keys.LEK.data, keys.LEK.md5);
  773. goto end;
  774. }
  775. }
  776. if (data.ContainsKey("pek") && data.ContainsKey("pek_md5"))
  777. {
  778. keys.PEK.data = data["pek"];
  779. keys.PEK.md5 = data["pek_md5"];
  780. if (!CheckMD5(keys.PEK.data, keys.PEK.md5))
  781. {
  782. msg = string.Format("PEK Key Value md5 Is Incorrect: data={0}, md5={1}", keys.PEK.data, keys.PEK.md5);
  783. goto end;
  784. }
  785. }
  786. if (data.ContainsKey("playready") && data.ContainsKey("playready_md5"))
  787. {
  788. keys.Playready.data = data["playready"];
  789. keys.Playready.md5 = data["playready_md5"];
  790. if (!CheckMD5(keys.Playready.data, keys.Playready.md5))
  791. {
  792. msg = string.Format("Playready Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Playready.data, keys.Playready.md5);
  793. goto end;
  794. }
  795. }
  796. if (data.ContainsKey("netfilx") && data.ContainsKey("netfilx_md5"))
  797. {
  798. keys.ESN.data = data["netfilx"];
  799. keys.ESN.md5 = data["netfilx_md5"];
  800. if (!CheckMD5(keys.ESN.data, keys.ESN.md5))
  801. {
  802. msg = string.Format("ESN Key Value md5 Is Incorrect: data={0}, md5={1}", keys.ESN.data, keys.ESN.md5);
  803. goto end;
  804. }
  805. }
  806. if (data.ContainsKey("hash") && data.ContainsKey("hash_md5"))
  807. {
  808. keys.Hashkey.data = data["hash"];
  809. keys.Hashkey.md5 = data["hash_md5"];
  810. if (!CheckMD5(keys.Hashkey.data, keys.Hashkey.md5))
  811. {
  812. msg = string.Format("Hashkey Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Hashkey.data, keys.Hashkey.md5);
  813. goto end;
  814. }
  815. }
  816. if (data.ContainsKey("mgk") && data.ContainsKey("mgk_md5"))
  817. {
  818. keys.MGK.data = data["mgk"];
  819. keys.MGK.md5 = data["mgk_md5"];
  820. if (!CheckMD5(keys.MGK.data, keys.MGK.md5))
  821. {
  822. msg = string.Format("MGK Key Value md5 Is Incorrect: data={0}, md5={1}", keys.MGK.data, keys.MGK.md5);
  823. goto end;
  824. }
  825. }
  826. if (data.ContainsKey("youtube") && data.ContainsKey("youtube_md5"))
  827. {
  828. keys.YouTube_KEY.data = data["youtube"];
  829. keys.YouTube_KEY.md5 = data["youtube_md5"];
  830. if (!CheckMD5(keys.YouTube_KEY.data, keys.YouTube_KEY.md5))
  831. {
  832. msg = string.Format("YouTube_KEY Key Value md5 Is Incorrect: data={0}, md5={1}", keys.YouTube_KEY.data, keys.YouTube_KEY.md5);
  833. goto end;
  834. }
  835. }
  836. if (data.ContainsKey("fairplay") && data.ContainsKey("fairplay_md5"))
  837. {
  838. keys.Fairplay.data = data["fairplay"];
  839. keys.Fairplay.md5 = data["fairplay_md5"];
  840. if (!CheckMD5(keys.Fairplay.data, keys.Fairplay.md5))
  841. {
  842. msg = string.Format("Fairplay Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Fairplay.data, keys.Fairplay.md5);
  843. goto end;
  844. }
  845. }
  846. if (data.ContainsKey("widevine") && data.ContainsKey("widevine_md5"))
  847. {
  848. keys.Widevine.data = data["widevine"];
  849. keys.Widevine.md5 = data["widevine_md5"];
  850. if (!CheckMD5(keys.Widevine.data, keys.Widevine.md5))
  851. {
  852. msg = string.Format("Widevine Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Widevine.data, keys.Widevine.md5);
  853. goto end;
  854. }
  855. }
  856. if (data.ContainsKey("attention") && data.ContainsKey("attention_md5"))
  857. {
  858. keys.Attestation.data = data["attention"];
  859. keys.Attestation.md5 = data["attention_md5"];
  860. if (!CheckMD5(keys.Attestation.data, keys.Attestation.md5))
  861. {
  862. msg = string.Format("Attestation Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Attestation.data, keys.Attestation.md5);
  863. goto end;
  864. }
  865. }
  866. if (data.ContainsKey("dsn") && data.ContainsKey("dsn_md5"))
  867. {
  868. keys.DSN.data = data["dsn"];
  869. keys.DSN.md5 = data["dsn_md5"];
  870. if (!CheckMD5(keys.DSN.data, keys.DSN.md5))
  871. {
  872. msg = string.Format("DSN Key Value md5 Is Incorrect: data={0}, md5={1}", keys.DSN.data, keys.DSN.md5);
  873. goto end;
  874. }
  875. }
  876. if (data.ContainsKey("dak") && data.ContainsKey("dak_md5"))
  877. {
  878. keys.DAK.data = data["dak"];
  879. keys.DAK.md5 = data["dak_md5"];
  880. if (!CheckMD5(keys.DAK.data, keys.DAK.md5))
  881. {
  882. msg = string.Format("DAK Key Value md5 Is Incorrect: data={0}, md5={1}", keys.DAK.data, keys.DAK.md5);
  883. goto end;
  884. }
  885. }
  886. result = true;
  887. }
  888. }
  889. }
  890. catch {
  891. msg = "解决Json失败";
  892. }
  893. }
  894. end:
  895. watch.Stop();
  896. Log.WriteInfoLog(string.Format("[GetIDMKeys] order={0},sn={1},Elapsed={2} GetKeys={3}", order, sn, watch.Elapsed.TotalMilliseconds, result ? "成功" : "失败"));
  897. if ( !result )
  898. ReportErrormsg("Fail to GetIDMKeys ", msg + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + httpResult.Html, connection);
  899. return result;
  900. }
  901. /// <summary>
  902. /// 异常上报
  903. /// </summary>
  904. /// <param name="reportType"></param>
  905. /// <param name="reportData"></param>
  906. /// <param name="sqliteConn"></param>
  907. /// <returns></returns>
  908. public static bool ReportErrormsg(string reportType, string reportData, SQLiteConnection sqliteConn)
  909. {
  910. // 计算函数耗时;
  911. Stopwatch watch = new Stopwatch();
  912. watch.Start();
  913. ErrorInfo ErrorInfo1 = new ErrorInfo { reportType = reportType, reportData = reportData.Replace("'", ""), version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), factory = FactoryName, mac = TestMode.MAC, reportTime = System.DateTime.Now.ToString(("yyyy-MM-dd HH:mm:ss")) };
  914. string jsonError = Newtonsoft.Json.JsonConvert.SerializeObject(ErrorInfo1, Newtonsoft.Json.Formatting.Indented);
  915. HttpHelper http = new HttpHelper();
  916. HttpItem item = new HttpItem()
  917. {
  918. URL = TestMode.errorreportServerurl,//URL这里都是测试URl   必需项
  919. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  920. //Encoding = Encoding.Default,
  921. Method = "post",//URL     可选项 默认为Get
  922. Postdata = jsonError,
  923. PostEncoding = Encoding.UTF8,
  924. KeepAlive = false
  925. };
  926. item.ContentType = "application/json;charset=utf-8";
  927. HttpResult result = http.GetHtml(item);
  928. string ParseText = result.Html;
  929. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  930. {
  931. Log.WriteGetKeyLog("\r\nReportErrormsg:\r\n" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + jsonError);
  932. }
  933. else
  934. {
  935. if (HTTPChecker(item.URL))
  936. SQLiteHelper.InsertDelayErrorReport(sqliteConn, new object[] { item.URL, item.Postdata });
  937. Log.WriteErrorLog("\r\nFail to ReportErrormsg:\r\n" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + jsonError);
  938. return false;
  939. }
  940. try
  941. {
  942. JObject jObject = JObject.Parse(ParseText);
  943. string message = jObject["message"].Value<string>();
  944. string code = jObject["code"].Value<string>();
  945. if (code != "1000")
  946. {
  947. Log.WriteErrorLog("\r\nError report return error:\r\n" + ParseText + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + jsonError);
  948. return false;
  949. }
  950. watch.Stop();
  951. Log.WriteInfoLog(string.Format("[ReportErrormsg] Elapsed={0}", watch.Elapsed.TotalMilliseconds));
  952. return true;
  953. }
  954. catch (Exception ex)
  955. {
  956. Log.WriteErrorLog("\r\nFail to parse Error report return info," + ex.Message + ":" + ParseText + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + jsonError);
  957. return false;
  958. }
  959. }
  960. /// <summary>
  961. /// 上报预绑定包下载结果
  962. /// </summary>
  963. /// <param name="url"></param>
  964. /// <param name="order"></param>
  965. /// <param name="sqliteConn"></param>
  966. /// <returns></returns>
  967. public static bool ReportDownloadStatus(string url, string order, SQLiteConnection sqliteConn)
  968. {
  969. HttpHelper http = new HttpHelper();
  970. HttpItem item = new HttpItem()
  971. {
  972. Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  973. //Encoding = Encoding.Default,
  974. Method = "post",//URL     可选项 默认为Get
  975. Postdata = ("orderNumber =" + order),
  976. KeepAlive = false
  977. };
  978. #region 新接口统一返回带/v1,getreportofflinesn.do依旧采用旧host地址;
  979. if (url.EndsWith("/v1"))
  980. {
  981. url = url.Replace("/v1", "");
  982. }
  983. url += "/getreportofflinesn.do?";
  984. item.URL = url;
  985. #endregion
  986. item.ContentType = "application/x-www-form-urlencoded";
  987. HttpResult result = http.GetHtml(item);
  988. string ParseText = result.Html;
  989. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  990. {
  991. //
  992. }
  993. else
  994. {
  995. Log.WriteErrorLog("\r\nFail to report download status:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order));
  996. CommonMethod.ReportErrormsg("Fail to report download status", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order), sqliteConn);
  997. return false;
  998. }
  999. try
  1000. {
  1001. JObject jObject = JObject.Parse(ParseText);
  1002. string des = JSON_SeleteNode(jObject, "des");
  1003. string code = JSON_SeleteNode(jObject, "code");
  1004. string data = JSON_SeleteNode(jObject, "data");
  1005. if (code == "1000")
  1006. return true;
  1007. else
  1008. {
  1009. Log.WriteErrorLog("\r\nError report download status,should be 1000:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order));
  1010. CommonMethod.ReportErrormsg("Error download status code return", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order), sqliteConn);
  1011. return false;
  1012. }
  1013. }
  1014. catch (Exception ex)
  1015. {
  1016. MessageBox.Show(ex.Message);
  1017. Log.WriteErrorLog("\r\n" + ex.Message + ":" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order));
  1018. CommonMethod.ReportErrormsg("Error parse download status return", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order), sqliteConn);
  1019. return false;
  1020. }
  1021. }
  1022. /// <summary>
  1023. /// 生成DB格式key json字符串
  1024. /// </summary>
  1025. /// <param name="keyInfo"></param>
  1026. /// <param name="keydata"></param>
  1027. /// <param name="error"></param>
  1028. /// <returns></returns>
  1029. public static bool CreateDBkeyString(KeyInfo keyInfo, out string keydata, out string error)
  1030. {
  1031. keydata = "";
  1032. error = "";
  1033. try
  1034. {
  1035. DBKeysStringFormat DBKeysStringFormat1 = new DBKeysStringFormat { cikey = keyInfo.CI_plus == null ? "" : keyInfo.CI_plus, esn = keyInfo.ESN == null ? "" : keyInfo.ESN, hdcp = keyInfo.HDCP == null ? "" : keyInfo.HDCP, widevine = keyInfo.Widevine == null ? "" : keyInfo.Widevine, hdcp22 = keyInfo.HDCP22 == null ? "" : keyInfo.HDCP22, did = keyInfo.DID == null ? "" : keyInfo.DID, mac = keyInfo.Mac == null ? "" : keyInfo.Mac, widi = keyInfo.WiDi == null ? "" : keyInfo.WiDi, attestation = keyInfo.Attestation == null ? "" : keyInfo.Attestation, mgk = keyInfo.MGK == null ? "" : keyInfo.MGK };
  1036. keydata = Newtonsoft.Json.JsonConvert.SerializeObject(DBKeysStringFormat1, Newtonsoft.Json.Formatting.Indented);
  1037. return true;
  1038. }
  1039. catch (Exception ex)
  1040. {
  1041. error = ex.Message;
  1042. return false;
  1043. }
  1044. }
  1045. /// <summary>
  1046. /// 获取相应json子节点的值
  1047. /// </summary>
  1048. /// <param name="childnodelist"></param>
  1049. public static string JSON_SeleteNode(JToken json, string ReName)
  1050. {
  1051. try
  1052. {
  1053. string result = "";
  1054. //这里6.0版块可以用正则匹配
  1055. var node = json.SelectToken("$.." + ReName);
  1056. if (node != null)
  1057. {
  1058. //判断节点类型
  1059. if (node.Type == JTokenType.String || node.Type == JTokenType.Integer || node.Type == JTokenType.Float)
  1060. {
  1061. //返回string值
  1062. result = node.Value<object>().ToString();
  1063. }
  1064. }
  1065. return result;
  1066. }
  1067. catch (Exception ex)
  1068. {
  1069. return "";
  1070. }
  1071. }
  1072. /// <summary>
  1073. /// 确认是否为有效的url(检测开头是否为http://或者https://)
  1074. /// </summary>
  1075. /// <param name="Value"></param>
  1076. /// <returns></returns>
  1077. public static bool HTTPChecker(string Value)
  1078. {
  1079. try
  1080. {
  1081. if ((Value.Substring(0, 7) == "http://") || (Value.Substring(0, 8) == "https://"))
  1082. {
  1083. return true;
  1084. }
  1085. else
  1086. return false;
  1087. }
  1088. catch (Exception ex)
  1089. {
  1090. return false;
  1091. }
  1092. }
  1093. /// <summary>
  1094. /// 检查是否能正常访问网络
  1095. /// </summary>
  1096. /// <param name="url"></param>
  1097. /// <returns></returns>
  1098. public static bool CheckUrlConnection(string url)
  1099. {
  1100. try
  1101. {
  1102. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  1103. myRequest.Method = "HEAD"; //设置提交方式可以为"get","head"等
  1104. myRequest.Timeout = 10000; //设置网页响应时间长度
  1105. myRequest.AllowAutoRedirect = false;//是否允许自动重定向
  1106. myRequest.UseDefaultCredentials = true;
  1107. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  1108. myRequest.UseDefaultCredentials = true;//权限设置
  1109. return (myResponse.StatusCode == HttpStatusCode.OK);//返回响应的状态
  1110. }
  1111. catch (Exception ex)
  1112. {
  1113. return false;
  1114. }
  1115. }
  1116. /// <summary>
  1117. /// 指定目录创建文件夹
  1118. /// </summary>
  1119. /// <param name="path"></param>
  1120. public static void CreateDirectory(string path)
  1121. {
  1122. if (!Directory.Exists(path))//如果不存在就创建file文件夹
  1123. {
  1124. Directory.CreateDirectory(path);
  1125. }
  1126. }
  1127. /// <summary>
  1128. /// 在线下载抄写模式抄写的上报
  1129. /// </summary>
  1130. /// <param name="content"></param>
  1131. /// <returns></returns>
  1132. public static bool UploadCopyResult(string content, string url, SQLiteConnection sqliteConn, string order, bool repeatupload, string id, int timeout)
  1133. {
  1134. if (timeout < 5000)
  1135. timeout = 5000;
  1136. HttpHelper http = new HttpHelper();
  1137. HttpItem item = new HttpItem()
  1138. {
  1139. Encoding = Encoding.UTF8,
  1140. Method = "post",
  1141. ContentType = "application/x-www-form-urlencoded",
  1142. Timeout = timeout,
  1143. KeepAlive = false
  1144. };
  1145. item.PostEncoding = Encoding.UTF8;
  1146. #region 新接口统一返回带/v1,report.do依旧采用旧host地址;
  1147. if ( url.EndsWith("/v1") )
  1148. {
  1149. url = url.Replace("/v1", "");
  1150. }
  1151. item.URL = url + "/report.do?";
  1152. #endregion
  1153. item.Postdata = content;
  1154. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  1155. stopwatch.Start();
  1156. HttpResult result = http.GetHtml(item);
  1157. stopwatch.Stop();
  1158. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  1159. stopwatch.Reset();
  1160. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1161. {
  1162. if (Xmlconfig.ReportXml(result.Html, "response", out string code, out string desc))
  1163. {
  1164. Log.WriteGetKeyLog("\r\nUpload copy status success:\r\nUsing time:" + usingtime + "ms\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  1165. return true;
  1166. }
  1167. else
  1168. {
  1169. CommonMethod.ReportErrormsg("Fail to parse copy result return", desc + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata, sqliteConn);
  1170. Log.WriteErrorLog(code + "Error");
  1171. if (repeatupload)
  1172. {
  1173. SQLiteHelper.DeleteDelayCopyReport(sqliteConn, id);
  1174. }
  1175. return false;
  1176. }
  1177. }
  1178. else
  1179. {
  1180. if (repeatupload)
  1181. return false;
  1182. if (CommonMethod.HTTPChecker(item.URL))
  1183. SQLiteHelper.InsertDelayCopyReport(sqliteConn, new object[] { order, item.URL, item.Postdata });
  1184. CommonMethod.ReportErrormsg("Fail to upload copy result", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata, sqliteConn);
  1185. return true;
  1186. }
  1187. }
  1188. /// <summary>
  1189. /// 本地DB存储的异常重新上报
  1190. /// </summary>
  1191. /// <param name="reportType"></param>
  1192. /// <param name="reportData"></param>
  1193. /// <param name="sqliteConn"></param>
  1194. /// <returns></returns>
  1195. public static bool ReportErrormsg2(string url, string content, out bool mark)
  1196. {
  1197. mark = false;
  1198. HttpHelper http = new HttpHelper();
  1199. HttpItem item = new HttpItem()
  1200. {
  1201. URL = url,//URL这里都是测试URl   必需项
  1202. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1203. //Encoding = Encoding.Default,
  1204. Method = "post",//URL     可选项 默认为Get
  1205. Postdata = content,
  1206. PostEncoding = Encoding.UTF8,
  1207. KeepAlive = false
  1208. };
  1209. item.ContentType = "application/json;charset=utf-8";
  1210. HttpResult result = http.GetHtml(item);
  1211. string ParseText = result.Html;
  1212. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1213. {
  1214. mark = true;
  1215. Log.WriteGetKeyLog("\r\nReportErrormsg:\r\n" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + content);
  1216. }
  1217. else
  1218. {
  1219. Log.WriteErrorLog("\r\nFail to ReportErrormsg:\r\n" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + content);
  1220. return false;
  1221. }
  1222. try
  1223. {
  1224. JObject jObject = JObject.Parse(ParseText);
  1225. string message = jObject["message"].Value<string>();
  1226. string code = jObject["code"].Value<string>();
  1227. if (code == "1000")
  1228. return true;
  1229. else
  1230. {
  1231. Log.WriteErrorLog("\r\nError report return error:\r\n" + ParseText + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + content);
  1232. return false;
  1233. }
  1234. }
  1235. catch (Exception ex)
  1236. {
  1237. Log.WriteErrorLog("\r\nFail to parse Error report return info," + ex.Message + ":" + ParseText + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + content);
  1238. return false;
  1239. }
  1240. }
  1241. /// <summary>
  1242. /// 判断目标字符串是否只有数字和字母
  1243. /// </summary>
  1244. /// <param name="value">目标字符串</param>
  1245. /// <returns></returns>
  1246. public static bool IsNumberAndWord(string value)
  1247. {
  1248. if (value is null)
  1249. return false;
  1250. Regex r = new Regex(@"^[a-zA-Z0-9]+$");
  1251. return r.Match(value).Success;
  1252. }
  1253. public static bool InsertJsonConfig(string node, string key)
  1254. {
  1255. JObject jObject;
  1256. using (StreamReader file = new StreamReader(LocalPath.localpath + "\\Config.json"))
  1257. {
  1258. using (JsonTextReader reader = new JsonTextReader(file))
  1259. {
  1260. jObject = (JObject)JToken.ReadFrom(reader);
  1261. jObject[node][key] = true;
  1262. }
  1263. }
  1264. using (StreamWriter writer = new StreamWriter(LocalPath.localpath + "\\Config.json"))
  1265. {
  1266. writer.Write(jObject.ToString());
  1267. }
  1268. return true;
  1269. }
  1270. public static bool AddJsonConfig(string key, object value)
  1271. {
  1272. JObject jObject;
  1273. using (StreamReader file = new StreamReader(LocalPath.localpath + "\\Config.json"))
  1274. {
  1275. using (JsonTextReader reader = new JsonTextReader(file))
  1276. {
  1277. jObject = (JObject)JToken.ReadFrom(reader);
  1278. jObject.Add(key, (JToken)value);
  1279. }
  1280. }
  1281. using (StreamWriter writer = new StreamWriter(LocalPath.localpath + "\\Config.json"))
  1282. {
  1283. writer.Write(jObject.ToString());
  1284. }
  1285. return false;
  1286. }
  1287. /// <summary>
  1288. /// 预下载模式抄写的上报
  1289. /// </summary>
  1290. /// <param name="copydate">烧录时间</param>
  1291. /// <returns></returns>
  1292. public static bool UploadPreloadCopyResult(string host, string copydate, string SN, SQLiteConnection localDBNow, SQLiteConnection sqliteConn, bool repeatupload, string id, int timeout)
  1293. {
  1294. string ParseText = "";
  1295. bool status;
  1296. try
  1297. {
  1298. List<PostDateClass> postList = new List<PostDateClass>();
  1299. postList.Add(new PostDateClass("sn", SN));
  1300. postList.Add(new PostDateClass("date", copydate));
  1301. #region 新接口统一返回带/v1,reportlist.do依旧采用旧host地址;
  1302. if (host.EndsWith("/v1"))
  1303. {
  1304. host = host.Replace("/v1", "");
  1305. }
  1306. #endregion
  1307. ParseText = FormPost.postFileMessage(host + "/reportlist.do?", postList, out status, timeout);
  1308. if (status)
  1309. {
  1310. JObject jObject = JObject.Parse(ParseText);
  1311. string des = CommonMethod.JSON_SeleteNode(jObject, "des");
  1312. string code = CommonMethod.JSON_SeleteNode(jObject, "code");
  1313. string data = JSON_SeleteNode(jObject, "data");
  1314. if (code == "1000")
  1315. {
  1316. //更新上报内容
  1317. if (SQLiteHelper.UpdateReportStatus(localDBNow, SN))
  1318. {
  1319. //更新成功
  1320. Log.WriteGetKeyLog("\r\nUpdateReportStatus success:\r\n" + host + "/reportlist.do?\r\n" + "sn=" + SN + "\r\ndate=" + copydate + "\r\n" + ParseText);
  1321. return true;
  1322. }
  1323. else
  1324. {
  1325. Log.WriteErrorLog("\r\nFail to UpdateReportStatus: " + SN);
  1326. return false;
  1327. }
  1328. }
  1329. else
  1330. {
  1331. CommonMethod.ReportErrormsg("Fail to UploadPreloadCopyResult", "Address: " + host + "/reportlist.do?" + "\r\nPostdata: " + "sn=" + SN + "\r\ndate=" + copydate + "\r\n" + ParseText, sqliteConn);
  1332. Log.WriteErrorLog("\r\nUpdateReportStatus code return error,should be 1000 " + "\r\n" + SN + ":" + ParseText + "\r\nAddress:" + host + "/reportlist.do?" + "\r\nPostdata: " + "sn=" + SN + "\r\ndate=" + copydate);
  1333. if (repeatupload)
  1334. {
  1335. SQLiteHelper.DeleteDelayCopyReport(sqliteConn, id);
  1336. }
  1337. return false;
  1338. }
  1339. }
  1340. else
  1341. {
  1342. if (repeatupload)
  1343. return false;
  1344. if (CommonMethod.HTTPChecker(host))
  1345. SQLiteHelper.InsertDelayCopyReport(sqliteConn, new object[] { "preload_mode", host, SN + "@" + copydate });
  1346. return false;
  1347. }
  1348. }
  1349. catch (Exception ex)
  1350. {
  1351. Log.WriteErrorLog("\r\nFail to parse UpdateReportStatus return: " + ex.Message + "\r\n" + SN + ":" + ParseText);
  1352. return false;
  1353. }
  1354. }
  1355. /// <summary>
  1356. /// 预下载模式抄写的批量上报
  1357. /// </summary>
  1358. /// <param name="host"></param>
  1359. /// <param name="postList"></param>
  1360. /// <param name="localDBNow"></param>
  1361. /// <param name="sqliteConn"></param>
  1362. /// <param name="repeatupload"></param>
  1363. /// <param name="id"></param>
  1364. /// <returns></returns>
  1365. public static bool UploadPreloadCopyResultList(string host, List<PostDateClass> postList, SQLiteConnection localDBNow, SQLiteConnection sqliteConn, bool repeatupload, string id)
  1366. {
  1367. string ParseText = "";
  1368. bool status;
  1369. try
  1370. {
  1371. #region 新接口统一返回带/v1,reportlist.do依旧采用旧host地址;
  1372. if (host.EndsWith("/v1"))
  1373. {
  1374. host = host.Replace("/v1", "");
  1375. }
  1376. #endregion
  1377. ParseText = FormPost.postFileMessage(host + "/reportlist.do?", postList, out status, 30000);
  1378. if (status)
  1379. {
  1380. JObject jObject = JObject.Parse(ParseText);
  1381. string des = CommonMethod.JSON_SeleteNode(jObject, "des");
  1382. string code = CommonMethod.JSON_SeleteNode(jObject, "code");
  1383. string data = JSON_SeleteNode(jObject, "data");
  1384. if (code == "1000")
  1385. {
  1386. Log.WriteGetKeyLog("UploadPreloadCopyResult successful\r\nAddress: " + host + "/reportlist.do?" + ParseText);
  1387. return true;
  1388. }
  1389. else
  1390. {
  1391. CommonMethod.ReportErrormsg("Fail to UploadPreloadCopyResult", "Address: " + host + "/reportlist.do?" + ParseText, sqliteConn);
  1392. Log.WriteErrorLog("\r\nUpdateReportStatus code return error,should be 1000 " + "\r\n" + ParseText + "\r\nAddress:" + host + "/reportlist.do?");
  1393. if (repeatupload)
  1394. {
  1395. SQLiteHelper.DeleteDelayCopyReport(sqliteConn, id);
  1396. }
  1397. return false;
  1398. }
  1399. }
  1400. else
  1401. {
  1402. Log.WriteErrorLog("Fail to UploadPreloadCopyResult\r\nAddress: " + host + "/reportlist.do?" + ParseText);
  1403. return false;
  1404. }
  1405. }
  1406. catch (Exception ex)
  1407. {
  1408. Log.WriteErrorLog("\r\nFail to parse UpdateReportStatus return: " + ex.Message + "\r\n" + ParseText);
  1409. return false;
  1410. }
  1411. }
  1412. /// <summary>
  1413. /// byte[]数组到int转换
  1414. /// </summary>
  1415. /// <param name="value"></param>
  1416. /// <returns></returns>
  1417. public static int BytelisttoInt(byte[] value)
  1418. {
  1419. Array.Reverse(value);
  1420. int result = 0;
  1421. for (int i = 0; i < value.Length; i++)
  1422. {
  1423. result += value[i] << i * 8;
  1424. }
  1425. return result;
  1426. }
  1427. /// <summary>
  1428. /// int类型到byte[]数组转换
  1429. /// </summary>
  1430. /// <param name="value"></param>
  1431. /// <returns></returns>
  1432. public static byte[] InttoBytelist(int value)
  1433. {
  1434. byte[] bytes = BitConverter.GetBytes(value);
  1435. Array.Reverse(bytes);
  1436. if (value == 0)
  1437. return bytes;
  1438. List<byte> list = new List<byte>();
  1439. list.AddRange(bytes);
  1440. while (true)
  1441. {
  1442. if (list[0] != 0)
  1443. break;
  1444. else
  1445. list.RemoveAt(0);
  1446. }
  1447. bytes = list.ToArray();
  1448. return bytes;
  1449. }
  1450. /// <summary>
  1451. /// 升级检查
  1452. /// </summary>
  1453. /// <param name="url"></param>
  1454. /// <param name="FactoryNum"></param>
  1455. /// <param name="sqliteConn"></param>
  1456. /// <param name="upmid"></param>
  1457. /// <returns></returns>
  1458. public static bool UpgradeCheck(string url, string FactoryNum, SQLiteConnection sqliteConn, out UpgradeMsgList upmid)
  1459. {
  1460. upmid = new UpgradeMsgList();
  1461. UpgradeCheckInput UpgradeCheckInput1 = new UpgradeCheckInput { appid = "SCBC_Factory_Tools", devicemodel = "SCBC_Factory_Tools", dnum = FactoryNum, ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), type = "pc" };//System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
  1462. string jsonorder = Newtonsoft.Json.JsonConvert.SerializeObject(UpgradeCheckInput1, Newtonsoft.Json.Formatting.Indented);
  1463. HttpHelper http = new HttpHelper();
  1464. HttpItem item = new HttpItem()
  1465. {
  1466. URL = url,//URL这里都是测试URl   必需项
  1467. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1468. //Encoding = Encoding.Default,
  1469. Method = "post",//URL     可选项 默认为Get
  1470. Postdata = jsonorder,
  1471. PostEncoding = Encoding.UTF8,
  1472. KeepAlive = false
  1473. };
  1474. item.ContentType = "application/json;charset=utf-8";
  1475. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
  1476. | SecurityProtocolType.Tls
  1477. | SecurityProtocolType.Tls11//(SecurityProtocolType)0x300 //Tls11
  1478. | SecurityProtocolType.Tls12;//(SecurityProtocolType)0xC00; //Tls12
  1479. HttpResult result = http.GetHtml(item);
  1480. string ParseText = result.Html;
  1481. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1482. {
  1483. Log.WriteGetKeyLog("\r\nUpgrade check," + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1484. }
  1485. else
  1486. {
  1487. Log.WriteErrorLog("\r\nFail to upgrade check:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1488. ReportErrormsg("Fail to upgrade check", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1489. return false;
  1490. }
  1491. try
  1492. {
  1493. JObject jObject = JObject.Parse(ParseText);
  1494. upmid.servertime = jObject["servertime"].Value<string>();
  1495. upmid.callid = jObject["callid"].Value<string>();
  1496. upmid.state = jObject["state"].Value<string>();
  1497. upmid.note = jObject["note"].Value<string>();
  1498. upmid.language = jObject["language"].Value<string>();
  1499. upmid.apiversion = jObject["apiversion"].Value<string>();
  1500. upmid.upgrade = new UpgradeMsgList2()
  1501. {
  1502. type = JSON_SeleteNode(jObject, "type"),
  1503. appid = JSON_SeleteNode(jObject, "appid"),
  1504. apptype = JSON_SeleteNode(jObject, "apptype"),
  1505. version = JSON_SeleteNode(jObject, "version"),
  1506. verid = JSON_SeleteNode(jObject, "verid"),
  1507. md5 = JSON_SeleteNode(jObject, "md5"),
  1508. size = JSON_SeleteNode(jObject, "size"),
  1509. minicon = JSON_SeleteNode(jObject, "minicon"),
  1510. midicon = JSON_SeleteNode(jObject, "midicon"),
  1511. fileurl = JSON_SeleteNode(jObject, "fileurl"),
  1512. increment = JSON_SeleteNode(jObject, "increment"),
  1513. appendver = JSON_SeleteNode(jObject, "appendver"),
  1514. updatetime = JSON_SeleteNode(jObject, "updatetime")
  1515. };
  1516. if (upmid.state == "0000")
  1517. return true;
  1518. else
  1519. {
  1520. return false;
  1521. }
  1522. }
  1523. catch (Exception ex)
  1524. {
  1525. Log.WriteErrorLog("\r\nFail to parse upgrade check info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1526. ReportErrormsg("Fail to parse upgrade check info", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1527. MessageBox.Show("Fail to parse upgrade check info\r\n " + ex.Message);
  1528. return false;
  1529. }
  1530. }
  1531. /// <summary>
  1532. /// 升级上报
  1533. /// </summary>
  1534. /// <param name="url"></param>
  1535. /// <param name="FactoryNum"></param>
  1536. /// <param name="Result"></param>
  1537. /// <param name="sqliteConn"></param>
  1538. /// <returns></returns>
  1539. public static bool UpgradeReport(string url, string FactoryNum, string Result, SQLiteConnection sqliteConn)
  1540. {
  1541. UpgradeReportInput UpgradeCheckInput1 = new UpgradeReportInput { appid = "SCBC_Factory_Tools", devmodel = "SCBC_Factory_Tools", dnum = FactoryNum, ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), type = "pc", result = Result };
  1542. string jsonorder = Newtonsoft.Json.JsonConvert.SerializeObject(UpgradeCheckInput1, Newtonsoft.Json.Formatting.Indented);
  1543. HttpHelper http = new HttpHelper();
  1544. HttpItem item = new HttpItem()
  1545. {
  1546. URL = url,//URL这里都是测试URl   必需项
  1547. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1548. //Encoding = Encoding.Default,
  1549. Method = "post",//URL     可选项 默认为Get
  1550. Postdata = jsonorder,
  1551. PostEncoding = Encoding.UTF8,
  1552. KeepAlive = false
  1553. };
  1554. item.ContentType = "application/json;charset=utf-8";
  1555. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
  1556. | SecurityProtocolType.Tls
  1557. | (SecurityProtocolType)0x300 //Tls11
  1558. | (SecurityProtocolType)0xC00; //Tls12
  1559. HttpResult result = http.GetHtml(item);
  1560. string ParseText = result.Html;
  1561. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1562. {
  1563. Log.WriteGetKeyLog("\r\nUpgrade report," + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1564. }
  1565. else
  1566. {
  1567. Log.WriteErrorLog("\r\nFail to upgrade report:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1568. ReportErrormsg("Fail to upgrade report", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1569. return false;
  1570. }
  1571. try
  1572. {
  1573. JObject jObject = JObject.Parse(ParseText);
  1574. return true;
  1575. }
  1576. catch (Exception ex)
  1577. {
  1578. Log.WriteErrorLog("\r\nFail to parse upgrade report info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1579. ReportErrormsg("Fail to parse upgrade report info", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1580. MessageBox.Show("Fail to parse upgrade report info\r\n " + ex.Message);
  1581. return false;
  1582. }
  1583. }
  1584. /// <summary>
  1585. /// 比较本地文件的MD5值
  1586. /// </summary>
  1587. /// <param name="filename"></param>
  1588. /// <param name="filetype"></param>
  1589. /// <param name="filemd5"></param>
  1590. /// <param name="error"></param>
  1591. /// <returns></returns>
  1592. public static bool CompareLocalkey(string filename, string filetype, string filemd5, out string error, out string filefullname)
  1593. {
  1594. error = "";
  1595. filefullname = "";
  1596. try
  1597. {
  1598. string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Keys\\" + filename + "\\" + filetype;
  1599. if (!Directory.Exists(filepath))
  1600. {
  1601. Directory.CreateDirectory(filepath);
  1602. }
  1603. DirectoryInfo Getkeyfile = new DirectoryInfo(filepath);
  1604. foreach (FileInfo keyfile in Getkeyfile.GetFiles())
  1605. {
  1606. string FileMD5 = GetMD5.GetMD5HashFromFile(keyfile.Name);
  1607. if (FileMD5 == filemd5)
  1608. {
  1609. Log.WriteGetKeyLog("Check md5file success,file md5:" + FileMD5 + "\r\nfile:" + keyfile.FullName);
  1610. filefullname = keyfile.FullName;
  1611. return true;
  1612. }
  1613. else
  1614. {
  1615. Log.WriteErrorLog("Check md5file error,delete file:" + keyfile.FullName + "\r\nfile md5" + FileMD5);
  1616. File.Delete(keyfile.FullName);
  1617. }
  1618. }
  1619. return false;
  1620. }
  1621. catch (Exception ex)
  1622. {
  1623. error = ex.Message;
  1624. return false;
  1625. }
  1626. }
  1627. /// <summary>
  1628. /// 数据MD5核对。当数据头为"MD5="时,key内容比对改为key内容MD5比对
  1629. /// </summary>
  1630. /// <param name="key"></param>
  1631. /// <param name="data"></param>
  1632. /// <param name="md5"></param>
  1633. /// <returns></returns>
  1634. public static bool CompareMD5(byte[] key, byte[] data)
  1635. {
  1636. string strdata = Encoding.ASCII.GetString(data);
  1637. Console.WriteLine(strdata);
  1638. if (strdata.StartsWith("MD5="))
  1639. {
  1640. string md5 = GetMD5.GetMD5Hash(key);
  1641. Console.WriteLine(md5);
  1642. return strdata.Remove(0, 4).Equals(md5);
  1643. }
  1644. return false;
  1645. }
  1646. /// <summary>
  1647. /// 将16进制字符串转为对应字节流写入生成key文件
  1648. /// </summary>
  1649. /// <param name="path"></param>
  1650. /// <param name="data"></param>
  1651. /// <param name="error"></param>
  1652. /// <returns></returns>
  1653. public static bool GetCreateHexkey(string path, string data, out string error)
  1654. {
  1655. error = "";
  1656. try
  1657. {
  1658. string filepath = path + ".bin";
  1659. byte[] bytedata = SerialInit.HexToByte(data);
  1660. //创建一个文件流
  1661. FileStream fs = new FileStream(filepath, FileMode.Create);
  1662. fs.Write(bytedata, 0, bytedata.Length);
  1663. fs.Close();
  1664. Log.WriteGetKeyLog("save key:" + filepath);
  1665. return true;
  1666. }
  1667. catch (Exception ex)
  1668. {
  1669. error = ex.Message;
  1670. Log.WriteErrorLog("save key error:" + error);
  1671. return false;
  1672. }
  1673. }
  1674. /// <summary>
  1675. /// 读取key文件内容转换为16进制字符串
  1676. /// </summary>
  1677. /// <param name="filepath"></param>
  1678. /// <param name="data"></param>
  1679. /// <param name="error"></param>
  1680. /// <returns></returns>
  1681. public static bool ReadHexFile(string filepath, out string data, out string error)
  1682. {
  1683. data = "";
  1684. error = "";
  1685. try
  1686. {
  1687. using FileStream fskey = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.None);
  1688. {
  1689. using BinaryReader brkey = new BinaryReader(fskey);
  1690. {
  1691. data = SerialInit.ByteToHex(brkey.ReadBytes((int)fskey.Length));
  1692. data = data.Replace(" ", "");
  1693. return true;
  1694. }
  1695. }
  1696. }
  1697. catch (Exception ex)
  1698. {
  1699. error = ex.Message;
  1700. return false;
  1701. }
  1702. }
  1703. /// <summary>
  1704. /// 账号登录
  1705. /// </summary>
  1706. /// <param name="url"></param>
  1707. /// <param name="account"></param>
  1708. /// <param name="password"></param>
  1709. /// <param name="pcmac"></param>
  1710. /// <param name="sqliteConn"></param>
  1711. /// <param name="loginOutput1"></param>
  1712. /// <returns></returns>
  1713. public static bool UserLogin(string url, string account, string password, string pcmac, SQLiteConnection sqliteConn, out LoginOutput loginOutput1)
  1714. {
  1715. loginOutput1 = new LoginOutput();
  1716. LoginInput LoginInput1 = new LoginInput { account = account, password = password, mac = pcmac };
  1717. string jsonorder = Newtonsoft.Json.JsonConvert.SerializeObject(LoginInput1, Newtonsoft.Json.Formatting.Indented);
  1718. HttpHelper http = new HttpHelper();
  1719. HttpItem item = new HttpItem()
  1720. {
  1721. URL = url,//URL这里都是测试URl   必需项
  1722. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1723. //Encoding = Encoding.Default,
  1724. Method = "post",//URL     可选项 默认为Get
  1725. Postdata = jsonorder,
  1726. PostEncoding = Encoding.UTF8,
  1727. KeepAlive = false
  1728. };
  1729. item.ContentType = "application/json;charset=utf-8";
  1730. ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;//
  1731. HttpResult result = http.GetHtml(item);
  1732. string ParseText = result.Html;
  1733. if (result.StatusCode == HttpStatusCode.OK)
  1734. {
  1735. Log.WriteGetKeyLog("\r\nLogin," + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1736. }
  1737. else
  1738. {
  1739. Log.WriteErrorLog("\r\nFail to login:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1740. ReportErrormsg("Fail to login", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1741. return false;
  1742. }
  1743. try
  1744. {
  1745. JObject jObject = JObject.Parse(ParseText);
  1746. loginOutput1.msg = jObject["msg"].Value<string>();
  1747. loginOutput1.code = jObject["code"].Value<string>();
  1748. loginOutput1.factoryname = JSON_SeleteNode(jObject, "factoryName");
  1749. loginOutput1.factorynum = JSON_SeleteNode(jObject, "factoryNum");
  1750. if (loginOutput1.code == "1000")
  1751. return true;
  1752. else
  1753. return false;
  1754. }
  1755. catch (Exception ex)
  1756. {
  1757. Log.WriteErrorLog("\r\nFail to login," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1758. ReportErrormsg("Fail to login", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1759. MessageBox.Show("Fail to parse login return\r\n " + ex.Message);
  1760. return false;
  1761. }
  1762. }
  1763. /// <summary>
  1764. /// 获取指定日期,在为一年中为第几周
  1765. /// </summary>
  1766. /// <param name="dt">指定时间</param>
  1767. /// <reutrn>返回第几周</reutrn>
  1768. public static int GetWeekOfYear(DateTime dt)
  1769. {
  1770. GregorianCalendar gc = new GregorianCalendar();
  1771. int weekOfYear = gc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
  1772. return weekOfYear;
  1773. }
  1774. /// <summary>
  1775. /// 版本号比较:new_version>=old_version
  1776. /// </summary>
  1777. /// <param name="new_version">8.4.0</param>
  1778. /// <param name="old_version">8.3.10</param>
  1779. /// <returns></returns>
  1780. public static bool CompareVersion(string new_version, string old_version)
  1781. {
  1782. try
  1783. {
  1784. if (string.IsNullOrEmpty(new_version) || string.IsNullOrEmpty(old_version))
  1785. return false;
  1786. Version v_new = new Version(new_version);
  1787. Version v_old = new Version(old_version);
  1788. if (v_new >= v_old)
  1789. return true;
  1790. return false;
  1791. }
  1792. catch (Exception ex)
  1793. {
  1794. throw ex;
  1795. }
  1796. }
  1797. /// <summary>
  1798. /// 获取白平衡信息
  1799. /// </summary>
  1800. /// <param name="url"></param>
  1801. /// <param name="order"></param>
  1802. /// <param name="pcmac"></param>
  1803. /// <param name="sqliteConn"></param>
  1804. /// <param name="WBList"></param>
  1805. /// <returns></returns>
  1806. public static bool GetWhiteBalanceInfo(string url, string order, string pcmac, SQLiteConnection sqliteConn, out WhiteBalanceList WBList)
  1807. {
  1808. WBList = new WhiteBalanceList();
  1809. WhiteBalanceInput WBInput1 = new WhiteBalanceInput { bid = order, mac = pcmac };//"74-27-EA-6E-DA-9F"
  1810. string jsonorder = Newtonsoft.Json.JsonConvert.SerializeObject(WBInput1, Newtonsoft.Json.Formatting.Indented);
  1811. HttpHelper http = new HttpHelper();
  1812. HttpItem item = new HttpItem()
  1813. {
  1814. URL = url,//URL这里都是测试URl   必需项
  1815. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1816. //Encoding = Encoding.Default,
  1817. Method = "post",//URL     可选项 默认为Get
  1818. Postdata = jsonorder,
  1819. PostEncoding = Encoding.UTF8,
  1820. KeepAlive = false
  1821. };
  1822. item.ContentType = "application/json;charset=utf-8";
  1823. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  1824. stopwatch.Start();
  1825. HttpResult result = http.GetHtml(item);
  1826. stopwatch.Stop();
  1827. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  1828. stopwatch.Reset();
  1829. string ParseText = result.Html;
  1830. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1831. {
  1832. Log.WriteGetKeyLog("\r\nGet WB info\r\nUsing time:" + usingtime + "ms\r\n" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1833. }
  1834. else
  1835. {
  1836. Log.WriteErrorLog("\r\nFail to WBinfo:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1837. ReportErrormsg("Fail to WBinfo", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1838. return false;
  1839. }
  1840. try
  1841. {
  1842. JObject jObject = JObject.Parse(ParseText);
  1843. WBList.message = jObject["message"].Value<string>();
  1844. WBList.code = jObject["code"].Value<string>();
  1845. object a = jObject["obj"];
  1846. if (jObject["obj"].ToString().Length > 0)
  1847. WBList.whiteBalanceInfo = new WhiteBalanceInfo()
  1848. {
  1849. id = JSON_SeleteNode(jObject["obj"], "id"),
  1850. ordernum = JSON_SeleteNode(jObject["obj"], "ordernum"),
  1851. hdmirgain = JSON_SeleteNode(jObject["obj"], "hdmirgain"),
  1852. hdmiggain = JSON_SeleteNode(jObject["obj"], "hdmiggain"),
  1853. hdmibgain = JSON_SeleteNode(jObject["obj"], "hdmibgain"),
  1854. nrgain = JSON_SeleteNode(jObject["obj"], "nrgain"),
  1855. nggain = JSON_SeleteNode(jObject["obj"], "nggain"),
  1856. nbgain = JSON_SeleteNode(jObject["obj"], "nbgain"),
  1857. lrgain = JSON_SeleteNode(jObject["obj"], "lrgain"),
  1858. lggain = JSON_SeleteNode(jObject["obj"], "lggain"),
  1859. lbgain = JSON_SeleteNode(jObject["obj"], "lbgain"),
  1860. updatetime = JSON_SeleteNode(jObject["obj"], "updatetime"),
  1861. createtime = JSON_SeleteNode(jObject["obj"], "createtime"),
  1862. isdelete = JSON_SeleteNode(jObject["obj"], "isdelete"),
  1863. isExemption = JSON_SeleteNode(jObject["obj"], "isExemption")
  1864. };
  1865. if (WBList.code == "1000")
  1866. return true;
  1867. else
  1868. return false;
  1869. }
  1870. catch (Exception ex)
  1871. {
  1872. Log.WriteErrorLog("\r\nFail to parse WB info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1873. ReportErrormsg("Fail to parse WB info", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1874. MessageBox.Show("Fail to parse WB info\r\n " + ex.Message);
  1875. return false;
  1876. }
  1877. }
  1878. private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  1879. {
  1880. return true;
  1881. }
  1882. /// <summary>
  1883. /// 导出日本的SN与ACASID到文本中;
  1884. /// </summary>
  1885. /// <param name="ordernum"></param>
  1886. /// <param name="sn"></param>
  1887. /// <param name="acasid"></param>
  1888. public static void ExportSNACASID(string ordernum, string sn, string acasid)
  1889. {
  1890. string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\SN-ACAS";
  1891. if (!Directory.Exists(path))
  1892. {
  1893. Directory.CreateDirectory(path);
  1894. }
  1895. if (!lastOrder.Equals(ordernum))
  1896. {
  1897. lastOrder = ordernum;
  1898. ListACASSN.Clear();
  1899. }
  1900. string msg = string.Format("{0};{1}", sn, acasid);
  1901. if (ListACASSN.Contains(msg))
  1902. {
  1903. return;
  1904. }
  1905. ListACASSN.Add(msg);
  1906. string fileName = path + "\\" + ordernum + ".txt";
  1907. var stream = new StreamWriter(fileName, true);
  1908. stream.WriteLine(msg);
  1909. stream.Close();
  1910. stream.Dispose();
  1911. }
  1912. /// <summary>
  1913. /// 是否抄写EDID;
  1914. /// </summary>
  1915. /// <param name="midInfo"></param>
  1916. /// <returns></returns>
  1917. public static bool IsEDIDCopy(MidList midInfo)
  1918. {
  1919. if (midInfo == null || midInfo.keytype == null)
  1920. return false;
  1921. if (!midInfo.keytype.ContainsKey("edid_pid") && !midInfo.keytype.ContainsKey("edid_model_name"))
  1922. return false;
  1923. return true;
  1924. }
  1925. #region G客户相关功能接口
  1926. public static bool GetMaInfo(string sn, out MaInfo maInfo, SQLiteConnection errorDBNow)
  1927. {
  1928. string desc;
  1929. maInfo = null;
  1930. string strPost = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
  1931. "<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/\">"+
  1932. "<soap:Body>"+
  1933. "<GetMaInfo xmlns=\"http://tempuri.org/\">"+
  1934. "<sn>"+sn+"</sn>"+
  1935. "</GetMaInfo>"+
  1936. "</soap:Body>"+
  1937. "</soap:Envelope>";
  1938. HttpHelper http = new HttpHelper();
  1939. HttpItem item = new HttpItem()
  1940. {
  1941. Encoding = Encoding.UTF8,
  1942. Method = "post",
  1943. ContentType = "text/xml",
  1944. KeepAlive = false
  1945. };
  1946. item.URL = "http://smes-prd-app01.tclking.com:9002/SAPService.asmx";
  1947. item.Postdata = strPost;
  1948. HttpResult result = http.GetHtml(item);
  1949. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1950. {
  1951. Log.WriteGetKeyLog("\r\nGetMaInfo :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  1952. if (!Xmlconfig.GetMaInfoXml(result.Html, out maInfo, out desc))
  1953. {
  1954. Log.WriteGetKeyLog("GetMaInfo error:" + desc + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  1955. return false;
  1956. }
  1957. }
  1958. else
  1959. {
  1960. Log.WriteGetKeyLog("GetMaInfo error:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  1961. return false;
  1962. }
  1963. return true;
  1964. }
  1965. /// <summary>
  1966. /// 上报数据抄写结果到MES系统中;
  1967. /// </summary>
  1968. /// <param name="fsn"></param>
  1969. /// <param name="dsn"></param>
  1970. /// <param name="dateTime">抄写时间</param>
  1971. /// <param name="result">抄写结果</param>
  1972. /// <param name="msg">如果上报失败,此参数为失败的描述信息</param>
  1973. /// <returns></returns>
  1974. public static bool ReportDataBurningResultToMES(string fsn, string dsn, string result, string dateTime, SQLiteConnection errorDBNow, out string msg, out string returnObject)
  1975. {
  1976. msg = "";
  1977. returnObject = "";
  1978. string strPost = string.Format("{{\"FSN\":\"{0}\",\"DSN\":\"{1}\",\"DataCopy\":\"{2}\",\"UPDATETIME\":\"{3}\"}}", fsn, dsn, result, dateTime);
  1979. HttpHelper http = new HttpHelper();
  1980. HttpItem item = new HttpItem()
  1981. {
  1982. Encoding = Encoding.UTF8,
  1983. Method = "post",
  1984. ContentType = "application/json",
  1985. KeepAlive = false
  1986. };
  1987. // 测试地址;
  1988. item.URL = "http://smes-app-prod.tclking.com/FireTVApi/api/SMES/RecordKey";
  1989. item.Postdata = strPost;
  1990. HttpResult hResult = http.GetHtml(item);
  1991. if (hResult.StatusCode == System.Net.HttpStatusCode.OK)
  1992. {
  1993. try
  1994. {
  1995. using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html)))
  1996. {
  1997. JObject jObject = (JObject)JToken.ReadFrom(reader);
  1998. reader.Close();
  1999. bool bResult = jObject["Result"].Value<bool>();
  2000. msg = jObject["EroMsg"].Value<string>();
  2001. if ( jObject["returnObject"] != null )
  2002. returnObject = jObject["returnObject"].ToString();
  2003. Log.WriteInfoLog("nReportDataBurningResultToMES:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2004. return bResult;
  2005. }
  2006. }
  2007. catch (Exception e)
  2008. {
  2009. msg = e.Message;
  2010. Log.WriteErrorLog("ReportDataBurningResultToMES Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2011. }
  2012. }
  2013. else
  2014. {
  2015. msg = hResult.StatusDescription;
  2016. Log.WriteInfoLog("ReportDataBurningResultToMES Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2017. }
  2018. // 没有进行数据库查重:惹数据已添加过,同一次FSN抄写多次失败后导致多条记录。
  2019. SQLiteHelper.InsertDelayErrorReport(errorDBNow, new object[] { item.URL, item.Postdata });
  2020. return false;
  2021. }
  2022. /// <summary>
  2023. /// 上传本地数据库失败的日志,重新上传;
  2024. /// </summary>
  2025. /// <param name="url"></param>
  2026. /// <param name="strPost"></param>
  2027. /// <param name="mark"></param>
  2028. /// <returns></returns>
  2029. public static bool ReportDataBurningResultToMES(string url, string strPost, out bool mark)
  2030. {
  2031. // 表示是否上传成功;
  2032. mark = false;
  2033. HttpHelper http = new HttpHelper();
  2034. HttpItem item = new HttpItem()
  2035. {
  2036. Encoding = Encoding.UTF8,
  2037. Method = "post",
  2038. ContentType = "application/json",
  2039. KeepAlive = false
  2040. };
  2041. item.URL = url;
  2042. item.Postdata = strPost;
  2043. HttpResult hResult = http.GetHtml(item);
  2044. if (hResult.StatusCode == System.Net.HttpStatusCode.OK)
  2045. {
  2046. try
  2047. {
  2048. using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html)))
  2049. {
  2050. JObject jObject = (JObject)JToken.ReadFrom(reader);
  2051. reader.Close();
  2052. mark = jObject["Result"].Value<bool>();
  2053. Log.WriteInfoLog("nReportDataBurningResultToMES:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2054. return true;
  2055. }
  2056. }
  2057. catch (Exception)
  2058. {
  2059. Log.WriteErrorLog("ReportDataBurningResultToMES error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2060. throw;
  2061. }
  2062. }
  2063. else
  2064. {
  2065. Log.WriteInfoLog("ReportDataBurningResultToMES error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2066. }
  2067. return false;
  2068. }
  2069. /// <summary>
  2070. /// 通过SN获取订单类型
  2071. /// </summary>
  2072. /// <param name="sn"></param>
  2073. /// <returns></returns>
  2074. public static bool GetOrderTypeFromSN(string sn, out string result, out string msg)
  2075. {
  2076. msg = "";
  2077. result = "";
  2078. HttpHelper http = new HttpHelper();
  2079. HttpItem item = new HttpItem()
  2080. {
  2081. Encoding = Encoding.UTF8,
  2082. Method = "post",
  2083. ContentType = "application/x-www-form-urlencoded",
  2084. KeepAlive = false
  2085. };
  2086. // 测试地址;
  2087. item.URL = "http://smes-app-prod.tclking.com/FireTVApi/api/SMES/getOrderProductStatus?sn=" + sn;
  2088. HttpResult hResult = http.GetHtml(item);
  2089. if (hResult.StatusCode == System.Net.HttpStatusCode.OK)
  2090. {
  2091. try
  2092. {
  2093. using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html)))
  2094. {
  2095. JObject jObject = (JObject)JToken.ReadFrom(reader);
  2096. reader.Close();
  2097. bool bResult = jObject["Result"].Value<bool>();
  2098. msg = jObject["EroMsg"].Value<string>();
  2099. string returnObject = jObject["returnObject"].ToString();
  2100. if (bResult)
  2101. {
  2102. if (returnObject.Contains("量产"))
  2103. result = "量产";
  2104. else if (returnObject.Contains("试产"))
  2105. result = "试产";
  2106. else
  2107. result = "错误返回";
  2108. Log.WriteInfoLog("\r\nIsCuringBOM :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + hResult.Html);
  2109. return true;
  2110. }
  2111. Log.WriteInfoLog("IsCuringBOM error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2112. }
  2113. }
  2114. catch (Exception e)
  2115. {
  2116. msg = e.Message;
  2117. Log.WriteErrorLog("IsCuringBOM Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2118. }
  2119. }
  2120. else
  2121. {
  2122. msg = hResult.StatusDescription;
  2123. Log.WriteInfoLog("IsCuringBOM Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2124. }
  2125. return false;
  2126. }
  2127. /// <summary>
  2128. // Section1:本机信息;
  2129. // Section2:SMES信息;
  2130. // Section3:实际抄写Key信息;
  2131. /// </summary>
  2132. /// <param name="Line"></param>
  2133. /// <param name="Station"></param>
  2134. /// <param name="order"></param>
  2135. /// <param name="sn"></param>
  2136. /// <param name="StartTime"></param>
  2137. /// <param name="EndOfTime"></param>
  2138. /// <param name="returnObject"></param>
  2139. /// <param name="key"></param>
  2140. /// <param name="result"></param>
  2141. /// <returns></returns>
  2142. public static bool GeneratKaylaData(string Line, string Station, string order, string sn, DateTime StartTime, DateTime EndOfTime, string returnObject, KeyInfo key, bool result)
  2143. {
  2144. Dictionary<string, string> localInfo = new Dictionary<string,string>();
  2145. // 本机信息;
  2146. localInfo.Add("Item", "Value");
  2147. localInfo.Add("factory_id_I", "TCL");
  2148. localInfo.Add("operator_I", Line);
  2149. localInfo.Add("station_I", Station);
  2150. localInfo.Add("fixture_id_I", "1_1");
  2151. localInfo.Add("gui_ver_I", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
  2152. localInfo.Add("route_check_I", "1");
  2153. // SMES信息;
  2154. string strLastOrder = ReadProfileString("MOKAFactoryTools", "LastOrder", "");
  2155. if ( !strLastOrder.Equals(order,StringComparison.OrdinalIgnoreCase) )
  2156. {
  2157. // 请求新的订单信息;
  2158. MaInfo maInfo;
  2159. if ( !GetMaInfo(sn, out maInfo, null) )
  2160. {
  2161. Log.WriteErrorLog("Kayla GetMaInfo失败");
  2162. return false;
  2163. }
  2164. LastKaylaOrderInfo = new Dictionary<string,string>();
  2165. if ( maInfo != null )
  2166. {
  2167. LastKaylaOrderInfo.Add("project_name_I", maInfo.project_name_I);
  2168. LastKaylaOrderInfo.Add("build_I", maInfo.build_I);
  2169. LastKaylaOrderInfo.Add("build_config_I", maInfo.build_config_I);
  2170. LastKaylaOrderInfo.Add("diag_ver_I", maInfo.diag_ver_I);
  2171. LastKaylaOrderInfo.Add("image_name_ver_I", maInfo.image_name_ver_I);
  2172. LastKaylaOrderInfo.Add("soc_type", maInfo.soc_type);
  2173. LastKaylaOrderInfo.Add("speaker_vender", maInfo.speaker_vender);
  2174. LastKaylaOrderInfo.Add("mic_vender", maInfo.mic_vender);
  2175. }
  2176. }
  2177. Dictionary<string, string> dic = localInfo.Union(LastKaylaOrderInfo).ToDictionary(k=>k.Key, v=>v.Value);
  2178. dic.Add("start_time_I", StartTime.ToString());
  2179. dic.Add("serial_id_I", key != null ? key.DSN : ""); // DSN;
  2180. dic.Add("FSN", sn);
  2181. // 耐压值;
  2182. if (returnObject != null && returnObject.Length > 0 && returnObject.Contains("Hightpot"))
  2183. {
  2184. Log.WriteInfoLog(String.Format("returnObjec={0}", returnObject));
  2185. // 删除头尾[];
  2186. returnObject = returnObject.Remove(0, 1);
  2187. returnObject = returnObject.Remove(returnObject.Length - 1, 1);
  2188. JObject jObject = JObject.Parse(returnObject);
  2189. string hightpot = JSON_SeleteNode(jObject, "Hightpot");
  2190. int pos = hightpot.LastIndexOf('/');
  2191. if (pos != -1)
  2192. {
  2193. hightpot = hightpot.Substring(pos+1);
  2194. pos = hightpot.IndexOf("mA");
  2195. // 使用正规去除数字+小数点以外的字符;
  2196. string AC = hightpot.Substring(0, pos + 2);
  2197. string DC = hightpot.Substring(pos + 2);
  2198. AC = System.Text.RegularExpressions.Regex.Replace(AC, @"[^\d.]*", "");
  2199. DC = System.Text.RegularExpressions.Regex.Replace(DC, @"[^\d.]*", "");
  2200. dic.Add("AC_I", AC);
  2201. dic.Add("DC_R", DC);
  2202. }
  2203. else
  2204. {
  2205. dic.Add("AC_I", "");
  2206. dic.Add("DC_R", "");
  2207. }
  2208. }
  2209. else
  2210. {
  2211. dic.Add("AC_I", "");
  2212. dic.Add("DC_R", "");
  2213. Log.WriteErrorLog("获取耐压值结果为空");
  2214. }
  2215. if ( key != null )
  2216. {
  2217. if (key.DID.enable)
  2218. {
  2219. dic.Add("DID", key.DID); // 本身就是字符串;
  2220. dic.Add("DID_check", key.DID.result ? "PASS" : "FAIL");
  2221. }
  2222. if (key.Mac.enable)
  2223. {
  2224. dic.Add("Mac", key.Mac);//keyInfo.Mac.data.Replace("-", ":");
  2225. dic.Add("Mac_check", key.Mac.result ? "PASS" : "FAIL");
  2226. }
  2227. if (key.HDCP.enable)
  2228. {
  2229. dic.Add("HDCP", key.HDCP);
  2230. dic.Add("HDCP_check", key.HDCP.result ? "PASS" : "FAIL");
  2231. }
  2232. if (key.HDCP22.enable)
  2233. {
  2234. dic.Add("HDCP22", key.HDCP22);
  2235. dic.Add("HDCP22_check", key.HDCP22.result ? "PASS" : "FAIL");
  2236. }
  2237. if (key.ESN.enable)
  2238. {
  2239. dic.Add("ESN", key.ESN);
  2240. dic.Add("ESN_check", key.ESN.result ? "PASS" : "FAIL");
  2241. }
  2242. if (key.WiDi.enable)
  2243. {
  2244. dic.Add("WiDi", key.WiDi);
  2245. dic.Add("WiDi_check", key.WiDi.result ? "PASS" : "FAIL");
  2246. }
  2247. if (key.Widevine.enable)
  2248. {
  2249. dic.Add("Widevine", key.Widevine);
  2250. dic.Add("Widevine_check", key.Widevine.result ? "PASS" : "FAIL");
  2251. }
  2252. if (key.CI_plus.enable)
  2253. {
  2254. dic.Add("CI_plus", key.CI_plus);
  2255. dic.Add("CI_plus_check", key.CI_plus.result ? "PASS" : "FAIL");
  2256. }
  2257. if (key.Attestation.enable)
  2258. {
  2259. dic.Add("Attestation", key.Attestation);
  2260. dic.Add("Attestation_check", key.Attestation.result ? "PASS" : "FAIL");
  2261. }
  2262. if (key.MGK.enable)
  2263. {
  2264. dic.Add("MGK", key.MGK);
  2265. dic.Add("MGK_check", key.MGK.result ? "PASS" : "FAIL");
  2266. }
  2267. if (key.Fairplay.enable)
  2268. {
  2269. dic.Add("Fairplay", key.Fairplay);
  2270. dic.Add("Fairplay_check", key.Fairplay.result ? "PASS" : "FAIL");
  2271. }
  2272. if (key.DSN.enable)
  2273. {
  2274. dic.Add("DSN", key.DSN);
  2275. dic.Add("DSN_check", key.DSN.result ? "PASS" : "FAIL");
  2276. }
  2277. if (key.WiFi_MAC.enable)
  2278. {
  2279. dic.Add("WiFi_MAC", key.WiFi_MAC);
  2280. dic.Add("WiFi_MAC_check", key.WiFi_MAC.result ? "PASS" : "FAIL");
  2281. }
  2282. if (key.BT_MAC.enable)
  2283. {
  2284. dic.Add("BT_MAC", key.BT_MAC);
  2285. dic.Add("BT_MAC_check", key.BT_MAC.result ? "PASS" : "FAIL");
  2286. }
  2287. if (key.LEK.enable)
  2288. {
  2289. dic.Add("LEK", key.LEK);
  2290. dic.Add("LEK_check", key.LEK.result ? "PASS" : "FAIL");
  2291. }
  2292. if (key.PEK.enable)
  2293. {
  2294. dic.Add("PEK", key.PEK);
  2295. dic.Add("PEK_check", key.PEK.result ? "PASS" : "FAIL");
  2296. }
  2297. if (key.Playready.enable)
  2298. {
  2299. dic.Add("Playready", key.Playready);
  2300. dic.Add("Playready_check", key.Playready.result ? "PASS" : "FAIL");
  2301. }
  2302. if (key.Hashkey.enable)
  2303. {
  2304. dic.Add("Hashkey", key.Hashkey);
  2305. dic.Add("Hashkey_check", key.Hashkey.result ? "PASS" : "FAIL");
  2306. }
  2307. if (key.ECP.enable)
  2308. {
  2309. dic.Add("ECP", key.ECP);
  2310. dic.Add("ECP_check", key.ECP.result ? "PASS" : "FAIL");
  2311. }
  2312. if (key.YouTube_KEY.enable)
  2313. {
  2314. dic.Add("YouTube_KEY", key.YouTube_KEY);
  2315. dic.Add("YouTube_KEY_check", key.YouTube_KEY.result ? "PASS" : "FAIL");
  2316. }
  2317. }
  2318. dic.Add("cycle_time_I", EndOfTime.Subtract(StartTime).Seconds.ToString());
  2319. dic.Add("test_result_I", result ? "PASS" : "FAIL");
  2320. try
  2321. {
  2322. // 以DSN命名文件;
  2323. if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory+ "Kayla"))
  2324. {
  2325. Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Kayla");
  2326. }
  2327. string savefile = string.Format("{0}Kayla\\{1}.csv", AppDomain.CurrentDomain.BaseDirectory, sn);
  2328. using (System.IO.StreamWriter file = new System.IO.StreamWriter(savefile, false))
  2329. {
  2330. foreach (KeyValuePair<string, string> kvp in dic)
  2331. {
  2332. file.WriteLine(string.Format("{0},{1}", kvp.Key, kvp.Value));
  2333. }
  2334. }
  2335. }
  2336. catch (Exception ex)
  2337. {
  2338. Log.WriteErrorLog(string.Format("保存csv文件失败:{0}", ex.ToString()));
  2339. return false;
  2340. }
  2341. StartProcess(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "tcp_send.exe", sn);
  2342. return true;
  2343. }
  2344. #endregion
  2345. #region 小米FireTV功能模块相关接口;
  2346. public static bool GetMIKey(string url, string sn, out MIKey mikey, out string msg)
  2347. {
  2348. msg = "";
  2349. mikey = new MIKey();
  2350. string strPost = String.Format("{{\"TVSN\":\"{0}\"}}", sn);
  2351. HttpHelper http = new HttpHelper();
  2352. HttpItem item = new HttpItem()
  2353. {
  2354. Encoding = Encoding.UTF8,
  2355. Method = "post",
  2356. ContentType = "application/json",
  2357. KeepAlive = false
  2358. };
  2359. item.URL = url;
  2360. item.Postdata = strPost;
  2361. HttpResult hResult = http.GetHtml(item);
  2362. if (hResult.StatusCode == System.Net.HttpStatusCode.OK)
  2363. {
  2364. try
  2365. {
  2366. using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html)))
  2367. {
  2368. JObject jObject = (JObject)JToken.ReadFrom(reader);
  2369. reader.Close();
  2370. bool bResult = jObject["Result"].Value<bool>();
  2371. msg = jObject["Error"].Value<string>();
  2372. JArray jAry = (JArray)JsonConvert.DeserializeObject(jObject["Data"].ToString());
  2373. if (jAry == null)
  2374. {
  2375. Log.WriteErrorLog("GetMIKey Json-null:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2376. return false;
  2377. }
  2378. if ( jAry.Count > 0 )
  2379. {
  2380. mikey = JsonConvert.DeserializeObject<MIKey>(jAry[0].ToString());
  2381. }
  2382. if (bResult)
  2383. {
  2384. Log.WriteInfoLog("GetMIKey Info:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2385. return true;
  2386. }
  2387. Log.WriteInfoLog("GetMIKey error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2388. }
  2389. }
  2390. catch (Exception e)
  2391. {
  2392. msg = e.Message;
  2393. Log.WriteErrorLog("GetMIKey Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2394. }
  2395. }
  2396. else
  2397. {
  2398. msg = hResult.StatusDescription;
  2399. Log.WriteInfoLog("GetMIKey Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2400. }
  2401. return false;
  2402. }
  2403. public static bool ReportMIKey(string url, ReportMIKey mikey, out string msg)
  2404. {
  2405. msg = "";
  2406. HttpHelper http = new HttpHelper();
  2407. HttpItem item = new HttpItem()
  2408. {
  2409. Encoding = Encoding.UTF8,
  2410. Method = "post",
  2411. ContentType = "application/json",
  2412. KeepAlive = false
  2413. };
  2414. item.URL = url;
  2415. item.Postdata = JsonConvert.SerializeObject(mikey); ;
  2416. HttpResult hResult = http.GetHtml(item);
  2417. if (hResult.StatusCode == System.Net.HttpStatusCode.OK)
  2418. {
  2419. try
  2420. {
  2421. using (JsonTextReader reader = new JsonTextReader(new StringReader(hResult.Html)))
  2422. {
  2423. JObject jObject = (JObject)JToken.ReadFrom(reader);
  2424. reader.Close();
  2425. bool bResult = jObject["Result"].Value<bool>();
  2426. msg = jObject["Error"].Value<string>();
  2427. string data = jObject["Data"].ToString();
  2428. if (bResult)
  2429. {
  2430. Log.WriteInfoLog("ReportMIKey info:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2431. return true;
  2432. }
  2433. Log.WriteInfoLog("ReportMIKey error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2434. }
  2435. }
  2436. catch (Exception e)
  2437. {
  2438. msg = e.Message;
  2439. Log.WriteErrorLog("ReportMIKey Json-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2440. }
  2441. }
  2442. else
  2443. {
  2444. msg = hResult.StatusDescription;
  2445. Log.WriteInfoLog("ReportMIKey Http-error:" + hResult.StatusDescription + "\r\n" + hResult.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2446. }
  2447. return false;
  2448. }
  2449. #endregion
  2450. }
  2451. /// <summary>
  2452. /// CRC32
  2453. /// </summary>
  2454. class CRC32
  2455. {
  2456. //生成CRC32码表
  2457. static uint[] Crc32Table;
  2458. public static void GetCRC32Table()
  2459. {
  2460. uint Crc;
  2461. Crc32Table = new uint[256];
  2462. int i, j;
  2463. for (i = 0; i < 256; i++)
  2464. {
  2465. Crc = (uint)i;
  2466. for (j = 8; j > 0; j--)
  2467. {
  2468. if ((Crc & 1) == 1)
  2469. Crc = (Crc >> 1) ^ 0xEDB88320;
  2470. else
  2471. Crc >>= 1;
  2472. }
  2473. Crc32Table[i] = Crc;
  2474. }
  2475. }
  2476. public static uint GetCRC32(byte[] bytes)
  2477. {
  2478. GetCRC32Table();
  2479. uint value = 0xffffffff;
  2480. int len = bytes.Length;
  2481. for (int i = 0; i < len; i++)
  2482. {
  2483. value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ bytes[i]];
  2484. }
  2485. return value ^ 0xffffffff;
  2486. }
  2487. }
  2488. /// <summary>
  2489. /// 本地抄写模式方法
  2490. /// </summary>
  2491. public class GetLocalKeyCount
  2492. {
  2493. static string LocalKeyPath = AppDomain.CurrentDomain.BaseDirectory + "\\Keys\\";
  2494. public static bool GetLocalDidCount(string order, out int num, ref MidList localmidList, SQLiteConnection errorDBNow)
  2495. {
  2496. num = 0;
  2497. try
  2498. {
  2499. string DIDpath = LocalKeyPath + order + "\\DeviceID";
  2500. if (Directory.Exists(DIDpath) == false)
  2501. return false;
  2502. else
  2503. {
  2504. DirectoryInfo GetDIDfile = new DirectoryInfo(DIDpath);
  2505. foreach (FileInfo keyfile in GetDIDfile.GetFiles())
  2506. {
  2507. string filename = keyfile.FullName;
  2508. if (keyfile.Name.Contains("Online"))
  2509. {
  2510. if (localmidList.host == null)
  2511. {
  2512. if (CommonMethod.GetMidInfo(TestMode.serverurl, order, TestMode.MAC, "", "", errorDBNow, out localmidList))
  2513. {
  2514. ;
  2515. }
  2516. else
  2517. {
  2518. num = -1;
  2519. return true;
  2520. }
  2521. }
  2522. HttpHelper http = new HttpHelper();
  2523. HttpItem item = new HttpItem()
  2524. {
  2525. Encoding = Encoding.Default,
  2526. Method = "post",
  2527. ContentType = "application/x-www-form-urlencoded"
  2528. };
  2529. string url1 = localmidList.host + "/statdeviceid.do?";
  2530. item.URL = url1;
  2531. item.Postdata = "devicetype=tcl_unknown_model";
  2532. HttpResult result = http.GetHtml(item);
  2533. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  2534. {
  2535. Log.WriteGetKeyLog("\r\nGetDeviceID count:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  2536. if (Xmlconfig.KeyCountXml(result.Html, "response", "count", out string code, out string desc, out string count))
  2537. {
  2538. num = Convert.ToInt32(count.Replace(",", ""));
  2539. return true;
  2540. }
  2541. else
  2542. {
  2543. num = -1;
  2544. return true;
  2545. }
  2546. }
  2547. else
  2548. {
  2549. num = -1;
  2550. return true;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. num = GetDIDfile.GetFiles().Length;
  2556. return true;
  2557. }
  2558. }
  2559. return true;
  2560. }
  2561. }
  2562. catch (Exception ex)
  2563. {
  2564. num = -1;
  2565. return true;
  2566. }
  2567. }
  2568. public static bool GetLocalMacCount(string order, out int num, ref MidList localmidList, SQLiteConnection errorDBNow)
  2569. {
  2570. num = 0;
  2571. try
  2572. {
  2573. string MACpath = LocalKeyPath + order + "\\MAC";
  2574. if (Directory.Exists(MACpath) == false)
  2575. return false;
  2576. else
  2577. {
  2578. DirectoryInfo GetMACfile = new DirectoryInfo(MACpath);
  2579. foreach (FileInfo keyfile in GetMACfile.GetFiles())
  2580. {
  2581. string filename = keyfile.FullName;
  2582. if (keyfile.Name.Contains("Online"))
  2583. {
  2584. if (localmidList.host == null)
  2585. {
  2586. if (CommonMethod.GetMidInfo(TestMode.serverurl, order, TestMode.MAC, "", "", errorDBNow, out localmidList))
  2587. {
  2588. ;
  2589. }
  2590. else
  2591. {
  2592. num = -1;
  2593. return true;
  2594. }
  2595. }
  2596. HttpHelper http = new HttpHelper();
  2597. HttpItem item = new HttpItem()
  2598. {
  2599. Encoding = Encoding.Default,
  2600. Method = "post",
  2601. ContentType = "application/x-www-form-urlencoded"
  2602. };
  2603. string url1 = localmidList.host + "/statmac.do?";
  2604. item.URL = url1;
  2605. item.Postdata = "typeString=" + localmidList.keytype["MAC"];
  2606. HttpResult result = http.GetHtml(item);
  2607. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  2608. {
  2609. Log.WriteGetKeyLog("\r\nGetMac count:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  2610. if (Xmlconfig.KeyCountXml(result.Html, "response", "count", out string code, out string desc, out string count))
  2611. {
  2612. num = Convert.ToInt32(count.Replace(",", ""));
  2613. return true;
  2614. }
  2615. else
  2616. {
  2617. num = -1;
  2618. return true;
  2619. }
  2620. }
  2621. else
  2622. {
  2623. num = -1;
  2624. return true;
  2625. }
  2626. }
  2627. else
  2628. {
  2629. num = GetMACfile.GetFiles().Length;
  2630. return true;
  2631. }
  2632. }
  2633. return true;
  2634. }
  2635. }
  2636. catch (Exception ex)
  2637. {
  2638. num = -1;
  2639. return true;
  2640. }
  2641. }
  2642. public static bool GetLocalHDCPKEYCount(string order, out int num)
  2643. {
  2644. num = 0;
  2645. try
  2646. {
  2647. string HDCPKEYpath = LocalKeyPath + order + "\\HDCPKEY";
  2648. if (Directory.Exists(HDCPKEYpath) == false)
  2649. return false;
  2650. else
  2651. {
  2652. DirectoryInfo GetHDCPKEYfile = new DirectoryInfo(HDCPKEYpath);
  2653. num = GetHDCPKEYfile.GetFiles().Length;
  2654. return true;
  2655. }
  2656. }
  2657. catch (Exception ex)
  2658. {
  2659. num = -1;
  2660. return true;
  2661. }
  2662. }
  2663. public static bool GetLocalHDCPKEY22Count(string order, out int num)
  2664. {
  2665. num = 0;
  2666. try
  2667. {
  2668. string HDCPKEY22path = LocalKeyPath + order + "\\HDCPKEY22";
  2669. if (Directory.Exists(HDCPKEY22path) == false)
  2670. return false;
  2671. else
  2672. {
  2673. DirectoryInfo GetHDCPKEY22file = new DirectoryInfo(HDCPKEY22path);
  2674. num = GetHDCPKEY22file.GetFiles().Length;
  2675. return true;
  2676. }
  2677. }
  2678. catch (Exception ex)
  2679. {
  2680. num = -1;
  2681. return true;
  2682. }
  2683. }
  2684. public static bool GetLocalWiDiCount(string order, out int num)
  2685. {
  2686. num = 0;
  2687. try
  2688. {
  2689. string WiDipath = LocalKeyPath + order + "\\WiDi";
  2690. if (Directory.Exists(WiDipath) == false)
  2691. return false;
  2692. else
  2693. {
  2694. DirectoryInfo GetWiDifile = new DirectoryInfo(WiDipath);
  2695. num = GetWiDifile.GetFiles().Length;
  2696. return true;
  2697. }
  2698. }
  2699. catch (Exception ex)
  2700. {
  2701. num = -1;
  2702. return true;
  2703. }
  2704. }
  2705. public static bool GetLocalWidevineCount(string order, out int num)
  2706. {
  2707. num = 0;
  2708. try
  2709. {
  2710. string Widevinepath = LocalKeyPath + order + "\\Widevine";
  2711. if (Directory.Exists(Widevinepath) == false)
  2712. return false;
  2713. else
  2714. {
  2715. DirectoryInfo GetWidevinefile = new DirectoryInfo(Widevinepath);
  2716. num = GetWidevinefile.GetFiles().Length;
  2717. return true;
  2718. }
  2719. }
  2720. catch (Exception ex)
  2721. {
  2722. num = -1;
  2723. return true;
  2724. }
  2725. }
  2726. public static bool GetLocalESNCount(string order, out int num)
  2727. {
  2728. num = 0;
  2729. try
  2730. {
  2731. string ESNpath = LocalKeyPath + order + "\\ESN";
  2732. if (Directory.Exists(ESNpath) == false)
  2733. return false;
  2734. else
  2735. {
  2736. DirectoryInfo GetESNfile = new DirectoryInfo(ESNpath);
  2737. num = GetESNfile.GetFiles().Length;
  2738. return true;
  2739. }
  2740. }
  2741. catch (Exception ex)
  2742. {
  2743. num = -1;
  2744. return true;
  2745. }
  2746. }
  2747. public static bool GetLocalMGKCount(string order, out int num)
  2748. {
  2749. num = 0;
  2750. try
  2751. {
  2752. string MGKpath = LocalKeyPath + order + "\\MGK";
  2753. if (Directory.Exists(MGKpath) == false)
  2754. return false;
  2755. else
  2756. {
  2757. DirectoryInfo GetMGKfile = new DirectoryInfo(MGKpath);
  2758. num = GetMGKfile.GetFiles().Length;
  2759. return true;
  2760. }
  2761. }
  2762. catch (Exception ex)
  2763. {
  2764. num = -1;
  2765. return true;
  2766. }
  2767. }
  2768. public static bool GetLocalFairplayCount(string order, out int num)
  2769. {
  2770. num = 0;
  2771. try
  2772. {
  2773. string Fairplaypath = LocalKeyPath + order + "\\Fairplay";
  2774. if (Directory.Exists(Fairplaypath) == false)
  2775. return false;
  2776. else
  2777. {
  2778. DirectoryInfo GetFairplayfile = new DirectoryInfo(Fairplaypath);
  2779. num = GetFairplayfile.GetFiles().Length;
  2780. return true;
  2781. }
  2782. }
  2783. catch (Exception ex)
  2784. {
  2785. num = -1;
  2786. return true;
  2787. }
  2788. }
  2789. public static bool GetLocalFolderKeyCount(string order, string keyname, out int num)
  2790. {
  2791. num = 0;
  2792. try
  2793. {
  2794. string Fairplaypath = LocalKeyPath + order + "\\" + keyname;
  2795. if (Directory.Exists(Fairplaypath) == false)
  2796. return false;
  2797. else
  2798. {
  2799. DirectoryInfo GetFairplayfile = new DirectoryInfo(Fairplaypath);
  2800. num = GetFairplayfile.GetFiles().Length;
  2801. return true;
  2802. }
  2803. }
  2804. catch (Exception ex)
  2805. {
  2806. num = -1;
  2807. return true;
  2808. }
  2809. }
  2810. public static bool GetLocalAttestationCount(string order, out int num)
  2811. {
  2812. num = 0;
  2813. try
  2814. {
  2815. string Attestationpath = LocalKeyPath + order + "\\Attestation";
  2816. if (Directory.Exists(Attestationpath) == false)
  2817. return false;
  2818. else
  2819. {
  2820. DirectoryInfo GetAttestationfile = new DirectoryInfo(Attestationpath);
  2821. num = GetAttestationfile.GetFiles().Length;
  2822. return true;
  2823. }
  2824. }
  2825. catch (Exception ex)
  2826. {
  2827. num = -1;
  2828. return true;
  2829. }
  2830. }
  2831. public static bool GetLocalCIKEYCount(string order, out int num)
  2832. {
  2833. num = 0;
  2834. try
  2835. {
  2836. string CIKEYpath = LocalKeyPath + order + "\\CIKEY";
  2837. if (Directory.Exists(CIKEYpath) == false)
  2838. return false;
  2839. else
  2840. {
  2841. DirectoryInfo GetCIKEYfile = new DirectoryInfo(CIKEYpath);
  2842. num = GetCIKEYfile.GetFiles().Length;
  2843. return true;
  2844. }
  2845. }
  2846. catch (Exception ex)
  2847. {
  2848. num = -1;
  2849. return true;
  2850. }
  2851. }
  2852. public static bool GetLocalStringKeys(string order, string keytype, int filesize, out FileStream fskey, out StreamReader brkey, out string data, out string filename, out string error, SQLiteConnection errorDBNow, ref MidList localmidList, string SN)
  2853. {
  2854. data = "";
  2855. filename = "";
  2856. error = "";
  2857. brkey = null;
  2858. fskey = null;
  2859. try
  2860. {
  2861. string keypath = LocalKeyPath + order + "\\" + keytype;
  2862. if (Directory.Exists(keypath) == false)
  2863. {
  2864. error = "File not exist";
  2865. return false;
  2866. }
  2867. else
  2868. {
  2869. DirectoryInfo Getkeyfile = new DirectoryInfo(keypath);
  2870. foreach (FileInfo keyfile in Getkeyfile.GetFiles())
  2871. {
  2872. filename = keyfile.FullName;
  2873. try
  2874. {
  2875. fskey = new FileStream(keyfile.FullName.ToString(), FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  2876. {
  2877. brkey = new StreamReader(fskey);
  2878. {
  2879. if (fskey.Length == filesize || filesize == -1)
  2880. {
  2881. data = brkey.ReadToEnd();
  2882. return true;
  2883. }
  2884. else
  2885. {
  2886. error = "File size error.\r\nFilename:" + keyfile.Name + "\r\nFilesize:" + fskey.Length.ToString();
  2887. return false;
  2888. }
  2889. }
  2890. }
  2891. }
  2892. catch
  2893. {
  2894. ;
  2895. }
  2896. }
  2897. return false;
  2898. }
  2899. }
  2900. catch (Exception ex)
  2901. {
  2902. error = ex.Message;
  2903. return false;
  2904. }
  2905. }
  2906. public static bool GetLocalHexKeys(string order, string keytype, int filesize, out FileStream fskey, out BinaryReader brkey, out string data, out string filename, out string error, SQLiteConnection errorDBNow, ref MidList localmidList, string SN)
  2907. {
  2908. data = "";
  2909. filename = "";
  2910. error = "";
  2911. brkey = null;
  2912. fskey = null;
  2913. try
  2914. {
  2915. string keypath = LocalKeyPath + order + "\\" + keytype;
  2916. if (Directory.Exists(keypath) == false)
  2917. {
  2918. error = "File not exist";
  2919. return false;
  2920. }
  2921. else
  2922. {
  2923. DirectoryInfo Getkeyfile = new DirectoryInfo(keypath);
  2924. foreach (FileInfo keyfile in Getkeyfile.GetFiles())
  2925. {
  2926. filename = keyfile.FullName;
  2927. try
  2928. {
  2929. fskey = new FileStream(keyfile.FullName.ToString(), FileMode.Open, FileAccess.Read, FileShare.None);
  2930. {
  2931. brkey = new BinaryReader(fskey);
  2932. {
  2933. if (fskey.Length == filesize || filesize == -1)
  2934. {
  2935. data = SerialInit.ByteToHex(brkey.ReadBytes((int)fskey.Length));
  2936. data = data.Replace(" ", "");
  2937. return true;
  2938. }
  2939. else
  2940. {
  2941. error = "File size error.\r\nFilename: " + keyfile.Name + "\r\nFilesize: " + fskey.Length.ToString();
  2942. return false;
  2943. }
  2944. }
  2945. }
  2946. }
  2947. catch
  2948. {
  2949. ;
  2950. }
  2951. }
  2952. return false;
  2953. }
  2954. }
  2955. catch (Exception ex)
  2956. {
  2957. error = ex.Message;
  2958. return false;
  2959. }
  2960. }
  2961. public static bool DeleteLocalKeys(ArrayList filelist, ArrayList Stream, ArrayList Reader)
  2962. {
  2963. try
  2964. {
  2965. if (filelist.Count == Reader.Count)
  2966. {
  2967. for (int i = 0; i < filelist.Count; i++)
  2968. {
  2969. if (filelist[i].ToString().Contains("DeviceID"))
  2970. ((StreamReader)Reader[i]).Dispose();
  2971. else
  2972. ((BinaryReader)Reader[i]).Dispose();
  2973. ((FileStream)Stream[i]).Dispose();
  2974. File.Delete(filelist[i].ToString());
  2975. }
  2976. return true;
  2977. }
  2978. else
  2979. return false;
  2980. }
  2981. catch (Exception ex)
  2982. {
  2983. for (int i = 0; i < Reader.Count; i++)
  2984. {
  2985. if (filelist[i].ToString().Contains("DeviceID"))
  2986. ((StreamReader)Reader[i]).Dispose();
  2987. else
  2988. ((BinaryReader)Reader[i]).Dispose();
  2989. ((FileStream)Stream[i]).Dispose();
  2990. }
  2991. return false;
  2992. }
  2993. }
  2994. public static bool UnlockLocalKeys(ArrayList filelist, ArrayList Stream, ArrayList Reader)
  2995. {
  2996. for (int i = 0; i < Reader.Count; i++)
  2997. {
  2998. if (filelist[i].ToString().Contains("DeviceID"))
  2999. ((StreamReader)Reader[i]).Dispose();
  3000. else
  3001. ((BinaryReader)Reader[i]).Dispose();
  3002. ((FileStream)Stream[i]).Dispose();
  3003. }
  3004. return true;
  3005. }
  3006. }
  3007. /// <summary>
  3008. /// 本地缓存记录
  3009. /// </summary>
  3010. public class LocalTxtRecord
  3011. {
  3012. public static bool LocalRecord(string record, string name)
  3013. {
  3014. try
  3015. {
  3016. string order = name + ".txt";
  3017. if (File.Exists(@order))
  3018. {
  3019. FileStream fs = new FileStream(@order, FileMode.Append);
  3020. byte[] data = System.Text.Encoding.Default.GetBytes("\r\n" + record);
  3021. fs.Write(data, 0, data.Length);
  3022. fs.Flush();
  3023. fs.Close();
  3024. }
  3025. else
  3026. {
  3027. FileStream fs = new FileStream(@order, FileMode.Create);
  3028. byte[] data = System.Text.Encoding.Default.GetBytes(record);
  3029. fs.Write(data, 0, data.Length);
  3030. fs.Flush();
  3031. fs.Close();
  3032. }
  3033. return true;
  3034. }
  3035. catch (Exception ex)
  3036. {
  3037. return false;
  3038. }
  3039. }
  3040. }
  3041. }