Beispiel #1
0
        /// <summary>
        /// Adds a task to the scheduler
        /// </summary>
        /// <param name="task">The task to add to the scheduler</param>
        /// <returns>Returns a AddTaskResult with all relevant information</returns>
        public AddTaskResult AddTask(IoTSchedulerTask task)
        {
            Debug.WriteLine("IoTScheduler: Adding task");

            // Determine the difference in time between the desired time and now
            DateTime current  = DateTime.Now;
            TimeSpan timeToGo = task.StartTime - current.TimeOfDay;

            // If we have passed the desired time to execute, do nothing
            if (timeToGo < TimeSpan.Zero)
            {
                Debug.WriteLine("IoTScheduler: Too late, desired time has passed");
                return(new AddTaskResult());
            }

            // Add the task to the list of tasks for today
            _tasks.Add(task);

            //// Initialize the timer to call the function
            //timer = new System.Threading.Timer(x =>
            //{
            //    Debug.WriteLine("IoTScheduler: Firing task");
            //    task.FireCallback();
            //    _timers.Remove(timer);
            //}, null, timeToGo, Timeout.InfiniteTimeSpan);

            //_timers.Add(timer)

            return(new AddTaskResult());
        }
Beispiel #2
0
        // Get all tasks that will need to fire next.
        private List <IoTSchedulerTask> GetNextTasks()
        {
            // Create a variable to hold the soonest task
            IoTSchedulerTask nextTask = new IoTSchedulerTask();

            // Go through all the tasks in the stack
            foreach (IoTSchedulerTask task in _tasks)
            {
                // If this is the first item in the stack (since newTask will have null values)
                if (nextTask.StartTime == null)
                {
                    // Set the next task to the first item
                    nextTask = task;
                }

                // If this task starts before the task marked as next
                if (task.StartTime < nextTask.StartTime)
                {
                    // Make this task the next task
                    nextTask = task;
                }
            }

            // Get all tasks that fire at this time
            List <IoTSchedulerTask> allTasks = _tasks.Where(t => t.StartTime == nextTask.StartTime).ToList();

            // Return the next task
            return(allTasks);
        }