Commits

Joel Ross committed 8837c98

updated how indicators are turned on and off

Comments (0)

Files changed (7)

src/Core/Eventing/Events/IndicatorsChanged.cs

+namespace RossCode.TrafficLight.Core.Eventing.Events
+{
+    public class IndicatorsChanged : IDomainEvent { }
+}

src/Core/Presenters/SettingsPresenter.cs

 using RossCode.TrafficLight.Core.Domain.Repositories;
+using RossCode.TrafficLight.Core.Eventing;
+using RossCode.TrafficLight.Core.Eventing.Events;
 using RossCode.TrafficLight.Core.Presenters.Views;
 
 namespace RossCode.TrafficLight.Core.Presenters
         {
             indicatorTypeRepository.EnableIndicators(View.GetIndicatoryTypes());
             View.Close();
+            DomainEvents.Raise(new IndicatorsChanged());
         }
 
         private void LoadConfiguration()

src/Core/RossCode.TrafficLight.Core.csproj

     <Compile Include="Eventing\DomainEvent.cs" />
     <Compile Include="Eventing\Events\BuildStatusChanged.cs" />
     <Compile Include="Eventing\Events\EditProjectRequested.cs" />
+    <Compile Include="Eventing\Events\IndicatorsChanged.cs" />
     <Compile Include="Eventing\Events\ProjectAdded.cs" />
     <Compile Include="Eventing\Handles.cs" />
     <Compile Include="Eventing\IDomainEvent.cs" />

src/Core/Services/Indicators/BuildIndicatorService.cs

 using System.Collections.Generic;
+using RossCode.TrafficLight.Core.Domain;
 using RossCode.TrafficLight.Core.Domain.Repositories;
+using RossCode.TrafficLight.Core.Eventing;
+using RossCode.TrafficLight.Core.Eventing.Events;
 
 namespace RossCode.TrafficLight.Core.Services.Indicators
 {
     public interface IBuildIndicatorService
     {
-        void StartIndicators();
+        void StartIndicators(BuildStatus currentStatus = BuildStatus.Unknown);
         void ShutDownIndicators();
     }
 
     {
         private readonly IIndicatorTypeRepository indicatorTypeRepository;
         private readonly IList<IBuildStatusIndicatorService> services = new List<IBuildStatusIndicatorService>();
+        private BuildStatus currentBuildStatus = BuildStatus.Unknown;
 
         public BuildIndicatorService(IIndicatorTypeRepository indicatorTypeRepository)
         {
             this.indicatorTypeRepository = indicatorTypeRepository;
+            DomainEvents.Register<IndicatorsChanged>(IndicatorsChanged);
+            DomainEvents.Register<BuildStatusChanged>(BuildStatusChanged);
         }
 
-        public void StartIndicators()
+        private void BuildStatusChanged(BuildStatusChanged args)
         {
+            currentBuildStatus = args.Status;
+        }
+
+        private void IndicatorsChanged(IndicatorsChanged args)
+        {
+            ShutDownIndicators();
+            StartIndicators(currentBuildStatus);
+        }
+
+        public void StartIndicators(BuildStatus currentStatus = BuildStatus.Unknown)
+        {
+            services.Clear();
             var indicatorTypes = indicatorTypeRepository.GetAll();
 
             foreach (var indicatorType in indicatorTypes)
             }
             foreach (var service in services)
             {
-                service.Start();
+                service.Start(currentStatus);
             }
         }
 

src/Core/Services/Indicators/BusyLightBuildStatusIndicatorService.cs

             {
                 { BuildStatus.Success, adapter => adapter.TurnGreen() },
                 { BuildStatus.Building, adapter => adapter.TurnYellow() },
-                { BuildStatus.Failing, adapter => adapter.TurnRed() }
+                { BuildStatus.Failing, adapter => adapter.TurnRed() },
+                { BuildStatus.Unknown, adapter => adapter.TurnOff() }
             };
 
         public BusyLightBuildStatusIndicatorService(IBusylightAdapter busylightAdapter)
             this.busylightAdapter = busylightAdapter;
         }
 
