public void Run(string[] args) { RebooterProperties = new AppProperties(); // Parse the command line arguments var oParser = new SimpleCommandLineParser(); oParser.Parse(args); RebooterProperties.CurrentModem = GetModemClass(); // Show startup text ShowIntro(RebooterProperties); // Read the command line parameters RebooterProperties.ReadParameters(oParser, RebooterProperties); // One minute timer Timer oTimer = new Timer { Interval = 60000 }; oTimer.Elapsed += (sender, e) => { HandleTimerElapsed(RebooterProperties); }; oTimer.Enabled = !String.IsNullOrEmpty(RebooterProperties.ModemIP); if (oTimer.Enabled) { DoTimedEvents(RebooterProperties); } DoConsoleRead(); oTimer.Dispose(); }
public void LogSyncRates(AppProperties aProperties) { // Only log if stats have changed if (GetSyncRates(aProperties) & (!String.Equals(Downrate + Uprate, aProperties.LastStats, StringComparison.OrdinalIgnoreCase))) { // Remember these stats aProperties.LastStats = Downrate + Uprate; aProperties.LogMessage(aProperties, $"Downstream (Kbps): {Downrate}, Upstream (Kbps): {Uprate}, Uptime: {UpTime}"); } }
public virtual Boolean PerformReboot(AppProperties aProperties) { GetSyncRates(aProperties); // Log the current stats just before the reboot aProperties.LogMessage(aProperties, $"Sending Reboot Command. Current Stats: Downstream (Kbps): {Downrate}, Upstream (Kbps): {Uprate}, Uptime: {UpTime}"); string sRebootHtml = GetPageHtml(aProperties, string.Format(GetRebootUrl(), aProperties.ModemIP)); return(!string.IsNullOrEmpty(sRebootHtml)); }
protected virtual int MinutesSinceLastReboot(AppProperties aProperties) { if (aProperties.HasReboot) { TimeSpan oTimeSpan = (DateTime.Now - aProperties.LastReboot); return(oTimeSpan.Minutes); } else { // We have not rebooted yet - so do a reboot if the time is right. return(60); } }
private void CheckForRebootRequired(AppProperties aProperties) { GetCurrentTime(out int iHours, out int iMinutes); TimeSpan dTimeNow = new TimeSpan(0, iHours, iMinutes, 0); Boolean bPerformReboot = false; // 5 minute period that we should try to reboot within TimeSpan dEndTime = aProperties.RebootTime.Add(TimeSpan.FromMinutes(5)); // Ensure we are within the reboot time Boolean bScheduleTimeReached = aProperties.ScheduleReboot && (dTimeNow >= aProperties.RebootTime && dTimeNow <= dEndTime); if (aProperties.MaxUpTimeReboot) { // Check if we have reached the maximum up time. Boolean bMaxupTimeReached = aProperties.MaxUpTimeReboot && (aProperties.CurrentModem.UpTimeHours >= aProperties.MaxUpTime); bPerformReboot = bMaxupTimeReached && bScheduleTimeReached; if (bPerformReboot) { aProperties.LogMessage(aProperties, "Schedule reboot time and maximum uptime reached."); } } else if (aProperties.ScheduleReboot) { bPerformReboot = bScheduleTimeReached; if (bPerformReboot) { aProperties.LogMessage(aProperties, "Schedule reboot time reached."); } } if (aProperties.RebootOnStart || bPerformReboot) { if (aProperties.CurrentModem.PerformReboot(aProperties)) { aProperties.LastReboot = DateTime.Now; aProperties.HasReboot = true; aProperties.RebootOnStart = false; } else { aProperties.LogMessage(aProperties, "Failed to send reboot command."); } } }
public void LogMessage(AppProperties aProperties, string aMessage) { string sOutput = $"{DateTime.Now.ToString(CultureInfo.InvariantCulture)}, {aMessage}"; WriteLine(sOutput); if (!string.IsNullOrEmpty(aProperties.LogPath)) { try { File.AppendAllText(aProperties.LogPath, sOutput + Environment.NewLine); } catch (Exception ex) { WriteLine($"An error occurred writing to the log: {aProperties.LogPath}. Error: {ex.Message}"); } } }
public void DoTimedEvents(AppProperties aProperties) { TimeSpan oStatDiff = DateTime.Now.Subtract(aProperties.LastStatCheck); // Minutes since the last reboot command was sent. int iMinutesSinceLastReboot = MinutesSinceLastReboot(aProperties); // Allow some time for the reboot to occur, then check the statistics. Boolean bJustRebooted = (iMinutesSinceLastReboot > 4) && (iMinutesSinceLastReboot < 10); if ((bJustRebooted) || (aProperties.InitialStatCheck || oStatDiff.Minutes > aProperties.UpdateInterval)) { aProperties.CurrentModem.LogSyncRates(aProperties); aProperties.InitialStatCheck = false; } if (!bJustRebooted && ((aProperties.ScheduleReboot || aProperties.RebootOnStart || aProperties.MaxUpTimeReboot))) { CheckForRebootRequired(aProperties); } }
protected override Boolean GetSyncRates(AppProperties aProperties) { string sPage = GetPageHtml(aProperties, string.Format(GetStatisticsUrl(), aProperties.ModemIP)); Uprate = ""; Downrate = ""; if (!string.IsNullOrEmpty(sPage)) { aProperties.LastStatCheck = DateTime.Now; Downrate = ExtractText(sPage, "B0 Line Rate - Downstream (Kbps):</td>"); Uprate = ExtractText(sPage, "B0 Line Rate - Upstream (Kbps):</td>"); UpTime = ExtractText(sPage, "Uptime:"); UpTimeHours = UpTimeToHours(UpTime); return(true); } else { return(false); } }
protected string GetPageHtml(AppProperties aProperties, string aUrl) { string sHtml = ""; using (WebClient oClient = new WebClient()) { if (aProperties.UseAuth) { oClient.UseDefaultCredentials = true; oClient.Credentials = new NetworkCredential(aProperties.Username, aProperties.Password); } try { sHtml = oClient.DownloadString(aUrl); } catch (WebException ex) { HandleWebException(ex, aUrl); } } return(sHtml); }
protected abstract Boolean GetSyncRates(AppProperties aProperties);
private void HandleTimerElapsed(AppProperties aProperties) { DoTimedEvents(aProperties); }
private static void ShowIntro(AppProperties aProperties) { Console.WriteLine( $"Started {"NBN Rebooter"}. Modem: {aProperties.CurrentModem.GetModemName()}. Version: {Assembly.GetExecutingAssembly().GetName().Version}"); }
public void ReadParameters(SimpleCommandLineParser aParser, AppProperties aProperties) { MaxUpTime = 0; aProperties.HasReboot = false; aProperties.RebootOnStart = aParser.Arguments.ContainsKey("rebootnow"); aProperties.ScheduleReboot = aParser.Arguments.ContainsKey("reboot"); aProperties.MaxUpTimeReboot = aParser.Arguments.ContainsKey("maxuptime"); aProperties.InitialStatCheck = true; aProperties.RebootTime = TimeSpan.Zero; aProperties.UseAuth = aParser.Arguments.ContainsKey("username") && aParser.Arguments.ContainsKey("password"); int iUpdateInterval; if (aParser.Arguments.ContainsKey("interval")) { int.TryParse(aParser.Arguments["interval"][0], out iUpdateInterval); } else { // Default interval to refresh statistics iUpdateInterval = 30; } aProperties.UpdateInterval = iUpdateInterval; if (aProperties.UseAuth) { aProperties.Username = aParser.Arguments["username"][0]; aProperties.Password = aParser.Arguments["password"][0]; } if (aProperties.MaxUpTimeReboot) { if (int.TryParse(aParser.Arguments["maxuptime"][0], out int iMaxUpTime)) { MaxUpTime = iMaxUpTime; } else { WriteLine($"Unable to convert {aParser.Arguments["maxuptime"][0]} to max up time."); } } aProperties.LogPath = (aParser.Arguments.ContainsKey("log")) ? aParser.Arguments["log"][0] : ""; if (String.IsNullOrEmpty(aProperties.LogPath)) { WriteLine("No log path specified. Console output only enabled."); } aProperties.ModemIP = (aParser.Arguments.ContainsKey("ip")) ? aParser.Arguments["ip"][0] : ""; if (String.IsNullOrEmpty(aProperties.ModemIP)) { WriteLine("No modem IP provide, please see the help for parameter usage."); aProperties.ModemIP = ""; return; } if (aProperties.ScheduleReboot) { TimeSpan tsScheduleTime; if (TimeSpan.TryParseExact(aParser.Arguments["reboot"][0], @"hh\:mm", CultureInfo.CurrentCulture, out tsScheduleTime)) { aProperties.RebootTime = tsScheduleTime; } else { aProperties.ScheduleReboot = false; WriteLine($"Unable to convert {aParser.Arguments["reboot"][0]} to reboot hour. Ensure it is in the format: hh:mm"); } } if (aProperties.RebootOnStart) { WriteLine("Reboot will occur immediately..."); } else if (aProperties.ScheduleReboot && (aProperties.RebootTime != TimeSpan.Zero)) { WriteLine($"Next reboot will occur at {aProperties.RebootTime.ToString()}"); if (MaxUpTime > 0) { WriteLine($"AND when the modem uptime has reached {MaxUpTime.ToString()} hours."); } } else if (MaxUpTime > 0) { WriteLine($"Next reboot will occur when the modem uptime has reached {MaxUpTime.ToString()} hours."); } else if ((!aProperties.ScheduleReboot) && !(MaxUpTime > 0)) { WriteLine("No reboot schedule set. Only logging sync rate statistics."); } }