CommonMethod.cs 122 KB

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