public static void Initialize() { ///////////////////////////////////////////////////////////////// // First check where we are running and prepare app // if (Get_Host_OS() == Host_OS.LINUX) { // Linux Source_Path = "/var/EFD/"; flights_Path = "/var/cbs/prediction/flights/"; System_Status_Path = "/var/cbs/prediction/systemStatus/"; Main_Status_Path = "/var/cbs/prediction/status/"; App_Settings_Path = "/var/cbs/settings/EFD/"; AIRAC_Data_Path = "/var/cbs/settings/AIRAC/"; Tmp_Directory = "/tmp/"; } // Now make sure that proper directory structure // is set up on the host machine if (Directory.Exists(App_Settings_Path) == false) { Directory.CreateDirectory(App_Settings_Path); WriteToLogFile("Creating " + App_Settings_Path); } if (Directory.Exists(Source_Path) == false) { Directory.CreateDirectory(Source_Path); WriteToLogFile("Creating " + Source_Path); } if (Directory.Exists(flights_Path) == false) { Directory.CreateDirectory(flights_Path); WriteToLogFile("Creating " + flights_Path); } if (Directory.Exists(System_Status_Path) == false) { Directory.CreateDirectory(System_Status_Path); WriteToLogFile("Creating " + System_Status_Path); } if (Directory.Exists(Main_Status_Path) == false) { Directory.CreateDirectory(Main_Status_Path); WriteToLogFile("Creating " + Main_Status_Path); } if (Directory.Exists(AIRAC_Data_Path) == false) { Directory.CreateDirectory(AIRAC_Data_Path); WriteToLogFile("Creating " + AIRAC_Data_Path); } if (Directory.Exists(Tmp_Directory) == false) { Directory.CreateDirectory(Tmp_Directory); WriteToLogFile("Creating " + Tmp_Directory); } // Check if cbs_config.txt exists, if so load settings // data saved from the previous session string Settings_Data; string FileName = Path.Combine(App_Settings_Path, "cbs_config.txt"); char[] delimiterChars = { ' ' }; StreamReader MyStreamReader; if (File.Exists(FileName)) { WriteToLogFile("Processing " + FileName); // Lets read in settings from the file MyStreamReader = System.IO.File.OpenText(FileName); while (MyStreamReader.Peek() >= 0) { Settings_Data = MyStreamReader.ReadLine(); if (Settings_Data[0] != '#') { string[] words = Settings_Data.Split(delimiterChars); switch (words[0]) { case "SOURCE_DIR": Source_Path = words[1]; break; case "FLIGHTS_DIR": flights_Path = words[1]; break; case "SYSTEM_STATUS_DIR": System_Status_Path = words[1]; break; case "MAIN_STATUS_DIR": Main_Status_Path = words[1]; break; case "HEART_BEAT": HEART_BEAT = words[1]; break; case "COLD_POWER_UP": Cold_Start_Timeout_Min = int.Parse(words[1]); break; case "SYS_STATUS_UPDATE_RATE": System_Status_Update_Rate_Sec = int.Parse(words[1]); break; case "NO_EFD_DATA_TIMEOUT": No_EFD_Data_Timout = int.Parse(words[1]); break; case "AIRAC_DATA_SOURCE": AIRAC_Data_Path = words[1]; break; case "MYSQL_SERVER": MySqlWriter.MySQLConnetionString.server_name = words[1]; break; case "MYSQL_USER": MySqlWriter.MySQLConnetionString.login_name = words[1]; break; case "MYSQL_DATABASE": MySqlWriter.MySQLConnetionString.database = words[1]; break; case "MYSQL_TABLE": MySqlWriter.MySQLConnetionString.table_name = words[1]; break; case "DEBUG": debug_on = words[1]; break; default: break; } } } MyStreamReader.Close(); MyStreamReader.Dispose(); // Here check if there has been more than parameter since // application has been down TimeSpan TenMin = new TimeSpan(0, Cold_Start_Timeout_Min, 0); TimeSpan AppDown = DateTime.UtcNow - Get_Power_OFF_Time(); // Now check if the application has been down for more than // 10 minutes. If so then clear the directory if (AppDown > TenMin) { ClearSourceDirectory(); WriteToLogFile("APP down for more then 10min, clearing " + Source_Path); } else { // Call routine to process all files that might have // arrived in the last 10 minutes or less. } // Lets save once so HART BEAT gets saved right away SaveSettings(); } else { // Lets then create one with default setting SaveSettings(); // Since we had no idea when the application was last powered off // we assume it has been more than timuout parameter, sop lets delete all files ClearSourceDirectory(); WriteToLogFile("APP down time unknown, clearing " + Source_Path); } // Now start heart beat timer. Cold_Start_Timer = new System.Timers.Timer(10000); // Set up the timer for 1minute Cold_Start_Timer.Elapsed += new ElapsedEventHandler(_HEART_BEAT_timer_Elapsed); Cold_Start_Timer.Enabled = true; // Start input data monitor EFD_File_Handler.Initialise(); // Open up connection to the MySQL database MySqlWriter.Initialise(); ////////////////////////////////////////////////////// // Start periodic timer that will drive system status // update logic // Now start heart beat timer. System_Status_Timer = new System.Timers.Timer((System_Status_Update_Rate_Sec * 100)); // Set up the timer for 1minute System_Status_Timer.Elapsed += new ElapsedEventHandler(System_Status_Periodic_Update); System_Status_Timer.Enabled = true; CBS_Main.WriteToLogFile("Started system status heart beat timer"); }
public static void Handle_New_File() { string[] filePaths = Directory.GetFiles(CBS_Main.Get_Source_Data_Path(), "*.log*", SearchOption.AllDirectories); foreach (string Path in filePaths) { try { using (MyStreamReader = System.IO.File.OpenText(Path)) { if (MyStreamReader != null) { //// Pass in stream reader and initialise new //// EFD message. EFD_Msg EDF_MESSAGE = new EFD_Msg(MyStreamReader); MyStreamReader.Close(); MyStreamReader.Dispose(); try { //// Generate output Generate_Output.Generate(EDF_MESSAGE); } catch (Exception e1) { CBS_Main.WriteToLogFile("Error in Generate_Output.Generate " + e1.Message); } try { // Write data to the MySqlDatabase MySqlWriter.Write_One_Message(EDF_MESSAGE); } catch (Exception e2) { CBS_Main.WriteToLogFile("Error in MySqlWriter.Write_One_Message " + e2.Message); } // Let the status handler know that the // message has arrived... try { CBS_Main.Notify_EFD_Message_Recived(); } catch (Exception e3) { CBS_Main.WriteToLogFile("Error in CBS_Main.Notify_EFD_Message_Recived " + e3.Message); } //// Once done with the file, //// lets delete it as we do not //// needed it any more try { System.IO.File.Delete(Path); } catch { CBS_Main.WriteToLogFile("Error in EFD_File_Handler, can't delete file " + Path); } break; } } } catch (Exception ex) { CBS_Main.WriteToLogFile("Exception EFD_Handler: " + ex.Message); } } }