Skip to content

Commit

Permalink
Added deleting of SMS messages and SMS_Group enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
arceryz committed Nov 1, 2022
1 parent fddc0ca commit 48f87d9
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions atlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
from time import *


class SMS_Group:
UNREAD = "REC UNREAD"
READ = "REC READ"
STORED_UNSENT = "STO UNSENT"
STORED_SENT = "STO SENT"
ALL = "ALL"


class Status:
OK = "OK"
PROMPT = "> "
Expand Down Expand Up @@ -192,20 +200,18 @@ def send_sms(self, nr, msg):
return status


def receive_sms(self, category="REC UNREAD"):
""" Receive text messages. The category can be one of several AT modes: REC UNREAD (default), REC READ, STO UNSENT, STO SENT, ALL.
Not all modes may work for a given device.
"""
def receive_sms(self, group=SMS_Group.UNREAD):
""" Receive text messages. See types of message from SMS_Group class. """
# Read unread. After reading they will not show up here anymore!
print("Scanning {:s} messages...".format(category))
print("Scanning {:s} messages...".format(group))
self.write("AT+CMGF=1")
status = self.read_status("Text mode")
if status != Status.OK: return status

# Read the messages.
self.write("AT+CMGL=\"{:s}\"".format(category))
resp = self.read()
if resp[-1] != "OK": return resp[-1]
if resp[-1] != Status.OK: return resp[-1]

# TotalElements = 2 + 2 * TotalMessages.
# First and last elements are echo/result.
Expand All @@ -221,7 +227,7 @@ def receive_sms(self, category="REC UNREAD"):
el = [sender, date, time, message]
table.append(el)

print("Received {:d} messages.".format(len(table)))
print("Received {:d} messages".format(len(table)))
return table


Expand All @@ -236,3 +242,16 @@ def await_sms(self, delay=5, timeout=100):
sec += delay
if sec >= timeout:
return []


def delete_sms(index):
""" Delete sms at index from given group.
Indices are 0-based indexing in the list of ALL mesages."""
self.write("AT+CMGD={:d}".format(index + 1))
return self.read_status("Deleting message")


def delete_read_sms():
""" Delete all messages except unread. Including drafts. """
self.write("AT+CMGD=1,3")
return self.read_status("Deleting message")

0 comments on commit 48f87d9

Please sign in to comment.