NetworkInterfaceSetupProcedure.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using AwInitilizer.Procedure.WifRssiCheck;
  2. using CsuWebApiLib;
  3. using InitializerModel;
  4. using PhihongEv.Lib;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace AwInitilizer.Procedure.NetworkInterfaceSetup
  11. {
  12. public enum ErrorType
  13. {
  14. None,
  15. WiFiSetFailed,
  16. TelcomSetFailed,
  17. RestartFailed
  18. }
  19. public enum LogEvent
  20. {
  21. }
  22. public class NetworkInterfaceSetupProcedure : ProcedureBase
  23. {
  24. public ErrorType Error { get; set; } = ErrorType.None;
  25. private ProcedureLog.LogWriter<NetworkInterfaceSetupProcedure, LogEvent> LogWriter;
  26. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  27. {
  28. };
  29. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  30. {
  31. };
  32. public NetworkInterfaceSetupProcedure() : base()
  33. {
  34. Name = "Network Interface Setup";
  35. Content = "Set Wi-Fi and Telcom setting if required";
  36. LogWriter = new ProcedureLog.LogWriter<NetworkInterfaceSetupProcedure, LogEvent>(this)
  37. {
  38. ReportPair = ReportDict,
  39. LogPair = LogDict
  40. };
  41. }
  42. internal override async Task<bool> Run()
  43. {
  44. var isWiFiSettingRequired = UpdateData.SystemID.ModelName.GetWiFiCnt() > 0;
  45. var isTelcomSettingRequired = UpdateData.SystemID.ModelName.GetTelecomCnt() > 0;
  46. if (isWiFiSettingRequired)
  47. {
  48. var setWifiResult = await SetWifiConfigAsync();
  49. if (!setWifiResult)
  50. {
  51. Error = ErrorType.WiFiSetFailed;
  52. LogWriter.Log("set wifi to station failed");
  53. return false;
  54. }
  55. }
  56. else
  57. {
  58. LogWriter.Log("Wifi not supported, skip Wi-Fi setting");
  59. }
  60. if (isTelcomSettingRequired)
  61. {
  62. var setTelcomResult = await EnableTelcomAsync();
  63. if (!setTelcomResult)
  64. {
  65. Error = ErrorType.TelcomSetFailed;
  66. LogWriter.Log("enable telcom failed");
  67. return false;
  68. }
  69. }
  70. else
  71. {
  72. LogWriter.Log("Telcom not supported, skip Telcom setting");
  73. }
  74. if (isWiFiSettingRequired || isTelcomSettingRequired)
  75. {
  76. var rebootResult = await RestartEvseAsync();
  77. if (!rebootResult)
  78. {
  79. Error = ErrorType.RestartFailed;
  80. LogWriter.Log("enable telcom failed");
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. internal async Task<bool> SetWifiConfigAsync()
  87. {
  88. var ssid = AppSettingConfig.Instance.WifiApSSID;
  89. var pwd = AppSettingConfig.Instance.WifiApPassword;
  90. EvApiResult<bool> cmdResult;
  91. int retryCnt = 0;
  92. do
  93. {
  94. LogWriter.Log("Trying to set wifi to station mode");
  95. cmdResult = await EvApi.SetWifiStationMode(ssid, pwd);
  96. if (cmdResult != null && cmdResult.Result)
  97. {
  98. break;
  99. }
  100. await Task.Delay(TimeSpan.FromMinutes(2));
  101. retryCnt++;
  102. }
  103. while (retryCnt < 5);
  104. if (retryCnt >= 5)
  105. {
  106. return false;
  107. }
  108. return cmdResult.Result;
  109. }
  110. internal async Task<bool> EnableTelcomAsync()
  111. {
  112. EvApiResult<bool> cmdResult;
  113. int retryCnt = 0;
  114. do
  115. {
  116. LogWriter.Log("Trying to enable telcom");
  117. cmdResult = await EvApi.SetTelcomEnabled(true);
  118. if (cmdResult != null && cmdResult.Result)
  119. {
  120. break;
  121. }
  122. await Task.Delay(TimeSpan.FromMinutes(2));
  123. retryCnt++;
  124. }
  125. while (retryCnt < 5);
  126. if (retryCnt >= 5)
  127. {
  128. return false;
  129. }
  130. return cmdResult.Result;
  131. }
  132. internal async Task<bool> RestartEvseAsync()
  133. {
  134. var restartResult = await EvApi.Restart();
  135. if (restartResult == null || !restartResult.Result)
  136. {
  137. return false;
  138. }
  139. int pollingCnt;
  140. for (pollingCnt = 0; pollingCnt < 16; pollingCnt++)
  141. {
  142. await Task.Delay(TimeSpan.FromSeconds(15));
  143. var response = await ChekCsuBootCompelete();
  144. if (response)
  145. break;
  146. }
  147. if (pollingCnt == 16)
  148. {
  149. LogWriter.Log("Wait reboot complete time out");
  150. }
  151. return true;
  152. }
  153. }
  154. }