From eeac486485a4b5b6f554e7d22b51fb8eea508760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henning=20Bj=C3=B8rgo?= Date: Thu, 2 May 2024 20:45:55 +0200 Subject: [PATCH] Sms refactor --- .../FunctionalityTest.cs | 2 +- src/HeboTech.ATLib/DTOs/Sms.cs | 30 +++++++------------ src/HeboTech.ATLib/DTOs/SmsBase.cs | 26 ---------------- src/HeboTech.ATLib/DTOs/SmsBaseWithIndex.cs | 14 --------- src/HeboTech.ATLib/DTOs/SmsDeliver.cs | 2 +- src/HeboTech.ATLib/DTOs/SmsStatusReport.cs | 2 +- src/HeboTech.ATLib/DTOs/SmsWithIndex.cs | 24 ++++----------- .../Extensions/SmsExtensions.cs | 4 +-- .../Modems/Generic/ModemBase.cs | 16 +++++----- src/HeboTech.ATLib/Modems/IModem.cs | 4 +-- src/HeboTech.ATLib/Modems/SIMCOM/SIM5320.cs | 8 ++--- src/HeboTech.ATLib/PDU/SmsDecoder.cs | 2 +- 12 files changed, 36 insertions(+), 98 deletions(-) delete mode 100644 src/HeboTech.ATLib/DTOs/SmsBase.cs delete mode 100644 src/HeboTech.ATLib/DTOs/SmsBaseWithIndex.cs diff --git a/src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs b/src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs index 0255aa5..145fa96 100644 --- a/src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs +++ b/src/HeboTech.ATLib.TestConsole/FunctionalityTest.cs @@ -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(); diff --git a/src/HeboTech.ATLib/DTOs/Sms.cs b/src/HeboTech.ATLib/DTOs/Sms.cs index 4fafdc6..368cae7 100644 --- a/src/HeboTech.ATLib/DTOs/Sms.cs +++ b/src/HeboTech.ATLib/DTOs/Sms.cs @@ -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}"; } } -} +} \ No newline at end of file diff --git a/src/HeboTech.ATLib/DTOs/SmsBase.cs b/src/HeboTech.ATLib/DTOs/SmsBase.cs deleted file mode 100644 index 3bd602b..0000000 --- a/src/HeboTech.ATLib/DTOs/SmsBase.cs +++ /dev/null @@ -1,26 +0,0 @@ -using HeboTech.ATLib.PDU; - -namespace HeboTech.ATLib.DTOs -{ - public class SmsBase - { - protected SmsBase(MessageTypeIndicatorInbound messageTypeIndicator) - { - MessageTypeIndicator = messageTypeIndicator; - } - - protected SmsBase(MessageTypeIndicatorInbound messageTypeIndicator, int messageReference) - : this(messageTypeIndicator) - { - MessageReference = messageReference; - } - - public int MessageReference { get; } - public MessageTypeIndicatorInbound MessageTypeIndicator { get; } - - public override string ToString() - { - return $"MTI: {MessageTypeIndicator}, Msg. ref.: {MessageReference}"; - } - } -} \ No newline at end of file diff --git a/src/HeboTech.ATLib/DTOs/SmsBaseWithIndex.cs b/src/HeboTech.ATLib/DTOs/SmsBaseWithIndex.cs deleted file mode 100644 index e572d4a..0000000 --- a/src/HeboTech.ATLib/DTOs/SmsBaseWithIndex.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace HeboTech.ATLib.DTOs -{ - public class SmsBaseWithIndex - { - public SmsBaseWithIndex(SmsBase sms, int index) - { - Sms = sms; - Index = index; - } - - public SmsBase Sms { get; } - public int Index { get; } - } -} \ No newline at end of file diff --git a/src/HeboTech.ATLib/DTOs/SmsDeliver.cs b/src/HeboTech.ATLib/DTOs/SmsDeliver.cs index 07f5a7d..ef7e3d0 100644 --- a/src/HeboTech.ATLib/DTOs/SmsDeliver.cs +++ b/src/HeboTech.ATLib/DTOs/SmsDeliver.cs @@ -6,7 +6,7 @@ namespace HeboTech.ATLib.DTOs /// /// Data object for a received SMS /// - public class SmsDeliver : SmsBase + public class SmsDeliver : Sms { public SmsDeliver(PhoneNumberDTO serviceCenterNumber, PhoneNumberDTO senderNumber, string message, DateTimeOffset timestamp) : base(MessageTypeIndicatorInbound.SMS_DELIVER) diff --git a/src/HeboTech.ATLib/DTOs/SmsStatusReport.cs b/src/HeboTech.ATLib/DTOs/SmsStatusReport.cs index a250581..ce64d0f 100644 --- a/src/HeboTech.ATLib/DTOs/SmsStatusReport.cs +++ b/src/HeboTech.ATLib/DTOs/SmsStatusReport.cs @@ -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) diff --git a/src/HeboTech.ATLib/DTOs/SmsWithIndex.cs b/src/HeboTech.ATLib/DTOs/SmsWithIndex.cs index fee9dbe..ee387fa 100644 --- a/src/HeboTech.ATLib/DTOs/SmsWithIndex.cs +++ b/src/HeboTech.ATLib/DTOs/SmsWithIndex.cs @@ -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(); - } } -} +} \ No newline at end of file diff --git a/src/HeboTech.ATLib/Extensions/SmsExtensions.cs b/src/HeboTech.ATLib/Extensions/SmsExtensions.cs index 111a0f0..f06d930 100644 --- a/src/HeboTech.ATLib/Extensions/SmsExtensions.cs +++ b/src/HeboTech.ATLib/Extensions/SmsExtensions.cs @@ -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); } } } diff --git a/src/HeboTech.ATLib/Modems/Generic/ModemBase.cs b/src/HeboTech.ATLib/Modems/Generic/ModemBase.cs index 3055ecb..209f053 100644 --- a/src/HeboTech.ATLib/Modems/Generic/ModemBase.cs +++ b/src/HeboTech.ATLib/Modems/Generic/ModemBase.cs @@ -375,14 +375,14 @@ public virtual async Task> SetPreferredM return ModemResponse.HasResultError(error); } - public virtual async Task> ReadSmsAsync(int index) + public virtual async Task> ReadSmsAsync(int index) { AtResponse pduResponse = await channel.SendMultilineCommand($"AT+CMGR={index}", null); if (pduResponse.Success) { if (!pduResponse.Intermediates.Any()) - return ModemResponse.HasResultError(); + return ModemResponse.HasResultError(); string line1 = pduResponse.Intermediates[0]; var line1Match = Regex.Match(line1, @"\+CMGR:\s(?\d),(""(?\w*)"")*,(?\d+)"); @@ -399,7 +399,7 @@ public virtual async Task> 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); } } @@ -407,20 +407,20 @@ public virtual async Task> ReadSmsAsync(int index) } AtErrorParsers.TryGetError(pduResponse.FinalResponse, out Error pduError); - return ModemResponse.HasResultError(pduError); + return ModemResponse.HasResultError(pduError); } - public virtual async Task>> ListSmssAsync(SmsStatus smsStatus) + public virtual async Task>> ListSmssAsync(SmsStatus smsStatus) { string command = $"AT+CMGL={(int)smsStatus}"; AtResponse response = await channel.SendMultilineCommand(command, null); - List smss = new List(); + List smss = new List(); if (response.Success) { if ((response.Intermediates.Count % 2) != 0) - return ModemResponse.HasResultError>(); + return ModemResponse.HasResultError>(); for (int i = 0; i < response.Intermediates.Count; i += 2) { @@ -435,7 +435,7 @@ public virtual async Task>> 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)); } } diff --git a/src/HeboTech.ATLib/Modems/IModem.cs b/src/HeboTech.ATLib/Modems/IModem.cs index 39e951a..20695d1 100644 --- a/src/HeboTech.ATLib/Modems/IModem.cs +++ b/src/HeboTech.ATLib/Modems/IModem.cs @@ -162,7 +162,7 @@ public interface IModem : IDisposable /// /// /// Command status with a list of SMSs - Task>> ListSmssAsync(SmsStatus smsStatus); + Task>> ListSmssAsync(SmsStatus smsStatus); /// /// Set preferred message storages @@ -190,7 +190,7 @@ public interface IModem : IDisposable /// /// /// Command status with SMS - Task> ReadSmsAsync(int index); + Task> ReadSmsAsync(int index); /// /// Resets the modem to factory defaults diff --git a/src/HeboTech.ATLib/Modems/SIMCOM/SIM5320.cs b/src/HeboTech.ATLib/Modems/SIMCOM/SIM5320.cs index 59cce99..04c4328 100644 --- a/src/HeboTech.ATLib/Modems/SIMCOM/SIM5320.cs +++ b/src/HeboTech.ATLib/Modems/SIMCOM/SIM5320.cs @@ -46,17 +46,17 @@ public override Task>> SendSmsAsync(SmsS return SendSmsAsync(request, false); } - public override async Task>> ListSmssAsync(SmsStatus smsStatus) + public override async Task>> ListSmssAsync(SmsStatus smsStatus) { string command = $"AT+CMGL={(int)smsStatus}"; AtResponse response = await channel.SendMultilineCommand(command, null); - List smss = new List(); + List smss = new List(); if (response.Success) { if ((response.Intermediates.Count % 2) != 0) - return ModemResponse.HasResultError>(); + return ModemResponse.HasResultError>(); for (int i = 0; i < response.Intermediates.Count; i += 2) { @@ -71,7 +71,7 @@ public override async Task>> 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)); } } diff --git a/src/HeboTech.ATLib/PDU/SmsDecoder.cs b/src/HeboTech.ATLib/PDU/SmsDecoder.cs index f9701e7..bbc0cff 100644 --- a/src/HeboTech.ATLib/PDU/SmsDecoder.cs +++ b/src/HeboTech.ATLib/PDU/SmsDecoder.cs @@ -5,7 +5,7 @@ namespace HeboTech.ATLib.PDU { internal class SmsDecoder { - public static SmsBase Decode(ReadOnlySpan bytes, SmsStatus status, int timestampYearOffset = 2000) + public static Sms Decode(ReadOnlySpan bytes, SmsStatus status, int timestampYearOffset = 2000) { int offset = 0;