Use timers properly

This commit is contained in:
blitzmann
2015-10-25 17:52:53 -04:00
parent 8f08f8efb8
commit 9d7a3605dc
3 changed files with 27 additions and 43 deletions

View File

@@ -27,10 +27,10 @@ class CrestFittings(wx.Frame):
self.stLogged = wx.StaticText(self, wx.ID_ANY, "Currently logged in as %s"%sCrest.implicitCharacter.name, wx.DefaultPosition, wx.DefaultSize)
self.stLogged.Wrap( -1 )
characterSelectSizer.Add( self.stLogged, 0, wx.ALL, 5 )
characterSelectSizer.Add( self.stLogged, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
else:
self.charChoice = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, [])
characterSelectSizer.Add( self.charChoice, 1, wx.ALL, 5 )
characterSelectSizer.Add( self.charChoice, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.updateCharList()
self.fetchBtn = wx.Button( self, wx.ID_ANY, u"Fetch Fits", wx.DefaultPosition, wx.DefaultSize, 5 )
@@ -113,21 +113,24 @@ class ExportToEve(wx.Frame):
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE))
sCrest = service.Crest.getInstance()
mainSizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
if sCrest.settings.get('mode') == 0:
self.stLogged = wx.StaticText(self, wx.ID_ANY, "Currently logged in as %s"%sCrest.implicitCharacter.name, wx.DefaultPosition, wx.DefaultSize)
self.stLogged.Wrap( -1 )
mainSizer.Add( self.stLogged, 0, wx.ALL, 5 )
hSizer.Add( self.stLogged, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
else:
self.charChoice = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, [])
mainSizer.Add( self.charChoice, 1, wx.ALL, 5 )
hSizer.Add( self.charChoice, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
self.updateCharList()
self.charChoice.SetSelection(0)
self.exportBtn = wx.Button( self, wx.ID_ANY, u"Export Fit", wx.DefaultPosition, wx.DefaultSize, 5 )
mainSizer.Add( self.exportBtn, 0, wx.ALL, 5 )
hSizer.Add( self.exportBtn, 0, wx.ALL, 5 )
mainSizer.Add( hSizer, 0, wx.EXPAND, 5 )
self.exportBtn.Bind(wx.EVT_BUTTON, self.exportFitting)
@@ -137,7 +140,7 @@ class ExportToEve(wx.Frame):
pub.subscribe(self.ssoLogout, 'logout_success')
self.SetSizer(mainSizer)
self.SetSizer(hSizer)
self.SetStatusBar(self.statusbar)
self.Layout()
@@ -200,7 +203,7 @@ class CrestCharacterInfo(wx.Dialog):
mainSizer.Add( self.characterText, 0, wx.ALIGN_CENTRE | wx.ALL, 5 )
self.pic = wx.StaticBitmap(self, -1, wx.EmptyBitmap(128, 128))
mainSizer.Add(self.pic, 0, wx.EXPAND|wx.ALIGN_CENTER, 5 )
mainSizer.Add(self.pic, 0, wx.ALIGN_CENTRE | wx.ALL, 5 )
self.coutdownText = wx.StaticText( self, wx.ID_ANY, "", wx.DefaultPosition, wx.DefaultSize)
self.coutdownText.Wrap( -1 )
@@ -300,7 +303,7 @@ class CrestMgmt(wx.Dialog):
class FittingsTreeView(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__ (self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, style=wx.TAB_TRAVERSAL)
wx.Panel.__init__(self, parent, id=wx.ID_ANY)
self.parent = parent
pmainSizer = wx.BoxSizer(wx.VERTICAL)

View File

@@ -1,25 +0,0 @@
from threading import Timer
class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)
def start(self):
if not self.is_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False

View File

@@ -9,8 +9,8 @@ from service import pycrest
import service
from service.server import *
import config
from gui.utils.repeatedTimer import RepeatedTimer
import logging
import threading
logger = logging.getLogger(__name__)
@@ -30,12 +30,13 @@ class Crest():
def __init__(self):
self.settings = service.settings.CRESTSettings.getInstance()
self.httpd = StoppableHTTPServer(('', 6461), AuthHandler)
logger.debug(self.httpd)
self.scopes = ['characterFittingsRead', 'characterFittingsWrite']
self.state = None
self.ssoTimer = RepeatedTimer(1, self.logout)
# these will be set when needed
self.httpd = None
self.state = None
self.ssoTimer = None
self.httpdTimer = None
# Base EVE connection that is copied to all characters
self.eve = pycrest.EVE(
@@ -89,18 +90,23 @@ class Crest():
return res
def logout(self):
logging.debug("Character logout")
self.implicitCharacter = None
self.ssoTimer.stop()
wx.CallAfter(pub.sendMessage, 'logout_success', message=None)
def stopServer(self):
logging.debug("Stopping Server")
self.httpd.stop()
self.httpdTimer.stop()
def startServer(self):
logging.debug("Starting server")
self.httpd = StoppableHTTPServer(('', 6461), AuthHandler)
thread.start_new_thread(self.httpd.serve, ())
self.httpdTimer = RepeatedTimer(60, self.stopServer)
# keep server going for only 60 seconds
self.httpdTimer = threading.Timer(60, self.stopServer)
self.httpdTimer.start()
self.state = str(uuid.uuid4())
return self.eve.auth_uri(scopes=self.scopes, state=self.state)
@@ -120,7 +126,7 @@ class Crest():
access_token=message['access_token'][0],
expires_in=int(message['expires_in'][0])
)
self.ssoTimer.interval = int(message['expires_in'][0])
self.ssoTimer = threading.Timer(int(message['expires_in'][0]), self.logout)
self.ssoTimer.start()
eve()