Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Daqifi.Desktop.Test/Device/AbstractStreamingDeviceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Daqifi.Desktop.Device;
using Daqifi.Desktop.DataModel.Network;
using Daqifi.Desktop.IO.Messages.Consumers;
using Daqifi.Desktop.IO.Messages.Producers;
using Daqifi.Core.Communication.Messages;
using Moq;

namespace Daqifi.Desktop.Test.Device;

Expand Down Expand Up @@ -128,6 +132,80 @@ public void DeviceType_Property_ShouldNotifyPropertyChanged()
Assert.IsTrue(propertyChanged, "DeviceType property should notify PropertyChanged");
}

[TestMethod]
public void DetectFromPartNumber_ShouldReturnNyquist1_ForNq1()
{
// Arrange & Act
var deviceType = DeviceTypeDetector.DetectFromPartNumber("Nq1");

// Assert
Assert.AreEqual(DeviceType.Nyquist1, deviceType, "Should detect Nyquist1 from Nq1 part number");
}

[TestMethod]
public void DetectFromPartNumber_ShouldReturnNyquist3_ForNq3()
{
// Arrange & Act
var deviceType = DeviceTypeDetector.DetectFromPartNumber("Nq3");

// Assert
Assert.AreEqual(DeviceType.Nyquist3, deviceType, "Should detect Nyquist3 from Nq3 part number");
}

[TestMethod]
public void DetectFromPartNumber_ShouldBeCaseInsensitive()
{
// Arrange & Act
var nq1Upper = DeviceTypeDetector.DetectFromPartNumber("NQ1");
var nq1Lower = DeviceTypeDetector.DetectFromPartNumber("nq1");
var nq1Mixed = DeviceTypeDetector.DetectFromPartNumber("Nq1");

// Assert
Assert.AreEqual(DeviceType.Nyquist1, nq1Upper, "Should detect Nyquist1 from uppercase NQ1");
Assert.AreEqual(DeviceType.Nyquist1, nq1Lower, "Should detect Nyquist1 from lowercase nq1");
Assert.AreEqual(DeviceType.Nyquist1, nq1Mixed, "Should detect Nyquist1 from mixed case Nq1");
}

[TestMethod]
public void DetectFromPartNumber_ShouldReturnUnknown_ForUnrecognizedPartNumber()
{
// Arrange & Act
var deviceType = DeviceTypeDetector.DetectFromPartNumber("UnknownDevice");

// Assert
Assert.AreEqual(DeviceType.Unknown, deviceType, "Should default to Unknown for unrecognized part number");
}

[TestMethod]
public void DetectFromPartNumber_ShouldReturnUnknown_ForEmptyString()
{
// Arrange & Act
var deviceType = DeviceTypeDetector.DetectFromPartNumber("");

// Assert
Assert.AreEqual(DeviceType.Unknown, deviceType, "Should return Unknown for empty string");
}

[TestMethod]
public void DetectFromPartNumber_ShouldReturnUnknown_ForNull()
{
// Arrange & Act
var deviceType = DeviceTypeDetector.DetectFromPartNumber(null);

// Assert
Assert.AreEqual(DeviceType.Unknown, deviceType, "Should return Unknown for null");
}

[TestMethod]
public void DetectFromPartNumber_ShouldReturnUnknown_ForWhitespace()
{
// Arrange & Act
var deviceType = DeviceTypeDetector.DetectFromPartNumber(" ");

// Assert
Assert.AreEqual(DeviceType.Unknown, deviceType, "Should return Unknown for whitespace");
}

/// <summary>
/// Test implementation of AbstractStreamingDevice for testing purposes
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Daqifi.Desktop/Device/AbstractStreamingDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,8 @@ protected void HydrateDeviceMetadata(DaqifiOutMessage message)
if (!string.IsNullOrWhiteSpace(message.DevicePn))
{
DevicePartNumber = message.DevicePn;
DeviceType = DeviceTypeDetector.DetectFromPartNumber(message.DevicePn);
AppLogger.Information($"Detected device type: {DeviceType} from part number: {message.DevicePn}");
}
if (message.DeviceSn != 0)
{
Expand Down
27 changes: 27 additions & 0 deletions Daqifi.Desktop/Device/DeviceTypeDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Daqifi.Desktop.Device;

/// <summary>
/// Provides device type detection logic based on device part numbers.
/// </summary>
public static class DeviceTypeDetector
{
/// <summary>
/// Detects the device type from a part number string.
/// </summary>
/// <param name="partNumber">The device part number (e.g., "Nq1", "Nq3")</param>
/// <returns>The detected DeviceType, or DeviceType.Unknown if not recognized</returns>
public static DeviceType DetectFromPartNumber(string partNumber)
{
if (string.IsNullOrWhiteSpace(partNumber))
{
return DeviceType.Unknown;
}

return partNumber.ToLowerInvariant() switch
{
"nq1" => DeviceType.Nyquist1,
"nq3" => DeviceType.Nyquist3,
_ => DeviceType.Unknown
};
}
}
Loading