using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace AwInitilizer.Procedure { public class BasicInfoUpdateProcedure : ProcedureBase { public BasicInfoUpdateProcedure() : base() { Name = "Basic Intlize"; Content = "Set and check Model Name,SerilNumber,DateTime."; } internal override async Task Run() { if (!await base.CheckAndCreateSocket()) { InfoLog += "EVSE connect failed\n"; return false; } //base.serialPortocol.OnMsgReceived += SerialPortocol_OnMsgReceived; if (!await serialPortocol.SetModelName(UpdateData.ModelName)) { InfoLog += "Model Name update failed\n"; Logger.Print("Model Name update Failed", isError: true); return false; } else { Logger.Print("Model Name write Success"); } if (!await serialPortocol.SetSerialNumber(UpdateData.SerialNumber)) { InfoLog += "Serial Number update failed\n"; Logger.Print("Serial Number update Failed", isError: true); return false; } else { Logger.Print("Serial Number write Success"); } var setDateTime = DateTime.Now.ToUniversalTime(); if (!await serialPortocol.SetUTCTime(setDateTime)) { InfoLog += "RTC update failed\n"; Logger.Print("RTC update Failed", isError: true); return false; } else { Logger.Print("RTC update write Success"); } if (!await serialPortocol.SettingChangeConfirm()) { InfoLog += "Setting save request failed\n"; Logger.Print("Setting save Failed", isError: true); return false; } else { Logger.Print("Setting save Success"); } Logger.Print("Waiting EVSE reboot..."); serialPortocol.Close(); await Task.Delay(TimeSpan.FromMinutes(1)); if (!await base.CheckAndCreateSocket()) { InfoLog += "EVSE not found after reboot\n"; Logger.Print("EVSE reboot timeout", isError: true); return false; } var receivedModelName = await serialPortocol.GetModelName(); if (string.IsNullOrEmpty(receivedModelName)) { InfoLog += "Model name get failed after reboot\n"; Logger.Print("Model Name read Failed", isError: true); return false; } else { InfoLog += $"Get Updated Model Name { receivedModelName }\n"; if (receivedModelName != UpdateData.ModelName) { InfoLog += "Updated Model Name Mismatched\n"; Logger.Print("Stored Model Name Mismatched", isError: true); return false; } Logger.Print("Model Name update Success"); } var receivedSeerialNumber = await serialPortocol.GetSerialNumber(); if (string.IsNullOrEmpty(receivedSeerialNumber)) { InfoLog += "Serial number get failed after reboot\n"; Logger.Print("Stored Serial read Failed", isError: true); return false; } else { InfoLog += $"Get Updated serial number { receivedSeerialNumber }\n"; if (receivedSeerialNumber != UpdateData.SerialNumber) { InfoLog += "Updated serial number mismatched\n"; Logger.Print("Stored Serial Number Mismatched", isError: true); return false; } Logger.Print("Serial Number update Success"); } var receivedDateTime = await serialPortocol.GetUTCTime(); if (string.IsNullOrEmpty(receivedModelName)) { InfoLog += "UTC Time receive failed after reboot\n"; Logger.Print("UTC Time receive failed", isError: true); return false; } else { InfoLog += $"UTC Time received : {receivedDateTime.Value.ToString("yyyyMMddHHmmss")}\n"; var diff = receivedDateTime.Value - DateTime.Now.ToUniversalTime(); if (Math.Abs(diff.TotalSeconds) > 10) { InfoLog += "Stored UTC time Mismatched\n"; Logger.Print("Stored UTC time Mismatched", isError: true); return false; } else { Logger.Print("Model Name update Success"); } } return true; } } }