static void Main(string[] args) { try { // load configuration from xml document XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "Configuration.xml"); // get settings from xml file XmlNodeList productionLines = xmlDoc.SelectNodes("//configuration/lines/line"); // list of production lines int days = Convert.ToInt32(xmlDoc.SelectSingleNode("//configuration/days").Attributes["days"].Value); // number of days Random randomNumber = new Random(); // initialize random numbers SolarCell solarCellObject = new SolarCell(); // create new solarCell object // create log file foreach (XmlNode productionLine in productionLines) // loop through all production machines { for (int i = 0; i < days; i++) // loop through all days { string FilePath = AppDomain.CurrentDomain.BaseDirectory + "\\logFiles\\" + productionLine.Attributes["line"].Value; System.IO.Directory.CreateDirectory(FilePath); // create path if it doesnt exist string FilePathAndName = FilePath + "\\" + DateTime.Today.AddDays(-i).ToString("yyyyMMdd") + ".txt"; if (!System.IO.File.Exists(FilePathAndName)) { System.IO.StreamWriter objFileWriter = new System.IO.StreamWriter(FilePathAndName, false); // create new file writer object DateTime timestamp = new DateTime(DateTime.Today.Year, DateTime.Now.Month, DateTime.Now.AddDays(-i).Day, 0, 0, 0); while (timestamp < DateTime.Today.AddDays(1 - i)) // loop until next day is reached { solarCellObject.GetRandomSolarCellProperties(solarCellObject, randomNumber); // create new solar cell with random metadata objFileWriter.Write("\n" + timestamp.ToString() + ";" + solarCellObject.Pmpp + ";" + solarCellObject.Quality); // append string to file timestamp = timestamp.AddSeconds(solarCellObject.ProductionTime); // add production time to timestamp } objFileWriter.Close(); // close file writer } } } } catch (Exception ex) { LoggToFile.AppendStringToLogFile("EXCEPTION || Message: " + ex.Message + " || StackTrace: " + ex.StackTrace); } }
public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); // Login - ? static void Main(string[] args) { int iUpdateDaysCount = 0; // count of past days which should be updated MySqlConnection objMySQLConnection = new MySqlConnection(); // object with MySQL connection info string sMySQLQuery = ""; // string for MySQL Query MySqlCommand objMySQLCommand = new MySqlCommand(); // object for MySQL command try { // load update settings from xml document XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "Configuration.xml"); XmlNodeList itemNodes = xmlDoc.SelectNodes("//configuration/machines/machine"); iUpdateDaysCount = Convert.ToInt32(xmlDoc.SelectSingleNode("//configuration/settings/updateTimeframe").Attributes["updateTimeframe"].Value); // count of past days which should be updated int bConsoleStatus = Convert.ToInt32(xmlDoc.SelectSingleNode("//configuration/settings/consoleStatus").Attributes["consoleStatus"].Value); // programm status as console output - 1:on / 0:off string sMySQLConnectionString = xmlDoc.SelectSingleNode("//configuration/settings/mySQLConnectionString").Attributes["mySQLConnectionString"].Value; string sMySQLConnPW = xmlDoc.SelectSingleNode("//configuration/settings/mySQLConnPW").Attributes["mySQLConnPW"].Value; string sDecryptKey = xmlDoc.SelectSingleNode("//configuration/settings/decryptKey").Attributes["decryptKey"].Value; objMySQLConnection = new MySqlConnection(sMySQLConnectionString + General.Decrypt.DecryptString(sMySQLConnPW, sDecryptKey)); // object with MySQL connection info if (bConsoleStatus == 1) { Console.WriteLine("DB_Update Progress:"); } // update progress status in console if (bConsoleStatus == 1) { Console.WriteLine("Started reading log files - Step 1/2"); } // update progress status in console int iTotalLoops = iUpdateDaysCount * itemNodes.Count; int iLoopCounter = 0; int iFileNotFoundCount = 0; string sDomainAdress = ""; objMySQLConnection.Open(); // open connection // read logfiles and update db for (DateTime dtDatum = DateTime.Today.AddDays(-iUpdateDaysCount); dtDatum <= DateTime.Today; dtDatum = dtDatum.AddDays(1)) // loop through last x days { string sDay = dtDatum.ToString("yyyyMMdd"); // date of day as string foreach (XmlNode itemNode in itemNodes) { string sToolID = itemNode.Attributes["ToolID"].Value; string sFilePath = itemNode.Attributes["FilePath"].Value + sDay + itemNode.Attributes["FileType"].Value; // Login on sourceFile computer if domain has changed if (sDomainAdress != itemNode.Attributes["DomainAdress"].Value && sDomainAdress != "") { sDomainAdress = itemNode.Attributes["DomainAdress"].Value; IntPtr admin_token = new IntPtr(); if (LogonUser("LoginUserName", sDomainAdress, "LoginPassword", 9, 0, ref admin_token) != 0) { WindowsImpersonationContext wic = new WindowsIdentity(admin_token).Impersonate(); } } iLoopCounter += 1; if (File.Exists(sFilePath)) // if machine log file exists: { // concatenate import query string sMySQLQuery = "LOAD DATA LOCAL INFILE '" + sFilePath.Replace(@"\", "/") + "'" + // add filepath to query command " REPLACE" + // replace existing db entrys with same primary key " INTO TABLE process_and_machine_data." + itemNode.Attributes["DBTable"].Value + // add table name to query " FIELDS TERMINATED BY ';'" + // set field seperator " (@field1, @field2, @field3)" + // assign input fields to variables " SET throughput_id = CONCAT('" + dtDatum.ToString("yyyy-MM-dd") + "',MID(@field1,12,8),'" + sToolID + "')" + ", production_timestamp = STR_TO_DATE(@field1, '%d.%m.%Y %H:%i:%s')" + ", line = " + sToolID + ", pmpp = CONVERT(REPLACE(@field2, ',', '.'), DECIMAL(6,5))" + ", quality = TRIM(@field3)"; objMySQLCommand = new MySqlCommand(sMySQLQuery, objMySQLConnection); // object for MySQL command try { int iTest = objMySQLCommand.ExecuteNonQuery(); } // execute command catch (Exception ex) { LoggToFile.AppendStringToLogFile("EXCEPTION || Message: " + ex.Message + " || FilePath: " + sFilePath + " || StackTrace: " + ex.StackTrace); objMySQLConnection.Close(); objMySQLConnection.Open(); } if (bConsoleStatus == 1) { Console.Write("\rCompleted: " + 100 * iLoopCounter / iTotalLoops + "%"); } // update progress status in console } else { LoggToFile.AppendStringToLogFile("\nCouldnt find file: " + sFilePath); iFileNotFoundCount += 1; } } } if (bConsoleStatus == 1) { Console.Write("\n" + iFileNotFoundCount + "/" + iTotalLoops + " Files not found)"); // update progress status in console } iLoopCounter = 0; } catch (Exception ex) { LoggToFile.AppendStringToLogFile("EXCEPTION || Message: " + ex.Message + " || StackTrace: " + ex.StackTrace); } finally { if (objMySQLConnection.State != ConnectionState.Closed) { objMySQLConnection.Close(); } // close mysql connection if still open } }
static void Main(string[] args) { try { // load xml document XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "Configuration.xml"); // get settings from xml file XmlNodeList itemNodes = xmlDoc.SelectNodes("//configuration/folders/folder"); // list of copyJobs int iSyncedDays = Convert.ToInt32(xmlDoc.SelectSingleNode("//configuration/settings/syncedDays").Attributes["syncedDays"].Value); // count of synchronised days int bConsoleStatus = Convert.ToInt32(xmlDoc.SelectSingleNode("//configuration/settings/consoleStatus").Attributes["consoleStatus"].Value); // programm status as console output - 1:on / 0:off string sDecryptKey = Convert.ToString(xmlDoc.SelectSingleNode("//configuration/settings/decryptKey").Attributes["decryptKey"].Value); // decryption Key if (bConsoleStatus == 1) { Console.WriteLine(DateTime.Now.ToString() + " CopyJob started "); } // loop through all folder nodes from xml file foreach (XmlNode itemNode in itemNodes) { // get copyJob settings string sNameOfCopyJob = itemNode.Attributes["copyJobName"].Value; string sSourcePath = itemNode.Attributes["sourcePath"].Value; string sDestinationPath = itemNode.Attributes["destinationPath"].Value; string sLoginUser = itemNode.Attributes["loginUser"].Value; string sLoginDomain = itemNode.Attributes["loginDomain"].Value; string sLoginPassword = itemNode.Attributes["loginPassword"].Value; if (bConsoleStatus == 1) { Console.WriteLine(DateTime.Now.ToString() + " Copying " + sNameOfCopyJob + " files"); } int iFileNumber = 0; // login if (sLoginUser != "" && sLoginDomain != "") { IntPtr intPtrLogin = new IntPtr(); if (LogonUser(sLoginUser, sLoginDomain, General.Decrypt.DecryptString(sLoginPassword, sDecryptKey), 9, 0, ref intPtrLogin) != 0) { WindowsImpersonationContext wic = new WindowsIdentity(intPtrLogin).Impersonate(); } else { throw new Exception("login as user: "******" at domain: " + sLoginDomain + " failed"); } } // copy files which are created within the syncronisation timeframe foreach (string sSourceFile in Directory.GetFiles(sSourcePath, "*", SearchOption.AllDirectories)) { if (bConsoleStatus == 1) { Console.Write("\r" + DateTime.Now.ToString() + " Processing File " + iFileNumber); iFileNumber += 1; } if (File.GetLastWriteTime(sSourceFile) > DateTime.Now.AddDays(-iSyncedDays)) { if (!Directory.Exists(Path.GetDirectoryName(sSourceFile.Replace(sSourcePath, sDestinationPath)))) { Directory.CreateDirectory(Path.GetDirectoryName(sSourceFile.Replace(sSourcePath, sDestinationPath))); // create destination folders if they dont exist } File.Copy(sSourceFile, sSourceFile.Replace(sSourcePath, sDestinationPath), true); // copy the file } } // delete empty folders and files which are older than the synchronisation timeframe in destination path if (bConsoleStatus == 1) { Console.WriteLine("\n" + DateTime.Now.ToString() + " Deleting old " + sNameOfCopyJob + " files and empty folders"); } foreach (string sDestinationFile in Directory.GetFiles(sDestinationPath, "*", SearchOption.AllDirectories)) { if (File.GetLastWriteTime(sDestinationFile) < DateTime.Now.AddDays(-iSyncedDays)) { File.Delete(sDestinationFile); } } foreach (string sDestinationFolder in Directory.GetDirectories(sDestinationPath, "*", SearchOption.AllDirectories)) { if (Directory.GetFiles(sDestinationFolder).Length == 0 && Directory.GetDirectories(sDestinationFolder).Length == 0) { Directory.Delete(sDestinationFolder, false); } } } if (bConsoleStatus == 1) { Console.WriteLine(DateTime.Now.ToString() + " CopyJob finished"); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } catch (Exception ex) { LoggToFile.AppendStringToLogFile("EXCEPTION\nMessage:\n" + ex.Message + "\nStackTrace:\n" + ex.StackTrace); } }