using AwInitilizer.Assist; using FluentFTP; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; 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 CheckGetQueryAction(); 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(); if (!signalUpdateResult) { LogWriter.Log($"SignalUpdate failed"); Error = ErrorType.SignalUpdateFailed; return false; } return result; } private async Task UploadWithFtp() { var updateList = UpdateData.FirmwareUpdateModels; var ipAddress = ""; if (!string.IsNullOrEmpty(UpdateData.IpAddress)) { ipAddress = UpdateData.IpAddress; } else { ipAddress = ServerIpAddress; } //FtpClient client = new FtpClient(ServerIpAddress, "root", "y42j/4cj84"); FtpClient client = new FtpClient(ipAddress, "vern", "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() { try { var result = await EvApi.SignalUpdateFirmware(); LogWriter.Log(result.Response); return result.Result; } catch (Exception e) { LogWriter.Log(e.Message, isError: true, isDebugLog: true); if (e.InnerException != null) { LogWriter.Log(e.InnerException.Message, isError: true, isDebugLog: true); } return false; } } } }