CommonMethod.cs 116 KB

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