SafeDispatch/LibrarySDR/Responses/StatusResponse.cs

114 lines
4.4 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using LibrarySDR.Enums;
using LibrarySDR.Requests;
using System;
using System.Globalization;
using System.Text;
using static LibrarySDR.Helpers.BinaryHexHelper;
namespace LibrarySDR.Responses
{
public class StatusResponse
{
public PDUType PduType => PDUType.STATUS_REQ_RESP;
private Int64 sourceISSI;
public Int64 SourceISSI { get { return sourceISSI; } }
private Int64 destinationISSI;
public Int64 DestinationISSI { get { return destinationISSI; } }
private ShortFormReportStatus status;
public ShortFormReportStatus Status { get { return status; } }
private Int64 transactionId;
public Int64 TransactionId { get { return transactionId; } }
private Boolean gwReport;
public Boolean GWReport { get { return gwReport; } }
public StatusResponse(String hexResponsePayload)
{
String resp = hexResponsePayload;
int i = 0;
int prevIdx = 0;
// get source SSI (32 bits = 4 byte = 8 hex characters)
String hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 4 * 2));
Int64.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out sourceISSI);
// get destination SSI (32 bits = 4 byte = 8 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 4 * 2));
Int64.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out destinationISSI);
// get status (8 bits = 1 byte = 2 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 2));
Int16 status = 0;
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out status);
// convert the response into an enum
this.status = (ShortFormReportStatus)status;
if (resp.Length > i + 4 * 2)
{
// get transaction ID (32 bits = 4 byte = 8 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 4 * 2));
Int64.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out transactionId);
Int16 gwReport;
// get gw report (8 bits = 1 byte = 2 hex characters)
hex = LittleToBigEndianHexString(resp.Substring(i = i + prevIdx, prevIdx = 1 * 2));
Int16.TryParse(hex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out gwReport);
this.gwReport = gwReport == 1 ? true : false;
}
}
public static String GetInstance(Int64 sourceISSI, Int64 destinationISSI, ShortFormReportStatus status, Int64 transactionId, bool reportRequired)
{
StringBuilder sb = new StringBuilder("");
// PDU Type (8 bits)
sb.Append(GetBits((int)PDUType.STATUS_REQ_RESP, 8));
// Source ISSI (32 bits)
sb.Append(GetBits(sourceISSI, 32));
// Destination ISSI (32 bits)
sb.Append(GetBits(destinationISSI, 32));
// status (16 bits)
sb.Append(GetBits((int)status, 32));
// transaction id (32 bits)
sb.Append(GetBits(transactionId, 32));
// Gw Report (8 bits)
sb.Append(GetBits(reportRequired == true ? 1 : 0, 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);
}
public static byte[] GetBytes(Int64 sourceISSI, Int64 destinationISSI, ShortFormReportStatus status, Int64 transactionId, bool reportRequired)
{
String res = GetInstance(sourceISSI, destinationISSI, status, transactionId, reportRequired);
byte[] response = new byte[res.Length / 2];
for (int i = 0; i < res.Length / 2; i++)
response[i] = (byte)Convert.ToInt16(res.Substring(i * 2, 2), 16);
return response;
}
public override String ToString()
{
return $"Source ISSI : {sourceISSI} | Destination ISSI : {destinationISSI} | Transaction ID : {transactionId} | Status : {status} | Report required : {gwReport}";
}
}
}