using PhihongEv.Lib; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MesAdaptor { public class SajectConnectSajet2 : ISajetConnect { private enum CMD { Signin = 1, SnCheck = 2, Report = 3, ValueReport = 4, } private readonly string _MechineCode; public SajectConnectSajet2(string MechineCode) { if (string.IsNullOrEmpty(MechineCode)) { throw new Exception("MechineCode cannot not be empty"); } _MechineCode = MechineCode; } public bool SajetTransStart() => SajetConnectAdapter.SajetTransStart(); public bool SajetTransClose() => SajetConnectAdapter.SajetTransClose(); private string userId = ""; public bool SajetTransSignIn(ref string data) { var attemptId = data; var msg = data + ";"; if (SajetTransData(CMD.Signin, ref data)) { if (string.IsNullOrEmpty(data) || data.StartsWith("NG")) { userId = ""; return false; } else { userId = attemptId; return true; } } userId = ""; return false; } [Obsolete] public bool SajetTransWoCheck(ref string workOrder) { return true; } //private SystemID systemID; string modelName; string serialNumber; public bool SajetTransSnCheck(ref string serialNumber) { if (!SystemID.TryLooseParse(serialNumber, out var id)) { return false; } this.modelName = null; this.serialNumber = null; var result = SajetTransSnCheck(ref serialNumber, id.ModelName.ToString()); if (result) { this.modelName = id.ModelName.ToString(); this.serialNumber = serialNumber; } return result; //var msg = string.Format("{0};{1};;", id.ModelName.ToString(), id.ToString()); //if (SajetTransData(CMD.SnCheck, ref msg)) //{ // if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG")) // { // systemID = null; // return false; // } // systemID = id; // return true; //} //systemID = null; //return false; } public bool SajetTransSnCheck(ref string sn, string model) { var msg = string.Format("{0};{1};;", model, sn); this.serialNumber = null; this.modelName = null; if (SajetTransData(CMD.SnCheck, ref msg)) { if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG")) { return false; } this.serialNumber = sn; this.modelName = model; return true; } return false; } //public bool SajetTransSnCheck(string partNO,string sn) //{ // var msg = string.Format("{0};{1};;", partNO, sn); // if (SajetTransData(CMD.SnCheck, ref msg)) // { // if (string.IsNullOrEmpty(msg) || msg.StartsWith("NG")) // { // return false; // } // return true; // } // return false; //} public bool SajetTranFinishSuccess() { //File.AppendAllText("debug.txt", string.Format("userId:{0}", userId)); //File.AppendAllText("debug.txt", string.Format("modelName:{0}", modelName)); //File.AppendAllText("debug.txt", string.Format("serialNumber:{0}", serialNumber)); if (string.IsNullOrEmpty(userId)) return false; if (modelName == null) return false; if (serialNumber == null) return false; //string msg = userId + ";" + systemID.ToString() + ";"; string msg = string.Format("{0};{1};{2};{3}:0;", userId, _MechineCode, modelName, serialNumber); if (string.IsNullOrEmpty(msg)) return false; return SajetTransData(CMD.Report, ref msg); } //public bool SajetTranFinishSuccess(string partNO, string sn) //{ // if (string.IsNullOrEmpty(userId)) // return false; // //string msg = userId + ";" + systemID.ToString() + ";"; // string msg = string.Format("{0};{1};{2};{3}:0;", userId, _MechineCode, partNO, sn); // if (string.IsNullOrEmpty(msg)) // return false; // return SajetTransData(CMD.Report, ref msg); //} public bool SajetTranFinishFail(MesErrorCode errorCode) { if (string.IsNullOrEmpty(userId)) return false; if (modelName == null) return false; if (serialNumber == null) return false; //string msg = userId + ";" + systemID.ToString() + ";"; string msg = string.Format("{0};{1};{2};{3}:1:", userId, _MechineCode, modelName, serialNumber); if (string.IsNullOrEmpty(msg)) return false; msg += $"{GetMesCodeString(errorCode)};"; //E114 return SajetTransData(CMD.Report, ref msg); } //public bool SajetTranFinishFail(string partNO, string sn, MesErrorCode errorCode) //{ // if (string.IsNullOrEmpty(userId)) // return false; // //string msg = userId + ";" + systemID.ToString() + ";"; // string msg = string.Format("{0};{1};{2};{3}:1:", userId, _MechineCode, partNO, sn); // if (string.IsNullOrEmpty(msg)) // return false; // msg += $"{errorCode};"; //E114 // return SajetTransData(CMD.Report, ref msg); //} [Obsolete] public string SajetTransRegisterHeader(string model, string header) { return ""; } public bool SajetTransReport(ValueReportDatas reportDatas) { if (string.IsNullOrEmpty(userId)) return false; if (modelName == null) return false; if (serialNumber == null) return false; var totalResult = true; foreach (var data in reportDatas) { var key = data.Key; var value = data.Val; var isSuccess = data.IsSuccess; value = value.Trim(); var det = isSuccess ? "0" : "1"; if (string.IsNullOrEmpty(value)) { value = "empty"; } else if (value.ToLower() == "fail") { value = "0"; } else if (value.ToLower() == "success") { value = "1"; } var msg = string.Format("{0};{1};{2};{3};{4}:{5}:{6};", userId, _MechineCode, modelName, serialNumber, key, value, det); var result = SajetTransData(CMD.ValueReport, ref msg); totalResult &= result; } return totalResult; } //public bool SajetTransReport(string partNO, string no, Dictionary reportPair) //{ // if (string.IsNullOrEmpty(userId)) // return false; // var totalResult = true; // foreach (var pair in reportPair) // { // var msg = string.Format("{0};{1};{2};{3};{4}:{5};", userId, _MechineCode, partNO, no, pair.Key, pair.Value); // var result = SajetTransData(CMD.ValueReport, ref msg); // totalResult &= result; // } // return totalResult; //} [Obsolete] public bool SajetTransReport(Dictionary resultPair, Dictionary codePair) { return true; } [Obsolete] public bool SajetTransReport(Dictionary resultPair, Dictionary codePair) { return true; } [Obsolete] public bool SajetTransLog(string data) { return true; } private bool SajetTransData(CMD command, ref string data) { return SajetConnectAdapter.SajetTransData((int)command, ref data); } private string GetMesCodeString(MesErrorCode mesError) { switch (mesError) { case MesErrorCode.ChargerConnectFail: return "A081"; case MesErrorCode.ModelNameUpadateFail: return "A082"; case MesErrorCode.SerilaNumberUpadateFail: return "A083"; case MesErrorCode.UtcDatetimeUpdateFail: return "A084"; case MesErrorCode.FourthGenModelVersionMismatch: return "A085"; case MesErrorCode.FourthGenSimInstartionMismatch: return "A086"; case MesErrorCode.FourthGenSimInfoMismatch: return "A087"; case MesErrorCode.WifiModeNotClient: return "A088"; case MesErrorCode.WifiRssiLow: return "A089"; case MesErrorCode.FirmwareUploadFail: return "A090"; case MesErrorCode.FirmwareUpdateTimeout: return "A091"; case MesErrorCode.FirmwareVersionCheckFail: return "A092"; case MesErrorCode.EmergencyButtonTestFail: return "A093"; case MesErrorCode.GreenButtonTestFail: return "A094"; case MesErrorCode.BlueButtonTestFail: return "A095"; case MesErrorCode.FactoryResetFail: return "A096"; case MesErrorCode.SetOptionsFail: return "ST001"; case MesErrorCode.ProgramFail: return "ST002"; case MesErrorCode.RestFail: return "ST003"; case MesErrorCode.ProgramCheckFail: return "ST004"; case MesErrorCode.GetCheckSumFail: return "ST005"; } return "A081"; } } }