diff --git a/IntegrationEgaltech/Generator.cs b/IntegrationEgaltech/Generator.cs index af1e0ec..99991c9 100644 --- a/IntegrationEgaltech/Generator.cs +++ b/IntegrationEgaltech/Generator.cs @@ -10,6 +10,7 @@ using System.Threading; using System.Threading.Tasks; using ib.essetre.integration; using ib.essetre.integration.egaltech; +using System.Runtime.InteropServices; namespace ib.essetre.integration.egaltech { @@ -36,6 +37,10 @@ namespace ib.essetre.integration.egaltech { callback.Progress( 0, "Init") ; + GetToolParams(parameters); + + GetMachineParams(parameters); + SqlConnectionStr = parameters.ConnectionString ; String ExePath = parameters.Read( "EGALTECH", "PATH_EXE", "") ; @@ -520,5 +525,183 @@ namespace ib.essetre.integration.egaltech return barLength ; } + //Import necessario per l'uso della funzione per scrivere sui file INI + [DllImport("kernel32", CharSet = CharSet.Unicode)] + static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath); + + //Scrive il file ToolConfig.ini che include gli utensili e i relativi parametri presi dal DB + private static void GetToolParams(InOutParameters parameters) + { + SqlConnectionStr = parameters.ConnectionString; + DataSet dsTools = new DataSet(); + + String BtlDir = parameters.Read("EGALTECH", "DIR_BTL", ""); + // Se la directory in cui salvare il file non esiste viene creata + DirectoryInfo di = new DirectoryInfo(BtlDir); + if (!di.Exists) + di.Create(); + + //string sql = "SELECT * FROM dbo.vw_Tool"; + string sqlTool = "SELECT [vw_Tool].[toolId], [code], [jobType], [toolType], " + + "[enabled], [note], [position], [file3ds], [rpm], [feed], [srot], " + + "[number], [vw_Tool].[configurationId], [vw_Tool].[flagDeleted], [lastModified], " + + "[key], [value] " + + "FROM [ESSETRE].[dbo].[vw_Tool] " + + "INNER JOIN [ESSETRE].[dbo].[vw_ToolParam] " + + "ON [vw_Tool].[toolId] = [vw_ToolParam].[toolId] "; + + // Connessione al DB ed esecuzione query + using (SqlConnection cn = new SqlConnection(SqlConnectionStr)) + { + cn.Open(); + using (SqlCommand cmd = new SqlCommand(sqlTool, cn)) + { + var dataAdapter = new SqlDataAdapter(cmd); + + // Lettura DB e riempimento DataSet + dsTools.Clear(); + dataAdapter.Fill(dsTools); + } + } + + // Scrittura del file INI + string IniName = "ToolConfig.ini"; + // Path completa del nome del file INI da salvare + string IniPath = Path.Combine(BtlDir, IniName); + + String Section = ""; + String Key = ""; + String Value = ""; + + foreach (DataTable table in dsTools.Tables) + { + int toolId = 0; + + foreach (DataRow row in table.Rows) + { + bool spaceBeforeSection = false; + + if ((int)row["toolId"] != toolId) + { + spaceBeforeSection = true; + toolId = (int)row["toolId"]; + Section = "Tool_" + toolId.ToString("00"); //row["code"].ToString(); + + //Questo foreach è all'interno dell'if in modo da essere eseguito una volta sola + foreach (DataColumn column in table.Columns) + { + if (!(column.ColumnName.Equals("key") || column.ColumnName.Equals("value"))) + { + Key = column.ColumnName; + Value = row[column].ToString(); + + WritePrivateProfileString(Section, Key, Value, IniPath); + } + } + } + + //Questo è all'esterno dell'if perché va eseguito più volte, per key e value di ogni ToolParam + Key = row["key"].ToString(); + Value = row["value"].ToString(); + + WritePrivateProfileString(Section, Key, Value, IniPath); + + if (spaceBeforeSection) + { + var txtLines = File.ReadAllLines(IniPath).ToList(); //Fill a list with the lines from the txt file. + var sectionIni = "[" + Section + "]"; + if (txtLines.IndexOf(sectionIni) > 0) + { + var lineBeforeSection = txtLines[txtLines.IndexOf(sectionIni) - 1].ToString(); + if (!String.IsNullOrEmpty(lineBeforeSection)) + { + txtLines.Insert(txtLines.IndexOf(sectionIni), Environment.NewLine); + File.WriteAllLines(IniPath, txtLines); + } + } + } + + } + } + + } + + //Scrive il file MachConfig_[MachineName].ini che include i parametri macchina presi dal DB + private static void GetMachineParams(InOutParameters parameters) + { + SqlConnectionStr = parameters.ConnectionString; + DataSet dsMachs = new DataSet(); + + String BtlDir = parameters.Read("EGALTECH", "DIR_BTL", ""); + // Se la directory in cui salvare il file non esiste viene creata + DirectoryInfo di = new DirectoryInfo(BtlDir); + if (!di.Exists) + di.Create(); + + string sqlMach = "SELECT [group], [key], [value] " + + "FROM [ESSETRE].[dbo].[vw_MachineParam] " + + "WHERE [group] = 'OFFSETS' " + + "OR [group] = 'TRAVE' "; + + // Connessione al DB ed esecuzione query + using (SqlConnection cn = new SqlConnection(SqlConnectionStr)) + { + cn.Open(); + using (SqlCommand cmd = new SqlCommand(sqlMach, cn)) + { + var dataAdapter = new SqlDataAdapter(cmd); + + // Lettura DB e riempimento DataSet + dsMachs.Clear(); + dataAdapter.Fill(dsMachs); + } + } + + // Scrittura del file INI + string IniName = "MachConfig_" + parameters.MachineName + ".ini"; + // Path completa del nome del file INI da salvare + string IniPath = Path.Combine(BtlDir, IniName); + + String Section = ""; + String Key = ""; + String Value = ""; + + foreach (DataTable table in dsMachs.Tables) + { + + foreach (DataRow row in table.Rows) + { + bool spaceBeforeSection = false; + + if ((string)row["group"] != Section) + spaceBeforeSection = true; + + //Questo è all'esterno dell'if perché va eseguito più volte, per key e value di ogni MachineParam + Section = row["group"].ToString(); + Key = row["key"].ToString(); + Value = row["value"].ToString(); + + WritePrivateProfileString(Section, Key, Value, IniPath); + + if (spaceBeforeSection) + { + var txtLines = File.ReadAllLines(IniPath).ToList(); //Fill a list with the lines from the txt file. + var sectionIni = "[" + Section + "]"; + if (txtLines.IndexOf(sectionIni) > 0) + { + var lineBeforeSection = txtLines[txtLines.IndexOf(sectionIni) - 1].ToString(); + if (!String.IsNullOrEmpty(lineBeforeSection)) + { + txtLines.Insert(txtLines.IndexOf(sectionIni), Environment.NewLine); + File.WriteAllLines(IniPath, txtLines); + } + } + } + + } + } + + } + } }