Record
public record ServiceResponse : System.ValueType, System.IEquatable<SharpMeter.Core.Protocol.ServiceResponse>

Namespace: SharpMeter.Core.Protocol

Represents an L7 PSEM service response from the meter.

Inheritance

Inherits from: System.ValueType

Implements: System.IEquatable<SharpMeter.Core.Protocol.ServiceResponse>

Constructors

NameDescription
ServiceResponse(ResponseCode Code, ReadOnlyMemory<byte> Data) Represents an L7 PSEM service response from the meter.

ServiceResponse(ResponseCode Code, ReadOnlyMemory Data)

ServiceResponse.ServiceResponse(ResponseCode Code, ReadOnlyMemory<byte> Data)

Represents an L7 PSEM service response from the meter.

Parameters

NameTypeDescription
CodeSharpMeter.Core.Protocol.ResponseCodeThe response code.
DataReadOnlyMemory<byte>The response data payload.

Properties

NameDescription
Code The response code.
Data The response data payload.
HasData Gets whether this response contains data.
IsOk Gets whether this response indicates success.

Code

ResponseCode ServiceResponse.Code { get; init; }

The response code.

Data

ReadOnlyMemory<byte> ServiceResponse.Data { get; init; }

The response data payload.

HasData

bool ServiceResponse.HasData { get; }

Gets whether this response contains data.

IsOk

bool ServiceResponse.IsOk { get; }

Gets whether this response indicates success.

Methods

NameDescription
Decode(ReadOnlySpan<byte> payload) static Decodes a service response from L7 payload bytes.
ToResult() Converts this response to a Result`1, failing if the response is not OK.

Decode(ReadOnlySpan payload)

Result<ServiceResponse> ServiceResponse.Decode(ReadOnlySpan<byte> payload)

Decodes a service response from L7 payload bytes.

Parameters

NameTypeDescription
payloadReadOnlySpan<byte>The L7 payload from the PSEM frame.

Returns: The decoded service response.

ToResult()

Result<ReadOnlyMemory<byte>> ServiceResponse.ToResult()

Converts this response to a Result`1, failing if the response is not OK.

Type Relationships
classDiagram
                    style ServiceResponse fill:#f9f,stroke:#333,stroke-width:2px
                    ServiceResponse --|> ValueType : inherits
                    ServiceResponse ..|> ServiceResponse~ : implements
                
View Source
/// <summary>
///     Represents an L7 PSEM service response from the meter.
/// </summary>
/// <param name = "Code">The response code.</param>
/// <param name = "Data">The response data payload.</param>
public readonly record struct ServiceResponse(ResponseCode Code, ReadOnlyMemory<byte> Data)
{
#region Properties
    /// <summary>Gets whether this response indicates success.</summary>
    public bool IsOk => Code == ResponseCode.Ok;
    /// <summary>Gets whether this response contains data.</summary>
    public bool HasData => Data.Length > 0;

#endregion
#region Methods
    /// <summary>
    ///     Decodes a service response from L7 payload bytes.
    /// </summary>
    /// <param name = "payload">The L7 payload from the PSEM frame.</param>
    /// <returns>The decoded service response.</returns>
    public static Result<ServiceResponse> Decode(ReadOnlySpan<byte> payload)
    {
        if (payload.Length < 1)
            return PsemError.Framing("Empty service response payload");
        var code = (ResponseCode)payload[0];
        var data = payload.Length > 1 ? payload[1..].ToArray() : [];
        return new ServiceResponse(code, data);
    }

    /// <summary>
    ///     Converts this response to a <see cref = "Result{T}"/>, failing if the response is not OK.
    /// </summary>
    public Result<ReadOnlyMemory<byte>> ToResult() => IsOk ? Result<ReadOnlyMemory<byte>>.Success(Data) : PsemError.Protocol(Code);
#endregion
}
Was this page helpful?