tbf/Config/CalendarEvent/Utils.cs

227 lines
9.6 KiB
C#

///
/// Copyright (c) 2020 Sensus Slovensko a.s.
///
using System;
using Config.Resources;
namespace Config.CalendarEvent
{
/// <summary>
/// Enumeration of calendar event actions
/// </summary>
public enum AutoAction
{
Undefined,
None, /// No action at all
Notification, /// Trigger a notification event
Warning, /// Trigger a warning event
Error, /// Trigger an error event and stop test bench function
FatalErr, /// Trigger a fatal error and stop test bench function
InvokeBeforeCycle, /// Invoke a maintenance procedure before a cycle after entering order number
InvokeInsideCycle, /// Invoke conditional tests inside a cycle (if any)
InvokeAfterCycle, /// Invoke a maintenance procedure after a cycle after saving results
BackupData,
Count,
}
/// <summary>
/// Enumeration of built-in recurring event frequencies
/// </summary>
public enum Frequency
{
Undefined,
Once, /// Indicates that the event is non recurring will occur only one time
Yearly, /// Indicates that the event will occur once a year, on the month and day specified
Monthly, /// Indicates that the event will occur every month
Weekly, /// Indicates that the event will occur every week
EveryTuTh, /// Indicates that the event will occur every Tuesday and Thursday
EveryMoWeFr, /// Indicates that the event will occur every Mon, Wed and Fri
EveryWkday, /// Indicates that the event will occur every week day (Mon - Fri)
Daily, /// Indicates that the event will occur every day
EveryShift,
Custom, /// Indicates that the recuring schedule of this event is unique
Count
}
public class Utils
{
public static string Action2String(AutoAction action)
{
return action.ToString();
}
public static AutoAction String2Action(string astr)
{
for (AutoAction a = AutoAction.None; a < AutoAction.Count; a++)
{
if (Action2String(a) == astr) return a;
}
return AutoAction.Undefined;
}
public static string Freq2String(Frequency f)
{
switch (f)
{
case Frequency.Custom: return Strings.Custom;
case Frequency.Daily: return Strings.Daily;
case Frequency.EveryMoWeFr: return Strings.Every_Mon_Wed_and_Fri;
case Frequency.EveryTuTh: return Strings.Every_Tue_and_Thu;
case Frequency.EveryWkday: return Strings.Every_Week_Day;
case Frequency.EveryShift: return Strings.Every_shift;
case Frequency.Monthly: return Strings.Every_Month;
case Frequency.Once: return Strings.Once;
case Frequency.Weekly: return Strings.Every_week;
case Frequency.Yearly: return Strings.Every_year;
default: return string.Empty;
}
}
public static Frequency String2Freq(string fstr)
{
for (Frequency f = Frequency.Once; f < Frequency.Count; f++)
{
if (Freq2String(f) == fstr) return f;
}
return Frequency.Undefined;
}
/// <summary>
/// Determine whether an event date matches given day and time (dt)
/// (whether an event needs to be rendered on a specific day).
/// </summary>
/// <param name="evnt">Calendar event</param>
/// <param name="dt">Day and time</param>
/// <returns>true = needs to be rendered</returns>
public static bool DayMatchesExactly(ICalendarEvent evnt, DateTime dt)
{
DayOfWeek dow = dt.DayOfWeek;
switch (evnt.Frequency)
{
case Frequency.EveryShift: return (dow == DayOfWeek.Sunday && evnt.Date.Hour >= 22) ||
dow == DayOfWeek.Monday ||
dow == DayOfWeek.Tuesday ||
dow == DayOfWeek.Wednesday ||
dow == DayOfWeek.Thursday ||
(dow == DayOfWeek.Friday && evnt.Date.Hour < 22);
case Frequency.Daily: return true;
case Frequency.EveryMoWeFr: return (dow == DayOfWeek.Monday) || (dow == DayOfWeek.Wednesday) || (dow == DayOfWeek.Friday);
case Frequency.EveryTuTh: return (dow == DayOfWeek.Tuesday) || (dow == DayOfWeek.Thursday);
case Frequency.EveryWkday: return (dow != DayOfWeek.Sunday) && (dow != DayOfWeek.Saturday);
case Frequency.Weekly: return evnt.Date.DayOfWeek == dow;
case Frequency.Monthly: return evnt.Date.Day == dt.Day;
case Frequency.Yearly: return evnt.Date.Month == dt.Month && evnt.Date.Day == dt.Day;
case Frequency.Once: return evnt.Date.Year == dt.Year && evnt.Date.Month == dt.Month && evnt.Date.Day == dt.Day;
case Frequency.Custom: return (evnt.CustomRecurringFunction != null) && evnt.CustomRecurringFunction(evnt, dt);
default: return false;
}
}
public static bool IsCalendarEventTrigerred(ICalendarEvent evnt, DateTime currentDate)
{
DateTime evntDT = evnt.AllDay
? new DateTime(evnt.Date.Year, evnt.Date.Month, evnt.Date.Day, 0, 0, 0)
: evnt.Date;
if (evnt.Frequency == Frequency.EveryShift)
{
DateTime dt1 = evntDT;
DateTime dt2 = evntDT + new TimeSpan(8, 0, 0);
DateTime dt3 = evntDT + new TimeSpan(16, 0, 0);
return (currentDate >= dt1) || (currentDate >= dt1) || (currentDate >= dt3);
}
else if (!evnt.TriggerOnExactDayOnly)
{
/// Trigger after event expires
return (currentDate >= evntDT);
}
else
{
/// Trigger event on exact date only
return (currentDate >= evntDT) && DayMatchesExactly(evnt, currentDate);
}
}
/// <summary>
/// Called to update event.Date after an event was trigerred and a required action was done.
/// </summary>
/// <param name="evnt">Calendar event</param>
/// <param name="currentDate">Current date and time which triggered the event</param>
/// <returns>true when event.Date was updated</returns>
public static bool UpdateRecurringDate(ICalendarEvent evnt, DateTime currentDate)
{
switch (evnt.Frequency)
{
case Frequency.Yearly:
do {
evnt.Date = evnt.Date.AddYears(1);
}
while (evnt.Date <= currentDate);
return true;
case Frequency.Monthly:
do {
evnt.Date = evnt.Date.AddMonths(1);
}
while (evnt.Date <= currentDate);
return true;
case Frequency.Weekly:
do {
evnt.Date += new TimeSpan(7, 0, 0, 0); /// add one week
}
while (evnt.Date <= currentDate);
return true;
case Frequency.EveryTuTh:
do {
evnt.Date += new TimeSpan(1, 0, 0, 0); /// add one day
}
while ((evnt.Date <= currentDate) || ((evnt.Date.DayOfWeek != DayOfWeek.Tuesday) &&
(evnt.Date.DayOfWeek != DayOfWeek.Thursday)));
return true;
case Frequency.EveryMoWeFr:
do {
evnt.Date += new TimeSpan(1, 0, 0, 0); /// add one day
}
while ((evnt.Date <= currentDate) || ((currentDate.DayOfWeek != DayOfWeek.Monday) &&
(currentDate.DayOfWeek != DayOfWeek.Wednesday) &&
(currentDate.DayOfWeek != DayOfWeek.Friday)));
return true;
case Frequency.EveryWkday:
do {
evnt.Date += new TimeSpan(1, 0, 0, 0); /// add one day
}
while ((evnt.Date <= currentDate) || (currentDate.DayOfWeek == DayOfWeek.Saturday) || (currentDate.DayOfWeek == DayOfWeek.Sunday));
return true;
case Frequency.Daily:
do {
evnt.Date += new TimeSpan(1, 0, 0, 0); /// add one day
}
while (evnt.Date <= currentDate);
return true;
case Frequency.EveryShift:
do {
evnt.Date += new TimeSpan(8, 0, 0); /// add eight hours
}
while (evnt.Date <= currentDate);
return true;
default:
/// This is not a recurring calendar event, 'Date' was not updated
return false;
}
}
}
}