ProcedureBase.cs 5.8 KB

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