CommonMethod.cs 118 KB

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