1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-12 13:22:54 +03:00

CAPI: Pass tk_response_event in Request to optionally use in response

EDMC.py will also need to make CAPI queries using the new threaded
method, but it has no tkinter, thus we need to pass in the event name
when we expect one to be generated, and only generate it if this is not
None.
This commit is contained in:
Athanasius 2021-08-24 12:41:02 +01:00
parent 1c93c3a9fa
commit 1dcefafc73
No known key found for this signature in database
GPG Key ID: AE3E527847057C7D
3 changed files with 37 additions and 15 deletions

View File

@ -375,6 +375,7 @@ SHIPYARD_HTML_TEMPLATE = """
class AppWindow(object): class AppWindow(object):
"""Define the main application window.""" """Define the main application window."""
_CAPI_RESPONSE_TK_EVENT_NAME = '<<CAPIResponse>>'
# Tkinter Event types # Tkinter Event types
EVENT_KEYPRESS = 2 EVENT_KEYPRESS = 2
EVENT_BUTTON = 4 EVENT_BUTTON = 4
@ -655,7 +656,7 @@ class AppWindow(object):
self.w.bind('<Return>', self.capi_request_data) self.w.bind('<Return>', self.capi_request_data)
self.w.bind('<KP_Enter>', self.capi_request_data) self.w.bind('<KP_Enter>', self.capi_request_data)
self.w.bind_all('<<Invoke>>', self.capi_request_data) # Ask for CAPI queries to be performed self.w.bind_all('<<Invoke>>', self.capi_request_data) # Ask for CAPI queries to be performed
self.w.bind_all('<<CAPIResponse>>', self.capi_handle_response) self.w.bind_all(self._CAPI_RESPONSE_TK_EVENT_NAME, self.capi_handle_response)
self.w.bind_all('<<JournalEvent>>', self.journal_event) # Journal monitoring self.w.bind_all('<<JournalEvent>>', self.journal_event) # Journal monitoring
self.w.bind_all('<<DashboardEvent>>', self.dashboard_event) # Dashboard monitoring self.w.bind_all('<<DashboardEvent>>', self.dashboard_event) # Dashboard monitoring
self.w.bind_all('<<PluginError>>', self.plugin_error) # Statusbar self.w.bind_all('<<PluginError>>', self.plugin_error) # Statusbar

View File

@ -516,9 +516,10 @@ class EDMCCAPIReturn:
"""Base class for Request, Failure or Response.""" """Base class for Request, Failure or Response."""
def __init__( def __init__(
self, query_time: int, retrying: bool = False, self, query_time: int, tk_response_event: Optional[str] = None,
play_sound: bool = False, auto_update: bool = False retrying: bool = False, play_sound: bool = False, auto_update: bool = False
): ):
self.tk_response_event = tk_response_event # Name of tk event to generate when response queued.
self.query_time: int = query_time # When this query is considered to have started (time_t). self.query_time: int = query_time # When this query is considered to have started (time_t).
self.retrying: bool = retrying # Whether this is already a retry. self.retrying: bool = retrying # Whether this is already a retry.
self.play_sound: bool = play_sound # Whether to play good/bad sounds for success/failure. self.play_sound: bool = play_sound # Whether to play good/bad sounds for success/failure.
@ -529,10 +530,14 @@ class EDMCCAPIRequest(EDMCCAPIReturn):
"""Encapsulates a request for CAPI data.""" """Encapsulates a request for CAPI data."""
def __init__( def __init__(
self, endpoint: str, self, endpoint: str, query_time: int,
query_time: int, retrying: bool = False, play_sound: bool = False, auto_update: bool = False tk_response_event: Optional[str] = None, retrying: bool = False,
play_sound: bool = False, auto_update: bool = False
): ):
super().__init__(query_time=query_time, retrying=retrying, play_sound=play_sound, auto_update=auto_update) super().__init__(
query_time=query_time, tk_response_event=tk_response_event, retrying=retrying,
play_sound=play_sound, auto_update=auto_update
)
self.endpoint: str = endpoint # The CAPI query to perform. self.endpoint: str = endpoint # The CAPI query to perform.
@ -927,6 +932,9 @@ class Session(object):
) )
) )
# If the query came from EDMC.(py|exe) there's no tk to send an
# event too, so assume it will be polling there response queue.
if query.tk_response_event is not None:
self.tk_master.event_generate('<<CAPIResponse>>') self.tk_master.event_generate('<<CAPIResponse>>')
logger.info('CAPI worker thread DONE') logger.info('CAPI worker thread DONE')
@ -936,12 +944,15 @@ class Session(object):
self.capi_query_queue.put(None) self.capi_query_queue.put(None)
def query( def query(
self, endpoint: str, self, endpoint: str, query_time: int,
query_time: int, retrying: bool = False, play_sound: bool = False, auto_update: bool = False tk_response_event: Optional[str] = None, retrying: bool = False,
play_sound: bool = False, auto_update: bool = False
) -> None: ) -> None:
""" """
Perform a query against the specified CAPI endpoint. Perform a query against the specified CAPI endpoint.
:param endpoint: The CAPI endpoint to query.
:param tk_response_event: Name of tk event to generate when response queued.
:param query_time: When this query was initiated. :param query_time: When this query was initiated.
:param retrying: Whether this is a retry. :param retrying: Whether this is a retry.
:param play_sound: Whether the app should play a sound on error. :param play_sound: Whether the app should play a sound on error.
@ -950,7 +961,10 @@ class Session(object):
logger.trace_if('capi.query', f'Performing query for endpoint "{endpoint}"') logger.trace_if('capi.query', f'Performing query for endpoint "{endpoint}"')
if self.state == Session.STATE_INIT: if self.state == Session.STATE_INIT:
if self.login(): if self.login():
self.query(endpoint, query_time, play_sound=play_sound, auto_update=auto_update) self.query(
endpoint, query_time, tk_response_event=tk_response_event, play_sound=play_sound,
auto_update=auto_update
)
return return
elif self.state == Session.STATE_AUTH: elif self.state == Session.STATE_AUTH:
@ -964,6 +978,7 @@ class Session(object):
self.capi_query_queue.put( self.capi_query_queue.put(
EDMCCAPIRequest( EDMCCAPIRequest(
endpoint=endpoint, endpoint=endpoint,
tk_response_event=tk_response_event,
retrying=retrying, retrying=retrying,
query_time=query_time, query_time=query_time,
play_sound=play_sound, play_sound=play_sound,
@ -973,30 +988,34 @@ class Session(object):
def profile( def profile(
self, self,
query_time: int = int(time.time()), retrying: bool = False, query_time: int = int(time.time()),
tk_response_event: Optional[str] = None, retrying: bool = False,
play_sound: bool = False, auto_update: bool = False play_sound: bool = False, auto_update: bool = False
) -> None: ) -> None:
""" """
Perform general CAPI /profile endpoint query. Perform general CAPI /profile endpoint query.
:param query_time: When this query was initiated. :param query_time: When this query was initiated.
:param tk_response_event: Name of tk event to generate when response queued.
:param retrying: Whether this is a retry. :param retrying: Whether this is a retry.
:param play_sound: Whether the app should play a sound on error. :param play_sound: Whether the app should play a sound on error.
:param auto_update: Whether this request was triggered automatically. :param auto_update: Whether this request was triggered automatically.
""" """
self.query( self.query(
self.FRONTIER_CAPI_PATH_PROFILE, query_time=query_time, retrying=retrying, self.FRONTIER_CAPI_PATH_PROFILE, query_time=query_time,
tk_response_event=tk_response_event, retrying=retrying,
play_sound=play_sound, auto_update=auto_update play_sound=play_sound, auto_update=auto_update
) )
def station( def station(
self, self, query_time: int, tk_response_event: Optional[str] = None,
query_time: int, retrying: bool = False, play_sound: bool = False, auto_update: bool = False retrying: bool = False, play_sound: bool = False, auto_update: bool = False
) -> None: ) -> None:
""" """
Perform CAPI quer(y|ies) for station data. Perform CAPI quer(y|ies) for station data.
:param query_time: When this query was initiated. :param query_time: When this query was initiated.
:param tk_response_event: Name of tk event to generate when response queued.
:param retrying: Whether this is a retry. :param retrying: Whether this is a retry.
:param play_sound: Whether the app should play a sound on error. :param play_sound: Whether the app should play a sound on error.
:param auto_update: Whether this request was triggered automatically. :param auto_update: Whether this request was triggered automatically.
@ -1005,6 +1024,7 @@ class Session(object):
self.capi_query_queue.put( self.capi_query_queue.put(
EDMCCAPIRequest( EDMCCAPIRequest(
endpoint=self._CAPI_PATH_STATION, endpoint=self._CAPI_PATH_STATION,
tk_response_event=tk_response_event,
query_time=query_time, query_time=query_time,
retrying=retrying, retrying=retrying,
play_sound=play_sound, play_sound=play_sound,

View File

@ -281,6 +281,7 @@ class StatsDialog():
self.parent.update_idletasks() self.parent.update_idletasks()
try: try:
# TODO: This should use cached data
data = companion.session.profile() data = companion.session.profile()
except companion.ServerError as e: except companion.ServerError as e: