123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using AwInitilizer.Procedure.WifRssiCheck;
- using CsuWebApiLib;
- using InitializerModel;
- using PhihongEv.Lib;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace AwInitilizer.Procedure.NetworkInterfaceSetup
- {
- public enum ErrorType
- {
- None,
- WiFiSetFailed,
- TelcomSetFailed,
- RestartFailed
- }
- public enum LogEvent
- {
- }
- public class NetworkInterfaceSetupProcedure : ProcedureBase
- {
- public ErrorType Error { get; set; } = ErrorType.None;
- private ProcedureLog.LogWriter<NetworkInterfaceSetupProcedure, LogEvent> LogWriter;
- private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
- {
- };
- private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
- {
- };
- public NetworkInterfaceSetupProcedure() : base()
- {
- Name = "Network Interface Setup";
- Content = "Set Wi-Fi and Telcom setting if required";
- LogWriter = new ProcedureLog.LogWriter<NetworkInterfaceSetupProcedure, LogEvent>(this)
- {
- ReportPair = ReportDict,
- LogPair = LogDict
- };
- }
- internal override async Task<bool> Run()
- {
- var isWiFiSettingRequired = UpdateData.SystemID.ModelName.GetWiFiCnt() > 0;
- var isTelcomSettingRequired = UpdateData.SystemID.ModelName.GetTelecomCnt() > 0;
- if (isWiFiSettingRequired)
- {
- var setWifiResult = await SetWifiConfigAsync();
- if (!setWifiResult)
- {
- Error = ErrorType.WiFiSetFailed;
- LogWriter.Log("set wifi to station failed");
- return false;
- }
- }
- else
- {
- LogWriter.Log("Wifi not supported, skip Wi-Fi setting");
- }
- if (isTelcomSettingRequired)
- {
- var setTelcomResult = await EnableTelcomAsync();
- if (!setTelcomResult)
- {
- Error = ErrorType.TelcomSetFailed;
- LogWriter.Log("enable telcom failed");
- return false;
- }
- }
- else
- {
- LogWriter.Log("Telcom not supported, skip Telcom setting");
- }
- if (isWiFiSettingRequired || isTelcomSettingRequired)
- {
- var rebootResult = await RestartEvseAsync();
- if (!rebootResult)
- {
- Error = ErrorType.RestartFailed;
- LogWriter.Log("enable telcom failed");
- return false;
- }
- }
- return true;
- }
- internal async Task<bool> SetWifiConfigAsync()
- {
- var ssid = AppSettingConfig.Instance.WifiApSSID;
- var pwd = AppSettingConfig.Instance.WifiApPassword;
- EvApiResult<bool> cmdResult;
- int retryCnt = 0;
- do
- {
- LogWriter.Log("Trying to set wifi to station mode");
- cmdResult = await EvApi.SetWifiStationMode(ssid, pwd);
- if (cmdResult != null && cmdResult.Result)
- {
- break;
- }
- await Task.Delay(TimeSpan.FromMinutes(2));
- retryCnt++;
- }
- while (retryCnt < 5);
- if (retryCnt >= 5)
- {
- return false;
- }
- return cmdResult.Result;
- }
- internal async Task<bool> EnableTelcomAsync()
- {
- EvApiResult<bool> cmdResult;
- int retryCnt = 0;
- do
- {
- LogWriter.Log("Trying to enable telcom");
- cmdResult = await EvApi.SetTelcomEnabled(true);
- if (cmdResult != null && cmdResult.Result)
- {
- break;
- }
- await Task.Delay(TimeSpan.FromMinutes(2));
- retryCnt++;
- }
- while (retryCnt < 5);
- if (retryCnt >= 5)
- {
- return false;
- }
- return cmdResult.Result;
- }
- internal async Task<bool> RestartEvseAsync()
- {
- var restartResult = await EvApi.Restart();
- if (restartResult == null || !restartResult.Result)
- {
- return false;
- }
- int pollingCnt;
- for (pollingCnt = 0; pollingCnt < 16; pollingCnt++)
- {
- await Task.Delay(TimeSpan.FromSeconds(15));
- var response = await ChekCsuBootCompelete();
- if (response)
- break;
- }
- if (pollingCnt == 16)
- {
- LogWriter.Log("Wait reboot complete time out");
- }
- return true;
- }
- }
- }
|