123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- 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<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 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<bool> 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<bool> 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;
- }
- }
- }
- }
|