Files
2024-06-26 10:30:00 +09:00

491 lines
26 KiB
C#

using CpTesterPlatform.CpLogUtil;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Xml.Linq;
using SystemX.Net;
using SystemX.Net.BaseProtocol;
using SystemX.Net.DB;
using SystemX.Net.Platform.Common.ExtensionMethods;
using static SystemX.Net.Platform.Common.Util.LogMessage;
namespace SystemX.Common.Log.LSU
{
public static class CpLogStoreData
{
public static string vLSaveTableName = string.Empty;
public static bool vLSaveDetailTable = false;
public static List<KeyValuePair<int, Tuple<string, bool, string, int, string, string, string>>> ExtrctInfo = null;
public static string vLLogAccessKey = string.Empty;
public static string vLProcNo = string.Empty;
public static string vLStation = string.Empty;
public static string vLPosition = string.Empty;
public static string vLPalletNumber = string.Empty;
public static string vLPalletID = string.Empty;
public static string vLPIndex = string.Empty;
public static string vLProdNo = string.Empty;
public static string vLProdID = string.Empty;
public static string vLSensorType = string.Empty;
public static string vLStatus = string.Empty;
public static string vLTestID = string.Empty;
public static string vLResult = string.Empty;
public static string vLTestDuration = string.Empty;
public static string[] vLVal_Meas;
public static void SetMeasLength(int iCnt)
{
vLVal_Meas = new string[iCnt];
Array.Clear(vLVal_Meas, 0, iCnt);
}
public static void SetMeasValue(int iPos, string strValue)
{
vLVal_Meas[iPos] = strValue;
}
public static string GetMeasValue(int iPos)
{
return vLVal_Meas[iPos];
}
public static void SetCpLogHeaderData(CpLogHeader cpLh)
{
vLStation = (cpLh.CHANNEL).Trim();
vLPosition = (cpLh.WORK_POSITION).Trim();
vLStatus = (cpLh.CONTROL + @"/" + cpLh.TYPE).Trim();
vLResult = (cpLh.RESULT).Trim();
var vTestDuration = (cpLh.DURATION).Trim();
double dGetDurtion = 0;
double.TryParse(vTestDuration.ToString().Trim(), out dGetDurtion);
dGetDurtion /= 1000.0;
vLTestDuration = dGetDurtion.ToString("F3");
}
}
public static class CustomLogStoreData
{
public static string vLSaveTableName = string.Empty;
public static bool vLSaveDetailTable = false;
public static List<KeyValuePair<int, Tuple<string, bool, string, int, string, string, string>>> ExtrctInfo = null;
public static string SelectProdNo = string.Empty;
public static string[] vLLogAccessKey = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLProcNo = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLPalletNumber = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLPalletID = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLPIndex = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLProdNo = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLProdID = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLSensorType = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLStatus = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLTestID = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLResult = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLVal_Min = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLVal_Max = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
public static string[] vLVal_Meas = new string[6] { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty };
}
public static class XLogDataControl_
{
public static DataTable dtCSVFileData = new DataTable();
private static Dictionary<string, Tuple<SqlDbType, int>> dicLogField;
public static void SetLogFieldInfo()
{
dicLogField = new Dictionary<string, Tuple<SqlDbType, int>>();
dicLogField.Clear();
dicLogField.Add("LogAccessKey", new Tuple<SqlDbType, int>(SqlDbType.NVarChar, 64));
dicLogField.Add("ProcNo", new Tuple<SqlDbType, int>(SqlDbType.BigInt, 0));
dicLogField.Add("Station", new Tuple<SqlDbType, int>(SqlDbType.NChar, 30));
dicLogField.Add("Position", new Tuple<SqlDbType, int>(SqlDbType.NChar, 30));
dicLogField.Add("PalletNumber", new Tuple<SqlDbType, int>(SqlDbType.NChar, 20));
dicLogField.Add("PalletID", new Tuple<SqlDbType, int>(SqlDbType.NChar, 30));
dicLogField.Add("PIndex", new Tuple<SqlDbType, int>(SqlDbType.TinyInt, 0));
dicLogField.Add("ProdNo", new Tuple<SqlDbType, int>(SqlDbType.NChar, 30));
dicLogField.Add("ProdID", new Tuple<SqlDbType, int>(SqlDbType.NChar, 50));
dicLogField.Add("SensorType", new Tuple<SqlDbType, int>(SqlDbType.TinyInt, 0));
dicLogField.Add("Status", new Tuple<SqlDbType, int>(SqlDbType.NChar, 50));
dicLogField.Add("TestID", new Tuple<SqlDbType, int>(SqlDbType.NChar, 20));
dicLogField.Add("Result", new Tuple<SqlDbType, int>(SqlDbType.Bit, 0));
dicLogField.Add("TestDuration", new Tuple<SqlDbType, int>(SqlDbType.Float, 0));
dicLogField.Add("Val_Min", new Tuple<SqlDbType, int>(SqlDbType.Float, 0));
dicLogField.Add("Val_Max", new Tuple<SqlDbType, int>(SqlDbType.Float, 0));
dicLogField.Add("Val_Meas", new Tuple<SqlDbType, int>(SqlDbType.Float, 0));
}
private static void AddLogField(string strName, SqlDbType setType, int iSize)
{
if (dicLogField.ContainsKey(strName) == false)
dicLogField.Add(strName, new Tuple<SqlDbType, int>(setType, iSize));
}
private static SqlParameter GetMakeSqlParameterInfo(string strSetName, object objValue)
{
if (dicLogField.ContainsKey(strSetName))
{
SqlParameter param = new SqlParameter("@" + strSetName, dicLogField[strSetName].Item1, dicLogField[strSetName].Item2);
param.Value = objValue;
return param;
}
else
return null;
}
public static T ConvertTextToTryValue<T>(string strText, object objFailValue)
{
object obj;
obj = typeof(T);
int iGetValue = 0;
byte ucGetValue = 0;
uint uiGetValue = 0;
double dGetValue = 0;
if (obj.ToString().IndexOf("Byte") >= 0)
{
if (!byte.TryParse(strText, out ucGetValue))
obj = objFailValue;
else
obj = ucGetValue;
}
if (obj.ToString().IndexOf("Int") >= 0)
{
if (!int.TryParse(strText, out iGetValue))
obj = objFailValue;
else
obj = iGetValue;
}
if (obj.ToString().IndexOf("UInt") >= 0)
{
if (!uint.TryParse(strText, out uiGetValue))
obj = objFailValue;
else
obj = uiGetValue;
}
if (obj.ToString().IndexOf("Double") >= 0)
{
if (!double.TryParse(strText, out dGetValue))
obj = objFailValue;
else
obj = dGetValue;
}
return (T)Convert.ChangeType(obj, typeof(T));
}
public static SqlCommand[] HISTTableProcess(eLogDataType eGetLogType)
{
SqlCommand[] cmd = null;
try
{
try
{
if (eGetLogType == eLogDataType.Normal)
{
cmd = new SqlCommand[3];
String sqlCommandInsert = "";
String sqlCommandSubInsert = "";
String sqlCommandUpdate = "";
string columns = string.Empty;
string subColumns = string.Empty;
if (CpLogStoreData.ExtrctInfo.Count > 0)
{
columns = "LogAccessKey,ProcNo,Station,Position,PalletNumber,PalletID,PIndex,ProdNo,ProdID,SensorType,Status,TestID,Result,TestDuration,";
subColumns = "LogAccessKey,ProcNo,PalletNumber,PalletID,PIndex,ProdNo,ProdID,TestID";
sqlCommandUpdate = "UPDATE " + CpLogStoreData.ExtrctInfo[0].Value.Item6 + " SET ";
foreach (KeyValuePair<int, Tuple<string, bool, string, int, string, string, string>> k in CpLogStoreData.ExtrctInfo)
{
sqlCommandUpdate += k.Value.Item7 + " = " + CpLogStoreData.GetMeasValue(k.Key) + ", ";
columns += k.Value.Item7 + ",";
AddLogField(k.Value.Item7, SqlDbType.Float, 0);
}
sqlCommandUpdate = sqlCommandUpdate.Remove(sqlCommandUpdate.Length - 2, 1);
sqlCommandUpdate += "WHERE PalletNumber = '" + CpLogStoreData.vLPalletNumber + "' AND PIndex = " + CpLogStoreData.vLPIndex + ";";
columns = columns.Remove(columns.Length - 1, 1);
cmd[2] = new SqlCommand(sqlCommandUpdate);
}
else
{
columns = "LogAccessKey,ProcNo,Station,Position,PalletNumber,PalletID,PIndex,ProdNo,ProdID,SensorType,Status,TestID,Result,TestDuration";
subColumns = "LogAccessKey,ProcNo,PalletNumber,PalletID,PIndex,ProdNo,ProdID,TestID";
cmd[2] = null;
}
string values = string.Join(",", columns.Split(',').Select(c => string.Format("@{0}", c)));
string subValues = string.Join(",", subColumns.Split(',').Select(c => string.Format("@{0}", c)));
sqlCommandInsert = string.Format("INSERT INTO " + CpLogStoreData.vLSaveTableName + " ({0}) VALUES ({1})", columns, values);
sqlCommandSubInsert = string.Format("INSERT INTO [HIST_TestNgResult] ({0}) VALUES ({1})", subColumns, subValues);
cmd[0] = new SqlCommand(sqlCommandInsert);
cmd[1] = new SqlCommand(sqlCommandSubInsert);
SqlParameter[] setParams = new SqlParameter[columns.Split(',').Count()];
SqlParameter[] setSubParams = new SqlParameter[subColumns.Split(',').Count()];
setParams[0] = GetMakeSqlParameterInfo("LogAccessKey", CpLogStoreData.vLLogAccessKey);
setParams[1] = GetMakeSqlParameterInfo("ProcNo", CpLogStoreData.vLProcNo);
setParams[2] = GetMakeSqlParameterInfo("Station", CpLogStoreData.vLStation);
setParams[3] = GetMakeSqlParameterInfo("Position", CpLogStoreData.vLPosition);
setParams[4] = GetMakeSqlParameterInfo("PalletNumber", CpLogStoreData.vLPalletNumber);
setParams[5] = GetMakeSqlParameterInfo("PalletID", CpLogStoreData.vLPalletID);
setParams[6] = GetMakeSqlParameterInfo("PIndex", ConvertTextToTryValue<byte>(CpLogStoreData.vLPIndex, 0));
setParams[7] = GetMakeSqlParameterInfo("ProdNo", CpLogStoreData.vLProdNo);
setParams[8] = GetMakeSqlParameterInfo("ProdID", CpLogStoreData.vLProdID);
setParams[9] = GetMakeSqlParameterInfo("SensorType", CpLogStoreData.vLSensorType);
setParams[10] = GetMakeSqlParameterInfo("Status", CpLogStoreData.vLStatus);
setParams[11] = GetMakeSqlParameterInfo("TestID", CpLogStoreData.vLTestID);
setSubParams[0] = GetMakeSqlParameterInfo("LogAccessKey", CpLogStoreData.vLLogAccessKey);
setSubParams[1] = GetMakeSqlParameterInfo("ProcNo", CpLogStoreData.vLProcNo);
setSubParams[2] = GetMakeSqlParameterInfo("PalletNumber", CpLogStoreData.vLPalletNumber);
setSubParams[3] = GetMakeSqlParameterInfo("PalletID", CpLogStoreData.vLPalletID);
setSubParams[4] = GetMakeSqlParameterInfo("PIndex", ConvertTextToTryValue<byte>(CpLogStoreData.vLPIndex, 0));
setSubParams[5] = GetMakeSqlParameterInfo("ProdNo", CpLogStoreData.vLProdNo);
setSubParams[6] = GetMakeSqlParameterInfo("ProdID", CpLogStoreData.vLProdID);
setSubParams[7] = GetMakeSqlParameterInfo("TestID", CpLogStoreData.vLTestID);
if (CpLogStoreData.vLResult.IndexOf("OK") >= 0)
{
setParams[12] = GetMakeSqlParameterInfo("Result", 1);
cmd[1] = null;
}
else
setParams[12] = GetMakeSqlParameterInfo("Result", 0);
setParams[13] = GetMakeSqlParameterInfo("TestDuration", ConvertTextToTryValue<double>(CpLogStoreData.vLTestDuration, 0));
int iSetPos = 14;
if (CpLogStoreData.ExtrctInfo.Count > 0)
{
foreach (KeyValuePair<int, Tuple<string, bool, string, int, string, string, string>> k in CpLogStoreData.ExtrctInfo)
setParams[iSetPos++] = GetMakeSqlParameterInfo(k.Value.Item7, ConvertTextToTryValue<double>(CpLogStoreData.GetMeasValue(k.Key), 0));
}
cmd[0].Parameters.AddRange(setParams);
if(cmd[1] != null)
cmd[1].Parameters.AddRange(setSubParams);
}
else
{
object[] objItems;
if (dtCSVFileData.Rows.Count == 0)
throw new Exception();
cmd = new SqlCommand[dtCSVFileData.Rows.Count + dtCSVFileData.Rows.Count];
string columns = string.Empty;
string subColumns = string.Empty;
String sqlCommandInsert = "";
String sqlCommandSubInsert = "";
if (eGetLogType == eLogDataType.CSV_Type0)
{
columns = "LogAccessKey,ProcNo,PalletNumber,PalletID,PIndex,ProdNo,ProdID,SensorType,Status,TestID,Result,Val_Min,Val_Max,Val_Meas";
string values = string.Join(",", columns.Split(',').Select(c => string.Format("@{0}", c)));
sqlCommandInsert = string.Format("INSERT INTO " + CustomLogStoreData.vLSaveTableName + " ({0}) VALUES ({1})", columns, values);
}
else
{
columns = "LogAccessKey,ProcNo,PalletNumber,PalletID,PIndex,ProdNo,ProdID,SensorType,Status,TestID,Result";
string values = string.Join(",", columns.Split(',').Select(c => string.Format("@{0}", c)));
sqlCommandInsert = string.Format("INSERT INTO " + CustomLogStoreData.vLSaveTableName + " ({0}) VALUES ({1})", columns, values);
}
subColumns = "LogAccessKey,ProcNo,PalletNumber,PalletID,PIndex,ProdNo,ProdID,TestID";
string subValues = string.Join(",", subColumns.Split(',').Select(c => string.Format("@{0}", c)));
sqlCommandSubInsert = string.Format("INSERT INTO [HIST_TestNgResult] ({0}) VALUES ({1})", subColumns, subValues);
int iSubPos = 0;
for (int i = 0; i < dtCSVFileData.Rows.Count; i++)
{
iSubPos = dtCSVFileData.Rows.Count + i;
cmd[i] = null;
cmd[iSubPos] = null;
objItems = dtCSVFileData.Rows[i].ItemArray;
cmd[i] = new SqlCommand(sqlCommandInsert);
cmd[iSubPos] = new SqlCommand(sqlCommandSubInsert);
int iIndex = 0;
if (eGetLogType == eLogDataType.CSV_Type0)
iIndex = ConvertTextToTryValue<int>(objItems[1].ToString(), 0);
else
iIndex = ConvertTextToTryValue<int>(objItems[0].ToString(), 0);
if (iIndex == 0)
throw new Exception();
else
iIndex -= 1;
SqlParameter[] setParams = new SqlParameter[columns.Split(',').Count()];
SqlParameter[] setSubParams = new SqlParameter[subColumns.Split(',').Count()];
setParams[0] = GetMakeSqlParameterInfo("LogAccessKey", CustomLogStoreData.vLLogAccessKey[iIndex]);
setParams[1] = GetMakeSqlParameterInfo("ProcNo", CustomLogStoreData.vLProcNo[iIndex]);
setParams[2] = GetMakeSqlParameterInfo("PalletNumber", CustomLogStoreData.vLPalletNumber[iIndex]);
setParams[3] = GetMakeSqlParameterInfo("PalletID", CustomLogStoreData.vLPalletID[iIndex]);
setParams[4] = GetMakeSqlParameterInfo("PIndex", ConvertTextToTryValue<byte>(CustomLogStoreData.vLPIndex[iIndex], 0));
setParams[5] = GetMakeSqlParameterInfo("ProdNo", CustomLogStoreData.vLProdNo[iIndex]);
setParams[6] = GetMakeSqlParameterInfo("ProdID", CustomLogStoreData.vLProdID[iIndex]);
setParams[7] = GetMakeSqlParameterInfo("SensorType", CustomLogStoreData.vLSensorType[iIndex]);
setParams[8] = GetMakeSqlParameterInfo("Status", CustomLogStoreData.vLStatus[iIndex]);
setParams[9] = GetMakeSqlParameterInfo("TestID", CustomLogStoreData.vLTestID[iIndex]);
setSubParams[0] = GetMakeSqlParameterInfo("LogAccessKey", CustomLogStoreData.vLLogAccessKey[iIndex]);
setSubParams[1] = GetMakeSqlParameterInfo("ProcNo", CustomLogStoreData.vLProcNo[iIndex]);
setSubParams[2] = GetMakeSqlParameterInfo("PalletNumber", CustomLogStoreData.vLPalletNumber[iIndex]);
setSubParams[3] = GetMakeSqlParameterInfo("PalletID", CustomLogStoreData.vLPalletID[iIndex]);
setSubParams[4] = GetMakeSqlParameterInfo("PIndex", ConvertTextToTryValue<byte>(CustomLogStoreData.vLPIndex[iIndex], 0));
setSubParams[5] = GetMakeSqlParameterInfo("ProdNo", CustomLogStoreData.vLProdNo[iIndex]);
setSubParams[6] = GetMakeSqlParameterInfo("ProdID", CustomLogStoreData.vLProdID[iIndex]);
setSubParams[7] = GetMakeSqlParameterInfo("TestID", CustomLogStoreData.vLTestID[iIndex]);
if (eGetLogType == eLogDataType.CSV_Type0)
{
if (objItems[4].ToString().Trim().IndexOf("OK") >= 0)
CustomLogStoreData.vLResult[iIndex] = "1";
else
CustomLogStoreData.vLResult[iIndex] = "0";
CustomLogStoreData.vLVal_Meas[iIndex] = objItems[3].ToString().Trim();
}
else
{
if (objItems[1].ToString().IndexOf("OK") >= 0)
CustomLogStoreData.vLResult[iIndex] = "1";
else
CustomLogStoreData.vLResult[iIndex] = "0";
}
//
if (CustomLogStoreData.vLResult[iIndex].IndexOf("1") >= 0)
cmd[iSubPos] = null;
setParams[10] = GetMakeSqlParameterInfo("Result", ConvertTextToTryValue<byte>(CustomLogStoreData.vLResult[iIndex], 0));
if (eGetLogType == eLogDataType.CSV_Type0)
{
setParams[11] = GetMakeSqlParameterInfo("Val_Min", ConvertTextToTryValue<double>(CustomLogStoreData.vLVal_Min[iIndex], 0));
setParams[12] = GetMakeSqlParameterInfo("Val_Max", ConvertTextToTryValue<double>(CustomLogStoreData.vLVal_Max[iIndex], 0));
setParams[13] = GetMakeSqlParameterInfo("Val_Meas", ConvertTextToTryValue<double>(CustomLogStoreData.vLVal_Meas[iIndex], 0));
}
cmd[i].Parameters.AddRange(setParams);
if(cmd[iSubPos] != null)
cmd[iSubPos].Parameters.AddRange(setSubParams);
}
}
}
catch (Exception ex)
{
for (int i = 0; i < cmd.Count(); i++)
cmd[i] = null;
MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @" " + eGetLogType.ToString() + " General log processing [Command create] failed. " + ex.Message + " [SystemX.Common.Protocol.Log.LSU_Trimming_4Th : XLogDataControl_.HISTTableProcess]", ConsoleColor.Yellow, LogMessageLevel.DEBUG);
cmd = null;
}
}
finally
{
;//
}
return cmd; //(SqlCommand[])cmd.Clone();
}
public static bool ReadFileData(eLogDataType eType, string strFileName)
{
string strSetHeader = string.Empty;
bool bReadStatus = true;
try
{
if (eType == eLogDataType.CSV_Type0)
strSetHeader = "No,Index,Time,MeasValue,Result";
else if (eType == eLogDataType.CSV_Type1)
strSetHeader = "Pallet_Num,Result,Time,X1,X2,X3,Y,Broken,LockingPinArea";
else if (eType == eLogDataType.CSV_Type2)
strSetHeader = "Pallet_Num,Result,Time,Pin_1_X_Position,Pin_1_Y_Position,Pin_2_X_Position,Pin_2_Y_Position,Pin_3_X_Position,Pin_3_Y_Position,Pin_4_X_Position,Pin_4_Y_Position,Pin_5_X_Position, Pin_5_Y_Position,Pin_6_X_Position,Pin_6_Y_Position,Pin_1_Thickness,Pin_2_Thickness,Pin_3_Thickness,Pin_4_Thickness,Pin_5_Thickness,Pin_6_Thickness,Locking Pin Area,Locking Pin Position";
using (StreamReader sr = new StreamReader(strFileName))
{
if (eType != eLogDataType.CSV_Type0)
sr.ReadLine();
dtCSVFileData = new DataTable();
while (!sr.EndOfStream)
{
string strGetData = sr.ReadLine();
strGetData = strGetData.Trim();
if (strGetData.Length <= 0)
continue;
string[] tempList = strGetData.Split(',');
for (int i = 0; i < tempList.Length; i++)
{
if (tempList[i].Length <= 0)
{
List<string> listData = tempList.ToList();
listData.RemoveAt(i);
tempList = listData.ToArray();
}
}
object[] objRows = tempList;
if (dtCSVFileData.Columns.Count <= 0)
{
foreach (string strData in strSetHeader.Split(','))
{
DataColumn dc = new DataColumn(strData);
dtCSVFileData.Columns.Add(dc);
}
dtCSVFileData.Rows.Add(objRows);
}
else
dtCSVFileData.Rows.Add(objRows);
}
}
}
catch (Exception ex)
{
MessageOutput.ConsoleWrite(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss>>") + @"General log processing [Read file] failed.[SystemX.Common.Protocol.Log.LSU_Trimming_4Th : STATIC XLogDataControl.ReadFileData]\r\n" + ex.Message, ConsoleColor.Yellow, LogMessageLevel.DEBUG);
bReadStatus = false;
}
return bReadStatus;
}
}
}