123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 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<RestarttoIdleProcedure, LogEvent> LogWriter;
- private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
- {
- { LogEvent.IdleCheck, "IdleCheck" }
- };
- private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
- {
- { 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<RestarttoIdleProcedure, LogEvent>(this)
- {
- ReportPair = ReportDict,
- LogPair = LogDict
- };
- }
- internal override async Task<bool> 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<bool> 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<bool> 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<Dictionary<int, string>> GetConnectorStatus()
- {
- //Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
- 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;
- }
- }
- }
- }
|