ProcedureBase.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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,Success,Failed
  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. internal SerialPortocol serialPortocol { get; private set; }
  31. internal static string ServerIpAddress = "192.168.1.10";
  32. public async Task<bool> DoWork() {
  33. if (!IsActivated)
  34. return false;
  35. InfoLog = "";
  36. Logger.Print(Name + "Started");
  37. var result = await Run();
  38. Dispose();
  39. Status = result ? ProcedureStatus.Success : ProcedureStatus.Failed;
  40. Logger.Print(Name + "Complete");
  41. return result;
  42. }
  43. internal virtual async Task<bool> Run()
  44. {
  45. return true;
  46. }
  47. public void Reset()
  48. {
  49. Status = ProcedureStatus.Idle;
  50. InfoLog = "";
  51. }
  52. internal async Task<bool> CheckAndCreateSocket()
  53. {
  54. TcpSerializer socketConnection = null;
  55. if (socketConnection == null || socketConnection.ConnectStatus == ConnectStatus.ConnectionFail ||
  56. socketConnection.ConnectStatus == ConnectStatus.DisConnected)
  57. {
  58. socketConnection = new Assist.TcpSerializer(ip:ServerIpAddress);
  59. await socketConnection.OpenAsync();
  60. if(socketConnection.ConnectStatus != ConnectStatus.Connected)
  61. {
  62. Logger.Print("EVSE tcp connection Failed,Check EVSE is connected", isError: true);
  63. return false;
  64. }
  65. Logger.Print("Connected");
  66. serialPortocol = new SerialPortocol(socketConnection);
  67. return true;
  68. }
  69. return true;
  70. }
  71. internal async Task<bool> ChekCsuBootCompelete()
  72. {
  73. //await Task.Delay(TimeSpan.FromMinutes(2));
  74. try
  75. {
  76. using (WebClientTimeout webClient = new WebClientTimeout())
  77. {
  78. NameValueCollection parameters = new NameValueCollection();
  79. parameters.Add("opt", "2");
  80. webClient.QueryString = parameters;
  81. using (Stream stream = await webClient.OpenReadTaskAsync($"https://{ServerIpAddress}/get_query_action.php"))
  82. // 使用 StreamReader 讀取 stream 內的字元
  83. using (StreamReader reader = new StreamReader(stream))
  84. {
  85. // 將 StreamReader 所讀到的字元轉為 string
  86. string request = reader.ReadToEnd();
  87. Regex rx = new Regex("(SystemStatus)\\\": ([0-9]*)");
  88. var matches = rx.Matches(request);
  89. bool isAllPassed = true;
  90. for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
  91. {
  92. var match = matches[matchIndex];
  93. if (match.Groups.Count != 3)
  94. {
  95. isAllPassed = false;
  96. break;
  97. }
  98. else
  99. {
  100. if (match.Groups[2].Value != "1")
  101. {
  102. isAllPassed = false;
  103. break;
  104. }
  105. }
  106. }
  107. return isAllPassed;
  108. }
  109. }
  110. }
  111. catch (Exception e)
  112. {
  113. return false;
  114. }
  115. }
  116. private void Dispose()
  117. {
  118. if(serialPortocol!=null)
  119. {
  120. try
  121. {
  122. serialPortocol.Close();
  123. }catch
  124. {
  125. }
  126. }
  127. }
  128. }
  129. }