📄 gammudispatcher.py
字号:
#! /usr/bin/env python# -*- coding: ISO8859-1 -*-## Copyright 2006 UNINETT AS## This file is part of Network Administration Visualized (NAV)## NAV is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## NAV is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with NAV; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#"""The smsd dispatcher for Gammu.This dispatcher takes care of all communication between smsd and Gammu. Gammuis used to send SMS messages via a cell phone connected to the server with aserial cable, USB cable, IR or Bluetooth. See http://www.gammu.org/ for moreinformation.Depends on python-gammu."""__copyright__ = "Copyright 2006 UNINETT AS"__license__ = "GPL"__author__ = "Stein Magnus Jodal (stein.magnus.jodal@uninett.no)"__id__ = "$Id: gammudispatcher.py 3963 2007-04-12 08:58:37Z mortenv $"from nav.smsd.dispatcher import *try: import gammuexcept ImportError, 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: ``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 = sms.decode('UTF-8') # We got a python-gammu binding :-) sm = gammu.StateMachine() try: # Typically ~root/.gammurc or ~navcron/.gammurc sm.ReadConfig() except IOError, 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, error: raise PermanentDispatcherError, \ "GSM %s error %d: %s" % (error[0]['Where'], error[0]['Code'], error[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, error: raise DispatcherError, "GSM %s error %d: %s" % (error[0]['Where'], error[0]['Code'], error[0]['Text']) except Exception, error: self.logger.critical('blipp blopp: %s', error) if type(smsid) == int: result = True else: result = False return (sms, sent, ignored, result, smsid)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -