FourGenModuleCheckProcedure.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AwInitilizer.Procedure.FourGenModuleCheck
  7. {
  8. public enum ErrorType
  9. {
  10. None,
  11. ConnectFail,
  12. VersionReadFail,
  13. VersionMismatch,
  14. SimStatusReadFail,
  15. SimStatusMismatch,
  16. IccidMistach,
  17. ImsiMistach,
  18. }
  19. public enum LogEvent
  20. {
  21. FourgenSocketConnect,
  22. FourgenModuleVersion,
  23. SimStatus,
  24. SimICCID,
  25. SimIMSI
  26. }
  27. public class FourGenModuleCheckProcedure : ProcedureBase
  28. {
  29. public ErrorType Error { get; set; } = ErrorType.None;
  30. private ProcedureLog.LogWriter<FourGenModuleCheckProcedure, LogEvent> LogWriter;
  31. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  32. {
  33. { LogEvent.FourgenSocketConnect, "FourgenSocketConnect" },
  34. { LogEvent.FourgenModuleVersion, "FourgenModuleVersion" },
  35. { LogEvent.SimStatus, "SimStatus" },
  36. { LogEvent.SimICCID, "SimICCID" },
  37. { LogEvent.SimIMSI, "SimIMSI" },
  38. };
  39. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  40. {
  41. { LogEvent.FourgenSocketConnect, "EVSE connect {0}" },
  42. { LogEvent.FourgenModuleVersion, "Read 4G Module version : {0}" },
  43. { LogEvent.SimStatus, "Get sim instert status {0}" },
  44. { LogEvent.SimICCID, "Get Sim ICCID : {0}" },
  45. { LogEvent.SimIMSI, "Get Sim IMSI : {0}" },
  46. };
  47. public FourGenModuleCheckProcedure() : base()
  48. {
  49. Name = "4G Check";
  50. Content = "Check 4G module version and SIM card information matches user input";
  51. LogWriter = new ProcedureLog.LogWriter<FourGenModuleCheckProcedure, LogEvent>(this)
  52. {
  53. ReportPair = ReportDict,
  54. LogPair = LogDict
  55. };
  56. }
  57. internal override async Task<bool> Run()
  58. {
  59. if (!UpdateData.SystemID.ModelName.Network.ToString().Contains("4G"))
  60. {
  61. //if does not support 4G then end init
  62. LogWriter.Log("4G not supported, skip procedure");
  63. return true;
  64. }
  65. //Logger.Print("Connecting to EVSE");
  66. if (!await base.CheckAndCreateSocket())
  67. {
  68. LogWriter.Report(LogEvent.FourgenSocketConnect,"fail");
  69. Error = ErrorType.ConnectFail;
  70. return false;
  71. }
  72. LogWriter.Report(LogEvent.FourgenSocketConnect, "success");
  73. var fourthGenModuleVersion = await serialPortocol.GetFourGenModuleVersion();
  74. LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion);
  75. if (string.IsNullOrEmpty(fourthGenModuleVersion))
  76. {
  77. Error = ErrorType.VersionReadFail;
  78. LogWriter.Log("4G module version read error");
  79. return false;
  80. }
  81. else
  82. {
  83. LogWriter.Log(string.Format("Read 4G Module version : {0} , Expect:{1}", fourthGenModuleVersion, UpdateData.FourGenModuleVersion));
  84. if (!fourthGenModuleVersion.ToLower().StartsWith(UpdateData.FourGenModuleVersion.ToLower()))
  85. {
  86. Error = ErrorType.VersionMismatch;
  87. return false;
  88. }
  89. }
  90. var simstatus = await serialPortocol.GetSimStatus();
  91. if(simstatus == null)
  92. {
  93. LogWriter.Report(LogEvent.SimStatus, "unknown");
  94. }
  95. else if(simstatus.IsInstalled)
  96. {
  97. LogWriter.Report(LogEvent.SimStatus, "inserted");
  98. }
  99. else
  100. {
  101. LogWriter.Report(LogEvent.SimStatus, "none");
  102. }
  103. if (simstatus == null)
  104. {
  105. Error = ErrorType.SimStatusReadFail;
  106. return false;
  107. }
  108. else
  109. {
  110. LogWriter.Log(string.Format("Get Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSimInsert ? "installed" : "uninstalled"));
  111. if (simstatus.IsInstalled != UpdateData.IsSimInsert)
  112. {
  113. Error = ErrorType.SimStatusMismatch;
  114. return false;
  115. }
  116. else
  117. {
  118. if (simstatus.IsInstalled)
  119. {
  120. var iccidByteList = simstatus.ICCID.ToList();
  121. iccidByteList.RemoveAll(x => x == 0x00);
  122. var iccidBytes = iccidByteList.ToArray();
  123. var imsiByteList = simstatus.IMSI.ToList();
  124. imsiByteList.RemoveAll(x => x == 0x00);
  125. var imsiBytes = imsiByteList.ToArray();
  126. var ICCIDstring = Encoding.ASCII.GetString(iccidBytes).Trim();
  127. var IMSIstring = Encoding.ASCII.GetString(imsiBytes).Trim();
  128. LogWriter.Report(LogEvent.SimICCID, ICCIDstring);
  129. LogWriter.Report(LogEvent.SimIMSI, IMSIstring);
  130. LogWriter.Log(string.Format("Get Sim ICCID : {0} , Expect:{1}", ICCIDstring, UpdateData.ICCID));
  131. LogWriter.Log(string.Format("Get Sim IMSI : {0} , Expect:{1}", IMSIstring, UpdateData.IMSI));
  132. if (ICCIDstring != UpdateData.ICCID)
  133. {
  134. LogWriter.Log("Sim card ICCID not match");
  135. Error = ErrorType.IccidMistach;
  136. return false;
  137. }
  138. if (IMSIstring != UpdateData.IMSI)
  139. {
  140. LogWriter.Log("Sim card IMSI not match");
  141. Error = ErrorType.ImsiMistach;
  142. return false;
  143. }
  144. }
  145. else
  146. {
  147. }
  148. }
  149. }
  150. return true;
  151. }
  152. }
  153. }