SafeDispatch/LibrarySDR/Responses/RegisterResponse.cs

56 lines
1.7 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using LibrarySDR.Enums;
using LibrarySDR.Requests;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LibrarySDR.Helpers.BinaryHexHelper;
namespace LibrarySDR.Responses
{
public class RegisterResponse
{
public PDUType PduType => PDUType.REGISTER_RESP;
private RegisterStatus registerStatus;
public RegisterStatus RegisterStatus { get { return registerStatus; } }
public RegisterResponse(String hexResponsePayload)
{
String resp = hexResponsePayload;
int i = 0;
int prevIdx = 0;
// get register status (8 bits = 1 byte = 2 hex characters)
Int16 regStatus = 0;
String hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 2));
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out regStatus);
// convert response to RegisterStatus
registerStatus = (RegisterStatus)regStatus;
}
public static String GetInstance(RegisterStatus regStatus)
{
StringBuilder sb = new StringBuilder("");
// PDU Type (8 bits)
sb.Append(GetBits((int)PDUType.REGISTER_RESP, 8));
// status (8 bits)
sb.Append(GetBits((int)regStatus, 8));
// get the header with the length
String header = Header.GetHeaderBinary(sb.ToString().Length / 8);
// add header in front of the request
return BinaryStringToHexString($"{header}{sb.ToString()}", Padding.RIGHT);
}
}
}