SafeDispatch/LibrarySDR/SDSMessageDecoder.cs
2024-02-22 18:43:59 +02:00

168 lines
5.9 KiB
C#

using LibrarySDR.Enums;
using LibrarySDR.Requests;
using LibrarySDR.Responses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibrarySDR
{
public class SDSMessageDecoder
{
private PDUType pduType;
public PDUType PduType { get { return pduType; } }
private String payload;
public String Payload { get { return payload; } }
public static Int64 GatewayISSI;
public Header header;
public SDSMessageDecoder()
{
}
public SDSMessageDecoder Decode(String hexResponse)
{
header = new Header(hexResponse);
try
{
if (hexResponse.Length > 10)
pduType = (PDUType)Convert.ToInt16(hexResponse.Substring(8, 2), 16);
else
pduType = PDUType.LIFE_SIGN_RESP;
}
catch(FormatException ex)
{
pduType = PDUType.BAD_FORMATTED;
}
// all other characters are from the payload
if (pduType != PDUType.LIFE_SIGN_RESP || pduType != PDUType.BAD_FORMATTED)
payload = hexResponse.Substring(10);
switch (PduType)
{
case PDUType.REGISTER_RESP:
{
// parse the payload
RegisterResponse reqResp = new RegisterResponse(Payload);
// fire event
OnRegisterResponseReceived?.Invoke(reqResp);
break;
}
case PDUType.DATA_REQ_RESP:
{
// parse the payload
SimpleTextSDSRequest textResp = new SimpleTextSDSRequest(Payload);
switch(textResp.ProtocolIdentifier)
{
case ProtocolIdentifier.GPS:
{
OnLIPLocationReceived?.Invoke(textResp);
break;
}
case ProtocolIdentifier.SimpleTextUsingSDSTL:
case ProtocolIdentifier.ImmediateTextUsingSDSTL:
{
OnSimpleTextSDSReceived?.Invoke(textResp);
break;
}
}
break;
}
case PDUType.REPORT_REQ_RESP:
{
GwSDSReportResponse reportResoponse = new GwSDSReportResponse(Payload);
// fire event
OnReportReceived?.Invoke(reportResoponse);
break;
}
case PDUType.STATUS_REQ_RESP:
{
StatusResponse statusResponse = new StatusResponse(Payload);
if (statusResponse.DestinationISSI != GatewayISSI)
OnEmergencyReceived?.Invoke(statusResponse);
else
// fire event
OnStatusResponseReceived?.Invoke(statusResponse);
break;
}
case PDUType.GROUP_MONITOR_EXT_RESP:
{
GroupMonitorExtResponse reqResp = new GroupMonitorExtResponse(Payload);
// fire event
OnGroupMonitorExtResponseReceived?.Invoke(reqResp);
break;
}
case PDUType.LIFE_SIGN_RESP:
{
// parse the payload
LifeSignResponse reqResp = new LifeSignResponse(Payload);
// fire event
OnLifeSignResponseReceived?.Invoke();
break;
}
case PDUType.GW_STATUS_REPORT_RESP:
{
GwStatusReportResponse gwStatusReportResponse = new GwStatusReportResponse(Payload);
// fire event
OnGwStatusReportResponseReceived?.Invoke(gwStatusReportResponse);
break;
}
}
return this;
}
#region EVENTS
public delegate void ReportReceived(GwSDSReportResponse response);
public event ReportReceived OnReportReceived;
public delegate void StatusResponseReceived(StatusResponse response);
public event StatusResponseReceived OnStatusResponseReceived;
public delegate void GroupMonitorExtResponseReceived(GroupMonitorExtResponse response);
public event GroupMonitorExtResponseReceived OnGroupMonitorExtResponseReceived;
public delegate void GwStatusReportResponseReceived(GwStatusReportResponse response);
public event GwStatusReportResponseReceived OnGwStatusReportResponseReceived;
public delegate void SimpleTextSDSReceived(SimpleTextSDSRequest response);
public event SimpleTextSDSReceived OnSimpleTextSDSReceived;
public delegate void EmergencyReceived(StatusResponse response);
public event EmergencyReceived OnEmergencyReceived;
public delegate void LIPLocationReceived(SimpleTextSDSRequest response);
public event LIPLocationReceived OnLIPLocationReceived;
public delegate void RegisterResponseReceived(RegisterResponse response);
public event RegisterResponseReceived OnRegisterResponseReceived;
public delegate void LifeSignResponseReceived();
public event LifeSignResponseReceived OnLifeSignResponseReceived;
#endregion
}
}