/// <summary>
 /// Stops any other peripherals and processes. This gets executed before the listener is stopped. Invoked by <see cref="ManagerBase.Stop"/>.
 /// </summary>
 protected override void PreStop()
 {
     log.Info("Stop monitoring user cache events");
     this.userCache.OnUpdate -= this.HandleUpdate;
     log.Info("Broadcasting server down message to all registered users");
     IRequest request = new StatusRequest(false);
     string command = Parser.Encode(request);
     Parallel.ForEach(this.userCache.GetRegisteredUsers(), user => this.NotifyHost(user, command));
 }
 public void TestTranslateServerRequestIsUp()
 {
     IRequest request = new StatusRequest(true);
     Parser.TranslateForDasBlinkenlichten(request);
     Assert.Fail();
 }
        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 TestTranslateServerRequestGoingDown()
 {
     IRequest request = new StatusRequest(false);
     string expectedGreenCommand = "green=on\n";
     string expectedRedCommand = "red=on\n";
     string expectedYellowCommand = "yellow=on\n";
     string expectedCommand = expectedGreenCommand + expectedRedCommand + expectedYellowCommand;
     string actualCommand = Encoding.ASCII.GetString(Parser.TranslateForDasBlinkenlichten(request));
     Assert.That(actualCommand.Length, Is.EqualTo(expectedCommand.Length));
     Assert.That(actualCommand.Contains(expectedGreenCommand));
     Assert.That(actualCommand.Contains(expectedRedCommand));
     Assert.That(actualCommand.Contains(expectedYellowCommand));
 }
 public void TestTranslateForBlink1StatusRequestServerUp()
 {
     StatusRequest request = new StatusRequest(true);
     Parser.TranslateForBlink1(request, 10);
 }
 public void TestTranslateForBlink1StatusRequestServerDown()
 {
     short length = 10;
     StatusRequest request = new StatusRequest(false);
     byte[] expectedBytes = { 0x01, 0x63, 0x00, 0x00, 0xFF, 0x00, 0x64, 0x00, 0x00, 0x00 };
     byte[] actualBytes = Parser.TranslateForBlink1(request, length);
     Console.WriteLine(BitConverter.ToString(expectedBytes));
     Assert.That(actualBytes.Length, Is.EqualTo(length));
     Assert.That(actualBytes, Is.EqualTo(expectedBytes).AsCollection);
 }
 public void TestEncodeServerRequest([Values(true, false)] bool serverStatus)
 {
     StatusRequest request = new StatusRequest(serverStatus);
     string typePart = Field.RequestTypeId + Packet.FieldSeparator + ((int)RequestType.ServerStatus).ToString(CultureInfo.InvariantCulture) + Packet.CommandSeparator;
     string statusPart = Field.ServerStatus + Packet.FieldSeparator + Convert.ToInt16(serverStatus).ToString(CultureInfo.InvariantCulture);
     string expectedCommand = typePart + statusPart + 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(StatusRequest request, short featureReportByteLength)
        {
            if (!request.Status)
            {
                // Blue
                return PackBlink1FadeToColorBytes(0, 0, 255, FadeMilliseconds, featureReportByteLength);
            }

            throw new InvalidTranslationRequestException(string.Format("The request type is not one that can be translated: {0}", request.GetType()));
        }