comparison ServerMonitor/Objects/Schedule.cs @ 0:3e1a2131f897

Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
author Brad Greco <brad@bgreco.net>
date Mon, 31 Dec 2018 18:32:14 -0500
parents
children 3142e52cbe69
comparison
equal deleted inserted replaced
-1:000000000000 0:3e1a2131f897
1 using System;
2
3 namespace ServerMonitorApp
4 {
5 public class Schedule
6 {
7 public Schedule() { }
8
9 public Schedule(FrequencyUnits units, int frequency, TimeSpan startTime, TimeSpan endTime)
10 {
11 Units = units;
12 Frequency = frequency;
13 StartTime = startTime;
14 EndTime = endTime;
15 }
16
17 public int Frequency { get; set; }
18
19 public FrequencyUnits Units { get; set; }
20
21 public TimeSpan StartTime { get; set; }
22
23 public TimeSpan EndTime { get; set; }
24
25 public DateTime GetNextTime(DateTime lastScheduledTime)
26 {
27 return GetNextTime(lastScheduledTime, DateTime.Now);
28 }
29
30 public DateTime GetNextTime(DateTime lastScheduledTime, DateTime minStartTime)
31 {
32 DateTime nextTime = lastScheduledTime;
33 if (Units == FrequencyUnits.Day)
34 {
35 //TODO what if nextTime is null or really long ago?
36 while (nextTime < minStartTime)
37 nextTime = nextTime.AddDays(Frequency).Date.Add(StartTime);
38 }
39 else
40 {
41 // If the last run time was more than a day ago, fast-forward to reduce the number of loops
42 if (nextTime < minStartTime.AddHours(-24))
43 nextTime = minStartTime.Date.Add(StartTime).AddHours(-24);
44 //TODO handle start time and end time
45 while (nextTime < minStartTime)
46 {
47 switch (Units)
48 {
49 case FrequencyUnits.Second: nextTime = nextTime.AddSeconds(Frequency); break;
50 case FrequencyUnits.Minute: nextTime = nextTime.AddMinutes(Frequency); break;
51 case FrequencyUnits.Hour: nextTime = nextTime.AddHours(Frequency); break;
52 default: throw new InvalidOperationException("Unexpected frequency units: " + Units);
53 }
54 }
55 if (StartTime < EndTime)
56 {
57 if (nextTime.TimeOfDay < StartTime)
58 nextTime = nextTime.Date + StartTime;
59 else if (nextTime.TimeOfDay > EndTime)
60 nextTime = nextTime.Date.AddDays(1) + StartTime;
61 }
62 else if (nextTime.TimeOfDay > EndTime && nextTime.TimeOfDay < StartTime)
63 {
64 nextTime = nextTime.Date + StartTime;
65 }
66 }
67 return nextTime;
68 }
69 }
70
71 public enum FrequencyUnits { Second, Minute, Hour, Day }
72 }