/// <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 TestManualBlinkingSequence()
        {
            LightsDeviceController controller = new LightsDeviceController(Config.GetUsbProductId(), Config.GetUsbVendorId(), Config.GetUsbUsage(), Config.GetUsbUsagePage(), Config.GetWaitForDeviceRetryPeriod(), Config.GetUsbControlTransferType());
            controller.Start();
            int sleepTime = 150;
            int iterations = 10;
            Thread.Sleep(sleepTime);
            Console.WriteLine("Start");
            string command = "red=on\ngreen=on\nyellow=on\n";
            Console.WriteLine(string.Format("Send: {0}", command.Replace('\n', '|')));
            Console.WriteLine(string.Format("Recv: {0}", controller.SendCommand(Encoding.ASCII.GetBytes(command))));
            Thread.Sleep(sleepTime);
            for (int i = 0; i < iterations; i++)
            {
                Console.WriteLine(string.Format("Iteration {0}", i));
                command = "red=on\ngreen=off\nyellow=off\n";
                Console.WriteLine(string.Format("\tSend: {0}", command.Replace('\n', '|')));
                Console.WriteLine(string.Format("\tRecv: {0}", controller.SendCommand(Encoding.ASCII.GetBytes(command))));
                Thread.Sleep(sleepTime);
                command = "red=off\ngreen=on\nyellow=off\n";
                Console.WriteLine(string.Format("\tSend: {0}", command.Replace('\n', '|')));
                Console.WriteLine(string.Format("\tRecv: {0}", controller.SendCommand(Encoding.ASCII.GetBytes(command))));
                Thread.Sleep(sleepTime);
                command = "red=off\ngreen=off\nyellow=on\n";
                Console.WriteLine(string.Format("\tSend: {0}", command.Replace('\n', '|')));
                Console.WriteLine(string.Format("\tRecv: {0}", controller.SendCommand(Encoding.ASCII.GetBytes(command))));
                Thread.Sleep(sleepTime);
            }

            Console.WriteLine("Stop");
            command = "red=on\ngreen=on\nyellow=on\n";
            Console.WriteLine(string.Format("\tSend: {0}", command.Replace('\n', '|')));
            Console.WriteLine(string.Format("\tRecv: {0}", controller.SendCommand(Encoding.ASCII.GetBytes(command))));
            Thread.Sleep(sleepTime);
        }
 public void TestGetFeatureReportByteLengthForNoDevice()
 {
     LightsDeviceController lightsDeviceController = new LightsDeviceController();
     Assert.That(lightsDeviceController.GetFeatureReportByteLength(), NUnit.Framework.Is.EqualTo(-1));
 }
        public void TestUsbControlTransferTypeFeatureReport()
        {
            // Setup
            LightsDeviceController lightsDeviceController = new LightsDeviceController(0, 0, 0, 0, 1000, UsbControlTransferType.FeatureReport);
            Mock<IHidDevice> mockDevice = this.mockFactory.CreateMock<IHidDevice>();
            mockDevice.Expects.AtLeastOne.GetProperty(p => p.IsConnected).WillReturn(true);
            mockDevice.Expects.AtLeastOne.GetProperty(p => p.IsOpen).WillReturn(true);
            mockDevice.Expects.One.Method(m => m.WriteFeatureData(new byte[0])).WithAnyArguments().WillReturn(true);
            mockDevice.Expects.One.Method(m => m.WriteFeatureData(new byte[0])).WithAnyArguments().WillReturn(false);
            lightsDeviceController.Device = mockDevice.MockObject;

            // Test
            Assert.That(lightsDeviceController.SendCommand(new byte[0]), NUnit.Framework.Is.EqualTo(LightsDeviceResult.Ack));
            Assert.That(lightsDeviceController.SendCommand(new byte[0]), NUnit.Framework.Is.EqualTo(LightsDeviceResult.Nak));
        }
        public void TestUnsupportedUsbControlTransferTypeGetsThrown()
        {
            // Setup
            LightsDeviceController lightsDeviceController = new LightsDeviceController(0, 0, 0, 0, 1000, UsbControlTransferType.None);
            Mock<IHidDevice> mockDevice = this.mockFactory.CreateMock<IHidDevice>();
            mockDevice.Expects.AtLeastOne.GetProperty(p => p.IsConnected).WillReturn(true);
            mockDevice.Expects.AtLeastOne.GetProperty(p => p.IsOpen).WillReturn(true);
            lightsDeviceController.Device = mockDevice.MockObject;

            // Test
            lightsDeviceController.SendCommand(new byte[0]);
            Assert.Fail();
        }
        public void TestStartAlreadyStarted()
        {
            // Setup
            LightsDeviceController lightsDeviceController = new LightsDeviceController();
            Assert.That(lightsDeviceController.Running, NUnit.Framework.Is.False);
            lightsDeviceController.Start();
            Assert.That(lightsDeviceController.Running, NUnit.Framework.Is.True);

            // Test
            lightsDeviceController.Start();
            Assert.That(lightsDeviceController.Running, NUnit.Framework.Is.True);

            // Clean-up
            lightsDeviceController.Stop();
            Assert.That(lightsDeviceController.Running, NUnit.Framework.Is.False);
        }
        public void TestManualSequenceLightsDeviceBlink1Device()
        {
            LightsDeviceController controller = new LightsDeviceController(0x01ED, 0x27B8, 0x0001, 0xFF00, Config.GetWaitForDeviceRetryPeriod(), UsbControlTransferType.FeatureReport);
            controller.Start();
            Assert.IsTrue(controller.Running);
            int sleepTimeout = 1000;
            List<IRequest> requests = new List<IRequest>();

            // B-Y-R-Y-R-Y-G-B
            requests.Add(new StatusRequest(false));
            requests.Add(new BuildActiveRequest(true));
            requests.Add(new AttentionRequest(true));
            requests.Add(new BuildActiveRequest(true));
            requests.Add(new AttentionRequest(true, true));
            requests.Add(new BuildActiveRequest(true));
            requests.Add(new AttentionRequest(false));
            requests.Add(new StatusRequest(false));
            Thread.Sleep(sleepTimeout);
            foreach (IRequest request in requests)
            {
                if (request.GetType() == typeof(StatusRequest))
                {
                    controller.SendCommand(Parser.TranslateForBlink1((StatusRequest)request, controller.GetFeatureReportByteLength()));
                }
                else if (request.GetType() == typeof(AttentionRequest))
                {
                    controller.SendCommand(Parser.TranslateForBlink1((AttentionRequest)request, controller.GetFeatureReportByteLength()));
                }
                else if (request.GetType() == typeof(BuildActiveRequest))
                {
                    controller.SendCommand(Parser.TranslateForBlink1((BuildActiveRequest)request, controller.GetFeatureReportByteLength()));
                }

                Thread.Sleep(sleepTimeout);
            }

            controller.Stop();
        }
        public void TestManualSequenceLightsDevice()
        {
            LightsDeviceController controller = new LightsDeviceController(Config.GetUsbProductId(), Config.GetUsbVendorId(), Config.GetUsbUsage(), Config.GetUsbUsagePage(), Config.GetWaitForDeviceRetryPeriod(), Config.GetUsbControlTransferType());
            controller.Start();
            int sleepTimeout = 3000;
            int sosSleepTimeout = 8000;
            List<IRequest> requests = new List<IRequest>();

            // RGY-RGX-RGY-RXY-XGY-RXY-SXY-XGY-RGY
            requests.Add(new StatusRequest(false));
            requests.Add(new BuildActiveRequest(false));
            requests.Add(new BuildActiveRequest(true));
            requests.Add(new AttentionRequest(true));
            requests.Add(new AttentionRequest(false));
            requests.Add(new AttentionRequest(true));
            requests.Add(new AttentionRequest(true, true));
            requests.Add(new AttentionRequest(false));
            requests.Add(new StatusRequest(false));
            Thread.Sleep(sleepTimeout);
            foreach (IRequest request in requests)
            {
                controller.SendCommand(Parser.TranslateForDasBlinkenlichten(request));
                Thread.Sleep((request.GetType() == typeof(AttentionRequest)) && ((AttentionRequest)request).IsPriority ? sosSleepTimeout : sleepTimeout);
            }

            controller.Stop();
        }
        public void TestManualInsertRemoveBlink1()
        {
            object insertedLock = new object();
            object removedLock = new object();

            LightsDeviceController controller = new LightsDeviceController(0x01ED, 0x27B8, 0x0001, 0xFF00, Config.GetWaitForDeviceRetryPeriod(), UsbControlTransferType.FeatureReport);

            controller.OnDeviceInserted += (sender, args) =>
            {
                lock (insertedLock)
                {
                    Monitor.Pulse(insertedLock);
                }
            };

            controller.OnDeviceRemoved += (sender, args) =>
            {
                lock (removedLock)
                {
                    Monitor.Pulse(removedLock);
                }
            };

            controller.Start();
            Assert.IsTrue(controller.Running);

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Please insert the device");
                lock (insertedLock)
                {
                    Monitor.Wait(insertedLock);
                }

                Console.WriteLine("Device inserted");
                controller.SendCommand(Parser.TranslateForBlink1(new AttentionRequest(false), controller.GetFeatureReportByteLength()));
                Console.WriteLine("Please remove the device");
                lock (removedLock)
                {
                    Monitor.Wait(removedLock);
                }

                Console.WriteLine("Device removed");
            }

            controller.Stop();
        }
 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 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();
                }
            }
        }