RestarttoIdelProcedure.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using AwInitilizer.Assist;
  2. using CsuWebApiLib;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.Specialized;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. namespace AwInitilizer.Procedure.RestarttoIdle
  15. {
  16. public enum ErrorType
  17. {
  18. None,
  19. SetFail,
  20. ConnectorNotIdle,
  21. }
  22. public enum LogEvent
  23. {
  24. IdleCheck
  25. }
  26. public class RestarttoIdleProcedure : ProcedureBase
  27. {
  28. public ErrorType Error { get; set; } = ErrorType.None;
  29. private ProcedureLog.LogWriter<RestarttoIdleProcedure, LogEvent> LogWriter;
  30. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  31. {
  32. { LogEvent.IdleCheck, "IdleCheck" }
  33. };
  34. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  35. {
  36. { LogEvent.IdleCheck, "Connector status is Idle Check :{0}" }
  37. };
  38. public RestarttoIdleProcedure() : base()
  39. {
  40. Name = "Restart To Idle";
  41. Content = "Restart EVSSE and check status back to Idle";
  42. LogWriter = new ProcedureLog.LogWriter<RestarttoIdleProcedure, LogEvent>(this)
  43. {
  44. ReportPair = ReportDict,
  45. LogPair = LogDict
  46. };
  47. }
  48. internal override async Task<bool> Run()
  49. {
  50. var result = await FactorySet();
  51. if (!result)
  52. {
  53. Error = ErrorType.SetFail;
  54. return false;
  55. }
  56. else
  57. {
  58. //LogWriter.Log("Waiting Factory reset complete...");
  59. //Logger.Print("Waiting Factory reset complete...");
  60. int retryCnt = 0;
  61. do
  62. {
  63. LogWriter.Log("Waiting Factory reset complete...");
  64. await Task.Delay(TimeSpan.FromMinutes(2));
  65. retryCnt++;
  66. }
  67. while (!await CheckAllIdle() && retryCnt < 4);
  68. if (retryCnt < 4)
  69. {
  70. LogWriter.Report(LogEvent.IdleCheck,"success");
  71. //Logger.Print("All Connetor is Idel");
  72. return true;
  73. }
  74. else
  75. {
  76. LogWriter.Report(LogEvent.IdleCheck, "fail", isError: true);
  77. Error = ErrorType.ConnectorNotIdle;
  78. return false;
  79. }
  80. }
  81. }
  82. private async Task<bool> FactorySet()
  83. {
  84. try
  85. {
  86. var result= await EvApi.FactorySet();
  87. LogWriter.Log($"factory set respons: {result.Response}", isDebugLog: true);
  88. return true;
  89. }
  90. catch (Exception e)
  91. {
  92. LogWriter.Log("Factory reset command failed");
  93. LogWriter.Log(e.Message, isDebugLog: true);
  94. //InfoLog += "Factory reset command failed\n";
  95. //InfoLog += e.Message;
  96. //InfoLog += "\n";
  97. //Logger.Print("Factory reset command failed", isError: true);
  98. //Logger.Print(e.Message, isError: true);
  99. return false;
  100. }
  101. }
  102. private async Task<bool> CheckAllIdle()
  103. {
  104. var statusPairs = await GetConnectorStatus();
  105. if (statusPairs is null)
  106. {
  107. return false;
  108. }
  109. foreach (var statusPair in statusPairs)
  110. {
  111. if (statusPair.Value != "1")
  112. {
  113. LogWriter.Log($"Connector {statusPair.Key} status not Idle");
  114. return false;
  115. //InfoLog += $"Connector {matchIndex} status not Idel\n";
  116. //Logger.Print($"Connector {matchIndex} status not Idel", isError: true);
  117. }
  118. }
  119. return true;
  120. }
  121. private async Task<Dictionary<int, string>> GetConnectorStatus()
  122. {
  123. //Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
  124. try
  125. {
  126. var result = await EvApi.GetConnectorStatus();
  127. LogWriter.Log($"get status respons:\n{result.Response}\n", isDebugLog: true);
  128. if (result.Result == null)
  129. {
  130. LogWriter.Log($"Connector status string mismatched");
  131. return null;
  132. }
  133. return result.Result;
  134. }
  135. catch (Exception e)
  136. {
  137. LogWriter.Log("Get connector status command failed");
  138. LogWriter.Log(e.Message, isDebugLog: true);
  139. //InfoLog += "Check all idel command failed\n";
  140. //InfoLog += e.Message;
  141. //InfoLog += "\n";
  142. //Logger.Print("Check all idel command failed", isError:true);
  143. //Logger.Print(e.Message, isError: true);
  144. return null;
  145. }
  146. }
  147. }
  148. }