/// <summary> /// Add the ping to the rig /// </summary> /// <param name="thisStation">The rig being updated</param> /// <param name="newStamp">The new ping stamp</param> /// <returns>The combined rig and ping stamp</returns> public static StationData appendPing(StationData thisStation, PingStamp newStamp) { //This creates a new day representation, which pings can be added to PingDay newDay = new PingDay(); //This adds the first stamp to the new day newDay.todayPings.Add(newStamp); //this adds the day to the station's ping list thisStation.thePings.Add(newDay); return thisStation; }
/// <summary> /// This method clarifies if we need to add the new ping status to the given StationData object. /// </summary> /// <param name="thisStation">The current station</param> /// <param name="newStamp">The new ping</param> /// <returns>The updated station</returns> public static StationData processPings(StationData thisStation, PingStamp newStamp) { Boolean newDate = true; //Check if there are no pings already try{ if (thisStation.thePings.Count == 0) { thisStation = appendPing(thisStation, newStamp); //this is not a new value newDate = false; } else for (int k = 0; k < thisStation.thePings.Count; k++) { if (thisStation.thePings[k].compareDates(newStamp.theDateTime.Date)) { thisStation.thePings[k].todayPings.Add(newStamp); newDate = false; k = thisStation.thePings.Count; } } } catch(Exception) { thisStation = appendPing(thisStation, newStamp); } //ekse compare the ping dates, add the ping to the particular date if (newDate) { thisStation = appendPing(thisStation, newStamp); } for (int i = 0; i <= thisStation.thePings.Count - 1; i++) { //Iterate through ping entries to find any that are older than 30 days, and then delete them if ((thisStation.thePings[i].todayPings[0].theDateTime < DateTime.Now.AddDays(-30)) && thisStation.thePings.Count > 1) { thisStation.thePings.RemoveAt(i); i--; } } return thisStation; }
/// <summary> /// For each station where it is enabled, send a ping. /// </summary> /// <param name="thisStation">The current rig</param> /// <param name="i">site index</param> /// <param name="j">rig index</param> /// <returns></returns> public override StationData eachStation(StationData thisStation, int i, int j) { Console.WriteLine(thisStation.Station_Name); if (!thisStation.OS_Type.Equals("DOS")) { Ping p1 = new Ping(); PingStamp newStamp = new PingStamp(); try { //Take a stamp of the time at which the ping is attempted newStamp.theDateTime = DateTime.Now; Console.WriteLine("Sending Ping"); //Send the ping PingReply PR = p1.Send(thisStation.Network_Name); //Deal with the response if ((PR.Status == IPStatus.Success)) { newStamp.result = "Online"; newStamp.roundtripTime = PR.RoundtripTime; newStamp.netStatus = PR.Status.ToString(); Console.WriteLine("Online"); } else { newStamp.result = "Offline"; newStamp.roundtripTime = PR.RoundtripTime; newStamp.netStatus = PR.Status.ToString(); Console.WriteLine("Offline"); } } catch (Exception e) { //if there was an error, log log the result Console.WriteLine("Ping Failed"); newStamp.result = "Error"; if (e.InnerException.Message.ToString().Equals("No such host is known")) { newStamp.stackTrace = "No such host is known"; } else { newStamp.stackTrace = e.ToString(); } } //If the ping data is different from the last one logged, add it to the list. try { if (!(thisStation.thePings[thisStation.thePings.Count - 1] .todayPings[thisStation.thePings[thisStation .thePings.Count - 1].todayPings.Count - 1].result == newStamp.result)) { thisStation = processPings(thisStation, newStamp); } } catch (Exception) { thisStation.thePings = new List<PingDay>(); //The exception is assumed to be a Null pointer, meaning that there is no date entered so the result to simply assign the ping. thisStation = processPings(thisStation, newStamp); } } return thisStation; }