/build/lib.linux-i686-2.6/rapidsms/backends/gsm.py

https://github.com/oluka/mapping_rapidsms
Python | 118 lines | 59 code | 35 blank | 24 comment | 14 complexity | ad0e2398d02f097ff83486a5fe4f3842 MD5 | raw file
  1. #!/usr/bin/env python
  2. # vim: ai ts=4 sts=4 et sw=4
  3. import time
  4. import pygsm
  5. from rapidsms.message import Message
  6. from rapidsms.connection import Connection
  7. from rapidsms.backends import Backend as BackendBase
  8. # number of seconds to wait between
  9. # polling the modem for incoming SMS
  10. POLL_INTERVAL = 10
  11. class Backend(BackendBase):
  12. #_title = "pyGSM"
  13. def configure(self, **kwargs):
  14. # strip any config settings that
  15. # obviously aren't for the modem
  16. for arg in ["title", "name"]:
  17. if arg in kwargs:
  18. kwargs.pop(arg)
  19. # store the rest to pass on to the
  20. # GsmModem() when RapidSMS starts
  21. self.modem_kwargs = kwargs
  22. self.modem = None
  23. def send(self, message):
  24. self.sent_messages += 1
  25. self.modem.send_sms(
  26. str(message.connection.identity),
  27. message.text)
  28. def gsm_log(self, modem, str, level):
  29. self.debug("%s: %s" % (level, str))
  30. def status(self):
  31. csq = self.modem.signal_strength()
  32. # convert the "real" signal
  33. # strength into a 0-4 scale
  34. if not csq: level = 0
  35. elif csq >= 30: level = 4
  36. elif csq >= 20: level = 3
  37. elif csq >= 10: level = 2
  38. else: level = 1
  39. vars = {
  40. "_signal": level,
  41. "_title": self.title,
  42. "Messages Sent": self.sent_messages,
  43. "Messages Received": self.received_messages }
  44. # pygsm can return the name of the network
  45. # operator since b19cf3. add it if we can
  46. if hasattr(self.modem, "network"):
  47. vars["Network Operator"] = self.modem.network
  48. return vars
  49. def run(self):
  50. while self.running:
  51. self.info("Polling modem for messages")
  52. msg = self.modem.next_message()
  53. if msg is not None:
  54. self.received_messages += 1
  55. # we got an sms! create RapidSMS Connection and
  56. # Message objects, and hand it off to the router
  57. c = Connection(self, msg.sender)
  58. m = Message(c, msg.text)
  59. self.router.send(m)
  60. # wait for POLL_INTERVAL seconds before continuing
  61. # (in a slightly bizarre way, to ensure that we abort
  62. # as soon as possible when the backend is asked to stop)
  63. for n in range(0, POLL_INTERVAL):
  64. if not self.running: return None
  65. time.sleep(1)
  66. def start(self):
  67. self.sent_messages = 0
  68. self.received_messages = 0
  69. self.modem = pygsm.GsmModem(
  70. logger=self.gsm_log,
  71. **self.modem_kwargs)
  72. # If we got the connection, call superclass to
  73. # start the run loop--it just sets self._running to True
  74. # and calls run.
  75. if self.modem is not None:
  76. BackendBase.start(self)
  77. def stop(self):
  78. # call superclass to stop--sets self._running
  79. # to False so that the 'run' loop will exit cleanly.
  80. BackendBase.stop(self)
  81. # disconnect from modem
  82. if self.modem is not None:
  83. self.modem.disconnect()