예제 #1
0
        public void StartScheduler()
        {
            RemainingTime = null;
            CurrentAlarm  = null;
            NextAlarmIfNoAlarmsForToday = "";
            NextAlarm = null;
            RTime     = null;
            var alarm = GetAlarm(GetScheduleFromDate(DateTime.Now));

            if (alarm != null)
            {
                CurrentAlarm = alarm;
                TimeSpan alarmTimespan = Convert.ToDateTime(CurrentAlarm.Time).TimeOfDay - DateTime.Now.TimeOfDay;
                alarmTimer.Interval = Convert.ToInt32(alarmTimespan.TotalMilliseconds);
                var index =
                    CurrentAlarm.Schedule1.Alarms.OrderBy(c => c.Time)
                    .ToList()
                    .IndexOf(CurrentAlarm);
                PreviousAlarmTime = DateTime.Now;

                if (index >= 1)
                {
                    PreviousAlarmTime = CurrentAlarm.Schedule1.Alarms.OrderBy(z => z.Time).ElementAt(index - 1).Time;
                }
                alarmTimer.Start();
                EverySecondTimer.Start();
            }
            else
            {
                NextAlarm = NextPossibleAlarm();

                if (NextAlarm != null && NextAlarm.Any())
                {
                    NextAlarmIfNoAlarmsForToday = "Next Alarm At : " +
                                                  NextPossibleAlarm()
                                                  .First()
                                                  .Value
                                                  .ToString("ddd, MMM d, yyyy") + " " +
                                                  NextAlarm.First().Key.Time.ToString("h:mm:ss tt");
                    PreviousAlarmTime = DateTime.Now;
                    EverySecondTimer.Start();
                }
            }

            MidnighTimer.Interval = (int)(DateTime.Today.AddDays(1) - DateTime.Now).TotalMilliseconds;

            MidnighTimer.Start();
            var schedule = DefaultWeeklySchedule;

            DefaultWeeklySchedule = null;
            DefaultWeeklySchedule = schedule;
        }
예제 #2
0
        void EverySecondTimer_Tick(object sender, EventArgs e)
        {
            RemainingTime = null;
            RTime         = null;

            if (CurrentAlarm != null)
            {
                RemainingTime = Convert.ToDateTime(CurrentAlarm.Time).TimeOfDay - DateTime.Now.TimeOfDay;


                var endTime   = new LocalDateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, CurrentAlarm.Time.Hour, CurrentAlarm.Time.Minute, CurrentAlarm.Time.Second);
                var startTime = new LocalDateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);


                RTime = Period.Between(startTime, endTime,
                                       PeriodUnits.Years | PeriodUnits.Months | PeriodUnits.Days | PeriodUnits.Hours | PeriodUnits.Minutes | PeriodUnits.Seconds);
            }
            else
            {
                if (NextAlarm != null && NextAlarm.Any())
                {
                    var endTime = new LocalDateTime(NextAlarm.First().Value.Year, NextAlarm.First().Value.Month,
                                                    NextAlarm.First().Value.Day, NextAlarm.First().Key.Time.Hour, NextAlarm.First().Key.Time.Minute,
                                                    NextAlarm.First().Key.Time.Second);
                    var startTime = new LocalDateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                                      DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);


                    RTime = Period.Between(startTime, endTime,
                                           PeriodUnits.Years | PeriodUnits.Months | PeriodUnits.Days | PeriodUnits.Hours |
                                           PeriodUnits.Minutes | PeriodUnits.Seconds);
                }
            }


            //if (RemainingTime == null) return;
            //var date = new DateTime(NextPossibleAlarm().First().Value.AddTicks(Convert.ToDateTime(NextPossibleAlarm().First().Key.Time).TimeOfDay.Ticks).Ticks);
            //TimeSpan = string.Format("{0:### Y;-## ; }  {1:### M;-## ; }  {2:### D;-## ; } {3:### H;-## ; }  {4:### M;-## ; } {5:### S;-## ; }", date.Year,date.Month,date.Day,date.Hour,date.Minute,date.Second);
        }