Пример #1
0
    def sendsms(self, phone, msgs):
        """
        Send SMS using Gammu.

        Arguments:
            ``phone'' is the phone number the messages are to be dispatched to.
            ``msgs'' is a list of messages ordered with the most severe first.
            Each message is a tuple with ID, text and severity of the message.

        Returns five values:
            The formatted SMS.
            A list of IDs of sent messages.
            A list of IDs of ignored messages.
            A boolean which is true for success and false for failure.
            An integer which is the sending ID if available or 0 otherwise.
        """

        # Format SMS
        (sms, sent, ignored) = self.formatsms(msgs)
        sms = decode_sms_to_unicode(sms)

        # We got a python-gammu binding :-)
        sm = gammu.StateMachine()

        try:
            # Typically ~root/.gammurc or ~$NAV_USER/.gammurc
            sm.ReadConfig()
        except IOError as error:
            raise PermanentDispatcherError(error)

        try:
            # Fails if e.g. phone is not connected
            # See http://www.gammu.org/wiki/index.php?title=Gammu:Error_Codes
            # for complete list of errors fetched here
            sm.Init()
        except gammu.GSMError as error:
            raise PermanentDispatcherError(
                "GSM %s error %d: %s" %
                (error.args[0]['Where'], error.args[0]['Code'],
                 error.args[0]['Text']))

        message = {'Text': sms, 'SMSC': {'Location': 1}, 'Number': phone}

        try:
            # Tested with:
            # - Nokia 6610, Tekram IRmate 410U and Gammu 1.07.00
            # - Sony Ericsson K310, USB cable, Gammu 1.06.00, python-gammu 0.13
            smsid = sm.SendSMS(message)
        except gammu.GSMError as error:
            raise DispatcherError(
                "GSM %s error %d: %s" %
                (error.args[0]['Where'], error.args[0]['Code'],
                 error.args[0]['Text']))

        if isinstance(smsid, int):
            result = True
        else:
            result = False

        return sms, sent, ignored, result, smsid
Пример #2
0
    def sendsms(self, phone, msgs):
        """Log SMS message and report pre-configured result."""

        # Format SMS
        (sms, sent_count, ignored_count) = self.formatsms(msgs)
        _logger.info("SMS to %s: %s", phone, sms)
        smsid = 0
        result = True

        if self.result == self.RESULT_FAIL:
            result = False
            _logger.info("Returning failed status")
        if self.result == self.RESULT_SUCCESS:
            _logger.info("Returning success status")
        elif self.result == self.RESULT_ERROR:
            _logger.info("Raising DispatcherError")
            raise DispatcherError("Failed, because I was configured to.")
        elif self.result == self.RESULT_PERMANENT:
            _logger.info("Raising PermanentDispatcherError")
            raise PermanentDispatcherError(
                "Failed permanently, because I was configured to.")

        return (sms, sent_count, ignored_count, result, smsid)
Пример #3
0
serial cable, USB cable, IR or Bluetooth. See http://www.gammu.org/ for more
information.

Depends on python-gammu.

"""

from nav.smsd.dispatcher import (Dispatcher, PermanentDispatcherError,
                                 DispatcherError)

from django.utils import six

try:
    import gammu
except ImportError as error:
    raise PermanentDispatcherError(
        'python-gammu not installed or misconfigured.')


class GammuDispatcher(Dispatcher):
    """The smsd dispatcher for Gammu."""
    def __init__(self, _config):
        """Constructor."""

        # Call mother's init
        Dispatcher.__init__(self)

    def sendsms(self, phone, msgs):
        """
        Send SMS using Gammu.

        Arguments: