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 LogWriter; private readonly static Dictionary ReportDict = new Dictionary() { { LogEvent.UploadStartWait, "UploadStartWait" }, { LogEvent.FirmwareUpload, "FirmwareUpload" }, }; private readonly static Dictionary LogDict = new Dictionary() { { 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(this) { ReportPair = ReportDict, LogPair = LogDict }; } internal override async Task 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 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 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; } } } }