Advertisement
sangueroots

schedule

Mar 17th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. public enum IntervaloEnum
  2. {
  3.     InicioJornada,
  4.     InicioIntervalo,
  5.     FimIntervalo,
  6.     FimJornada
  7. }
  8.  
  9. public class Intervalo
  10. {
  11.     public int Hora { get; set; }
  12.     public int Minutos { get; set; }
  13.     public int Segundos { get; set; }
  14.     public IntervaloEnum Descricao { get; set; }
  15.  
  16.     private DateTime TimeNow { get
  17.         {
  18.             return DateTime.Now;
  19.         }
  20.     }
  21.    
  22.     public DateTime Data
  23.     {
  24.         get{
  25.             return new DateTime(TimeNow.Year, TimeNow.Month, TimeNow.Day, Hora, Minutos, Segundos);
  26.         }
  27.     }
  28.    
  29.     public List<Intervalo> ListTime()
  30.     {
  31.    
  32.         var rnd = new Random();
  33.         var list = new List<Intervalo>
  34.         {
  35.             new Intervalo{
  36.                 Hora =  rnd.Next(9, 11),
  37.                 Minutos = (Hora == 9)? rnd.Next(1,59) : rnd.Next(1,10),
  38.                 Segundos = rnd.Next(1,59),
  39.                 Descricao = IntervaloEnum.InicioJornada
  40.             },
  41.             new Intervalo
  42.             {
  43.                 Hora = rnd.Next(12,14),
  44.                 Minutos = (Hora == 12)? rnd.Next(1,59) : rnd.Next(1,10),
  45.                 Segundos = rnd.Next(1,59),
  46.                 Descricao = IntervaloEnum.InicioIntervalo
  47.             },
  48.             new Intervalo
  49.             {
  50.                 Hora = 13,
  51.                 Minutos = (Hora == 12)? rnd.Next(1,59) : rnd.Next(1,30),
  52.                 Segundos = rnd.Next(1,59),
  53.                 Descricao = IntervaloEnum.FimIntervalo
  54.             },
  55.             new Intervalo
  56.             {
  57.                 Hora = rnd.Next(18,19),
  58.                 Minutos = (Hora == 18)? rnd.Next(1,59) : rnd.Next(1,30),
  59.                 Segundos = rnd.Next(1,59),
  60.                 Descricao = IntervaloEnum.FimJornada
  61.             }
  62.         };
  63.        
  64.         var modificadorFimJornada = list.Where(x => x.Descricao == IntervaloEnum.InicioJornada).FirstOrDefault().Hora;
  65.         if(modificadorFimJornada == 10){
  66.             list.Where(x => x.Descricao == IntervaloEnum.FimJornada).FirstOrDefault().Hora = 19;
  67.         }
  68.        
  69.         var modificadorFimIntervalo = list.Where(x => x.Descricao == IntervaloEnum.InicioIntervalo).FirstOrDefault().Hora;
  70.         if(modificadorFimIntervalo == 13){
  71.             list.Where(x => x.Descricao == IntervaloEnum.FimIntervalo).FirstOrDefault().Hora = 14;
  72.         }
  73.        
  74.         return list;
  75.     }
  76. }
  77.  
  78.     public class ScheduleHelper
  79.     {
  80.         static System.Timers.Timer timer;
  81.         private static Intervalo _intervalo;
  82.        
  83.  
  84.         public ScheduleHelper()
  85.         {
  86.             _intervalo = new Intervalo();
  87.         }
  88.  
  89.         public void Run()
  90.         {
  91.             DateTime now = DateTime.Now;
  92.             Console.WriteLine($"Iniciando schedule em:{now}");
  93.             if (!(now.DayOfWeek == DayOfWeek.Sunday || now.DayOfWeek == DayOfWeek.Saturday))
  94.                 schedule_Timer_PortalRh();
  95.         }
  96.  
  97.         static void schedule_Timer_PortalRh()
  98.         {
  99.             Console.WriteLine("..:: Timer Schedule Started ::..");
  100.             DateTime nowTime = DateTime.Now;
  101.             var intervalo = _intervalo.ListTime().Where(x => x.Data < nowTime).LastOrDefault();
  102.             Console.WriteLine($"..:: Previsão de execução as { intervalo.Hora}:{intervalo.Minutos}:{intervalo.Segundos} ::..");
  103.             DateTime scheduledTime = intervalo.Data;
  104.             if (nowTime > scheduledTime)
  105.             {
  106.                 scheduledTime = scheduledTime.AddDays(1);
  107.                 if (intervalo.Descricao == IntervaloEnum.FimJornada)
  108.                     _intervalo = new Intervalo();
  109.             }
  110.  
  111.             double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds;
  112.             timer = new System.Timers.Timer(tickTime);
  113.             timer.Elapsed += new ElapsedEventHandler(timer_Elapsed_PortalRh);
  114.             timer.Start();
  115.         }
  116.  
  117.         /// <summary>
  118.         ///
  119.         /// </summary>
  120.         /// <param name="sender"></param>
  121.         /// <param name="e"></param>
  122.         static void timer_Elapsed_PortalRh(object sender, ElapsedEventArgs e)
  123.         {
  124.             Console.WriteLine("..:: Timer Schedule Stopped ::..");
  125.             var nowTime = DateTime.Now;
  126.             timer.Stop();
  127.             Console.WriteLine($"Iniciando processo {nowTime}");
  128.  
  129.             schedule_Timer_PortalRh();
  130.         }
  131.     }
  132.  
  133.  
  134. void Main()
  135. {
  136.     var schedule = new ScheduleHelper();
  137.     schedule.Run();
  138.     Console.Read();
  139. }
  140.  
  141. // Define other methods and classes here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement