123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using AwInitilizer.Assist;
- using AwInitilizer.Model;
- using InitializerModel;
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- namespace AwInitilizer.Procedure
- {
- public class FirmwareUploadProcedure : ProcedureBase
- {
- internal string Version;
- internal string fileName;
- internal string module;
- private FirmwareUpdateModel _model;
- public FirmwareUploadProcedure(FirmwareUpdateModel model) : base()
- {
- _model = model;
- Name = string.Format("Firmware {0} upload", model.Module);
- Content = string.Format("Update {0} Firemware", model.Module);
- Version = model.Version;
- fileName = model.FirmwareFileName;
- module = model.Module;
- }
- internal override async Task<bool> Run()
- {
- Logger.Print("Firmware Uploading...");
- var uploadResult = await Uploadfirmware(fileName);
- MesLogData.Add($"{module}Upload", uploadResult?"1":"0", uploadResult);
- return uploadResult;
- }
- internal async Task<bool> Uploadfirmware(string fileName)
- {
- try
- {
- var result = await EvApi.Uploadfirmware(fileName);
- InfoLog += $"get firmware update response {result.Response}\n";
- return result.Result;
- //using (var stream = File.Open(fileName, FileMode.Open))
- //{
- // UploadFileAsync(
- // new NameValueCollection()
- // {
- // {"fw_tag","iso" }
- // },
- // new UploadFile()
- // {
- // Name = "file",
- // Filename = Path.GetFileName(fileName),
- // Stream = stream
- // }
- // );
- //}
- //return true;
- //using (var stream = File.Open(fileName, FileMode.Open))
- //{
- // var response = await UploadFiles(
- // $"https://{ServerIpAddress}/upgrade_iso_action.php",
- // new List<UploadFile>() {
- // new UploadFile()
- // {
- // Name="file",
- // Filename= Path.GetFileName(fileName),
- // Stream = stream
- // }
- // },
- // new NameValueCollection() {
- // {"fw_tag","iso" }
- // }
- // );
- // var responseStr = Encoding.ASCII.GetString(response).ToLower();
- // InfoLog += $"get firmware update response {responseStr}\n";
- // if (responseStr.Contains("file is uploaded"))
- // return true;
- // return false;
- //}
- //return true;
- //using (WebClient webClient = new WebClient())
- //{
- // NameValueCollection parameters = new NameValueCollection();
- // parameters.Add("fw_tag", "iso");
- // webClient.QueryString = parameters;
- // var responseBytes = await webClient.UploadFileTaskAsync($"https://{ServerIpAddress}/upgrade_iso_action.php", fileName);
- // string responseString = Encoding.ASCII.GetString(responseBytes);
- // return true;
- //}
- }
- catch (Exception e)
- {
- Logger.Print("Upload Firmware Failed", isError: true);
- Logger.Print(e.Message + "", isError: true);
- InfoLog += "Upload Firmware Failed\n";
- InfoLog += e.Message;
- InfoLog += "\n";
- return false;
- }
- }
- public async Task<byte[]> UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
- {
- var request = (HttpWebRequest)HttpWebRequest.Create(address);
- request.Timeout = 60 * 1000;
- request.KeepAlive = true;
- request.Accept = "*/*";
- request.Method = "POST";
- request.Referer = address;
- request.Expect = "";
- var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
- request.ContentType = "multipart/form-data; boundary=" + boundary;
- boundary = "--" + boundary;
- using (var requestStream = request.GetRequestStream())
- {
- // Write the values
- foreach (string name in values.Keys)
- {
- var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
- requestStream.Write(buffer, 0, buffer.Length);
- buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
- requestStream.Write(buffer, 0, buffer.Length);
- buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
- requestStream.Write(buffer, 0, buffer.Length);
- }
- // Write the files
- foreach (var file in files)
- {
- var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
- requestStream.Write(buffer, 0, buffer.Length);
- buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
- requestStream.Write(buffer, 0, buffer.Length);
- var ctype = MimeMapping.GetMimeMapping(file.Filename);
- buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", MimeMapping.GetMimeMapping(file.Filename), Environment.NewLine));
- requestStream.Write(buffer, 0, buffer.Length);
- file.Stream.CopyTo(requestStream);
- buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
- requestStream.Write(buffer, 0, buffer.Length);
- }
- var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
- requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
- }
- using (var response = await request.GetResponseAsync())
- using (var responseStream = response.GetResponseStream())
- using (var stream = new MemoryStream())
- {
- responseStream.CopyTo(stream);
- return stream.ToArray();
- }
- }
- }
- }
|