CommonMethod.cs 122 KB

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