Skip to content

Commit

Permalink
Sms refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjorgo committed May 2, 2024
1 parent 0234a40 commit eeac486
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 98 deletions.
2 changes: 1 addition & 1 deletion src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static async Task RunAsync(System.IO.Stream stream, string pin)
var selectMessageService = await modem.SetSelectMessageService(0);
Console.WriteLine($"Setting select message service: {selectMessageService}");

var newSmsIndicationResult = await modem.SetNewSmsIndicationAsync(2, 1, 0, 2, 0); // 2, 2, 2, 1, 1 -> PDU (2, 1, 2, 1, 0)
var newSmsIndicationResult = await modem.SetNewSmsIndicationAsync(2, 1, 0, 2, 0); // 2, 1, 0, 2, 0 (CSMS=0)
Console.WriteLine($"Setting new SMS indication: {newSmsIndicationResult}");

var supportedStorages = await modem.GetSupportedPreferredMessageStoragesAsync();
Expand Down
30 changes: 10 additions & 20 deletions src/HeboTech.ATLib/DTOs/Sms.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
using System;
using HeboTech.ATLib.PDU;

namespace HeboTech.ATLib.DTOs
{
public class Sms
{
public Sms(SmsStatus status, PhoneNumberDTO sender, DateTimeOffset receiveTime, string message)
: this(status, sender, receiveTime, message, 0, 1, 1)
protected Sms(MessageTypeIndicatorInbound messageTypeIndicator)
{
MessageTypeIndicator = messageTypeIndicator;
}

public Sms(SmsStatus status, PhoneNumberDTO sender, DateTimeOffset receiveTime, string message, int messageReferenceNumber, int totalNumberOfParts, int partNumber)
protected Sms(MessageTypeIndicatorInbound messageTypeIndicator, int messageReference)
: this(messageTypeIndicator)
{
Status = status;
Sender = sender;
ReceiveTime = receiveTime;
Message = message;
MessageReferenceNumber = messageReferenceNumber;
TotalNumberOfParts = totalNumberOfParts;
PartNumber = partNumber;
MessageReference = messageReference;
}

public SmsStatus Status { get; }
public PhoneNumberDTO Sender { get; }
public DateTimeOffset ReceiveTime { get;}
public string Message { get; }
public int MessageReferenceNumber { get; }
public int TotalNumberOfParts { get; }
public int PartNumber { get; }
public int MessageReference { get; }
public MessageTypeIndicatorInbound MessageTypeIndicator { get; }

public override string ToString()
{
return $"Sender:\t\t{Sender}{Environment.NewLine}ReceiveTime:\t{ReceiveTime}{Environment.NewLine}Ref. no.:\t{MessageReferenceNumber}{Environment.NewLine}Part:\t\t{PartNumber}/{TotalNumberOfParts}{Environment.NewLine}Message:\t{Message}";
return $"MTI: {MessageTypeIndicator}, Msg. ref.: {MessageReference}";
}
}
}
}
26 changes: 0 additions & 26 deletions src/HeboTech.ATLib/DTOs/SmsBase.cs

This file was deleted.

