FourGenModuleCheckProcedure.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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", isError: true);
  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. LogWriter.Report(LogEvent.FourgenModuleVersion, "empty" , isError: true);
  80. return false;
  81. }
  82. else
  83. {
  84. LogWriter.Log(string.Format("Read 4G Module version : {0} , Expect:{1}", fourthGenModuleVersion, UpdateData.FourGenModuleVersion));
  85. if (!fourthGenModuleVersion.ToLower().StartsWith(UpdateData.FourGenModuleVersion.ToLower()))
  86. {
  87. Error = ErrorType.VersionMismatch;
  88. LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion, isError: true);
  89. return false;
  90. }
  91. LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion);
  92. }
  93. var simstatus = await serialPortocol.GetSimStatus();
  94. string simstatusString;
  95. if (simstatus == null)
  96. {
  97. simstatusString = "unknown";
  98. //LogWriter.Report(LogEvent.SimStatus, "unknown");
  99. }
  100. else if(simstatus.IsInstalled)
  101. {
  102. simstatusString = "inserted";
  103. //LogWriter.Report(LogEvent.SimStatus, "inserted");
  104. }
  105. else
  106. {
  107. simstatusString = "none";
  108. //LogWriter.Report(LogEvent.SimStatus, "none");
  109. }
  110. if (simstatus == null)
  111. {
  112. Error = ErrorType.SimStatusReadFail;
  113. LogWriter.Report(LogEvent.SimStatus, simstatusString , isError: true);
  114. return false;
  115. }
  116. else
  117. {
  118. LogWriter.Log(string.Format("Get Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSimInsert ? "installed" : "uninstalled"));
  119. if (simstatus.IsInstalled != UpdateData.IsSimInsert)
  120. {
  121. Error = ErrorType.SimStatusMismatch;
  122. LogWriter.Report(LogEvent.SimStatus, simstatusString, isError: true);
  123. return false;
  124. }
  125. else
  126. {
  127. LogWriter.Report(LogEvent.SimStatus, simstatusString);
  128. if (simstatus.IsInstalled)
  129. {
  130. var iccidByteList = simstatus.ICCID.ToList();
  131. iccidByteList.RemoveAll(x => x == 0x00);
  132. var iccidBytes = iccidByteList.ToArray();
  133. var imsiByteList = simstatus.IMSI.ToList();
  134. imsiByteList.RemoveAll(x => x == 0x00);
  135. var imsiBytes = imsiByteList.ToArray();
  136. var ICCIDstring = Encoding.ASCII.GetString(iccidBytes).Trim();
  137. var IMSIstring = Encoding.ASCII.GetString(imsiBytes).Trim();
  138. //LogWriter.Report(LogEvent.SimICCID, ICCIDstring);
  139. //LogWriter.Report(LogEvent.SimIMSI, IMSIstring);
  140. LogWriter.Log(string.Format("Get Sim ICCID : {0} , Expect:{1}", ICCIDstring, UpdateData.ICCID));
  141. LogWriter.Log(string.Format("Get Sim IMSI : {0} , Expect:{1}", IMSIstring, UpdateData.IMSI));
  142. if (ICCIDstring != UpdateData.ICCID)
  143. {
  144. LogWriter.Log("Sim card ICCID not match");
  145. Error = ErrorType.IccidMistach;
  146. LogWriter.Report(LogEvent.SimICCID, ICCIDstring, isError: true);
  147. return false;
  148. }
  149. else
  150. {
  151. LogWriter.Report(LogEvent.SimICCID, ICCIDstring);
  152. }
  153. if (IMSIstring != UpdateData.IMSI)
  154. {
  155. LogWriter.Log("Sim card IMSI not match");
  156. Error = ErrorType.ImsiMistach;
  157. LogWriter.Report(LogEvent.SimIMSI, IMSIstring, isError: true);
  158. return false;
  159. }
  160. else
  161. {
  162. LogWriter.Report(LogEvent.SimICCID, IMSIstring);
  163. }
  164. }
  165. else
  166. {
  167. }
  168. }
  169. }
  170. return true;
  171. }
  172. }
  173. }