Skip to content

Commit 0298f28

Browse files
authored
feat: add protobuf message concept (#21)
1 parent aa88924 commit 0298f28

File tree

9 files changed

+3109
-46
lines changed

9 files changed

+3109
-46
lines changed

src/Daqifi.Core.Tests/Communication/Producers/ScpiMessageProducerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void ForceBootloader_ReturnsCorrectMessage()
289289
Assert.Equal("SYSTem:FORceBoot", message.Data);
290290
}
291291

292-
private static void AssertMessageFormat(IMessage message)
292+
private static void AssertMessageFormat(IOutboundMessage<string> message)
293293
{
294294
var bytes = message.GetBytes();
295295
var expectedBytes = Encoding.ASCII.GetBytes($"{message.Data}\r\n");

src/Daqifi.Core/Communication/Messages/DaqifiOutMessage.cs

Lines changed: 2996 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Daqifi.Core.Communication.Messages;
2+
3+
/// <summary>
4+
/// Represents a message containing data received from the DAQiFi device (Inbound).
5+
/// </summary>
6+
/// <typeparam name="T">The type of the data payload.</typeparam>
7+
public interface IInboundMessage<out T> // Using 'out' for covariance
8+
{
9+
/// <summary>
10+
/// Gets the payload data received from the device.
11+
/// </summary>
12+
T Data { get; }
13+
}

src/Daqifi.Core/Communication/Messages/IMessage.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Daqifi.Core.Communication.Messages;
2+
3+
/// <summary>
4+
/// Represents a message to be sent to the DAQiFi device (Outbound).
5+
/// Defines the contract for serializing the message data into bytes.
6+
/// </summary>
7+
/// <typeparam name="T">The type of the data payload.</typeparam>
8+
public interface IOutboundMessage<T>
9+
{
10+
/// <summary>
11+
/// Gets or sets the payload data to be sent.
12+
/// </summary>
13+
T Data { get; set;}
14+
15+
/// <summary>
16+
/// Converts the message data into a byte array suitable for transmission.
17+
/// </summary>
18+
/// <returns>A byte array representing the message.</returns>
19+
byte[] GetBytes();
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Daqifi.Core.Communication.Messages;
2+
3+
/// <summary>
4+
/// Represents a message containing incoming data from the DAQiFi device,
5+
/// formatted using Google Protocol Buffers (Protobuf).
6+
/// The name DaqifiOutMessage signifies data coming "out" from the device.
7+
/// Implements IInboundMessage.
8+
/// </summary>
9+
public class ProtobufMessage : IInboundMessage<DaqifiOutMessage>
10+
{
11+
/// <summary>
12+
/// Gets the data associated with the message, which is a DaqifiOutMessage
13+
/// received from the device.
14+
/// </summary>
15+
public DaqifiOutMessage Data { get; }
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="ProtobufMessage"/> class
19+
/// to wrap incoming device data.
20+
/// </summary>
21+
/// <param name="message">The DaqifiOutMessage received from the device.</param>
22+
public ProtobufMessage(DaqifiOutMessage message)
23+
{
24+
Data = message;
25+
}
26+
}

src/Daqifi.Core/Communication/Messages/ScpiMessage.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22

33
namespace Daqifi.Core.Communication.Messages;
44

5-
public class ScpiMessage(string command) : IMessage
5+
/// <summary>
6+
/// Represents a Standard Commands for Programmable Instruments (SCPI) message
7+
/// to be sent to the device (Outbound).
8+
/// Implements IOutboundMessage.
9+
/// </summary>
10+
/// <param name="command">The SCPI command string.</param>
11+
public class ScpiMessage(string command) : IOutboundMessage<string>
612
{
7-
public object Data { get; set; } = command;
13+
/// <summary>
14+
/// Gets or sets the data associated with the message, which is the SCPI command string.
15+
/// </summary>
16+
public string Data { get; set; } = command;
817

18+
/// <summary>
19+
/// Converts the SCPI command string to a byte array suitable for transmission,
20+
/// appending the required carriage return and newline characters.
21+
/// </summary>
22+
/// <returns>A byte array representing the SCPI message.</returns>
923
public byte[] GetBytes()
1024
{
11-
return Encoding.ASCII.GetBytes((string)Data + "\r\n");
25+
return Encoding.ASCII.GetBytes(Data + "\r\n");
1226
}
1327
}

0 commit comments

Comments
 (0)