14 changes: 0 additions & 14 deletions src/HeboTech.ATLib/DTOs/SmsBaseWithIndex.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/HeboTech.ATLib/DTOs/SmsDeliver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace HeboTech.ATLib.DTOs
/// <summary>
/// Data object for a received SMS
/// </summary>
public class SmsDeliver : SmsBase
public class SmsDeliver : Sms
{
public SmsDeliver(PhoneNumberDTO serviceCenterNumber, PhoneNumberDTO senderNumber, string message, DateTimeOffset timestamp)
: base(MessageTypeIndicatorInbound.SMS_DELIVER)
Expand Down
2 changes: 1 addition & 1 deletion src/HeboTech.ATLib/DTOs/SmsStatusReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace HeboTech.ATLib.DTOs
{
public class SmsStatusReport : SmsBase
public class SmsStatusReport : Sms
{
public SmsStatusReport(int messageReference, PhoneNumberDTO recipientAddress, DateTimeOffset serviceCenterTimestamp, DateTimeOffset dischargeTime, SmsDeliveryStatus status)
: base(MessageTypeIndicatorInbound.SMS_STATUS_REPORT, messageReference)
Expand Down
24 changes: 6 additions & 18 deletions src/HeboTech.ATLib/DTOs/SmsWithIndex.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
using System;

namespace HeboTech.ATLib.DTOs
namespace HeboTech.ATLib.DTOs
{
public class SmsWithIndex : Sms
public class SmsWithIndex
{
public SmsWithIndex(int index, SmsStatus status, PhoneNumberDTO sender, DateTimeOffset receiveTime, string message)
: base(status, sender, receiveTime, message)
{
Index = index;
}

public SmsWithIndex(int index, SmsStatus status, PhoneNumberDTO sender, DateTimeOffset receiveTime, string message, int messageReferenceNumber, int totalNumberOfParts, int partNumber)
: base(status, sender, receiveTime, message, messageReferenceNumber, totalNumberOfParts, partNumber)
public SmsWithIndex(Sms sms, int index)
{
Sms = sms;
Index = index;
}

public Sms Sms { get; }
public int Index { get; }

public override string ToString()
{
return $"Index:\t\t{Index}{Environment.NewLine}" + base.ToString();
}
}
}
}
4 changes: 2 additions & 2 deletions src/HeboTech.ATLib/Extensions/SmsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace HeboTech.ATLib.Extensions
{
internal static class SmsExtensions
{
public static SmsBaseWithIndex ToSmsWithIndex(this SmsBase sms, int index)
public static SmsWithIndex ToSmsWithIndex(this Sms sms, int index)
{
return new SmsBaseWithIndex(sms, index);
return new SmsWithIndex(sms, index);
}
}
}
16 changes: 8 additions & 8 deletions src/HeboTech.ATLib/Modems/Generic/ModemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,14 @@ public virtual async Task<ModemResponse<PreferredMessageStorages>> SetPreferredM
return ModemResponse.HasResultError<PreferredMessageStorages>(error);
}

public virtual async Task<ModemResponse<SmsBase>> ReadSmsAsync(int index)
public virtual async Task<ModemResponse<Sms>> ReadSmsAsync(int index)
{
AtResponse pduResponse = await channel.SendMultilineCommand($"AT+CMGR={index}", null);

if (pduResponse.Success)
{
if (!pduResponse.Intermediates.Any())
return ModemResponse.HasResultError<SmsBase>();
return ModemResponse.HasResultError<Sms>();

string line1 = pduResponse.Intermediates[0];
var line1Match = Regex.Match(line1, @"\+CMGR:\s(?<status>\d),(""(?<alpha>\w*)"")*,(?<length>\d+)");
Expand All @@ -399,28 +399,28 @@ public virtual async Task<ModemResponse<SmsBase>> ReadSmsAsync(int index)
if (line2Match.Success)
{
string pduString = line2Match.Groups["pdu"].Value;
SmsBase sms = SmsDecoder.Decode(pduString.ToByteArray(), status);
Sms sms = SmsDecoder.Decode(pduString.ToByteArray(), status);
return ModemResponse.IsResultSuccess(sms);
}
}
}
}

AtErrorParsers.TryGetError(pduResponse.FinalResponse, out Error pduError);
return ModemResponse.HasResultError<SmsBase>(pduError);
return ModemResponse.HasResultError<Sms>(pduError);
}

