コード例 #1
0
 /// <summary>
 /// Main application entry point.
 /// </summary>
 private static void Main()
 {
     LightsManager lightsManager = null;
     try
     {
         LightsDeviceController lightsDeviceController = new LightsDeviceController(Config.GetUsbProductId(), Config.GetUsbVendorId(), Config.GetUsbUsage(), Config.GetUsbUsagePage(), Config.GetWaitForDeviceRetryPeriod(), Config.GetUsbControlTransferType());
         lightsManager = new LightsManager(lightsDeviceController, Config.GetLightsManagerPort(), Config.GetNotificationManagerHost(), Config.GetNotificationManagerPort(), Config.GetRegistrationRetryPeriod(), Config.GetUsbProtocolType());
         Thread thread = new Thread(lightsManager.Start);
         thread.Start();
         Console.WriteLine("Press any key to terminate...");
         Console.ReadKey(true);
         lightsManager.Stop();
         thread.Join();
         Console.WriteLine("Press any key to close...");
         Console.ReadKey(true);
     }
     catch (Exception e)
     {
         Console.Error.WriteLine(e);
         Console.ReadKey(true);
     }
     finally
     {
         if (lightsManager != null)
         {
             lightsManager.Dispose();
         }
     }
 }
 public void TestStopAndStopAgain()
 {
     LightsManager lightsManager = null;
     try
     {
         LightsDeviceController lightsDeviceController = new LightsDeviceController(0, 0, 0, 0);
         lightsManager = new LightsManager(lightsDeviceController);
         lightsManager.Start();
         Assert.That(lightsManager.Running, NUnit.Framework.Is.True);
         lightsManager.Stop();
         Assert.That(lightsManager.Running, NUnit.Framework.Is.False);
         lightsManager.Stop();
         Assert.That(lightsManager.Running, NUnit.Framework.Is.False);
     }
     finally
     {
         if (lightsManager != null)
         {
             lightsManager.Dispose();
         }
     }
 }
        public void TestBlink1NotifyUsbDeviceTestAllRequests()
        {
            LightsManager lightsManager = null;
            try
            {
                // Mocks
                Mock<LightsDeviceController> lightsDeviceController = this.mockFactory.CreateMock<LightsDeviceController>();

                // Expectations
                lightsDeviceController.Expects.AtLeastOne.Method(x => x.GetFeatureReportByteLength()).WillReturn(10);
                lightsDeviceController.Expects.AtLeastOne.Method(x => x.SendCommand(new byte[10])).WithAnyArguments().WillReturn(LightsDeviceResult.Ack);

                // Run and test
                lightsManager = new LightsManager(lightsDeviceController.MockObject, UsbProtocolType.Blink1);

                // Build activity
                LightsDeviceResult buildActiveRequestResult = lightsManager.NotifyLightsDevice(new BuildActiveRequest(true));
                Assert.That(buildActiveRequestResult, NUnit.Framework.Is.EqualTo(LightsDeviceResult.Ack));
                buildActiveRequestResult = lightsManager.NotifyLightsDevice(new BuildActiveRequest(false));
                Assert.That(buildActiveRequestResult, NUnit.Framework.Is.EqualTo(LightsDeviceResult.Ack));

                // Attention
                LightsDeviceResult attentionRequestResult = lightsManager.NotifyLightsDevice(new AttentionRequest(true));
                Assert.That(attentionRequestResult, NUnit.Framework.Is.EqualTo(LightsDeviceResult.Ack));
                attentionRequestResult = lightsManager.NotifyLightsDevice(new AttentionRequest(false));
                Assert.That(attentionRequestResult, NUnit.Framework.Is.EqualTo(LightsDeviceResult.Ack));
                attentionRequestResult = lightsManager.NotifyLightsDevice(new AttentionRequest(true, true));
                Assert.That(attentionRequestResult, NUnit.Framework.Is.EqualTo(LightsDeviceResult.Ack));

                // System status
                LightsDeviceResult statusRequestRequestResult = lightsManager.NotifyLightsDevice(new StatusRequest(true));
                Assert.That(statusRequestRequestResult, NUnit.Framework.Is.EqualTo(LightsDeviceResult.Ack));
                statusRequestRequestResult = lightsManager.NotifyLightsDevice(new StatusRequest(false));
                Assert.That(statusRequestRequestResult, NUnit.Framework.Is.EqualTo(LightsDeviceResult.Ack));
            }
            finally
            {
                if (lightsManager != null)
                {
                    lightsManager.Dispose();
                }
            }
        }
        public void TestNotifyUsbDeviceRespondsToBuildAttentionRequest()
        {
            LightsManager lightsManager = null;
            try
            {
                // Mocks
                Mock<IHidDevice> mockDevice = this.mockFactory.CreateMock<IHidDevice>();

                // Expectations
                mockDevice.Expects.AtLeastOne.GetProperty(x => x.IsConnected).WillReturn(false);

                // Run
                LightsDeviceController lightsDeviceController = new LightsDeviceController(0, 0, 0, 0, 1000, UsbControlTransferType.Raw) {
                                                                                                                                                 Device = mockDevice.MockObject
                                                                                                                                         };
                lightsManager = new LightsManager(lightsDeviceController, UsbProtocolType.DasBlinkenlichten);
                AttentionRequest request = new AttentionRequest(true);

                // Test
                Assert.That(lightsManager.NotifyLightsDevice(request), NUnit.Framework.Is.EqualTo(LightsDeviceResult.NotConnected));
                this.mockFactory.VerifyAllExpectationsHaveBeenMet();
            }
            finally
            {
                if (lightsManager != null)
                {
                    lightsManager.Dispose();
                }
            }
        }
        public void TestNotifyUsbDeviceRespondsNak()
        {
            LightsManager lightsManager = null;
            try
            {
                // Mocks
                Mock<IHidDevice> mockDevice = this.mockFactory.CreateMock<IHidDevice>();
                byte[] mockDataBytes = Encoding.ASCII.GetBytes(UsbResponse.Nak);
                HidDeviceData mockData = new HidDeviceData(mockDataBytes, HidDeviceData.ReadStatus.Success);
                HidReport mockReport = new HidReport(1, mockData);

                // Expectations
                mockDevice.Expects.AtLeastOne.GetProperty(x => x.IsConnected).WillReturn(true);
                using (this.mockFactory.Ordered)
                {
                    mockDevice.Expects.One.GetProperty(x => x.IsOpen).WillReturn(false);
                    mockDevice.Expects.AtLeastOne.GetProperty(x => x.IsOpen).WillReturn(true);
                }

                mockDevice.Expects.AtLeastOne.Method(x => x.OpenDevice());
                mockDevice.Expects.AtLeastOne.Method(x => x.CreateReport()).WillReturn(mockReport);
                mockDevice.Expects.AtLeastOne.Method(x => x.WriteReport(null)).WithAnyArguments().WillReturn(true);
                mockDevice.Expects.AtLeastOne.Method(x => x.Read()).WillReturn(mockData);

                // Run
                LightsDeviceController lightsDeviceController = new LightsDeviceController(0, 0, 0, 0, 1000, UsbControlTransferType.Raw) {
                                                                                                                                                 Device = mockDevice.MockObject
                                                                                                                                         };
                lightsManager = new LightsManager(lightsDeviceController, UsbProtocolType.DasBlinkenlichten);
                BuildActiveRequest buildActiveRequest = new BuildActiveRequest(true);

                // Test
                Assert.That(lightsManager.NotifyLightsDevice(buildActiveRequest), NUnit.Framework.Is.EqualTo(LightsDeviceResult.Nak));
                this.mockFactory.VerifyAllExpectationsHaveBeenMet();
            }
            finally
            {
                if (lightsManager != null)
                {
                    lightsManager.Dispose();
                }
            }
        }
        public void TestNotifyUsbDeviceIsNullRespondsNotConnected()
        {
            // Run
            LightsDeviceController lightsDeviceController = new LightsDeviceController(0, 0, 0, 0, 1000, UsbControlTransferType.Raw) {
                                                                                                                                             Device = null
                                                                                                                                     };
            LightsManager lightsManager = null;
            try
            {
                lightsManager = new LightsManager(lightsDeviceController, UsbProtocolType.DasBlinkenlichten);
                BuildActiveRequest buildActiveRequest = new BuildActiveRequest(true);

                // Test
                Assert.That(lightsManager.NotifyLightsDevice(buildActiveRequest), NUnit.Framework.Is.EqualTo(LightsDeviceResult.NotConnected));
            }
            finally
            {
                if (lightsManager != null)
                {
                    lightsManager.Dispose();
                }
            }
        }
        public void TestHandleCommandForServerDownRequest()
        {
            // Mocks
            Mock<ILightsDeviceController> mockLightsDeviceController = this.mockFactory.CreateMock<ILightsDeviceController>();
            mockLightsDeviceController.Expects.One.Method(x => x.Start());
            mockLightsDeviceController.Expects.One.Method(x => x.Stop());
            mockLightsDeviceController.IgnoreUnexpectedInvocations = true;

            // One for HandleCommand; one for Stop
            mockLightsDeviceController.Expects.Exactly(2).Method(x => x.SendCommand(null)).WithAnyArguments().WillReturn(LightsDeviceResult.Ack);

            // Run
            StatusRequest request = new StatusRequest(false);
            LightsManager lightsManager = null;
            try
            {
                lightsManager = new LightsManager(mockLightsDeviceController.MockObject, UsbProtocolType.DasBlinkenlichten);
                lightsManager.Start();
                Assert.That(lightsManager.Running, NUnit.Framework.Is.True);
                lightsManager.HandleCommand(request, EventArgs.Empty);
                lightsManager.Stop();
                Assert.That(lightsManager.Running, NUnit.Framework.Is.False);
            }
            finally
            {
                if (lightsManager != null)
                {
                    lightsManager.Dispose();
                }
            }

            this.mockFactory.VerifyAllExpectationsHaveBeenMet();
        }
        public void TestHandleCommandForAllLightsDeviceResults([Values(LightsDeviceResult.Ack, LightsDeviceResult.Nak, LightsDeviceResult.NoResponse, LightsDeviceResult.NotConnected, LightsDeviceResult.NotOpen)] LightsDeviceResult result)
        {
            this.mockFactory.ClearExpectations();

            // Mocks
            Mock<ILightsDeviceController> mockLightsDeviceController = this.mockFactory.CreateMock<ILightsDeviceController>();
            mockLightsDeviceController.Expects.One.Method(x => x.SendCommand(null)).WithAnyArguments().WillReturn(result);

            // Any request that steps into the send command logic is sufficient
            AttentionRequest request = new AttentionRequest(true);

            // Run
            LightsManager lightsManager = null;
            try
            {
                lightsManager = new LightsManager(mockLightsDeviceController.MockObject, UsbProtocolType.DasBlinkenlichten);
                lightsManager.HandleCommand(request, EventArgs.Empty);
            }
            finally
            {
                if (lightsManager != null)
                {
                    lightsManager.Dispose();
                }
            }

            // Ensure a clean end to the test
            this.mockFactory.VerifyAllExpectationsHaveBeenMet();
        }