265 lines
6.4 KiB
C#
265 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using OfficeOpenXml;
|
|
using System.IO;
|
|
using System.Data;
|
|
|
|
namespace friction
|
|
{
|
|
public class DataHandler
|
|
{
|
|
public class CalcResult
|
|
{
|
|
public int m_iCnt;
|
|
public float m_fAvgRPN;
|
|
public float m_fStdRPN;
|
|
public float m_fDiffByForce;
|
|
public float m_fDiffByTemp;
|
|
public float m_fDiffByHumid;
|
|
public float m_fDiffByVel;
|
|
}
|
|
|
|
public struct CHART
|
|
{
|
|
public float HUMIDITY;
|
|
public float TEMPERATURE;
|
|
public float RPN;
|
|
|
|
override public string ToString()
|
|
{
|
|
return string.Format("Humi({0}) Temp({1}) RPN({2})", HUMIDITY, TEMPERATURE, RPN);
|
|
}
|
|
}
|
|
|
|
|
|
DataTable m_Data = new DataTable();
|
|
|
|
string m_strFileName = "";
|
|
string m_strFilePath = "";
|
|
|
|
List<string> m_ColumnsNames = new List<string>();
|
|
List<string> m_ActiveColumns = new List<string>();
|
|
List<string> m_NonactiveColumns = new List<string>();
|
|
List<string> m_MaterialSpring = new List<string>();
|
|
List<string> m_MaterialTable = new List<string>();
|
|
|
|
Dictionary<string, string> m_ColumnMap = new Dictionary<string, string>();
|
|
|
|
public DataHandler()
|
|
{
|
|
m_ColumnMap["spring"] = "Material spring";
|
|
m_ColumnMap["table"] = "material 2 table";
|
|
m_ColumnMap["rpn"] = "Risk priority number";
|
|
m_ColumnMap["force"] = "normal force";
|
|
m_ColumnMap["temp"] = "Temperature";
|
|
m_ColumnMap["humidity"] = "Relative humidity";
|
|
m_ColumnMap["velocity"] = "Relative velocity";
|
|
}
|
|
|
|
bool IsNumberColumn(string strColumn)
|
|
{
|
|
return (strColumn == m_ColumnMap["rpn"] ||
|
|
strColumn == m_ColumnMap["force"] ||
|
|
strColumn == m_ColumnMap["temp"] ||
|
|
strColumn == m_ColumnMap["humidity"] ||
|
|
strColumn == m_ColumnMap["velocity"]);
|
|
}
|
|
|
|
public void LoadData(string strFilePath)
|
|
{
|
|
using (ExcelPackage package = new ExcelPackage())
|
|
{
|
|
using (var stream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
|
package.Load(stream);
|
|
ExcelWorksheet Sheet = package.Workbook.Worksheets[1];
|
|
|
|
m_Data.Columns.Clear();
|
|
m_Data.Rows.Clear();
|
|
|
|
// read column
|
|
for (int iCol = Sheet.Dimension.Start.Column; iCol <= Sheet.Dimension.End.Column; iCol++)
|
|
{
|
|
string strCol = (string)Sheet.Cells[Sheet.Dimension.Start.Row, iCol].Value;
|
|
m_Data.Columns.Add(new DataColumn(strCol, IsNumberColumn(strCol) ? typeof(float) : typeof(string)));
|
|
}
|
|
|
|
// read data
|
|
for (int iRow = Sheet.Dimension.Start.Row + 1; iRow <= Sheet.Dimension.End.Row; iRow++)
|
|
{
|
|
DataRow newRow = m_Data.NewRow();
|
|
|
|
for (int iCol = Sheet.Dimension.Start.Column; iCol <= Sheet.Dimension.End.Column; iCol++)
|
|
{
|
|
var value = Sheet.Cells[iRow, iCol].Value;
|
|
if (m_Data.Columns[iCol-1].DataType == typeof(float))
|
|
{
|
|
float fData = 0;
|
|
if(value != null)
|
|
fData = (float)(double)value;
|
|
newRow[iCol - 1] = fData;
|
|
}
|
|
else
|
|
{
|
|
string strData = "";
|
|
if (value != null)
|
|
{
|
|
strData = value.ToString();
|
|
strData = strData.Trim();
|
|
}
|
|
|
|
newRow[iCol - 1] = strData;
|
|
}
|
|
}
|
|
|
|
m_Data.Rows.Add(newRow);
|
|
}
|
|
|
|
m_strFilePath = strFilePath;
|
|
int iSeperate = strFilePath.LastIndexOf('\\');
|
|
m_strFileName = strFilePath.Substring(iSeperate+1);
|
|
}
|
|
|
|
|
|
m_MaterialSpring = (from r in m_Data.AsEnumerable()
|
|
select r[m_ColumnMap["spring"]]).Distinct().Cast<string>().ToList();
|
|
|
|
m_MaterialTable = (from r in m_Data.AsEnumerable()
|
|
select r[m_ColumnMap["table"]]).Distinct().Cast<string>().ToList();
|
|
|
|
m_ColumnsNames = (from c in m_Data.Columns.Cast<DataColumn>()
|
|
select c.ColumnName).ToList<string>();
|
|
|
|
|
|
m_ActiveColumns.Clear();
|
|
m_NonactiveColumns.Clear();
|
|
for (int i=0; i< m_ColumnsNames.Count; i++)
|
|
{
|
|
if(m_Data.Columns[i].DataType == typeof(string))
|
|
{
|
|
if(m_Data.AsEnumerable().Any(r => (string)r[i] != ""))
|
|
m_ActiveColumns.Add(m_ColumnsNames[i]);
|
|
else
|
|
m_NonactiveColumns.Add(m_ColumnsNames[i]);
|
|
}
|
|
else
|
|
{
|
|
m_ActiveColumns.Add(m_ColumnsNames[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetSelectedMaterial(string strSpring, string strTable)
|
|
{
|
|
m_strCurSpring = strSpring;
|
|
m_strCurTable = strTable;
|
|
}
|
|
|
|
public string GetCurSpring()
|
|
{
|
|
return m_strCurSpring;
|
|
}
|
|
|
|
public string GetCurTable()
|
|
{
|
|
return m_strCurTable;
|
|
}
|
|
|
|
public List<CHART> GetHumidityChart(string strSpring, string strTable)
|
|
{
|
|
string strQuery = string.Format("[{0}]='{1}' and [{2}]='{3}'", m_ColumnMap["spring"], strSpring, m_ColumnMap["table"], strTable);
|
|
DataRow[] rows = m_Data.Select(strQuery);
|
|
|
|
List<CHART> result = new List<CHART>();
|
|
foreach(DataRow r in rows)
|
|
{
|
|
result.Add(new CHART
|
|
{
|
|
HUMIDITY = (float)r[m_ColumnMap["humidity"]],
|
|
TEMPERATURE = (float)r[m_ColumnMap["temp"]],
|
|
RPN = (float)r[m_ColumnMap["rpn"]],
|
|
});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
#region calculation
|
|
public CalcResult GetCalc(string strSpring, string strTable)
|
|
{
|
|
CalcResult result = new CalcResult();
|
|
|
|
string strQuery = string.Format("[{0}]='{1}' and [{2}]='{3}'", m_ColumnMap["spring"], strSpring, m_ColumnMap["table"], strTable);
|
|
DataRow[] rows = m_Data.Select(strQuery);
|
|
|
|
result.m_iCnt = rows.Length;
|
|
result.m_fAvgRPN = rows.Average(r => (float)r[m_ColumnMap["rpn"]]);
|
|
result.m_fStdRPN = rows.Average(r => (float)Math.Pow((float)r[m_ColumnMap["rpn"]] - result.m_fAvgRPN, 2));
|
|
result.m_fStdRPN = (float)Math.Sqrt(result.m_fStdRPN);
|
|
|
|
var aa = rows
|
|
.GroupBy(r => r[m_ColumnMap["force"]])
|
|
.Select(t => t.Average(k => (float)k[m_ColumnMap["rpn"]]));
|
|
|
|
//foreach( var a in aa )
|
|
//{
|
|
// float fForce = (float)a.Key;
|
|
// float fAvg = a.Average(r => (float)r[m_ColumnMap["rpn"]]);
|
|
//}
|
|
|
|
//.Min(r => (float)r[m_ColumnMap["rpn"]]);
|
|
//aa.Min()
|
|
//float fMin = rows.GroupBy(r => r[m_ColumnMap["force"]])..Min(r => r[m_ColumnMap["rpn"]]);
|
|
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region get
|
|
public DataTable GetData()
|
|
{
|
|
return m_Data;
|
|
}
|
|
|
|
public string GetFilePath()
|
|
{
|
|
return m_strFilePath;
|
|
}
|
|
|
|
public string GetFileName()
|
|
{
|
|
return m_strFileName;
|
|
}
|
|
|
|
public List<string> GetMaterialSpring()
|
|
{
|
|
return m_MaterialSpring;
|
|
}
|
|
|
|
public List<string> GetMaterialTable()
|
|
{
|
|
return m_MaterialTable;
|
|
}
|
|
|
|
public List<string> GetColumns()
|
|
{
|
|
return m_ColumnsNames;
|
|
}
|
|
|
|
public List<string> GetActiveColumns()
|
|
{
|
|
return m_ActiveColumns;
|
|
}
|
|
|
|
public List<string> GetNonactiveColumns()
|
|
{
|
|
return m_NonactiveColumns;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|