Source

TrafficLight / src / Core / Services / Indicators / BusyLightBuildStatusIndicatorService.cs

Full commit
using System;
using System.Collections.Generic;
using RossCode.TrafficLight.Core.Adapters;
using RossCode.TrafficLight.Core.Domain;
using RossCode.TrafficLight.Core.Eventing;
using RossCode.TrafficLight.Core.Eventing.Events;

namespace RossCode.TrafficLight.Core.Services.Indicators
{
    public class BusyLightBuildStatusIndicatorService : IBuildStatusIndicatorService, IDisposable
    {
        private readonly IBusylightAdapter busylightAdapter;
        private readonly IDictionary<BuildStatus, Action<IBusylightAdapter>> setBusylightStatus = new Dictionary<BuildStatus, Action<IBusylightAdapter>>
            {
                { BuildStatus.Success, adapter => adapter.TurnGreen() },
                { BuildStatus.Building, adapter => adapter.TurnYellow() },
                { BuildStatus.Failing, adapter => adapter.TurnRed() },
                { BuildStatus.Unknown, adapter => adapter.TurnOff() }
            };

        public BusyLightBuildStatusIndicatorService(IBusylightAdapter busylightAdapter)
        {
            this.busylightAdapter = busylightAdapter;
        }

        public void Start(BuildStatus currentStatus)
        {
            DomainEvents.Register<BuildStatusChanged>(Handle);
            setBusylightStatus[currentStatus](busylightAdapter);
        }

        private void Handle(BuildStatusChanged args)
        {
            setBusylightStatus[args.Status](busylightAdapter);
        }

        public void Stop()
        {
            DomainEvents.Unregister<BuildStatusChanged>(Handle);
            busylightAdapter.TurnOff();
        }

        public void Dispose()
        {
            Stop();
        }
    }
}