ProcedureBase.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using AwInitilizer.Assist;
  2. using AwInitilizer.Interface;
  3. using AwInitilizer.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace AwInitilizer.Procedure
  11. {
  12. public enum ProcedureStatus
  13. {
  14. Idle,Success,Failed
  15. }
  16. public class ProcedureBase
  17. {
  18. public string Name { get; set; }
  19. public string Content { get; set; }
  20. public bool IsActivated { get; set; } = true;
  21. public ProcedureStatus Status { get; private set; }
  22. public string InfoLog { get; internal set; }
  23. public static UpdateData UpdateData { get; set; }
  24. public static IIogger Logger { get; set; }
  25. public Dictionary<string, string> LogPair { get; private set; } = new Dictionary<string, string>();
  26. internal SerialPortocol serialPortocol { get; private set; }
  27. internal static string ServerIpAddress = "192.168.1.10";
  28. public async Task<bool> DoWork() {
  29. if (!IsActivated)
  30. return false;
  31. InfoLog = "";
  32. Logger.Print(Name + "Started");
  33. var result = await Run();
  34. Dispose();
  35. Status = result ? ProcedureStatus.Success : ProcedureStatus.Failed;
  36. Logger.Print(Name + "Complete");
  37. return result;
  38. }
  39. internal virtual async Task<bool> Run()
  40. {
  41. return true;
  42. }
  43. public void Reset()
  44. {
  45. Status = ProcedureStatus.Idle;
  46. InfoLog = "";
  47. }
  48. internal async Task<bool> CheckAndCreateSocket()
  49. {
  50. TcpSerializer socketConnection = null;
  51. if (socketConnection == null || socketConnection.ConnectStatus == ConnectStatus.ConnectionFail ||
  52. socketConnection.ConnectStatus == ConnectStatus.DisConnected)
  53. {
  54. socketConnection = new Assist.TcpSerializer(ip:ServerIpAddress);
  55. await socketConnection.OpenAsync();
  56. if(socketConnection.ConnectStatus != ConnectStatus.Connected)
  57. {
  58. Logger.Print("EVSE tcp connection Failed,Check EVSE is connected", isError: true);
  59. return false;
  60. }
  61. Logger.Print("Connected");
  62. serialPortocol = new SerialPortocol(socketConnection);
  63. return true;
  64. }
  65. return true;
  66. }
  67. private void Dispose()
  68. {
  69. if(serialPortocol!=null)
  70. {
  71. try
  72. {
  73. serialPortocol.Close();
  74. }catch
  75. {
  76. }
  77. }
  78. }
  79. }
  80. }