RestarttoIdelProcedure.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. await Task.Delay(TimeSpan.FromMinutes(2));
  60. if (await CheckAllIdle())
  61. {
  62. LogWriter.Report(LogEvent.IdleCheck,"success");
  63. //Logger.Print("All Connetor is Idel");
  64. return true;
  65. }
  66. else
  67. {
  68. LogWriter.Report(LogEvent.IdleCheck, "fail", isError: true);
  69. Error = ErrorType.ConnectorNotIdle;
  70. return false;
  71. }
  72. }
  73. }
  74. private async Task<bool> FactorySet()
  75. {
  76. try
  77. {
  78. var result= await EvApi.FactorySet();
  79. LogWriter.Log($"factory set respons: {result.Response}", isDebugLog: true);
  80. return true;
  81. }
  82. catch (Exception e)
  83. {
  84. LogWriter.Log("Factory reset command failed");
  85. LogWriter.Log(e.Message, isDebugLog: true);
  86. //InfoLog += "Factory reset command failed\n";
  87. //InfoLog += e.Message;
  88. //InfoLog += "\n";
  89. //Logger.Print("Factory reset command failed", isError: true);
  90. //Logger.Print(e.Message, isError: true);
  91. return false;
  92. }
  93. }
  94. private async Task<bool> CheckAllIdle()
  95. {
  96. var statusPairs = await GetConnectorStatus();
  97. if (statusPairs is null)
  98. {
  99. return false;
  100. }
  101. foreach (var statusPair in statusPairs)
  102. {
  103. if (statusPair.Value != "1")
  104. {
  105. LogWriter.Log($"Connector {statusPair.Key} status not Idle");
  106. return false;
  107. //InfoLog += $"Connector {matchIndex} status not Idel\n";
  108. //Logger.Print($"Connector {matchIndex} status not Idel", isError: true);
  109. }
  110. }
  111. return true;
  112. }
  113. private async Task<Dictionary<int, string>> GetConnectorStatus()
  114. {
  115. //Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
  116. try
  117. {
  118. var result = await EvApi.GetConnectorStatus();
  119. LogWriter.Log($"get status respons:\n{result.Response}\n", isDebugLog: true);
  120. if (result.Result == null)
  121. {
  122. LogWriter.Log($"Connector status string mismatched");
  123. return null;
  124. }
  125. return result.Result;
  126. }
  127. catch (Exception e)
  128. {
  129. LogWriter.Log("Get connector status command failed");
  130. LogWriter.Log(e.Message, isDebugLog: true);
  131. //InfoLog += "Check all idel command failed\n";
  132. //InfoLog += e.Message;
  133. //InfoLog += "\n";
  134. //Logger.Print("Check all idel command failed", isError:true);
  135. //Logger.Print(e.Message, isError: true);
  136. return null;
  137. }
  138. }
  139. }
  140. }