Skip to content

Commit

Permalink
Move URL for polling server from code to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Nagle committed Jun 23, 2016
1 parent d7873f4 commit 2ee6e6e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ System requirements:
Computer: Intel 32-bit, with hardware serial port. (USB to serial
converters will usually not work at 45.45 baud.)
Operating system Windows 7/Linux.
Python system: 2.6 or 2.7 (No 3.x yet.)
Required packages: win32api
Python system: 2.7 or 3.x
Required packages: bs4
pyserial
feedparser
Baudot teletype: Model 15, 19 or 28 in proper working condition.
Expand All @@ -57,7 +57,15 @@ System requirements:
the appropriate times.
For information on interfacing a current-loop Teletype to a PC,
see http://www.aetherltd.com"
see http://www.aetherltd.com".
For sending SMS messages:
An account with Twilio.
For receiving SMS messages:
A server which supports our message API.

System configuration issues:

Expand Down
6 changes: 0 additions & 6 deletions messager/baudotrss.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
import baudottty


#
# Suppress deprecation warnings. We know feedparser and BeautifulSoup need updates.
#
####warnings.filterwarnings(action='ignore', category=DeprecationWarning, module='BeautifulSoup')
####warnings.filterwarnings(action='ignore', category=DeprecationWarning, module='feedparser')
#
# get_script_dir
#
Expand All @@ -49,7 +44,6 @@ def get_script_dir(follow_symlinks=True):
path = os.path.realpath(path)
return os.path.dirname(path)

####print(get_script_dir())

#
# opentty -- create and open the TTY device
Expand Down
20 changes: 11 additions & 9 deletions messager/twiliofeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#
# Constants
#
SERVERPOLLURL = "http://www.aetherltd.com/cgi/ttypoll.cgi?" # URL for polling
TWILIOBASE = "https://api.twilio.com/2010-04-01/" # Twilio base URL

def expandplaceabbrev(fields, fieldname, placetable) :
Expand Down Expand Up @@ -70,7 +69,7 @@ def maketimelocal(dt) :
return(dt + delta) # apply time delta


def doservercmd(logger, accountsid, ourphoneno, cmd, v1=None, v2=None) :
def doservercmd(logger, serverpollurl, accountsid, ourphoneno, cmd, v1=None, v2=None) :
"""
Send command to server at Aetheric (not Twilio), get XML reply
"""
Expand All @@ -79,7 +78,7 @@ def doservercmd(logger, accountsid, ourphoneno, cmd, v1=None, v2=None) :
fields["v1"] = v1
if v2:
fields["v2"] = v2
url = SERVERPOLLURL + urllib.parse.urlencode(fields) # construct cmd URL
url = serverpollurl + urllib.parse.urlencode(fields) # construct cmd URL
logger.debug("SMS server cmd: " + url)
fd = urllib.request.urlopen(url) # open url
result = fd.read() # read contents
Expand All @@ -104,14 +103,17 @@ class Twiliofeed(feedmanager.Feed) :
#
# Called from outside the thread
#
def __init__(self, accountsid, authtoken, ourphoneno, logger) :
def __init__(self, serverpollurl, accountsid, authtoken, ourphoneno, hdrtitle, logger) :
feedmanager.Feed.__init__(self, "SMS", logger)
self.lock = threading.Lock() # lock object. One login at a time.
self.serverpollurl = serverpollurl # our site to poll for incoming SMS
self.accountsid = accountsid # accounts ID - for all requests
self.authtoken = authtoken # auth token
self.ourphoneno = ourphoneno # our phone number
if hdrtitle is None : # if no header title
hdrtitle = "SMS message" # default title for feed
self.hdrtitle = hdrtitle # title of this SMS source
self.errmsg = None # no pending error message
self.hdrtitle = "Aetheric Message"
self.url = self.hdrtitle
self.msgfrom = None # phone number of last message returned to user
self.logger = logger # debug og to here
Expand Down Expand Up @@ -246,7 +248,7 @@ def fetchitems(self) : # fetch more items from feed
while True : # until all available read
self.logger.debug("Polling SMS server starting after serial #%s"
% (self.lastserial,))
replyxml = doservercmd(self.logger, self.accountsid, self.ourphoneno, "getnext", self.lastserial + 1, None) # get next msg
replyxml = doservercmd(self.logger, self.serverpollurl, self.accountsid, self.ourphoneno, "getnext", self.lastserial + 1, None) # get next msg
newserial = self.handlereply(replyxml) # handle message
self.logger.debug("Poll complete.")
if newserial and newserial > self.lastserial: # if got message
Expand Down Expand Up @@ -374,7 +376,7 @@ def markitemsdone(self) : # mark any printed items as
while True: # until Queue.empty or network error
item = self.donequeue.get_nowait() # get input if any
serial = item.serial # serial number of done item
reply = doservercmd(self.logger, self.accountsid, self.ourphoneno,
reply = doservercmd(self.logger, self.serverpollurl, self.accountsid, self.ourphoneno,
"printed", serial, serial)
except queue.Empty: # if empty
return(True) # success
Expand All @@ -385,7 +387,7 @@ def markitemsdone(self) : # mark any printed items as
return(False)

#
# Unit test
# Unit test ***NEEDS WORK*** Obsolete
#
def test(accountsid) :
import logging
Expand All @@ -394,7 +396,7 @@ def test(accountsid) :
logger = logging.getLogger('Messager') # main logger
logger.setLevel(logging.DEBUG) # very verbose

feed = Twiliofeed(accountsid, logger)
feed = Twiliofeed(accountsid, logger) # ***NEEDS WORK***
feed.start()
for i in xrange(300) :
msg = feed.getitem()
Expand Down
9 changes: 5 additions & 4 deletions messager/userinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@ def __init__(self, tty, newsfeeds, config, logger) :
# SMS feed initialization
if config.has_section("twilio") : # if Twilio mode
self.smsmsgfeed = twiliofeed.Twiliofeed(
config.get("twilio", "serverpollurl"),
config.get("twilio", "accountsid"),
config.get("twilio", "authtoken"),
config.get("twilio", "phone"),
config.get("twilio", "title"),
self.logger)
if config.has_section("format") : # format config
self.cutmarks = config.getboolean("format","cutmarks")
Expand Down Expand Up @@ -530,10 +532,9 @@ def runui(self, initialcmd = None) :
self.abortthreads() # abort all threads
raise
except (KeyboardInterrupt) as message : # if shutdown
self.logger.error("Shutting down: " + str(message))
self.abortthreads() # abort all threads
raise
except (RuntimeError) as message : # if trouble
self.logger.error("Control-C - Shutting down and exiting.")
sys.exit() # exit normally
except (RuntimeError) as message : # if trouble
self.logger.exception("Unrecoverable error, aborting: " + str(message))
self.abortthreads() # abort all threads
raise # re-raise exception with other thread exited.
Expand Down

0 comments on commit 2ee6e6e

Please sign in to comment.