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();
                }
            }
        }
 /// <summary>
 /// Handles a cache update.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="eventArgs">The <see cref="System.EventArgs"/> instance containing the event data.</param>
 public void HandleUpdate(object sender, EventArgs eventArgs)
 {
     User user = (User)sender;
     if (user.IsBuildActive())
     {
         IRequest buildActiveRequest = new BuildActiveRequest(user.IsBuildActive());
         this.NotifyHost(user, Parser.Encode(buildActiveRequest));
     }
     else
     {
         IRequest attentionRequest = new AttentionRequest(user.IsAttentionRequired(), user.IsAttentionPriority);
         this.NotifyHost(user, Parser.Encode(attentionRequest));
     }
 }
        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();
        }
        public void TestTranslateForBlink1AttentionRequestAttentionRequired()
        {
            short length = 10;

            // Attention is required
            AttentionRequest request = new AttentionRequest(true);
            byte[] expectedBytesForRed = { 0x01, 0x63, 0xFF, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00 };
            byte[] actualBytesForRed = Parser.TranslateForBlink1(request, length);
            Assert.That(actualBytesForRed.Length, Is.EqualTo(length));
            Console.WriteLine(BitConverter.ToString(expectedBytesForRed));
            Console.WriteLine(BitConverter.ToString(actualBytesForRed));
            Assert.That(actualBytesForRed, Is.EqualTo(expectedBytesForRed).AsCollection);

            // Priority red will currently look the same as normal priority (plain attention required)
            request = new AttentionRequest(true, true);
            actualBytesForRed = Parser.TranslateForBlink1(request, length);
            Assert.That(actualBytesForRed.Length, Is.EqualTo(length));
            Assert.That(actualBytesForRed, Is.EqualTo(expectedBytesForRed).AsCollection);

            // No attention required
            request = new AttentionRequest(false);
            byte[] expectedBytesForGreen = { 0x01, 0x63, 0x00, 0xFF, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00 };
            byte[] actualBytesForGreen = Parser.TranslateForBlink1(request, length);
            Assert.That(actualBytesForGreen.Length, Is.EqualTo(length));
            Assert.That(actualBytesForGreen, Is.EqualTo(expectedBytesForGreen).AsCollection);
        }
        public void TestTranslateAttentionRequestAttentionRequiredWithPriority()
        {
            IRequest request = new AttentionRequest(true, true);
            string expectedRedCommand = "red=sos\n";
            string expectedGreenCommand = "green=off\n";

            byte[] expectedRedBytes = Encoding.ASCII.GetBytes(expectedRedCommand);
            byte[] expectedGreenBytes = Encoding.ASCII.GetBytes(expectedGreenCommand);
            string expectedRedString = Encoding.ASCII.GetString(expectedRedBytes);
            string expectedGreenString = Encoding.ASCII.GetString(expectedGreenBytes);
            int numberOfExpectedBytes = (expectedRedString + expectedGreenString).Length;
            byte[] actualBytes = Parser.TranslateForDasBlinkenlichten(request);
            string actualBytesString = Encoding.ASCII.GetString(actualBytes);

            Assert.That(actualBytesString.Length, Is.EqualTo(numberOfExpectedBytes));
            Assert.That(actualBytesString.Contains(expectedRedString));
            Assert.That(actualBytesString.Contains(expectedGreenString));
        }
 public void TestEncodeAttentionRequestWithPriority([Values(true, false)] bool attentionPriority)
 {
     IRequest request = new AttentionRequest(true, attentionPriority);
     string typePart = Field.RequestTypeId + Packet.FieldSeparator + ((int)RequestType.Attention).ToString(CultureInfo.InvariantCulture) + Packet.CommandSeparator;
     string attentionPart = Field.AttentionRequired + Packet.FieldSeparator + Convert.ToInt16(true).ToString(CultureInfo.InvariantCulture) + Packet.CommandSeparator;
     string priorityPart = Field.AttentionPriority + Packet.FieldSeparator + Convert.ToInt16(attentionPriority).ToString(CultureInfo.InvariantCulture);
     string expectedCommand = typePart + attentionPart + priorityPart + Packet.PacketTerminator;
     string actualCommand = Parser.Encode(request);
     Assert.That(actualCommand, Is.EqualTo(expectedCommand));
 }
        /// <summary>
        /// Translates a for blink1.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="featureReportByteLength">Length of the feature report byte.</param>
        /// <returns>
        /// An array of byte arrays (commands).
        /// </returns>
        public static byte[] TranslateForBlink1(AttentionRequest request, short featureReportByteLength)
        {
            if (request.IsAttentionRequired)
            {
                // Red
                // TODO: Figure out a way to handle priority requests
                return PackBlink1FadeToColorBytes(255, 0, 0, FadeMilliseconds, featureReportByteLength);
            }

            // Green
            return PackBlink1FadeToColorBytes(0, 255, 0, FadeMilliseconds, featureReportByteLength);
        }