123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace AwInitilizer.Procedure.FourGenModuleCheck
- {
- public enum ErrorType
- {
- None,
- ConnectFail,
- VersionReadFail,
- VersionMismatch,
- SimStatusReadFail,
- SimStatusMismatch,
- IccidMistach,
- ImsiMistach,
- }
- public enum LogEvent
- {
- FourgenSocketConnect,
- FourgenModuleVersion,
- SimStatus,
- SimICCID,
- SimIMSI
- }
- public class FourGenModuleCheckProcedure : ProcedureBase
- {
- public ErrorType Error { get; set; } = ErrorType.None;
- private ProcedureLog.LogWriter<FourGenModuleCheckProcedure, LogEvent> LogWriter;
- private readonly static Dictionary<LogEvent, string> ReportDict = new Dictionary<LogEvent, string>()
- {
- { LogEvent.FourgenSocketConnect, "FourgenSocketConnect" },
- { LogEvent.FourgenModuleVersion, "FourgenModuleVersion" },
- { LogEvent.SimStatus, "SimStatus" },
- { LogEvent.SimICCID, "SimICCID" },
- { LogEvent.SimIMSI, "SimIMSI" },
- };
- private readonly static Dictionary<LogEvent, string> LogDict = new Dictionary<LogEvent, string>()
- {
- { LogEvent.FourgenSocketConnect, "EVSE connect {0}" },
- { LogEvent.FourgenModuleVersion, "Read 4G Module version : {0}" },
- { LogEvent.SimStatus, "Get sim instert status {0}" },
- { LogEvent.SimICCID, "Get Sim ICCID : {0}" },
- { LogEvent.SimIMSI, "Get Sim IMSI : {0}" },
- };
- public FourGenModuleCheckProcedure() : base()
- {
- Name = "4G Check";
- Content = "Check 4G module version and SIM card information matches user input";
- LogWriter = new ProcedureLog.LogWriter<FourGenModuleCheckProcedure, LogEvent>(this)
- {
- ReportPair = ReportDict,
- LogPair = LogDict
- };
- }
- internal override async Task<bool> Run()
- {
- if (!UpdateData.SystemID.ModelName.Network.ToString().Contains("4G"))
- {
- //if does not support 4G then end init
- LogWriter.Log("4G not supported, skip procedure");
- return true;
- }
- //Logger.Print("Connecting to EVSE");
- if (!await base.CheckAndCreateSocket())
- {
- LogWriter.Report(LogEvent.FourgenSocketConnect,"fail");
- Error = ErrorType.ConnectFail;
- return false;
- }
- LogWriter.Report(LogEvent.FourgenSocketConnect, "success");
- var fourthGenModuleVersion = await serialPortocol.GetFourGenModuleVersion();
- LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion);
-
- if (string.IsNullOrEmpty(fourthGenModuleVersion))
- {
- Error = ErrorType.VersionReadFail;
- LogWriter.Log("4G module version read error");
- return false;
- }
- else
- {
- LogWriter.Log(string.Format("Read 4G Module version : {0} , Expect:{1}", fourthGenModuleVersion, UpdateData.FourGenModuleVersion));
- if (!fourthGenModuleVersion.ToLower().StartsWith(UpdateData.FourGenModuleVersion.ToLower()))
- {
- Error = ErrorType.VersionMismatch;
- return false;
- }
- }
- var simstatus = await serialPortocol.GetSimStatus();
- if(simstatus == null)
- {
- LogWriter.Report(LogEvent.SimStatus, "unknown");
- }
- else if(simstatus.IsInstalled)
- {
- LogWriter.Report(LogEvent.SimStatus, "inserted");
- }
- else
- {
- LogWriter.Report(LogEvent.SimStatus, "none");
- }
- if (simstatus == null)
- {
- Error = ErrorType.SimStatusReadFail;
- return false;
- }
- else
- {
- LogWriter.Log(string.Format("Get Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSimInsert ? "installed" : "uninstalled"));
- if (simstatus.IsInstalled != UpdateData.IsSimInsert)
- {
- Error = ErrorType.SimStatusMismatch;
- return false;
- }
- else
- {
- if (simstatus.IsInstalled)
- {
- var iccidByteList = simstatus.ICCID.ToList();
- iccidByteList.RemoveAll(x => x == 0x00);
- var iccidBytes = iccidByteList.ToArray();
- var imsiByteList = simstatus.IMSI.ToList();
- imsiByteList.RemoveAll(x => x == 0x00);
- var imsiBytes = imsiByteList.ToArray();
- var ICCIDstring = Encoding.ASCII.GetString(iccidBytes).Trim();
- var IMSIstring = Encoding.ASCII.GetString(imsiBytes).Trim();
- LogWriter.Report(LogEvent.SimICCID, ICCIDstring);
- LogWriter.Report(LogEvent.SimIMSI, IMSIstring);
- LogWriter.Log(string.Format("Get Sim ICCID : {0} , Expect:{1}", ICCIDstring, UpdateData.ICCID));
- LogWriter.Log(string.Format("Get Sim IMSI : {0} , Expect:{1}", IMSIstring, UpdateData.IMSI));
- if (ICCIDstring != UpdateData.ICCID)
- {
- LogWriter.Log("Sim card ICCID not match");
- Error = ErrorType.IccidMistach;
- return false;
- }
- if (IMSIstring != UpdateData.IMSI)
- {
- LogWriter.Log("Sim card IMSI not match");
- Error = ErrorType.ImsiMistach;
- return false;
- }
- }
- else
- {
- }
- }
- }
- return true;
- }
- }
- }
|