Source

TrafficLight / src / Core / Services / ApplicationService.cs

using System;
using System.Linq;
using RossCode.TrafficLight.Core.Domain.Repositories;
using RossCode.TrafficLight.Core.Utilities;

namespace RossCode.TrafficLight.Core.Services
{
    public interface IApplicationService
    {
        void StartMonitoring();
    }

    public class ApplicationService : IApplicationService
    {
        private readonly IProjectRepository projectRepository;
        private readonly IProjectService projectService;
        private readonly ITimer timer;

        public ApplicationService(IProjectRepository projectRepository, IProjectService projectService, ITimer timer)
        {
            this.projectRepository = projectRepository;
            this.projectService = projectService;
            this.timer = timer;
        }

        public void StartMonitoring() 
        {
            projectService.CheckAndSetStatusesFor(projectRepository.GetAll().ToList());

            timer.TimerElapsed += TimerElapsed;
            timer.IntervalInSeconds = 15;
            timer.Start();
        }

        private void TimerElapsed(object sender, EventArgs e)
        {
            timer.Stop();
            projectService.CheckAndSetStatusesFor(projectRepository.GetAll().ToList());
            timer.Start();
        }
    }
}