123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using CCWin;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using SufeiUtil;
- using SXLibrary;
- using System;
- using System.Collections.Generic;
- using System.Data.Common;
- using System.Data.SQLite;
- using System.IO;
- using System.Net;
- using System.Threading;
- using System.Windows.Forms;
- namespace MOKA_Factory_Tools
- {
- public partial class Upgrade : Skin_Color
- {
- public string RequireHost;
- public string DownloadLink;
- public string DownloadSize;
- public string Ordername;
- public string Ordernumber;
- public string Orderclient;
- public string OrderPID;
- public string OrderSV;
- public string packet_md5;
- public bool[] threadendTag;
- public string[] filenameList;
- public int[] filestartpos;
- public int[] filesize;
- public string url;
- public bool union;
- public int thread;
- public bool EEPROMNow;
- public SQLiteConnection errorDBNow;
- public int wholesize;
- public string factorynumNow;
- public Upgrade(System.Data.SQLite.SQLiteConnection errorDB, string DownloadLinkInput, string factorynum)
- {
- InitializeComponent();
- skinLabel1.Text = LResource.DownloadUpgradePackage;
- this.Text = LResource.Download;
- Control.CheckForIllegalCrossThreadCalls = false;
- DownloadLink = DownloadLinkInput;
- errorDBNow = errorDB;
- factorynumNow = factorynum;
- }
- private void skinButton1_Click(object sender, EventArgs e)
- {
- this.Dispose();
- this.Close();
- }
- public void StartDownload()
- {
- url = DownloadLink;
- HttpWebRequest request;
- long filesizew = 0;
- try
- {
- request = (HttpWebRequest)HttpWebRequest.Create(url);
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
- filesizew = response.ContentLength;
- response.Close();
- request.Abort();
- }
- catch (Exception ex)
- {
- MessageBox.Show(LResource.Error + ": " + ex.Message, LResource.Error);
- Log.WriteErrorLog("\r\nCreate Http require error:"+ ex.Message);
- CommonMethod.ReportErrormsg("Fail to init download process", ex.Message + "\r\ndownload link:" + url, errorDBNow);
- return;
- }
- thread = Convert.ToInt32("1", 10);
- threadendTag = new bool[thread];
- filenameList = new string[thread];
- filestartpos = new int[thread];
- filesize = new int[thread];
- int filethread = (int)filesizew / thread;
- int filethreade = filethread + (int)filesizew % thread;
- string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\";
-
- for (int i = 0; i < thread; i++)
- {
- threadendTag[i] = false;
- filenameList[i] = filePath + Path.GetFileName(DownloadLink); ;
- if (i < thread - 1)
- {
- filestartpos[i] = filethread * i;
- filesize[i] = filethread - 1;
- }
- else
- {
- filestartpos[i] = filethread * i;
- filesize[i] = filethreade - 1;
- }
- }
- Thread[] threadk = new Thread[thread];
- HttpFile2[] httpfile = new HttpFile2[thread];
-
- for (int j = 0; j < thread; j++)
- {
- httpfile[j] = new HttpFile2(this, j);
- threadk[j] = new Thread(new ThreadStart(httpfile[j].ReceiveFile));
- threadk[j].Start();
- }
- }
- private void Download_Shown(object sender, EventArgs e)
- {
-
- StartDownload();
- }
- private void Download_FormClosed(object sender, FormClosedEventArgs e)
- {
- this.Dispose();
- this.Close();
- }
- public void SetspecificText(Label label, string str)
- {
- if (label.InvokeRequired)
- {
- BeginInvoke(new Action<string>(x => { label.Text = x.ToString(); }), str);
- }
- else
- {
- label.Text = str;
- }
- }
- }
- public class HttpFile2
- {
- public Upgrade formm;
- public int threadIndex;
- public string filenamea;
- public string strUrl;
- public FileStream fs;
- public HttpWebRequest request;
- public HttpWebResponse response;
- public Stream ns;
- public byte[] nbytes;
- public int nreadsize;
- public HttpFile2(Upgrade form, int thread)
- {
- formm = form;
- threadIndex = thread;
- }
- ~HttpFile2()
- {
- formm.Dispose();
- }
- public int GetThread
- {
- get
- {
- return threadIndex;
- }
- }
- public int GetFilesize
- {
- get
- {
- return formm.filesize[threadIndex];
- }
- }
- public void ReceiveFile()
- {
- filenamea = formm.filenameList[threadIndex];
- strUrl = formm.DownloadLink;
- ns = null;
- nbytes = new byte[1024];
- nreadsize = 0;
-
- formm.ProgressBar1.Maximum = Convert.ToInt32(Math.Ceiling(formm.filesize[threadIndex] / 1024.0f));
- fs = new FileStream(filenamea, FileMode.Create);
-
- try
- {
- request = (HttpWebRequest)WebRequest.Create(strUrl);
- request.KeepAlive = false;
-
- request.Proxy = null;
-
-
- request.Timeout = 5 * 60 * 1000;
- System.Net.ServicePointManager.DefaultConnectionLimit = 200;
-
-
- response = request.GetResponse() as HttpWebResponse;
- ns = response.GetResponseStream();
- nreadsize = ns.Read(nbytes, 0, nbytes.Length);
- int preocess = 0;
- while (nreadsize > 0)
- {
- fs.Write(nbytes, 0, nreadsize);
-
- nreadsize = ns.Read(nbytes, 0, nbytes.Length);
- preocess += (int)Math.Ceiling(nreadsize / 1024.0f);
- if(preocess<= formm.ProgressBar1.Maximum)
- {
- formm.ProgressBar1.Value = preocess;
- }
- }
- fs.Close();
- ns.Close();
- response.Close();
- GC.Collect();
- CommonMethod.UpgradeReport(UpgradeReportUrl.Officialurl, formm.factorynumNow, "Success", formm.errorDBNow);
- MessageBox.Show(LResource.UpgradeDownloadSuccess);
- System.Diagnostics.Process.Start(formm.filenameList[0]);
- Thread.Sleep(500);
- Environment.Exit(0);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, LResource.Error) ;
- Log.WriteErrorLog("\r\nDownload error:" + ex.Message);
- CommonMethod.ReportErrormsg("Fail to download upgrade package", ex.Message+"\r\ndownload link:"+ formm.DownloadLink, formm.errorDBNow);
- fs.Close();
- response.Close();
- formm.Close();
- GC.Collect();
- return;
- }
-
- formm.Close();
- }
- private static string JSON_SeleteNode(JToken json, string ReName)
- {
- try
- {
- string result = "";
-
- var node = json.SelectToken("$.." + ReName);
- if (node != null)
- {
-
- if (node.Type == JTokenType.String || node.Type == JTokenType.Integer || node.Type == JTokenType.Float)
- {
-
- result = node.Value<object>().ToString();
- }
- }
- return result;
- }
- catch (Exception ex)
- {
- return "";
- }
- }
- }
- }
|