diff --git a/apps/python/ACLIB/ACLIB.ini b/apps/python/ACLIB/ACLIB.ini new file mode 100644 index 0000000..f742321 --- /dev/null +++ b/apps/python/ACLIB/ACLIB.ini @@ -0,0 +1,19 @@ +[DEFAULT] +; Global +write_meta = False ; Store Meta Data (Creates a .ini file for the car in usage and stores values for inspection [e.g. ideal tyre temperature]). +write_acd = True ; Store ACD Files (Creates a folder of files that are compressed in a .acd file for the car in usage.) +log_to_AC = False ; Log to AC (Logs to the AC py_log.txt log file if True, otherwise creates a new log file only for ACLIB.) +debug = False ; Debug (Defines if ACLIB is started in debug mode) +update_to_latest = False ; Download the latest version of this library automatically. + +[FILES AND FOLDERS] +; Files and Folders +log_folder = ; Log Folder (Relative directory to ACLIB to store log files) ; local directory +log_file = log.txt ; Log File ; local file (*.txt) +meta_folder = metadata ; Meta Data Folder (Stores .ini files for meta data [see write_meta]) ; local directory +acd_folder = metadata ; Meta Data Folder (Stores folders of acd data [see write_acd]) ; local directory + +[ACLIB FUEL] +mode = essential; Display mode ; essential, minimal, compact +scale = 10 ; App scale ; from 10% to 250% +PIT = 83 ; Teleport to pits; keyboard button \ No newline at end of file diff --git a/apps/python/ACLIB/ACLIB.py b/apps/python/ACLIB/ACLIB.py index 6040456..9432453 100644 --- a/apps/python/ACLIB/ACLIB.py +++ b/apps/python/ACLIB/ACLIB.py @@ -12,8 +12,10 @@ from settings import * from util.log import log, tb, Log +from util.net import Repository from memory.ac_data import ACData from memory.ac_meta import ACMeta +from memory.aclib_helper import ACLIBHelper from ui.gui.ac_widget import ACApp @@ -23,6 +25,7 @@ class ACLIB: DEBUG = False AC_DATA = None AC_META = None + ACLIB_HELPER = None @staticmethod def init(): @@ -36,8 +39,11 @@ def init(): Log.LOG_2_AC = get_or_set('log_to_AC', False) + Repository() + ACLIB.AC_DATA = ACData() ACLIB.AC_META = ACMeta(ACLIB.AC_DATA) + ACLIB.ACLIB_HELPER = ACLIBHelper(ACLIB.AC_DATA, ACLIB.AC_META) # Dummy used to put apps in the same category. aclib = ACApp('ACLIB', -10000, -10000, 0, 0, True, True) @@ -77,6 +83,7 @@ def acMain(version: int = 0): tb(e) ACLIB.AC_DATA.init() + #devices.mouse.run() except Exception as e: tb(e) @@ -96,6 +103,10 @@ def acUpdate(delta: float): ACLIB.AC_DATA.update(delta) ac.ext_perfEnd('ACLIB_Standalone') + ac.ext_perfBegin('ACLIB_Helper') + ACLIB.ACLIB_HELPER.update(delta) + ac.ext_perfEnd('ACLIB_Helper') + # Call the update function for every app stored in the app list. for _, app in ACLIB.APPS.items(): if app.active: diff --git a/apps/python/ACLIB/source/memory/aclib_helper.py b/apps/python/ACLIB/source/memory/aclib_helper.py new file mode 100644 index 0000000..a9da749 --- /dev/null +++ b/apps/python/ACLIB/source/memory/aclib_helper.py @@ -0,0 +1,51 @@ +from memory.ac_data import ACData +from memory.ac_meta import ACMeta + + +class ACLIBHelper: + def __init__(self, ac_data: ACData, ac_meta: ACMeta): + self._data = ac_data + self._meta = ac_meta + + self._fuel = { + 'km_ref': 0.0, + 'km_avg': 0.0, + 'min_ref': 0.0, + 'min_avg': 0.0, + 'lap_ref': 0.0, + 'lap_avg': 0.0 + } + + self._data.on(ACData.EVENT.READY, self.on_ready) + + def on_ready(self): + self._data.on(ACData.EVENT.KM_CHANGED, self.on_km) + #self._data.on(ACData.EVENT.MIN_CHANGED, self.on_min) + self._data.on(ACData.EVENT.LAP_CHANGED, self.on_lap) + + # Fuel + + def on_km(self, km: int): + fuel_used = min(self._fuel['km_ref'] - self._data.car.fuel, 0) + self._fuel['km_avg'] = ACLIBHelper.avg(self._fuel['km_avg'], fuel_used) + self._fuel['km_ref'] = self._data.car.fuel + + def on_min(self, minute: int): + fuel_used = min(self._fuel['min_ref'] - self._data.car.fuel, 0) + self._fuel['min_avg'] = ACLIBHelper.avg(self._fuel['min_avg'], fuel_used) + self._fuel['min_ref'] = self._data.car.fuel + + def on_lap(self, lap: int): + fuel_used = min(self._fuel['lap_ref'] - self._data.car.fuel, 0) + self._fuel['lap_avg'] = ACLIBHelper.avg(self._fuel['lap_avg'], fuel_used) + self._fuel['lap_ref'] = self._data.car.fuel + + @property + def fuel(self): + return self._fuel + + @staticmethod + def avg(avg: float, new: float): + if avg == 0: + return round(new, 1) + return round((avg + new) / 2, 1) diff --git a/apps/python/ACLIB/source/settings.py b/apps/python/ACLIB/source/settings.py index 96985c6..0d50812 100644 --- a/apps/python/ACLIB/source/settings.py +++ b/apps/python/ACLIB/source/settings.py @@ -45,3 +45,4 @@ def get_or_set(key: str, value): get_or_set('write_acd', True) get_or_set('write_meta', False) get_or_set('log_to_AC', False) +get_or_set('update_to_latest', False) diff --git a/apps/python/ACLIB/source/util/net.py b/apps/python/ACLIB/source/util/net.py new file mode 100644 index 0000000..f147320 --- /dev/null +++ b/apps/python/ACLIB/source/util/net.py @@ -0,0 +1,45 @@ +import io +import requests +import zipfile + +from settings import AC_INSTALL_DIR, VERSION, get +from util.log import log + + +class Repository: + def __init__(self): + self._is_latest = True + self._latest = VERSION + + if get('update_to_latest'): + response = requests.get('https://api.github.com/repos/styinx/ACLIB/releases/latest').json() + self._latest = response.get('tag_name', VERSION) + + if self._latest != VERSION: + log('New library version available: ' + self._latest) + self._is_latest = False + + @staticmethod + def archive_files(archive: zipfile.ZipFile, remove_prefix: str=''): + for zipinfo in archive.infolist(): + file = zipinfo.filename + + # Modify only files + if not file.endswith('/'): + + # Remove prefix if any + folder_index = file.find(remove_prefix) + subfolder_start = file.find('/', folder_index) + 1 + new_file = file[subfolder_start:] + + zipinfo.filename = new_file + yield zipinfo + + def update(self): + if not self._is_latest: + log('Downloading latest version ...') + response = requests.get('https://github.com/styinx/ACLIB/archive/{}.zip'.format(self._latest)) + log('Done\nExtracing files ...') + archive = zipfile.ZipFile(io.BytesIO(response.content)) + archive.extractall(AC_INSTALL_DIR, Repository.archive_files(archive, 'ACLIB-' + self._latest)) + log('Done\nLatest version will be available with next start!')