CommonMethod.cs 134 KB

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