using AwInitilizer.Assist; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace AwInitilizer.Procedure.RestarttoIdle { public enum ErrorType { None, SetFail, ConnectorNotIdle, } public enum LogEvent { IdleCheck } public class RestarttoIdleProcedure : ProcedureBase { public ErrorType Error { get; set; } = ErrorType.None; private ProcedureLog.LogWriter LogWriter; private readonly static Dictionary ReportDict = new Dictionary() { { LogEvent.IdleCheck, "IdleCheck" } }; private readonly static Dictionary LogDict = new Dictionary() { { LogEvent.IdleCheck, "Connector status is Idle Check :{0}" } }; public RestarttoIdleProcedure() : base() { Name = "Restart To Idle"; Content = "Restart EVSSE and check status back to Idle"; LogWriter = new ProcedureLog.LogWriter(this) { ReportPair = ReportDict, LogPair = LogDict }; } internal override async Task Run() { var result = await FactorySet(); if (!result) { Error = ErrorType.SetFail; return false; } else { //LogWriter.Log("Waiting Factory reset complete..."); //Logger.Print("Waiting Factory reset complete..."); int retryCnt = 0; do { LogWriter.Log("Waiting Factory reset complete..."); await Task.Delay(TimeSpan.FromMinutes(2)); retryCnt++; } while (!await CheckAllIdle() && retryCnt < 4); if (retryCnt < 4) { LogWriter.Report(LogEvent.IdleCheck,"success"); //Logger.Print("All Connetor is Idel"); return true; } else { LogWriter.Report(LogEvent.IdleCheck, "fail", isError: true); Error = ErrorType.ConnectorNotIdle; return false; } } } private async Task FactorySet() { try { var result= await EvApi.FactorySet(); LogWriter.Log($"factory set respons: {result.Response}", isDebugLog: true); return true; } catch (Exception e) { LogWriter.Log("Factory reset command failed"); LogWriter.Log(e.Message, isDebugLog: true); //InfoLog += "Factory reset command failed\n"; //InfoLog += e.Message; //InfoLog += "\n"; //Logger.Print("Factory reset command failed", isError: true); //Logger.Print(e.Message, isError: true); return false; } } private async Task CheckAllIdle() { var statusPairs = await GetConnectorStatus(); if (statusPairs is null) { return false; } foreach (var statusPair in statusPairs) { if (statusPair.Value != "1") { LogWriter.Log($"Connector {statusPair.Key} status not Idle"); return false; //InfoLog += $"Connector {matchIndex} status not Idel\n"; //Logger.Print($"Connector {matchIndex} status not Idel", isError: true); } } return true; } private async Task> GetConnectorStatus() { //Dictionary connectorStatusPair = new Dictionary(); try { var result = await EvApi.GetConnectorStatus(); LogWriter.Log($"get status respons:\n{result.Response}\n", isDebugLog: true); if (result.Result == null) { LogWriter.Log($"Connector status string mismatched"); return null; } return result.Result; } catch (Exception e) { LogWriter.Log("Get connector status command failed"); LogWriter.Log(e.Message, isDebugLog: true); //InfoLog += "Check all idel command failed\n"; //InfoLog += e.Message; //InfoLog += "\n"; //Logger.Print("Check all idel command failed", isError:true); //Logger.Print(e.Message, isError: true); return null; } } } }