Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add long sms concat info for received sms #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/sms_handler_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
from gsmmodem.modem import GsmModem

def handleSms(sms):
print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time, sms.text))
# long sms Concatenation support: reference, parts, number
concat = sms.concat.__dict__ if sms.concat else {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to have this line here?
Could you move this logic to the module?
Just thinking.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this process just for print purpose,
in GsmModem, the sms.concat property will be:

  • dict (for long sms)
  • None (for normal sms)

I think None is more clear than {} for normal sms 's concat info,

what do you think about it?

print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nconcat: {2}\nMessage:\n{3}\n'.format(sms.number, sms.time, concat, sms.text))
print('Replying to SMS...')
sms.reply(u'SMS received: "{0}{1}"'.format(sms.text[:20], '...' if len(sms.text) > 20 else ''))
print('SMS sent.\n')
Expand Down
20 changes: 16 additions & 4 deletions gsmmodem/modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .serial_comms import SerialComms
from .exceptions import CommandError, InvalidStateException, CmeError, CmsError, InterruptedException, TimeoutException, PinRequiredError, IncorrectPinError, SmscNumberUnknownError
from .pdu import encodeSmsSubmitPdu, decodeSmsPdu, encodeGsm7, encodeTextMode
from .pdu import encodeSmsSubmitPdu, decodeSmsPdu, encodeGsm7, encodeTextMode, Concatenation
from .util import SimpleOffsetTzInfo, lineStartingWith, allLinesMatchingPattern, parseTextModeTimeStr, removeAtPrefix

#from . import compat # For Python 2.6 compatibility
Expand Down Expand Up @@ -54,13 +54,14 @@ def __init__(self, number, text, smsc=None):
class ReceivedSms(Sms):
""" An SMS message that has been received (MT) """

def __init__(self, gsmModem, status, number, time, text, smsc=None, udh=[], index=None):
def __init__(self, gsmModem, status, number, time, text, smsc=None, udh=[], index=None, concat=None):
super(ReceivedSms, self).__init__(number, text, smsc)
self._gsmModem = weakref.proxy(gsmModem)
self.status = status
self.time = time
self.udh = udh
self.index = index
self.concat = concat

def reply(self, message):
""" Convenience method that sends a reply SMS to the sender of this message """
Expand Down Expand Up @@ -1083,6 +1084,15 @@ def processStoredSms(self, unreadOnly=False):
else:
raise ValueError('GsmModem.smsReceivedCallback not set')

def _getConcat(self, smsDict):
concat = None
if smsDict.has_key('udh'):
for i in smsDict['udh']:
if isinstance(i, Concatenation):
concat = i
break
return concat

def listStoredSms(self, status=Sms.STATUS_ALL, memory=None, delete=False):
""" Returns SMS messages currently stored on the device/SIM card.

Expand Down Expand Up @@ -1153,7 +1163,8 @@ def listStoredSms(self, status=Sms.STATUS_ALL, memory=None, delete=False):
# todo: make better fix
else:
if smsDict['type'] == 'SMS-DELIVER':
sms = ReceivedSms(self, int(msgStat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc'], smsDict.get('udh', []), msgIndex)
concat = self._getConcat(smsDict)
sms = ReceivedSms(self, int(msgStat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc'], smsDict.get('udh', []), msgIndex, concat)
elif smsDict['type'] == 'SMS-STATUS-REPORT':
sms = StatusReport(self, int(msgStat), smsDict['reference'], smsDict['number'], smsDict['time'], smsDict['discharge'], smsDict['status'])
else:
Expand Down Expand Up @@ -1457,7 +1468,8 @@ def readStoredSms(self, index, memory=None):
pdu = msgData[1]
smsDict = decodeSmsPdu(pdu)
if smsDict['type'] == 'SMS-DELIVER':
return ReceivedSms(self, int(stat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc'], smsDict.get('udh', []))
concat = self._getConcat(smsDict)
return ReceivedSms(self, int(stat), smsDict['number'], smsDict['time'], smsDict['text'], smsDict['smsc'], smsDict.get('udh', []), concat)
elif smsDict['type'] == 'SMS-STATUS-REPORT':
return StatusReport(self, int(stat), smsDict['reference'], smsDict['number'], smsDict['time'], smsDict['discharge'], smsDict['status'])
else:
Expand Down