RestarttoIdelProcedure.cs 5.2 KB

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