mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-06-07 19:03:23 +03:00
Switched to using a timer for inara updates
This adds a new thread that will run in a loop sending inara events once ever 30 seconds. The old method of sending once for "special" events has been removed.
This commit is contained in:
parent
9fcaeb5feb
commit
72498b3bbc
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import json
|
import json
|
||||||
|
from typing import Any, Union
|
||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
@ -26,7 +27,7 @@ FAKE = ['CQC', 'Training', 'Destination'] # Fake systems that shouldn't be sent
|
|||||||
CREDIT_RATIO = 1.05 # Update credits if they change by 5% over the course of a session
|
CREDIT_RATIO = 1.05 # Update credits if they change by 5% over the course of a session
|
||||||
|
|
||||||
|
|
||||||
this = sys.modules[__name__] # For holding module globals
|
this: Any = sys.modules[__name__] # For holding module globals
|
||||||
this.session = requests.Session()
|
this.session = requests.Session()
|
||||||
this.queue = Queue() # Items to be sent to Inara by worker thread
|
this.queue = Queue() # Items to be sent to Inara by worker thread
|
||||||
this.lastlocation = None # eventData from the last Commander's Flight Log event
|
this.lastlocation = None # eventData from the last Commander's Flight Log event
|
||||||
@ -50,7 +51,8 @@ this.fleet = None
|
|||||||
this.shipswap = False # just swapped ship
|
this.shipswap = False # just swapped ship
|
||||||
|
|
||||||
this.last_update_time = time.time() # last time we updated (set to now because we're going to update quickly)
|
this.last_update_time = time.time() # last time we updated (set to now because we're going to update quickly)
|
||||||
FLOOD_LIMIT_SECONDS = 45 # minimum time between sending non-major cargo triggered messages
|
FLOOD_LIMIT_SECONDS = 30 # minimum time between sending events
|
||||||
|
this.timer_run = True
|
||||||
|
|
||||||
|
|
||||||
# Main window clicks
|
# Main window clicks
|
||||||
@ -85,6 +87,10 @@ def plugin_start3(plugin_dir):
|
|||||||
this.thread = Thread(target = worker, name = 'Inara worker')
|
this.thread = Thread(target = worker, name = 'Inara worker')
|
||||||
this.thread.daemon = True
|
this.thread.daemon = True
|
||||||
this.thread.start()
|
this.thread.start()
|
||||||
|
|
||||||
|
this.timer_thread = Thread(target=call_timer, name='Inara timer')
|
||||||
|
this.timer_thread.daemon = True
|
||||||
|
this.timer_thread.start()
|
||||||
return 'Inara'
|
return 'Inara'
|
||||||
|
|
||||||
def plugin_app(parent):
|
def plugin_app(parent):
|
||||||
@ -95,12 +101,14 @@ def plugin_app(parent):
|
|||||||
|
|
||||||
def plugin_stop():
|
def plugin_stop():
|
||||||
# Send any unsent events
|
# Send any unsent events
|
||||||
call()
|
call(force=True)
|
||||||
# Signal thread to close and wait for it
|
# Signal thread to close and wait for it
|
||||||
this.queue.put(None)
|
this.queue.put(None)
|
||||||
this.thread.join()
|
this.thread.join()
|
||||||
this.thread = None
|
this.thread = None
|
||||||
|
|
||||||
|
this.timer_run = False
|
||||||
|
|
||||||
def plugin_prefs(parent, cmdr, is_beta):
|
def plugin_prefs(parent, cmdr, is_beta):
|
||||||
|
|
||||||
PADX = 10
|
PADX = 10
|
||||||
@ -188,7 +196,7 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
|
|
||||||
# Send any unsent events when switching accounts
|
# Send any unsent events when switching accounts
|
||||||
if cmdr and cmdr != this.cmdr:
|
if cmdr and cmdr != this.cmdr:
|
||||||
call()
|
call(force=True)
|
||||||
|
|
||||||
this.cmdr = cmdr
|
this.cmdr = cmdr
|
||||||
this.FID = state['FID']
|
this.FID = state['FID']
|
||||||
@ -245,8 +253,6 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
|
|
||||||
if config.getint('inara_out') and not is_beta and not this.multicrew and credentials(cmdr):
|
if config.getint('inara_out') and not is_beta and not this.multicrew and credentials(cmdr):
|
||||||
try:
|
try:
|
||||||
old_events = len(this.events) # Will only send existing events if we add a new event below
|
|
||||||
|
|
||||||
# Dump starting state to Inara
|
# Dump starting state to Inara
|
||||||
|
|
||||||
if (this.newuser or
|
if (this.newuser or
|
||||||
@ -305,6 +311,8 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
|
|
||||||
this.loadout = make_loadout(state)
|
this.loadout = make_loadout(state)
|
||||||
add_event('setCommanderShipLoadout', entry['timestamp'], this.loadout)
|
add_event('setCommanderShipLoadout', entry['timestamp'], this.loadout)
|
||||||
|
|
||||||
|
call() # Call here just to be sure that if we can send, we do, otherwise it'll get it in the next tick
|
||||||
|
|
||||||
|
|
||||||
# Promotions
|
# Promotions
|
||||||
@ -437,23 +445,17 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
|
|||||||
# if our cargo differers from last we checked, we're at a station,
|
# if our cargo differers from last we checked, we're at a station,
|
||||||
# and our flood limit isnt covered, queue an update
|
# and our flood limit isnt covered, queue an update
|
||||||
should_poll = this.cargo != cargo and time.time() - this.last_update_time > FLOOD_LIMIT_SECONDS
|
should_poll = this.cargo != cargo and time.time() - this.last_update_time > FLOOD_LIMIT_SECONDS
|
||||||
|
|
||||||
# Send event(s) to Inara
|
|
||||||
if entry['event'] == 'ShutDown' or len(this.events) > old_events or should_poll:
|
|
||||||
|
|
||||||
# Send cargo and materials if changed
|
# Send cargo and materials if changed
|
||||||
if this.cargo != cargo:
|
if this.cargo != cargo:
|
||||||
add_event('setCommanderInventoryCargo', entry['timestamp'], cargo)
|
add_event('setCommanderInventoryCargo', entry['timestamp'], cargo)
|
||||||
this.cargo = cargo
|
this.cargo = cargo
|
||||||
materials = []
|
materials = []
|
||||||
for category in ['Raw', 'Manufactured', 'Encoded']:
|
for category in ['Raw', 'Manufactured', 'Encoded']:
|
||||||
materials.extend([ OrderedDict([('itemName', k), ('itemCount', state[category][k])]) for k in sorted(state[category]) ])
|
materials.extend([ OrderedDict([('itemName', k), ('itemCount', state[category][k])]) for k in sorted(state[category]) ])
|
||||||
if this.materials != materials:
|
if this.materials != materials:
|
||||||
add_event('setCommanderInventoryMaterials', entry['timestamp'], materials)
|
add_event('setCommanderInventoryMaterials', entry['timestamp'], materials)
|
||||||
this.materials = materials
|
this.materials = materials
|
||||||
|
|
||||||
# Queue a call to Inara
|
|
||||||
call()
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug('Adding events', exc_info=e)
|
logger.debug('Adding events', exc_info=e)
|
||||||
@ -856,12 +858,20 @@ def add_event(name, timestamp, data):
|
|||||||
('eventData', data),
|
('eventData', data),
|
||||||
]))
|
]))
|
||||||
|
|
||||||
|
def call_timer(wait=FLOOD_LIMIT_SECONDS):
|
||||||
|
while this.timer_run:
|
||||||
|
time.sleep(wait)
|
||||||
|
if this.timer_run: # check again in here just in case we're closing and the stars align
|
||||||
|
call()
|
||||||
|
|
||||||
# Queue a call to Inara, handled in Worker thread
|
# Queue a call to Inara, handled in Worker thread
|
||||||
def call(callback=None):
|
def call(callback=None, force=False):
|
||||||
if not this.events:
|
if not this.events:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if (time.time() - this.last_update_time) <= FLOOD_LIMIT_SECONDS and not force:
|
||||||
|
return
|
||||||
|
|
||||||
this.last_update_time = time.time()
|
this.last_update_time = time.time()
|
||||||
|
|
||||||
data = OrderedDict([
|
data = OrderedDict([
|
||||||
|
Loading…
x
Reference in New Issue
Block a user