FourGenModuleCheckProcedure.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. //InfoLog += "model name does not support 4g ,skip update process\n";
  64. //ReportLog.Add("4G not supported, skip procedure");
  65. return true;
  66. }
  67. //Logger.Print("Connecting to EVSE");
  68. if (!await base.CheckAndCreateSocket())
  69. {
  70. LogWriter.Report(LogEvent.FourgenSocketConnect,"fail");
  71. Error = ErrorType.ConnectFail;
  72. //InfoLog += "EVSE connect failed\n";
  73. //LogPair.Add("FourgenSocketConnect", "0");
  74. //ReportLog.Add("EVSE connect failed");
  75. return false;
  76. }
  77. LogWriter.Report(LogEvent.FourgenSocketConnect, "success");
  78. var fourthGenModuleVersion = await serialPortocol.GetFourGenModuleVersion();
  79. LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion);
  80. //LogPair.Add("FourgenModuleVersion", fourthGenModuleVersion);
  81. if (string.IsNullOrEmpty(fourthGenModuleVersion))
  82. {
  83. Error = ErrorType.VersionReadFail;
  84. LogWriter.Log("4G module version read error");
  85. //InfoLog += "4G module version read error\n";
  86. //Logger.Print("4G module version read error", isError: true);
  87. //ReportLog.Add("4G module version read error");
  88. return false;
  89. }
  90. else
  91. {
  92. //InfoLog += $"Get 4G Module version :{fourthGenModuleVersion}\n";
  93. //ReportLog.Add(string.Format("Read 4G Module version : {0} , Expect:{1}", fourthGenModuleVersion, UpdateData.FourGenModuleVersion));
  94. if (!fourthGenModuleVersion.ToLower().StartsWith(UpdateData.FourGenModuleVersion.ToLower()))
  95. {
  96. Error = ErrorType.VersionMismatch;
  97. //InfoLog += "4G module version not matched\n";
  98. //Logger.Print("4G module version not matched", isError: true);
  99. return false;
  100. }
  101. }
  102. var simstatus = await serialPortocol.GetSimStatus();
  103. if(simstatus == null)
  104. {
  105. LogWriter.Report(LogEvent.SimStatus, "unknown");
  106. }
  107. else if(simstatus.IsInstalled)
  108. {
  109. LogWriter.Report(LogEvent.SimStatus, "inserted");
  110. }
  111. else
  112. {
  113. LogWriter.Report(LogEvent.SimStatus, "none");
  114. }
  115. if (simstatus == null)
  116. {
  117. Error = ErrorType.SimStatusReadFail;
  118. //InfoLog += "Get sim status failed\n";
  119. //Logger.Print("Get sim status failed", isError: true);
  120. //LogPair.Add("SimStatus", "ReadFail");
  121. //ReportLog.Add("Get sim status failed");
  122. return false;
  123. }
  124. else
  125. {
  126. //LogPair.Add("SimStatus", simstatus.IsInstalled? "1":"0");
  127. //InfoLog += $"iccidBytes,{BitConverter.ToString(simstatus.ICCID).Replace("-","")}\n";
  128. //InfoLog += $"imsiBytes,{BitConverter.ToString(simstatus.IMSI).Replace("-", "")}\n";
  129. if (simstatus.IsInstalled != UpdateData.IsSimInsert)
  130. {
  131. LogWriter.Log(string.Format("Get Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSimInsert ? "installed" : "uninstalled"));
  132. Error = ErrorType.SimStatusMismatch;
  133. //ReportLog.Add(string.Format("Get Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSimInsert ? "installed" : "uninstalled"));
  134. //Logger.Print("sim install status not matched", isError: true);
  135. return false;
  136. }
  137. else
  138. {
  139. if (simstatus.IsInstalled)
  140. {
  141. var iccidByteList = simstatus.ICCID.ToList();
  142. iccidByteList.RemoveAll(x => x == 0x00);
  143. var iccidBytes = iccidByteList.ToArray();
  144. var imsiByteList = simstatus.IMSI.ToList();
  145. imsiByteList.RemoveAll(x => x == 0x00);
  146. var imsiBytes = imsiByteList.ToArray();
  147. var ICCIDstring = Encoding.ASCII.GetString(iccidBytes).Trim();
  148. var IMSIstring = Encoding.ASCII.GetString(imsiBytes).Trim();
  149. LogWriter.Report(LogEvent.SimICCID, ICCIDstring);
  150. LogWriter.Report(LogEvent.SimIMSI, IMSIstring);
  151. //LogPair.Add("SimICCID", ICCIDstring);
  152. //LogPair.Add("SimIMSI", IMSIstring);
  153. //ReportLog.Add(string.Format("Get Sim ICCID : {0} , Expect:{1}", ICCIDstring, UpdateData.ICCID));
  154. //ReportLog.Add(string.Format("Get Sim IMSI : {0} , Expect:{1}", IMSIstring, UpdateData.IMSI));
  155. //InfoLog += $"Get sim info, inserted:{simstatus.IsInstalled},ICCID:{ICCIDstring},IMSI:{IMSIstring}\n";
  156. if (ICCIDstring != UpdateData.ICCID)
  157. {
  158. LogWriter.Log("Sim card ICCID not match");
  159. Error = ErrorType.IccidMistach;
  160. //Logger.Print("sim card ICCID not matched", isError: true);
  161. //InfoLog += $"Sim card ICCID not matched,{ICCIDstring}:{UpdateData.ICCID}\n";
  162. return false;
  163. }
  164. if (IMSIstring != UpdateData.IMSI)
  165. {
  166. LogWriter.Log("Sim card IMSI not match");
  167. Error = ErrorType.ImsiMistach;
  168. //Logger.Print("sim card IMSI not matched", isError: true);
  169. //InfoLog += $"Sim card IMSI not matched,{IMSIstring}:{UpdateData.IMSI}\n";
  170. return false;
  171. }
  172. }
  173. else
  174. {
  175. //InfoLog += $"Get sim info, inserted:{simstatus.IsInstalled}\n";
  176. //if (!simstatus.ICCID.SequenceEqual(new byte[22]))
  177. //{
  178. // InfoLog += $"ICCID not empty : { BitConverter.ToString(simstatus.ICCID).Replace("-", " ")}\n";
  179. // ReportLog.Add("ICCID not empty");
  180. // Logger.Print("sim card ICCID not empty", isError: true);
  181. // return false;
  182. //}
  183. //if (!simstatus.IMSI.SequenceEqual(new byte[16]))
  184. //{
  185. // InfoLog += $"IMSI not empty : { BitConverter.ToString(simstatus.IMSI).Replace("-", " ")}\n";
  186. // ReportLog.Add("IMSI not empty");
  187. // Logger.Print("sim card IMSI not empty", isError: true);
  188. // return false;
  189. //}
  190. }
  191. }
  192. }
  193. return true;
  194. }
  195. }
  196. }