/// <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 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 TestNotifyUsbDeviceRespondsToBuildActiveRequest() { 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); BuildActiveRequest buildActiveRequest = new BuildActiveRequest(true); // Test Assert.That(lightsManager.NotifyLightsDevice(buildActiveRequest), NUnit.Framework.Is.EqualTo(LightsDeviceResult.NotConnected)); 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 TestTranslateForBlink1BuildsActive() { short length = 10; BuildActiveRequest request = new BuildActiveRequest(true); byte[] expectedBytes = { 0x01, 0x63, 0xFF, 0x65, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00 }; byte[] actualBytes = Parser.TranslateForBlink1(request, length); Assert.That(actualBytes.Length, Is.EqualTo(length)); Assert.That(actualBytes, Is.EqualTo(expectedBytes).AsCollection); }
public void TestTranslateBuildActiveRequestIsInactive() { IRequest request = new BuildActiveRequest(false); string expectedCommand = "yellow=off\n"; byte[] expectedBytes = Encoding.ASCII.GetBytes(expectedCommand); byte[] actualBytes = Parser.TranslateForDasBlinkenlichten(request); Assert.That(actualBytes, Is.EqualTo(expectedBytes)); }
public void TestEncodeBuildActiveRequest([Values(true, false)] bool buildsActive) { IRequest request = new BuildActiveRequest(buildsActive); string typePart = Field.RequestTypeId + Packet.FieldSeparator + ((int)RequestType.BuildActive).ToString(CultureInfo.InvariantCulture) + Packet.CommandSeparator; string buildActivePart = Field.BuildsActive + Packet.FieldSeparator + Convert.ToInt16(buildsActive).ToString(CultureInfo.InvariantCulture); string expectedCommand = typePart + buildActivePart + 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(BuildActiveRequest request, short featureReportByteLength) { if (request.IsBuildsActive) { // Yellow return PackBlink1FadeToColorBytes(255, 210, 0, FadeMilliseconds, featureReportByteLength); } throw new InvalidTranslationRequestException(string.Format("The request type is not one that can be translated: {0}", request.GetType())); }