FourGenModuleCheckProcedure.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using AwInitilizer.Assist;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace AwInitilizer.Procedure.FourGenModuleCheck
  8. {
  9. public enum ErrorType
  10. {
  11. None,
  12. ConnectFail,
  13. VersionReadFail,
  14. VersionMismatch,
  15. SimStatusReadFail,
  16. SimStatusMismatch,
  17. IccidMistach,
  18. ImsiMistach,
  19. }
  20. public enum LogEvent
  21. {
  22. FourgenSocketConnect,
  23. FourgenModuleVersion,
  24. SubFourgenModuleVersion,
  25. SimStatus,
  26. SimICCID,
  27. SimIMSI,
  28. SubSimStatus,
  29. SubSimICCID,
  30. SubSimIMSI
  31. }
  32. public class FourGenModuleCheckProcedure : ProcedureBase
  33. {
  34. public ErrorType Error { get; set; } = ErrorType.None;
  35. private ProcedureLog.LogWriter<FourGenModuleCheckProcedure, LogEvent> LogWriter;
  36. private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
  37. {
  38. { LogEvent.FourgenSocketConnect, "FourgenSocketConnect" },
  39. { LogEvent.FourgenModuleVersion, "FourgenModuleVersion" },
  40. { LogEvent.SubFourgenModuleVersion, "SubFourgenModuleVersion" },
  41. { LogEvent.SimStatus, "SimStatus" },
  42. { LogEvent.SimICCID, "SimICCID" },
  43. { LogEvent.SimIMSI, "SimIMSI" },
  44. { LogEvent.SubSimStatus, "SubSimStatus" },
  45. { LogEvent.SubSimICCID, "SubSimICCID" },
  46. { LogEvent.SubSimIMSI, "SubSimIMSI" },
  47. };
  48. private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
  49. {
  50. { LogEvent.FourgenSocketConnect, "EVSE connect {0}" },
  51. { LogEvent.FourgenModuleVersion, "Read 4G Module version : {0}" },
  52. { LogEvent.SubFourgenModuleVersion, "Read sub 4G Module version : {0}" },
  53. { LogEvent.SimStatus, "Get sim instert status {0}" },
  54. { LogEvent.SimICCID, "Get Sim ICCID : {0}" },
  55. { LogEvent.SimIMSI, "Get Sim IMSI : {0}" },
  56. { LogEvent.SubSimStatus, "Get sub sim instert status {0}" },
  57. { LogEvent.SubSimICCID, "Get sub Sim ICCID : {0}" },
  58. { LogEvent.SubSimIMSI, "Get sub Sim IMSI : {0}" },
  59. };
  60. public FourGenModuleCheckProcedure() : base()
  61. {
  62. Name = "4G Check";
  63. Content = "Check 4G module version and SIM card information matches user input";
  64. LogWriter = new ProcedureLog.LogWriter<FourGenModuleCheckProcedure, LogEvent>(this)
  65. {
  66. ReportPair = ReportDict,
  67. LogPair = LogDict
  68. };
  69. }
  70. internal override async Task<bool> Run()
  71. {
  72. //if (!UpdateData.SystemID.ModelName.Network.Description.Contains("4G"))
  73. if (!UpdateData.SystemID.ModelName.Network.SupportTelecom)
  74. {
  75. //if does not support 4G then end init
  76. LogWriter.Log("4G not supported, skip procedure");
  77. return true;
  78. }
  79. //Logger.Print("Connecting to EVSE");
  80. int pollingCnt;
  81. bool response;
  82. for (pollingCnt = 0; pollingCnt < 56; pollingCnt++)
  83. {
  84. await Task.Delay(TimeSpan.FromSeconds(15));
  85. response = await ChekCsuBootCompelete();
  86. if (response)
  87. break;
  88. }
  89. LogWriter.Log(string.Format("EVSE connet elapsed minute(s) : {0}, Expect:<16", (pollingCnt * 0.25) + 2));
  90. //timeout
  91. if (pollingCnt >= 56)
  92. {
  93. LogWriter.Report(LogEvent.FourgenSocketConnect, "fail", isError: true);
  94. Error = ErrorType.ConnectFail;
  95. return false;
  96. }
  97. //if (!await base.CheckAndCreateSocket())
  98. //{
  99. // LogWriter.Report(LogEvent.FourgenSocketConnect, "fail", isError: true);
  100. // Error = ErrorType.ConnectFail;
  101. // return false;
  102. //}
  103. LogWriter.Report(LogEvent.FourgenSocketConnect, "success");
  104. //var fourthGenModuleVersion = await serialPortocol.GetFourGenModuleVersion();
  105. var getfourthGenModuleVersion = await EvApi.GetTelcomModemFwRev();
  106. var fourthGenModuleVersion = getfourthGenModuleVersion.Result;
  107. //LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion);
  108. if (string.IsNullOrEmpty(fourthGenModuleVersion))
  109. {
  110. Error = ErrorType.VersionReadFail;
  111. LogWriter.Log("4G module version read error");
  112. LogWriter.Report(LogEvent.FourgenModuleVersion, "empty", isError: true);
  113. return false;
  114. }
  115. LogWriter.Log(string.Format("Read 4G Module version : {0} , Expect:{1}", fourthGenModuleVersion, UpdateData.FourGenModuleVersion));
  116. if (!fourthGenModuleVersion.ToLower().StartsWith(UpdateData.FourGenModuleVersion.ToLower()))
  117. {
  118. Error = ErrorType.VersionMismatch;
  119. LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion, isError: true);
  120. return false;
  121. }
  122. LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion);
  123. if (UpdateData.SystemID.ModelName.Network.SupportSubTelecom)
  124. {
  125. //var fourthGenModuleVersion = await serialPortocol.GetFourGenModuleVersion();
  126. getfourthGenModuleVersion = await EvApi.GetTelcomSubModemFwRev();
  127. fourthGenModuleVersion = getfourthGenModuleVersion.Result;
  128. //LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion);
  129. int retryCnt = 0;
  130. while (string.IsNullOrEmpty(fourthGenModuleVersion) && retryCnt < 30)
  131. {
  132. retryCnt++;
  133. await Task.Delay(TimeSpan.FromSeconds(10));
  134. getfourthGenModuleVersion = await EvApi.GetTelcomSubModemFwRev();
  135. fourthGenModuleVersion = getfourthGenModuleVersion.Result;
  136. }
  137. LogWriter.Log($"Get sub sim version retry {retryCnt}");
  138. if (string.IsNullOrEmpty(fourthGenModuleVersion))
  139. {
  140. Error = ErrorType.VersionReadFail;
  141. LogWriter.Log("sub 4G module version read error");
  142. LogWriter.Report(LogEvent.SubFourgenModuleVersion, "empty", isError: true);
  143. return false;
  144. }
  145. LogWriter.Log(string.Format("Read sub 4G Module version : {0} , Expect:{1}", fourthGenModuleVersion, UpdateData.SubFourGenModuleVersion));
  146. if (!fourthGenModuleVersion.ToLower().StartsWith(UpdateData.SubFourGenModuleVersion.ToLower()))
  147. {
  148. Error = ErrorType.VersionMismatch;
  149. LogWriter.Report(LogEvent.SubFourgenModuleVersion, fourthGenModuleVersion, isError: true);
  150. return false;
  151. }
  152. LogWriter.Report(LogEvent.SubFourgenModuleVersion, fourthGenModuleVersion);
  153. }
  154. //var simstatus = await serialPortocol.GetSimStatus();
  155. var getResult = await EvApi.GetTelcomSimStatus();
  156. var simstatus = getResult.Result;
  157. string simstatusString;
  158. if (simstatus == null)
  159. {
  160. simstatusString = "unknown";
  161. //LogWriter.Report(LogEvent.SimStatus, "unknown");
  162. }
  163. else if (simstatus.IsInstalled)
  164. {
  165. simstatusString = "inserted";
  166. //LogWriter.Report(LogEvent.SimStatus, "inserted");
  167. }
  168. else
  169. {
  170. simstatusString = "none";
  171. //LogWriter.Report(LogEvent.SimStatus, "none");
  172. }
  173. if (simstatus == null)
  174. {
  175. Error = ErrorType.SimStatusReadFail;
  176. LogWriter.Report(LogEvent.SimStatus, simstatusString, isError: true);
  177. return false;
  178. }
  179. LogWriter.Log(string.Format("Get Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSimInsert ? "installed" : "uninstalled"));
  180. if (simstatus.IsInstalled != UpdateData.IsSimInsert)
  181. {
  182. Error = ErrorType.SimStatusMismatch;
  183. LogWriter.Report(LogEvent.SimStatus, simstatusString, isError: true);
  184. return false;
  185. }
  186. LogWriter.Report(LogEvent.SimStatus, simstatusString);
  187. if (simstatus.IsInstalled)
  188. {
  189. //var iccidByteList = simstatus.ICCID.ToList();
  190. //iccidByteList.RemoveAll(x => x == 0x00);
  191. //var iccidBytes = iccidByteList.ToArray();
  192. //var imsiByteList = simstatus.IMSI.ToList();
  193. //imsiByteList.RemoveAll(x => x == 0x00);
  194. //var imsiBytes = imsiByteList.ToArray();
  195. //var ICCIDstring = Encoding.ASCII.GetString(iccidBytes).Trim();
  196. //var IMSIstring = Encoding.ASCII.GetString(imsiBytes).Trim();
  197. var ICCIDstring = simstatus.ICCID;
  198. var IMSIstring = simstatus.IMSI;
  199. //LogWriter.Report(LogEvent.SimICCID, ICCIDstring);
  200. //LogWriter.Report(LogEvent.SimIMSI, IMSIstring);
  201. LogWriter.Log(string.Format("Get Sim ICCID : {0} , Expect:{1}", ICCIDstring, UpdateData.SimICCID));
  202. LogWriter.Log(string.Format("Get Sim IMSI : {0} , Expect:{1}", IMSIstring, UpdateData.SimIMSI));
  203. if (ICCIDstring != UpdateData.SimICCID)
  204. {
  205. LogWriter.Log("Sim card ICCID not match");
  206. Error = ErrorType.IccidMistach;
  207. LogWriter.Report(LogEvent.SimICCID, ICCIDstring, isError: true);
  208. return false;
  209. }
  210. else
  211. {
  212. LogWriter.Report(LogEvent.SimICCID, ICCIDstring);
  213. }
  214. if (IMSIstring != UpdateData.SimIMSI)
  215. {
  216. LogWriter.Log("Sim card IMSI not match");
  217. Error = ErrorType.ImsiMistach;
  218. LogWriter.Report(LogEvent.SimIMSI, IMSIstring, isError: true);
  219. return false;
  220. }
  221. else
  222. {
  223. LogWriter.Report(LogEvent.SimIMSI, IMSIstring);
  224. }
  225. }
  226. else
  227. {
  228. }
  229. if (!UpdateData.SystemID.ModelName.Network.SupportSubTelecom)
  230. {
  231. return true;
  232. }
  233. getResult = await EvApi.GetTelcomSubSimStatus();
  234. simstatus = getResult.Result;
  235. if (simstatus == null)
  236. {
  237. simstatusString = "unknown";
  238. //LogWriter.Report(LogEvent.SimStatus, "unknown");
  239. }
  240. else if (simstatus.IsInstalled)
  241. {
  242. simstatusString = "inserted";
  243. //LogWriter.Report(LogEvent.SimStatus, "inserted");
  244. }
  245. else
  246. {
  247. simstatusString = "none";
  248. //LogWriter.Report(LogEvent.SimStatus, "none");
  249. }
  250. if (simstatus == null)
  251. {
  252. Error = ErrorType.SimStatusReadFail;
  253. LogWriter.Report(LogEvent.SubSimStatus, simstatusString, isError: true);
  254. return false;
  255. }
  256. LogWriter.Log(string.Format("Get Sub Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSubSimInsert ? "installed" : "uninstalled"));
  257. if (simstatus.IsInstalled != UpdateData.IsSubSimInsert)
  258. {
  259. Error = ErrorType.SimStatusMismatch;
  260. LogWriter.Report(LogEvent.SubSimStatus, simstatusString, isError: true);
  261. return false;
  262. }
  263. else
  264. {
  265. LogWriter.Report(LogEvent.SubSimStatus, simstatusString);
  266. if (simstatus.IsInstalled)
  267. {
  268. //var iccidByteList = simstatus.ICCID.ToList();
  269. //iccidByteList.RemoveAll(x => x == 0x00);
  270. //var iccidBytes = iccidByteList.ToArray();
  271. //var imsiByteList = simstatus.IMSI.ToList();
  272. //imsiByteList.RemoveAll(x => x == 0x00);
  273. //var imsiBytes = imsiByteList.ToArray();
  274. //var ICCIDstring = Encoding.ASCII.GetString(iccidBytes).Trim();
  275. //var IMSIstring = Encoding.ASCII.GetString(imsiBytes).Trim();
  276. var ICCIDstring = simstatus.ICCID;
  277. var IMSIstring = simstatus.IMSI;
  278. //LogWriter.Report(LogEvent.SimICCID, ICCIDstring);
  279. //LogWriter.Report(LogEvent.SimIMSI, IMSIstring);
  280. LogWriter.Log(string.Format("Get Sim ICCID : {0} , Expect:{1}", ICCIDstring, UpdateData.SubSimICCID));
  281. LogWriter.Log(string.Format("Get Sim IMSI : {0} , Expect:{1}", IMSIstring, UpdateData.SubSimIMSI));
  282. if (ICCIDstring != UpdateData.SubSimICCID)
  283. {
  284. LogWriter.Log("Sim card ICCID not match");
  285. Error = ErrorType.IccidMistach;
  286. LogWriter.Report(LogEvent.SubSimICCID, ICCIDstring, isError: true);
  287. return false;
  288. }
  289. else
  290. {
  291. LogWriter.Report(LogEvent.SubSimICCID, ICCIDstring);
  292. }
  293. if (IMSIstring != UpdateData.SubSimIMSI)
  294. {
  295. LogWriter.Log("Sim card IMSI not match");
  296. Error = ErrorType.ImsiMistach;
  297. LogWriter.Report(LogEvent.SubSimIMSI, IMSIstring, isError: true);
  298. return false;
  299. }
  300. else
  301. {
  302. LogWriter.Report(LogEvent.SubSimIMSI, IMSIstring);
  303. }
  304. }
  305. else
  306. {
  307. }
  308. }
  309. return true;
  310. }
  311. }
  312. }