ProcedureBase.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. internal SerialPortocol serialPortocol { get; private set; }
  26. internal static string ServerIpAddress = "192.168.1.10";
  27. public async Task<bool> DoWork() {
  28. if (!IsActivated)
  29. return false;
  30. InfoLog = "";
  31. Logger.Print(Name + "Started");
  32. var result = await Run();
  33. Dispose();
  34. Status = result ? ProcedureStatus.Success : ProcedureStatus.Failed;
  35. Logger.Print(Name + "Complete");
  36. return result;
  37. }
  38. internal virtual async Task<bool> Run()
  39. {
  40. return true;
  41. }
  42. public void Reset()
  43. {
  44. Status = ProcedureStatus.Idle;
  45. InfoLog = "";
  46. }
  47. internal async Task<bool> CheckAndCreateSocket()
  48. {
  49. TcpSerializer socketConnection = null;
  50. if (socketConnection == null || socketConnection.ConnectStatus == ConnectStatus.ConnectionFail ||
  51. socketConnection.ConnectStatus == ConnectStatus.DisConnected)
  52. {
  53. socketConnection = new Assist.TcpSerializer(ip:ServerIpAddress);
  54. await socketConnection.OpenAsync();
  55. if(socketConnection.ConnectStatus != ConnectStatus.Connected)
  56. {
  57. Logger.Print("EVSE tcp connection Failed,Check EVSE is connected", isError: true);
  58. return false;
  59. }
  60. Logger.Print("Connected");
  61. serialPortocol = new SerialPortocol(socketConnection);
  62. return true;
  63. }
  64. return true;
  65. }
  66. private void Dispose()
  67. {
  68. if(serialPortocol!=null)
  69. {
  70. try
  71. {
  72. serialPortocol.Close();
  73. }catch
  74. {
  75. }
  76. }
  77. }
  78. }
  79. }