123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using FluentFTP;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace AwInitilizer.Procedure.FirmwareBundleUpload
- {
- public class FirmwareFtpUploadProcedure : ProcedureBase
- {
- public ErrorType Error { get; set; } = ErrorType.None;
- private ProcedureLog.LogWriter<FirmwareFtpUploadProcedure, LogEvent> LogWriter;
- private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
- {
- { LogEvent.UploadStartWait, "UploadStartWait" },
- { LogEvent.FirmwareUpload, "FirmwareUpload" },
- };
- private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
- {
- { LogEvent.UploadStartWait, "Wait restart {0}" },
- { LogEvent.FirmwareUpload, "Firmware Upload {0}" },
- };
- public FirmwareFtpUploadProcedure() : base()
- {
- Name = string.Format("Firmware Upload");
- Content = string.Format("Upload all Firemware");
- LogWriter = new ProcedureLog.LogWriter<FirmwareFtpUploadProcedure, LogEvent>(this)
- {
- ReportPair = ReportDict,
- LogPair = LogDict
- };
- }
- internal override async Task<bool> Run()
- {
- bool response = false;
- int pollingCnt = 0;
- for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
- {
- await Task.Delay(TimeSpan.FromSeconds(15));
- response = await CheckGetQuertyAction();
- if (response)
- break;
- }
- LogWriter.Log(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<14", pollingCnt * 0.25));
- //timeout
- if (pollingCnt >= 56)
- {
- LogWriter.Report(LogEvent.UploadStartWait, "fail", isError: true);
- Error = ErrorType.StartWaitTimeout;
- return false;
- }
- LogWriter.Report(LogEvent.UploadStartWait, "success");
- //upload firmware
- bool result = false;
- for (int tryCnt = 0; tryCnt < 10; tryCnt++)
- {
- result = await UploadWithFtp();
- if (result)
- break;
- else
- await Task.Delay(1000);
- }
- if (!result)
- {
- Error = ErrorType.UploadFailed;
- return false;
- }
- //signal Update start
- var signalUpdateResult = await SignalUpdate();
- return result;
- }
- private async Task<bool> UploadWithFtp()
- {
- var updateList = UpdateData.FirmwareUpdateModels;
- //FtpClient client = new FtpClient(ServerIpAddress, "root", "y42j/4cj84");
- FtpClient client = new FtpClient(ServerIpAddress, "vern", "delta");
- try
- {
- var profile = await client.AutoConnectAsync();
- if (profile == null)
- {
- throw new Exception("profile null");
- }
- }
- catch
- {
- LogWriter.Log($"Ftp Connect failed");
- LogWriter.Report(LogEvent.FirmwareUpload, "fail", isError: true);
- return false;
- }
- for (int modelIndex = 0; modelIndex < updateList.Count; modelIndex++) //foreach (var model in updateList)
- {
- var model = updateList[modelIndex];
- var firmwareFilePath = Path.Combine(Directory.GetCurrentDirectory(), model.FirmwareFileName);
- var remoteFilePath = string.Format("/mnt/{0}", Path.GetFileName(model.FirmwareFileName));
- var status = await client.UploadFileAsync(model.FirmwareFileName, remoteFilePath);
- if(status != FtpStatus.Success)
- {
- LogWriter.Log($"Firmware {modelIndex} upload failed");
- LogWriter.Report(LogEvent.FirmwareUpload, "fail", isError: true);
- return false;
- }
- }
- await client.DisconnectAsync();
- return true;
- }
- private async Task<bool> SignalUpdate()
- {
- var updateList = UpdateData.FirmwareUpdateModels;
- var restClient = new RestSharp.RestClient($"https://{ServerIpAddress}");
- restClient.ConfigureWebRequest((r) => { r.KeepAlive = true; });
- var request = new RestSharp.RestRequest("upgrade_iso_action.php", RestSharp.Method.POST);
- request.AlwaysMultipartFormData = true;
- request.AddHeader("Content-Type", "multipart/form-data");
- request.AddParameter("fw_tag", "iso");
- //request.AddFile("files[]", new byte[0] , "dummy");
- RestSharp.IRestResponse response = await restClient.ExecuteAsync(request);
- if (response.IsSuccessful)
- {
- return true;
- }
- else
- {
- if (response.ErrorException != null)
- {
- LogWriter.Log(response.ErrorException.Message, isError: true, isDebugLog: true);
- if (response.ErrorException.InnerException != null)
- {
- LogWriter.Log(response.ErrorException.InnerException.Message, isError: true, isDebugLog: true);
- }
- LogWriter.Log(response.StatusCode.ToString(), isError: true, isDebugLog: true);
- }
- return false;
- }
- }
- }
- }
|