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 LogWriter; private readonly static Dictionary ReportDict = new Dictionary() { { LogEvent.FourgenSocketConnect, "FourgenSocketConnect" }, { LogEvent.FourgenModuleVersion, "FourgenModuleVersion" }, { LogEvent.SimStatus, "SimStatus" }, { LogEvent.SimICCID, "SimICCID" }, { LogEvent.SimIMSI, "SimIMSI" }, }; private readonly static Dictionary LogDict = new Dictionary() { { 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(this) { ReportPair = ReportDict, LogPair = LogDict }; } internal override async Task Run() { if (!UpdateData.SystemID.ModelName.Network.ToString().Contains("4G")) { //if does not support 4G then end init LogWriter.Log("4G not supported, skip procedure"); //InfoLog += "model name does not support 4g ,skip update process\n"; //ReportLog.Add("4G not supported, skip procedure"); return true; } //Logger.Print("Connecting to EVSE"); if (!await base.CheckAndCreateSocket()) { LogWriter.Report(LogEvent.FourgenSocketConnect,"fail"); Error = ErrorType.ConnectFail; //InfoLog += "EVSE connect failed\n"; //LogPair.Add("FourgenSocketConnect", "0"); //ReportLog.Add("EVSE connect failed"); return false; } LogWriter.Report(LogEvent.FourgenSocketConnect, "success"); var fourthGenModuleVersion = await serialPortocol.GetFourGenModuleVersion(); LogWriter.Report(LogEvent.FourgenModuleVersion, fourthGenModuleVersion); //LogPair.Add("FourgenModuleVersion", fourthGenModuleVersion); if (string.IsNullOrEmpty(fourthGenModuleVersion)) { Error = ErrorType.VersionReadFail; LogWriter.Log("4G module version read error"); //InfoLog += "4G module version read error\n"; //Logger.Print("4G module version read error", isError: true); //ReportLog.Add("4G module version read error"); return false; } else { //InfoLog += $"Get 4G Module version :{fourthGenModuleVersion}\n"; //ReportLog.Add(string.Format("Read 4G Module version : {0} , Expect:{1}", fourthGenModuleVersion, UpdateData.FourGenModuleVersion)); if (!fourthGenModuleVersion.ToLower().StartsWith(UpdateData.FourGenModuleVersion.ToLower())) { Error = ErrorType.VersionMismatch; //InfoLog += "4G module version not matched\n"; //Logger.Print("4G module version not matched", isError: true); 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; //InfoLog += "Get sim status failed\n"; //Logger.Print("Get sim status failed", isError: true); //LogPair.Add("SimStatus", "ReadFail"); //ReportLog.Add("Get sim status failed"); return false; } else { //LogPair.Add("SimStatus", simstatus.IsInstalled? "1":"0"); //InfoLog += $"iccidBytes,{BitConverter.ToString(simstatus.ICCID).Replace("-","")}\n"; //InfoLog += $"imsiBytes,{BitConverter.ToString(simstatus.IMSI).Replace("-", "")}\n"; if (simstatus.IsInstalled != UpdateData.IsSimInsert) { LogWriter.Log(string.Format("Get Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSimInsert ? "installed" : "uninstalled")); Error = ErrorType.SimStatusMismatch; //ReportLog.Add(string.Format("Get Sim Status : {0} , Expect:{1}", simstatus.IsInstalled ? "installed" : "uninstalled", UpdateData.IsSimInsert ? "installed" : "uninstalled")); //Logger.Print("sim install status not matched", isError: true); 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); //LogPair.Add("SimICCID", ICCIDstring); //LogPair.Add("SimIMSI", IMSIstring); //ReportLog.Add(string.Format("Get Sim ICCID : {0} , Expect:{1}", ICCIDstring, UpdateData.ICCID)); //ReportLog.Add(string.Format("Get Sim IMSI : {0} , Expect:{1}", IMSIstring, UpdateData.IMSI)); //InfoLog += $"Get sim info, inserted:{simstatus.IsInstalled},ICCID:{ICCIDstring},IMSI:{IMSIstring}\n"; if (ICCIDstring != UpdateData.ICCID) { LogWriter.Log("Sim card ICCID not match"); Error = ErrorType.IccidMistach; //Logger.Print("sim card ICCID not matched", isError: true); //InfoLog += $"Sim card ICCID not matched,{ICCIDstring}:{UpdateData.ICCID}\n"; return false; } if (IMSIstring != UpdateData.IMSI) { LogWriter.Log("Sim card IMSI not match"); Error = ErrorType.ImsiMistach; //Logger.Print("sim card IMSI not matched", isError: true); //InfoLog += $"Sim card IMSI not matched,{IMSIstring}:{UpdateData.IMSI}\n"; return false; } } else { //InfoLog += $"Get sim info, inserted:{simstatus.IsInstalled}\n"; //if (!simstatus.ICCID.SequenceEqual(new byte[22])) //{ // InfoLog += $"ICCID not empty : { BitConverter.ToString(simstatus.ICCID).Replace("-", " ")}\n"; // ReportLog.Add("ICCID not empty"); // Logger.Print("sim card ICCID not empty", isError: true); // return false; //} //if (!simstatus.IMSI.SequenceEqual(new byte[16])) //{ // InfoLog += $"IMSI not empty : { BitConverter.ToString(simstatus.IMSI).Replace("-", " ")}\n"; // ReportLog.Add("IMSI not empty"); // Logger.Print("sim card IMSI not empty", isError: true); // return false; //} } } } return true; } } }