CommonMethod.cs 121 KB

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