using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace friction { public class Config { public struct RANGE { public float m_fLow; public float m_fHigh; } private static Config m_instance = null; string m_strPath = ""; public static readonly RANGE TEMP_ALL = new RANGE { m_fLow = -100.0f, m_fHigh = 100.0f }; public static readonly RANGE TEMP_LOW = new RANGE { m_fLow = -100.0f, m_fHigh = 0.0f }; public static readonly RANGE TEMP_NORMAL = new RANGE { m_fLow = 15.0f, m_fHigh = 25.0f }; public static readonly RANGE TEMP_HIGH = new RANGE { m_fLow = 40.0f, m_fHigh = 90.0f }; public static readonly RANGE HUMID_ALL = new RANGE { m_fLow = -100.0f, m_fHigh = 100.0f }; public static readonly RANGE HUMID_LOW = new RANGE { m_fLow = 0.0f, m_fHigh = 60.0f }; public static readonly RANGE HUMID_HIGH = new RANGE { m_fLow = 60.0f, m_fHigh = 100.0f }; public class UncheckedMaterial { public List m_Springs = new List(); public List m_Tables = new List(); } public Dictionary m_UncheckedMaterial = new Dictionary(); public string m_strCurMaterial = ""; public bool m_bOnLoad = false; private Config() { } public static Config GetInstance() { if (m_instance == null) m_instance = new Config(); return m_instance; } public void Init() { string strPrjName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name; string strFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), strPrjName); m_strPath = strFolder + "\\config.ini"; if (!Directory.Exists(strFolder)) Directory.CreateDirectory(strFolder); Load(); } [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); private UncheckedMaterial LoadUncheckedMaterial(string section) { UncheckedMaterial cm = new UncheckedMaterial(); StringBuilder temp = new StringBuilder(10240); int iRes = GetPrivateProfileString(section, "unchecked-spring", "", temp, 10240, m_strPath); if(temp.Length > 0) { string[] astrToken = temp.ToString().Split(','); cm.m_Springs = astrToken.ToList(); } iRes = GetPrivateProfileString(section, "unchecked-table", "", temp, 10240, m_strPath); if (temp.Length > 0) { string[] astrToken = temp.ToString().Split(','); cm.m_Tables = astrToken.ToList(); } return cm; } public bool HasPref(string strName) { foreach(var pair in m_UncheckedMaterial) { if (pair.Key == strName) return true; } return false; } public void InsertPref(string strName) { m_UncheckedMaterial.Add(strName, new UncheckedMaterial()); } public void RemovePref(string strName) { m_UncheckedMaterial.Remove(strName); WritePrivateProfileString(strName, null, null, m_strPath); } public void Load() { StringBuilder temp = new StringBuilder(10240); int iRes = GetPrivateProfileString("Option", "recent", "", temp, 10240, m_strPath); if(temp.Length > 0) { string[] astrList = temp.ToString().Split(','); foreach (string strFile in astrList) OPTION.GetInstance().AddRecentFile(strFile); } iRes = GetPrivateProfileString("Option", "bound", "", temp, 10240, m_strPath); if(temp.Length > 0) { string[] astrBound = temp.ToString().Split(','); int iLeft, iWidth, iTop, iHeight; int.TryParse(astrBound[0], out iLeft); int.TryParse(astrBound[1], out iWidth); int.TryParse(astrBound[2], out iTop); int.TryParse(astrBound[3], out iHeight); OPTION.GetInstance().WindowBound = new Rectangle(iLeft, iTop, iWidth, iHeight); } iRes = GetPrivateProfileString("Option", "checked-column", "", temp, 10240, m_strPath); if (temp.Length > 0) { string[] astrBound = temp.ToString().Split(','); OPTION.GetInstance().CheckedColumns = astrBound.ToList(); } iRes = GetPrivateProfileString("Option", "unchecked-column", "", temp, 10240, m_strPath); if (temp.Length > 0) { string[] astrBound = temp.ToString().Split(','); OPTION.GetInstance().UncheckedColumns = astrBound.ToList(); } iRes = GetPrivateProfileString("Option", "conf-names", "", temp, 10240, m_strPath); if (temp.Length > 0) { m_UncheckedMaterial.Clear(); string[] astrSections = temp.ToString().Split(','); foreach(string section in astrSections) { UncheckedMaterial cm = LoadUncheckedMaterial(section); m_UncheckedMaterial.Add(section, cm); } } iRes = GetPrivateProfileString("Option", "current-conf", "", temp, 10240, m_strPath); if (temp.Length > 0) { m_strCurMaterial = temp.ToString(); } if(m_UncheckedMaterial.Count == 0) { m_strCurMaterial = "Default"; m_UncheckedMaterial.Add(m_strCurMaterial, new UncheckedMaterial()); } } public void Save(bool bSavePref) { WritePrivateProfileString("Option", "recent", OPTION.GetInstance().GetRecentAll(), m_strPath); WritePrivateProfileString("Option", "bound", OPTION.GetInstance().WindowBoundStr(), m_strPath); WritePrivateProfileString("Option", "checked-column", OPTION.GetInstance().CheckedColumnsStr(), m_strPath); WritePrivateProfileString("Option", "unchecked-column", OPTION.GetInstance().UncheckedColumnsStr(), m_strPath); WritePrivateProfileString("Option", "conf-names", string.Join(",", m_UncheckedMaterial.Keys.ToArray()), m_strPath); WritePrivateProfileString("Option", "current-conf", m_strCurMaterial, m_strPath); if (bSavePref == true) { WritePrivateProfileString(m_strCurMaterial, "unchecked-spring", string.Join(",", m_UncheckedMaterial[m_strCurMaterial].m_Springs.ToArray()), m_strPath); WritePrivateProfileString(m_strCurMaterial, "unchecked-table", string.Join(",", m_UncheckedMaterial[m_strCurMaterial].m_Tables.ToArray()), m_strPath); } } public void SetSpringChecked(List CheckedItems) { if (m_bOnLoad == true) return; m_UncheckedMaterial[m_strCurMaterial].m_Springs = CheckedItems; } public List GetSpringChecked() { return m_UncheckedMaterial[m_strCurMaterial].m_Springs; } public void SetTableChecked(List CheckedItems) { if (m_bOnLoad == true) return; m_UncheckedMaterial[m_strCurMaterial].m_Tables = CheckedItems; } public List GetTableUnchecked() { return m_UncheckedMaterial[m_strCurMaterial].m_Tables; } public struct COLUMN_NAME { public static string SPRING = "SurfaceMtSpring"; public static string TABLE = "SurfaceMtCarriage"; public static string RPN = "RPN"; public static string FORCE = "normal force"; public static string TEMP = "temperature"; public static string HUMIDITY = "humidity"; public static string VELOCITY = "velocity"; } public class OPTION { private static OPTION m_instance = null; public List m_RecentList = new List(); public Rectangle WindowBound { set; get; } public List CheckedColumns { set; get; } public List UncheckedColumns { set; get; } public static OPTION GetInstance() { if (m_instance == null) m_instance = new OPTION(); return m_instance; } public void AddRecentFile(string strPath) { if(m_RecentList.Find(s => s==strPath) != null) m_RecentList.Remove(strPath); m_RecentList.Add(strPath); } public string GetRecentAll() { return string.Join(",", m_RecentList); } public string WindowBoundStr() { return string.Format("{0}, {1}, {2}, {3}", WindowBound.Left, WindowBound.Width, WindowBound.Top, WindowBound.Height); } public string CheckedColumnsStr() { string strResult = ""; if(CheckedColumns != null) { foreach (var item in CheckedColumns) { if (strResult.Length > 0) strResult += ","; strResult += item; } } return strResult; } public string UncheckedColumnsStr() { string strResult = ""; if(UncheckedColumns != null) { foreach (var item in UncheckedColumns) { if (strResult.Length > 0) strResult += ","; strResult += item; } } return strResult; } } public class ANALYSIS { public enum RISK { NO=3, POTENTIAL=5, HIGH=10 } public enum DEPENDANCY { NO, POTENTIAL, HIGH, NOT_ENNOUGH_DATA } public static RISK GetRisk(float fAvg) { if (fAvg <= 4) return RISK.NO; else if (fAvg <= 6) return RISK.POTENTIAL; else return RISK.HIGH; } public static DEPENDANCY GetDependancy(float fStdev, int iTestCnt) { if (iTestCnt <= 3) return DEPENDANCY.NOT_ENNOUGH_DATA; else if (fStdev < 3.5f) return DEPENDANCY.NO; else if (fStdev < 5.5f) return DEPENDANCY.POTENTIAL; else return DEPENDANCY.HIGH; } } } }