using System; using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Microsoft.VisualBasic; using Microsoft.VisualBasic.CompilerServices; using iMESCore.Settings; using static iMESCore.DataBase.iMESSql; using static iMESCore.Base.iMESComXML; namespace AutoLoaderService_Base { static class modAutoLoader { private static AppSettings objSetting = new AppSettings(); public const int defInteger = -999; // 宣告整數使用的常數,用以辨識參數是否有傳入Function中。 public const string defString = "Null"; // 宣告字串使用的常數,用以辨識參數是否有傳入Function中。 public static DateTime defDateTime = DateTime.Parse("1900-12-31"); // 宣告日期使用的常數,用以辨識參數是否有傳入Function中。 static string GetConnectionString_ConnectionString() { var argstrDataBaseType = "Null"; return objSetting.GetConnectionString(strDataBaseType: argstrDataBaseType); } public static string ConnectionString = GetConnectionString_ConnectionString(); public static void funProcessJob(object State) { SomeStateType strJob = (SomeStateType)State; string strJobExecutionFile = strJob.strJobExecutionFile; // 來源DLL string strJobNo = strJob.strJobNo; string strJobName = strJob.strCommandName; string strSourcePath = strJob.strSourcePath; // 來源路徑 string strDestinationPath = strJob.strDestinationPath; // 目的路徑 string strQueuePath = strJob.strQueuePath; // Queue string strFailPath = strJob.strFailPath; // Fail string strLogFilePath = strJob.strLogFilePath; long lngSplitFileSize = 10485760L; long lbgSplitRecordsLimit = 5000L; string strJobFunction = strJob.strJobFunction; string strParameter1 = strJob.strParameter1; // 輸入參數1 string strParameter2 = strJob.strParameter2; // 輸入參數2 string strParameter3 = strJob.strParameter3; // 輸入參數3 string strParameter4 = strJob.strParameter4; // 輸入參數4 string strParameter5 = strJob.strParameter5; // 輸入參數5 string strParameter6 = strJob.strParameter6; // 輸入參數5 string strParameter7 = strJob.strParameter7; // 輸入參數5 string strParameter8 = strJob.strParameter8; // 輸入參數5 string strParameter9 = strJob.strParameter9; // 輸入參數5 string strParameter10 = strJob.strParameter10; // 輸入參數5 var StartTime = DateTime.Now; string strStatus = "Fail"; // 回傳狀況先設定為Fail string strReturnMsg = string.Empty; string strMemo = ""; string errorCode = "0000-999999"; // 2019/12/10 yenru, 建立AutoLoader Base 可動態呼叫行業包AutoLoader特定DLL // Dim objAutoRun As New AutoLoaderLib.clsAutoLoaderLibrary try { var colParameters = new Collection(); colParameters.Add(strJobNo, "JobNo"); colParameters.Add(strSourcePath, "SourcePath"); colParameters.Add(strDestinationPath, "DestinationPath"); colParameters.Add(strQueuePath, "QueuePath"); colParameters.Add(strFailPath, "FailPath"); colParameters.Add(lngSplitFileSize, "SplitFileSize"); colParameters.Add(lbgSplitRecordsLimit, "SplitRecordsLimit"); colParameters.Add(strJobName, "JobName"); colParameters.Add(strJobFunction, "JobFunction"); colParameters.Add(strParameter1, "Parameter1"); colParameters.Add(strParameter2, "Parameter2"); colParameters.Add(strParameter3, "Parameter3"); colParameters.Add(strParameter4, "Parameter4"); colParameters.Add(strParameter5, "Parameter5"); colParameters.Add(strParameter6, "Parameter6"); colParameters.Add(strParameter7, "Parameter7"); colParameters.Add(strParameter8, "Parameter8"); colParameters.Add(strParameter9, "Parameter9"); colParameters.Add(strParameter10, "Parameter10"); // strReturnMsg = objAutoRun.ExecuteFunction(strJobName, colParameters) string strBasePath = AppDomain.CurrentDomain.BaseDirectory; string strFilePath = strBasePath + "/" + strJobExecutionFile + ".dll"; // Dll CreateInstance var mainAssembly = System.Reflection.Assembly.LoadFrom(strFilePath); var objBRClass = new object(); object objArgs = new object[] { strJobName, colParameters }; objBRClass = mainAssembly.CreateInstance(strJobExecutionFile + ".clsAutoLoaderLibrary"); strReturnMsg = objBRClass.GetType().InvokeMember("ExecuteFunction", System.Reflection.BindingFlags.InvokeMethod, null, objBRClass, (object[])objArgs).ToString(); } catch (Exception ex) { // // Error Message strReturnMsg = ex.Message; strMemo = "funProcessJob Error : " + ex.Message; } finally { // //Return Fail if (strReturnMsg != "success") { modWIN.WriteLog("JobNo:"+ strJobNo + "fail"+ strMemo, iMESLog.iMESLogLevel.Trace); // 取得ErrorCode if (strReturnMsg.Substring(0, 10) == "ErrorCode:") { strReturnMsg = strReturnMsg.Remove(0, 10); // 移除"ErrorCode:" string[] temp = Strings.Split(strReturnMsg, ";"); if (temp[0].Length == 11) { errorCode = temp[0]; } } // Job完成後的後續動作: // 刪除已經執行完畢之工作 (最先做, 減少執行完畢而未刪的情形) // 註記最後執行時間 // 增加log string computerNameTmp = System.Windows.Forms.SystemInformation.ComputerName; funEndProcessJob(strJobNo, strStatus, CInput(ref strReturnMsg), CInput(ref computerNameTmp), StartTime, DateTime.Now, Convert.ToInt32(DateAndTime.DateDiff(DateInterval.Second, StartTime, DateTime.Now)), strMemo, errorCode, "AutoLoader"); // //發生錯誤將Log記錄下來 // Call objAutoRun.AddErrorLog("MESAutoLoader", "AutoLoader", "JobNo(JobName)", strJobNo & "(" & strJobName & ")", , strReturnMsg, My.Computer.Name) // //將Error Log存入檔案 // WriteLogFile(strLogFilePath, "[" + Strings.Format(DateTime.Now, "yyyy/MM/dd HH:mm:ss") + "]" + strReturnMsg + Constants.vbCrLf); } } } /// /// 執行Job完成後的後續動作 /// private static int funEndProcessJob(string JobNo, string Status = defString, string ReturnMsg = defString, string ServerName = defString, DateTime StartTime = default(DateTime), DateTime EndTime = default(DateTime), int RunTime = defInteger, string Memo = defString, string ErrorCode = "0000-999999", string LogClass = defString) { int funEndProcessJobRet = default(int); // 刪除已經執行完畢之工作 (最先做, 減少執行完畢而未刪的情形) // 註記最後執行時間 // 增加log funEndProcessJobRet = -1; // //Web Service相關變數 var XmlDoc = new System.Xml.XmlDocument(); string InXml, OutXml, strIdentity, strParameter; try { // 將傳入參數組成XML字串 // 定義Identity string argSendTime = Conversions.ToString(DateTime.Now); strIdentity = modWIN.CombineXMLIdentity(ref modWIN.gComputerName, ref modWIN.gUserNo, ref argSendTime); // 定義Parameter strParameter = string.Empty; string argvalue_name = "jobno"; string argname = "JobNo"; string argtype = "String"; string argdesc = ""; strParameter = modWIN.CombineXMLParameter(ref argvalue_name, ref argname, ref argtype, ref JobNo, ref argdesc); if ((Status ?? "") != defString) { string argvalue_name1 = "status"; string argname1 = "Status"; string argtype1 = "String"; string argdesc1 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name1, ref argname1, ref argtype1, ref Status, ref argdesc1); } if ((ReturnMsg ?? "") != defString) { string argvalue_name2 = "returnmsg"; string argname2 = "ReturnMsg"; string argtype2 = "String"; string argdesc2 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name2, ref argname2, ref argtype2, ref ReturnMsg, ref argdesc2); } if ((ServerName ?? "") != defString) { string argvalue_name3 = "servername"; string argname3 = "ServerName"; string argtype3 = "String"; string argdesc3 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name3, ref argname3, ref argtype3, ref ServerName, ref argdesc3); } if (StartTime != defDateTime) { string argvalue_name4 = "starttime"; string argname4 = "StartTime"; string argtype4 = "DateTime"; string argvalue = Strings.Format(StartTime, "yyyy/MM/dd HH:mm:ss"); string argdesc4 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name4, ref argname4, ref argtype4, ref argvalue, ref argdesc4); } // 2016-11-09, Joe, Format日期格式 if (EndTime != defDateTime) { string argvalue_name5 = "endtime"; string argname5 = "EndTime"; string argtype5 = "DateTime"; string argvalue1 = Strings.Format(EndTime, "yyyy/MM/dd HH:mm:ss"); string argdesc5 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name5, ref argname5, ref argtype5, ref argvalue1, ref argdesc5); } // 2016-11-09, Joe, Format日期格式 if (RunTime != defInteger) { string argvalue_name6 = "runtime"; string argname6 = "RunTime"; string argtype6 = "Integer"; string argvalue2 = RunTime.ToString(); string argdesc6 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name6, ref argname6, ref argtype6, ref argvalue2, ref argdesc6); RunTime = Conversions.ToInteger(argvalue2); } if ((Memo ?? "") != defString) { string argvalue_name7 = "memo"; string argname7 = "Memo"; string argtype7 = "String"; string argdesc7 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name7, ref argname7, ref argtype7, ref Memo, ref argdesc7); } if ((LogClass ?? "") != defString) { string argvalue_name8 = "logclass"; string argname8 = "LogClass"; string argtype8 = "String"; string argdesc8 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name8, ref argname8, ref argtype8, ref LogClass, ref argdesc8); } string argvalue_name9 = "errorcode"; string argname9 = "ErrorCode"; string argtype9 = "String"; string argdesc9 = ""; strParameter += modWIN.CombineXMLParameter(ref argvalue_name9, ref argname9, ref argtype9, ref ErrorCode, ref argdesc9); // request XML字串 InXml = modWIN.CombineXMLRequest(ref strIdentity, ref strParameter); OutXml = InvokeSrv("wsAUT.funEndProcessJob_ErrorCode", InXml); XmlDoc.LoadXml(OutXml); if (!modWIN.chkExecutionSuccess(ref XmlDoc)) { throw new Exception(modWIN.GetExceptionSysMsg(ref XmlDoc) + '\r' + modWIN.GetExceptionMesMsg(ref XmlDoc)); } funEndProcessJobRet = 0; } catch (Exception e1) { // funWriteTxtFile("funEndProcessJob (JobNo : " & JobNo & ") Error : " & e1.Message) // funWriteToAnEventLog("funEndProcessJob (JobNo : " & JobNo & ") Error : " & e1.Message, EventLogEntryType.Error, 9000) } return funEndProcessJobRet; } /// /// 叫用Web Service, 並回傳Response XML /// /// 要呼叫哪一個WebService的方法, ex: wsWIP.LoadLotBasis /// InXml /// 是否客製的WebService /// public static string InvokeSrv(string Method, string InXml, bool Customize = false) { object result; try { using (var ws = new AutoLoaderLib_Base.wsInvoke.wsInvoke()) { ws.Url = modWIN.LocalizeWebService(ws.Url.ToString()); ws.EnableDecompression = true; result = ws.invokeSrv(Method, new object[] { InXml }); } } catch (Exception ex) { throw; } return Conversions.ToString(result); } public static void funInitProcess(object State) { string strReturnMsg = string.Empty; // Dim objAutoRun As New AutoLoaderLib.clsAutoLoaderLibrary try { // // var colParameters = new Collection(); colParameters.Add(Environment.MachineName, "ComputerName"); } // //Init Binning // strReturnMsg = objAutoRun.ExecuteFunction("funInitBinning", colParameters) catch (Exception ex) { // // Error Message strReturnMsg = ex.Message; } finally { // // Release Object // objAutoRun = Nothing } } public class SomeStateType { public string strJobExecutionFile; public string strJobNo; public string strCommandName; public string strSourcePath; public string strDestinationPath; public string strQueuePath; public string strFailPath; public string strLogFilePath; public long SplitFileSize; public long SplitRecordsLimit; public string strJobName; public string strJobFunction; public string strParameter1; public string strParameter2; public string strParameter3; public string strParameter4; public string strParameter5; public string strParameter6; public string strParameter7; public string strParameter8; public string strParameter9; public string strParameter10; public void SomeState(string strJE, string strJN, string strCN, string strSP, string strDP, string strQP, string strFP, string strLF, long lngSplitFileSize, long lngSplitRecordsLimit, string str_JobName, string strJob_Function, string strPara1, string strPara2, string strPara3, string strPara4, string strPara5, string strPara6, string strPara7, string strPara8, string strPara9, string strPara10) { strJobExecutionFile = strJE; strJobNo = strJN; strCommandName = strCN; strSourcePath = strSP; strDestinationPath = strDP; strQueuePath = strQP; strFailPath = strFP; strLogFilePath = strLF; SplitFileSize = lngSplitFileSize; SplitRecordsLimit = lngSplitRecordsLimit; strJobName = str_JobName; strJobFunction = strJob_Function; strParameter1 = strPara1; strParameter2 = strPara2; strParameter3 = strPara3; strParameter4 = strPara4; strParameter5 = strPara5; strParameter6 = strPara6; strParameter7 = strPara7; strParameter8 = strPara8; strParameter9 = strPara9; strParameter10 = strPara10; } } public static bool PrevInstance() { if (Information.UBound(Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)) > 0) { return true; } else { return false; } } public static string CInput(ref string strInput) { string CInputRet = default(string); // 將傳入值內的單引號轉換為可存入資料庫的格式 // 2. 將傳入值內的 &, >, < 三個特殊字元轉換為XmlDocument可解譯之代替符號 // 傳入值: strInput包含特殊字元的字串 // 傳回值: 將特殊字元變更為代替符號的字串 // 轉換 ' 為 '' (單引號轉為兩個單引號) CInputRet = Strings.Replace(strInput, "'", "''"); // 轉換 & 為 & CInputRet = Strings.Replace(CInputRet, "&", "&"); // CInput = Replace(CInput, """", "''") 'AddFlow的Xml字串不可將雙引號轉為兩個單引號,XMLToFlow會Error // 轉換 > 為 > CInputRet = Strings.Replace(CInputRet, ">", ">"); // 轉換 < 為 < CInputRet = Strings.Replace(CInputRet, "<", "<"); return CInputRet; } public static void WriteLogFile(string dir, string WriteTxt) { try { string path = Path.Combine(dir, Strings.Format(DateTime.Now, "yyyyMMdd").ToString() + ".log"); if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir); using (StreamWriter sw = new StreamWriter(path, true)) { sw.WriteLine(WriteTxt); } } catch (Exception ex2) { } } } }