-        public void Start()
+        public void Start(BuildStatus currentStatus)
         {
             DomainEvents.Register<BuildStatusChanged>(Handle);
-            busylightAdapter.TurnOff();
+            setBusylightStatus[currentStatus](busylightAdapter);
         }
 
         private void Handle(BuildStatusChanged args)
 
         public void Stop()
         {
+            DomainEvents.Unregister<BuildStatusChanged>(Handle);
             busylightAdapter.TurnOff();
         }
 

src/Core/Services/Indicators/DelcomTrafficLightBuildStatusIndicatorService.cs

 using System;
+using System.Collections.Generic;
 using System.Text;
 using Microsoft.Win32;
 using RossCode.TrafficLight.Core.Domain;
         private uint deviceHandle;
         private BuildStatus currentBuildStatus;
 
+        private IDictionary<BuildStatus, Action> setBuildStatus;
+
+        public DelcomTrafficLightBuildStatusIndicatorService()
+        {
+            setBuildStatus = new Dictionary<BuildStatus, Action>
+                {
+                    {BuildStatus.Unknown, ClearDisplayState},
+                    {BuildStatus.Building, () => Delcom.DelcomLEDControl(deviceHandle, Delcom.YELLOWLED, Delcom.LEDON)},
+                    {BuildStatus.Failing, () => Delcom.DelcomLEDControl(deviceHandle, Delcom.REDLED, Delcom.LEDON)},
+                    {BuildStatus.Success, () => Delcom.DelcomLEDControl(deviceHandle, Delcom.GREENLED, Delcom.LEDON)}
+                };
+        }
+
         public void Stop()
         {
             try
             {
+                DomainEvents.Unregister<BuildStatusChanged>(Handle);
                 ClearDisplayState();
                 Delcom.DelcomCloseDevice(deviceHandle);
             }
             catch { }
         }
 
-        public void Start()
+        public void Start(BuildStatus currentStatus)
         {
             var deviceName = new StringBuilder(Delcom.MAXDEVICENAMELEN);
 
                 if (result == 0) return;
 
                 deviceHandle = Delcom.DelcomOpenDevice(deviceName, 0);
-                SetBuildStatus(BuildStatus.Unknown);
+                SetBuildStatus(currentStatus);
                 DomainEvents.Register<BuildStatusChanged>(Handle);
 
                 SystemEvents.PowerModeChanged += PowerModeChanged;
 
         public void SetBuildStatus(BuildStatus buildStatus)
         {
-            if (currentBuildStatus != buildStatus)
-            {
-                ClearDisplayState();
-                currentBuildStatus = buildStatus;
-            }
+            currentBuildStatus = buildStatus;
             ClearDisplayState();
-            switch (buildStatus)
-            {
-                case BuildStatus.Building:
-                    Delcom.DelcomLEDControl(deviceHandle, Delcom.YELLOWLED, Delcom.LEDON);
-                    break;
-
-                case BuildStatus.Failing:
-                    Delcom.DelcomLEDControl(deviceHandle, Delcom.REDLED, Delcom.LEDON);
-                    break;
-
-                case BuildStatus.Success:
-                    Delcom.DelcomLEDControl(deviceHandle, Delcom.GREENLED, Delcom.LEDON);
-                    break;
-            }
+            setBuildStatus[buildStatus]();
         }
 
         private void ClearDisplayState()
 
     public interface IBuildStatusIndicatorService
     {
-        void Start();
+        void Start(BuildStatus currentStatus);
         void Stop();
     }
 }

src/UI/TrafficLightApplicationContext.cs

             menuIndex++;
 
             showSettingsMenuItem.Index = menuIndex;
-            showSettingsMenuItem.Text = "Show Se&showBuildStatusMenuItemttings";
+            showSettingsMenuItem.Text = "Show Se&ttings";
             showSettingsMenuItem.Click += (sender, args) => ActivateForm<SettingsPresenter, ISettingsView>();
             menuIndex++;