public virtual async Task<ModemResponse<List<SmsBaseWithIndex>>> ListSmssAsync(SmsStatus smsStatus)
public virtual async Task<ModemResponse<List<SmsWithIndex>>> ListSmssAsync(SmsStatus smsStatus)
{
string command = $"AT+CMGL={(int)smsStatus}";

AtResponse response = await channel.SendMultilineCommand(command, null);

List<SmsBaseWithIndex> smss = new List<SmsBaseWithIndex>();
List<SmsWithIndex> smss = new List<SmsWithIndex>();
if (response.Success)
{
if ((response.Intermediates.Count % 2) != 0)
return ModemResponse.HasResultError<List<SmsBaseWithIndex>>();
return ModemResponse.HasResultError<List<SmsWithIndex>>();

for (int i = 0; i < response.Intermediates.Count; i += 2)
{
Expand All @@ -435,7 +435,7 @@ public virtual async Task<ModemResponse<List<SmsBaseWithIndex>>> ListSmssAsync(S
// Sent when AT+CSDH=1 is set
int length = int.Parse(match.Groups["length"].Value);

SmsBase sms = SmsDecoder.Decode(messageLine.ToByteArray(), status);
Sms sms = SmsDecoder.Decode(messageLine.ToByteArray(), status);
smss.Add(sms.ToSmsWithIndex(index));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/HeboTech.ATLib/Modems/IModem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public interface IModem : IDisposable
/// </summary>
/// <param name="smsStatus"></param>
/// <returns>Command status with a list of SMSs</returns>
Task<ModemResponse<List<SmsBaseWithIndex>>> ListSmssAsync(SmsStatus smsStatus);
Task<ModemResponse<List<SmsWithIndex>>> ListSmssAsync(SmsStatus smsStatus);

/// <summary>
/// Set preferred message storages
Expand Down Expand Up @@ -190,7 +190,7 @@ public interface IModem : IDisposable
/// </summary>
/// <param name="index"></param>
/// <returns>Command status with SMS</returns>
Task<ModemResponse<SmsBase>> ReadSmsAsync(int index);
Task<ModemResponse<Sms>> ReadSmsAsync(int index);

/// <summary>
/// Resets the modem to factory defaults
Expand Down
8 changes: 4 additions & 4 deletions src/HeboTech.ATLib/Modems/SIMCOM/SIM5320.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ public override Task<IEnumerable<ModemResponse<SmsReference>>> SendSmsAsync(SmsS
return SendSmsAsync(request, false);
}

public override async Task<ModemResponse<List<SmsBaseWithIndex>>> ListSmssAsync(SmsStatus smsStatus)
public override async Task<ModemResponse<List<SmsWithIndex>>> ListSmssAsync(SmsStatus smsStatus)
{
string command = $"AT+CMGL={(int)smsStatus}";

AtResponse response = await channel.SendMultilineCommand(command, null);

List<SmsBaseWithIndex> smss = new List<SmsBaseWithIndex>();
List<SmsWithIndex> smss = new List<SmsWithIndex>();
if (response.Success)
{
if ((response.Intermediates.Count % 2) != 0)
return ModemResponse.HasResultError<List<SmsBaseWithIndex>>();
return ModemResponse.HasResultError<List<SmsWithIndex>>();

for (int i = 0; i < response.Intermediates.Count; i += 2)
{
Expand All @@ -71,7 +71,7 @@ public override async Task<ModemResponse<List<SmsBaseWithIndex>>> ListSmssAsync(
// Sent when AT+CSDH=1 is set
int length = int.Parse(match.Groups["length"].Value);

SmsBase sms = SmsDeliverDecoder.Decode(messageLine.ToByteArray());
Sms sms = SmsDeliverDecoder.Decode(messageLine.ToByteArray());
smss.Add(sms.ToSmsWithIndex(index));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/HeboTech.ATLib/PDU/SmsDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace HeboTech.ATLib.PDU
{
internal class SmsDecoder
{
public static SmsBase Decode(ReadOnlySpan<byte> bytes, SmsStatus status, int timestampYearOffset = 2000)
public static Sms Decode(ReadOnlySpan<byte> bytes, SmsStatus status, int timestampYearOffset = 2000)
{
int offset = 0;

Expand Down

0 comments on commit eeac486

Please sign in to comment.