ProcedureBase.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using AwInitilizer.Assist;
  2. using AwInitilizer.Interface;
  3. using AwInitilizer.Model;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.Specialized;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net.Sockets;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. namespace AwInitilizer.Procedure
  15. {
  16. public enum ProcedureStatus
  17. {
  18. IDLE,PASS,FAIL
  19. }
  20. public class ProcedureBase
  21. {
  22. public string Name { get; set; }
  23. public string Content { get; set; }
  24. public bool IsActivated { get; set; } = true;
  25. public ProcedureStatus Status { get; private set; }
  26. public string InfoLog { get; internal set; }
  27. public static UpdateData UpdateData { get; set; }
  28. public static IIogger Logger { get; set; }
  29. public Dictionary<string, string> LogPair { get; private set; } = new Dictionary<string, string>();
  30. public List<string> ReportLog { get; private set; } = new List<string>();
  31. internal SerialPortocol serialPortocol { get; private set; }
  32. internal static string ServerIpAddress = "192.168.1.10";
  33. public async Task<bool> DoWork() {
  34. if (!IsActivated)
  35. return false;
  36. InfoLog = "";
  37. Logger.Print(Name + "Started");
  38. var result = await Run();
  39. Dispose();
  40. Status = result ? ProcedureStatus.PASS : ProcedureStatus.FAIL;
  41. Logger.Print(Name + "Complete");
  42. return result;
  43. }
  44. internal virtual async Task<bool> Run()
  45. {
  46. return true;
  47. }
  48. public void Reset()
  49. {
  50. Status = ProcedureStatus.IDLE;
  51. InfoLog = "";
  52. }
  53. internal async Task<bool> CheckAndCreateSocket()
  54. {
  55. TcpSerializer socketConnection = null;
  56. if (socketConnection == null || socketConnection.ConnectStatus == ConnectStatus.ConnectionFail ||
  57. socketConnection.ConnectStatus == ConnectStatus.DisConnected)
  58. {
  59. socketConnection = new Assist.TcpSerializer(ip:ServerIpAddress);
  60. await socketConnection.OpenAsync();
  61. if(socketConnection.ConnectStatus != ConnectStatus.Connected)
  62. {
  63. Logger.Print("EVSE tcp connection Failed,Check EVSE is connected", isError: true);
  64. return false;
  65. }
  66. Logger.Print("Connected");
  67. serialPortocol = new SerialPortocol(socketConnection);
  68. return true;
  69. }
  70. return true;
  71. }
  72. public async Task<bool> ChekCsuBootCompelete()
  73. {
  74. //await Task.Delay(TimeSpan.FromMinutes(2));
  75. try
  76. {
  77. using (WebClientTimeout webClient = new WebClientTimeout())
  78. {
  79. NameValueCollection parameters = new NameValueCollection();
  80. parameters.Add("opt", "2");
  81. webClient.QueryString = parameters;
  82. using (Stream stream = webClient.OpenRead($"https://{ServerIpAddress}/get_query_action.php"))
  83. // 使用 StreamReader 讀取 stream 內的字元
  84. using (StreamReader reader = new StreamReader(stream))
  85. {
  86. // 將 StreamReader 所讀到的字元轉為 string
  87. string request = reader.ReadToEnd();
  88. Regex rx = new Regex("(SystemStatus)\\\": ([0-9]*)");
  89. var matches = rx.Matches(request);
  90. bool isAllPassed = true;
  91. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  92. {
  93. var match = matches[matchIndex];
  94. if (match.Groups.Count != 3)
  95. {
  96. isAllPassed = false;
  97. break;
  98. }
  99. else
  100. {
  101. if (match.Groups[2].Value != "1")
  102. {
  103. isAllPassed = false;
  104. break;
  105. }
  106. }
  107. }
  108. return isAllPassed;
  109. }
  110. }
  111. }
  112. catch (Exception e)
  113. {
  114. return false;
  115. }
  116. }
  117. public async Task<bool> CheckGetQuertyAction()
  118. {
  119. //await Task.Delay(TimeSpan.FromMinutes(2));
  120. try
  121. {
  122. using (WebClientTimeout webClient = new WebClientTimeout())
  123. {
  124. NameValueCollection parameters = new NameValueCollection();
  125. parameters.Add("opt", "2");
  126. webClient.QueryString = parameters;
  127. using (Stream stream = webClient.OpenRead($"https://{ServerIpAddress}/get_query_action.php"))
  128. // 使用 StreamReader 讀取 stream 內的字元
  129. using (StreamReader reader = new StreamReader(stream))
  130. {
  131. // 將 StreamReader 所讀到的字元轉為 string
  132. string request = reader.ReadToEnd();
  133. return true;
  134. }
  135. }
  136. }
  137. catch (Exception e)
  138. {
  139. return false;
  140. }
  141. }
  142. private void Dispose()
  143. {
  144. if(serialPortocol!=null)
  145. {
  146. try
  147. {
  148. serialPortocol.Close();
  149. }catch
  150. {
  151. }
  152. }
  153. }
  154. }
  155. }