123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace AwInitilizer.Procedure.RestarttoIdel
- {
- public enum ErrorType
- {
- None,
- SetFail,
- ConnectorNotIdel,
- }
- public enum LogEvent
- {
- IdelCheck
- }
- public class RestarttoIdelProcedure : ProcedureBase
- {
- public ErrorType Error { get; set; } = ErrorType.None;
- private ProcedureLog.LogWriter<RestarttoIdelProcedure, LogEvent> LogWriter;
- private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
- {
- { LogEvent.IdelCheck, "IdelCheck" }
- };
- private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
- {
- { LogEvent.IdelCheck, "Connector status is Idel Check :{0}" }
- };
- public RestarttoIdelProcedure() : base()
- {
- Name = "Restart To Idel";
- Content = "Restart EVSSE and check status back to Idel";
- LogWriter = new ProcedureLog.LogWriter<RestarttoIdelProcedure, LogEvent>(this)
- {
- ReportPair = ReportDict,
- LogPair = LogDict
- };
- }
- internal override async Task<bool> Run()
- {
- var result = await FactorySet();
- if (!result)
- {
- Error = ErrorType.SetFail;
- return false;
- }
- else
- {
- LogWriter.Log("Waiting Factory reset complete...");
- //Logger.Print("Waiting Factory reset complete...");
- await Task.Delay(TimeSpan.FromMinutes(2));
- if (await CheckAllIdel())
- {
- LogWriter.Report(LogEvent.IdelCheck,"success");
- //Logger.Print("All Connetor is Idel");
- return true;
- }
- else
- {
- LogWriter.Report(LogEvent.IdelCheck, "fail");
- Error = ErrorType.ConnectorNotIdel;
- return false;
- }
- }
- }
- private async Task<bool> FactorySet()
- {
- try
- {
- using (WebClient webClient = new WebClient())
- {
- NameValueCollection parameters = new NameValueCollection();
- parameters.Add("SystemId", "");
- parameters.Add("SystemDateTime", "");
- parameters.Add("PhaseLossPolicy", "");
- parameters.Add("FactoryConfiguration", "1");
- parameters.Add("AuthorisationMode", "");
- parameters.Add("isAPP", "");
- parameters.Add("isQRCode", "");
- parameters.Add("isRFID", "");
- parameters.Add("QRCodeMadeMode", "");
- parameters.Add("QRCodeContent", "");
- parameters.Add("Intensity", "");
- parameters.Add("RfidCardNumEndian", "");
- parameters.Add("PsuAcInputType", "");
- webClient.QueryString = parameters;
- using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/set_system_action.php"))
- // 使用 StreamReader 讀取 stream 內的字元
- using (StreamReader reader = new StreamReader(stream))
- {
- // 將 StreamReader 所讀到的字元轉為 string
- string response = reader.ReadToEnd();
- //InfoLog += $"factory set respons:\n{request}\n";
- LogWriter.Log($"factory set respons: {response}", isDebugLog: true);
- var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);
- }
- }
- return true;
- }
- catch (Exception e)
- {
- LogWriter.Log("Factory reset command failed");
- LogWriter.Log(e.Message, isDebugLog: true);
- //InfoLog += "Factory reset command failed\n";
- //InfoLog += e.Message;
- //InfoLog += "\n";
- //Logger.Print("Factory reset command failed", isError: true);
- //Logger.Print(e.Message, isError: true);
- return false;
- }
- }
- private async Task<bool> CheckAllIdel()
- {
- var statusPairs = await GetConnectorStatus();
- if (statusPairs is null)
- return false;
- else
- {
- foreach (var statusPair in statusPairs)
- {
- if (statusPair.Value != "1")
- {
- LogWriter.Log($"Connector {statusPair.Key} status not Idel");
- return false;
- //InfoLog += $"Connector {matchIndex} status not Idel\n";
- //Logger.Print($"Connector {matchIndex} status not Idel", isError: true);
- }
- }
- return true;
- }
- }
- private async Task<Dictionary<int, string>> GetConnectorStatus()
- {
- Dictionary<int, string> connectorStatusPair = new Dictionary<int, string>();
- try
- {
- using (WebClient webClient = new WebClient())
- {
- NameValueCollection parameters = new NameValueCollection();
- parameters.Add("opt", "2");
- webClient.QueryString = parameters;
- using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
- // 使用 StreamReader 讀取 stream 內的字元
- using (StreamReader reader = new StreamReader(stream))
- {
- // 將 StreamReader 所讀到的字元轉為 string
- string request = reader.ReadToEnd();
- LogWriter.Log($"get status respons:\n{request}\n", isDebugLog: true);
- //InfoLog += $"get status respons:\n{request}\n";
- //LogPair.Add($"EvseStatus", request);
- Regex rx = new Regex("(SystemStatus)\\\": (\\d)");
- var matches = rx.Matches(request);
- for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
- {
- var match = matches[matchIndex];
- if (match.Groups.Count != 3)
- {
- LogWriter.Log($"Connector {matchIndex} status string mismatched");
- return null;
- //InfoLog += $"Connector {matchIndex} status string mismatched\n";
- //Logger.Print($"Connector {matchIndex} status string mismatched", isError:true);
- }
- else
- {
- connectorStatusPair.Add(matchIndex, match.Groups[2].Value);
- }
- }
- }
- }
- return connectorStatusPair;
- }
- catch (Exception e)
- {
- LogWriter.Log("Get connector status command failed");
- LogWriter.Log(e.Message, isDebugLog: true);
- //InfoLog += "Check all idel command failed\n";
- //InfoLog += e.Message;
- //InfoLog += "\n";
- //Logger.Print("Check all idel command failed", isError:true);
- //Logger.Print(e.Message, isError: true);
- return null;
- }
- }
- }
- }
|