diff --git a/examples/sms_handler_demo.py b/examples/sms_handler_demo.py index 1fbfdeb..c7901a6 100755 --- a/examples/sms_handler_demo.py +++ b/examples/sms_handler_demo.py @@ -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 {} + 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') diff --git a/gsmmodem/modem.py b/gsmmodem/modem.py index a7e914c..4b47c93 100644 --- a/gsmmodem/modem.py +++ b/gsmmodem/modem.py @@ -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 @@ -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 """ @@ -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. @@ -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: @@ -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: