auto import wip

Updated remote_fetch to allow flexible machine_status input and reorganized file saving logic to categorize downloads based on file types. Modified test configuration to ensure compatibility with the new changes.
This commit is contained in:
edo-neo 2024-10-24 11:11:03 +02:00
parent fce0151ecc
commit 27fd430a0b
27 changed files with 52 additions and 14 deletions

View File

@ -25,7 +25,7 @@ class ArchiveSynchronizer(Component):
def __init__(self, config=None, name=None, period=1, lazy=True, paused=False, threaded=True):
super().__init__(config=config, name=name, period=period, lazy=lazy, paused=paused, threaded=threaded)
self.simulate = "--sim-archiver" in sys.argv
self.status= ArchiveSynchronizer.machine_status
self.machine_status = "offline"
def config_changed(self):
self.machine_id = self.config.machine_id
@ -57,20 +57,16 @@ class ArchiveSynchronizer(Component):
if self.hold_time > 0:
QThread.msleep(self.hold_time)
self.gcs_bucket = None
if "--dev-portal" in sys.argv:
self.update_machine_status()
self.remote_fetch(self.machine_status,self.machine_id)
super()._get()
def update_machine_status(self):
self.status = ArchiveSynchronizer.machine_status
#print(f"machine_status_global: {ArchiveSynchronizer.machine_status}") # TESTING
self.status_endpoint = f"https://dev.r5portal.it/api/device-info-update?machine-id={self.machine_id.upper()}&status={self.status}"
self.status_endpoint = f"https://dev.r5portal.it/api/device-info-update?machine-id={self.machine_id.upper()}&status={self.machine_status}"
if self.status not in ["working", "logged-in", "logged-out"]:
status_dict = {"last_status": "offline"}
else:
status_dict = {"last_status": self.status}
status_dict = {"last_status": self.machine_status}
response = None
try:
@ -82,21 +78,21 @@ class ArchiveSynchronizer(Component):
raise AssertionError("bad status response")
except AssertionError as e:
self.log.warning(
f"Status: {self.status}: failed to update machine status: {str(e)}: {response.status_code if response else 'no response'}: {response.content if response else 'no response'}"
f"Status: {self.machine_status}: failed to update machine status: {str(e)}: {response.status_code if response else 'no response'}: {response.content if response else 'no response'}"
)
return False
except (requests.ConnectionError, requests.Timeout) as e:
self.log.warning(
f"Status: {self.status}: failed to update machine status, archive_endpoint might be unreachable: {str(e)}"
f"Status: {self.machine_status}: failed to update machine status, archive_endpoint might be unreachable: {str(e)}"
)
return False
except Exception:
self.log.error(
f"Status: {self.status}: failed to update machine status:\n{traceback.format_exc()}:\n{response.status_code if response else 'no response'}: {response.content if response else 'no response'}"
f"Status: {self.machine_status}: failed to update machine status:\n{traceback.format_exc()}:\n{response.status_code if response else 'no response'}: {response.content if response else 'no response'}"
)
return False
self.log.info(f"Status: {self.status}: Machine Status Updated Successfully")
self.log.info(f"Status: {self.machine_status}: Machine Status Updated Successfully")
return True
def remote_archive(self, record):
r = None
@ -268,7 +264,6 @@ class ArchiveSynchronizer(Component):
f"An unexpected error occurred while downloading {specific_file_url}: {str(e)}")
errors[specific_file_url] = "Unexpected error"
# Return the list of downloaded files and any errors
return {
"downloaded_files": downloaded_files,
"errors": errors

View File

@ -1,6 +1,7 @@
import csv
import locale
import os
import shutil
import sys
import weakref
from glob import glob
@ -13,6 +14,8 @@ from ui.crud import Crud, Json_External_Dialog_Editor_Cell_Widget
from ui.helpers import replace_widget
from ui.recipe_spec_and_step_editor import Recipe_Spec_And_Step_Editor
from ui.widget import Widget
from datetime import datetime
from src.components import ArchiveSynchronizer
class Noner:
@ -30,6 +33,7 @@ class Recipe_Selection(Widget):
global noner
super().__init__()
self.config = config
self.archive_sync = ArchiveSynchronizer()
self.second_leak_test_enabled = self.config["hardware_config"]["second_leak_test"] == "present"
self.defaults = self.config.get("recipes_defaults", noner)
self.unsupported_steps = unsupported_steps
@ -282,7 +286,7 @@ class Recipe_Selection(Widget):
if csv_path is None:
csv_path, _ = QFileDialog.getOpenFileName(
None,
"Esportazione ricette",
"Importazione ricette",
"ricette.csv",
"CSV data (*.csv);;All Files (*)",
)
@ -508,3 +512,42 @@ class Recipe_Selection(Widget):
Recipes.delete().execute()
Steps.delete().execute()
self.crud.refresh()
def backup_current_recipes(self):
# Define the backup directory and file name
backup_dir = os.path.join('config', 'csv_import', 'backup_csv')
timestamp = datetime.now().strftime("%d%m%y")
backup_file = f"backup_{timestamp}.csv"
backup_path = os.path.join(backup_dir, backup_file)
# Ensure the backup directory exists
os.makedirs(backup_dir, exist_ok=True)
# Export current recipes to backup file
self.export_recipes(csv_path=backup_path)
def move_imported_csv(self, csv_path):
# Move the imported CSV to the 'imported_csv' directory
imported_dir = os.path.join('config', 'csv_import', 'imported_csv')
os.makedirs(imported_dir, exist_ok=True)
imported_path = os.path.join(imported_dir, os.path.basename(csv_path))
shutil.move(csv_path, imported_path)
self.log.info(f"Imported CSV moved to {imported_path}")
return imported_path
def check_and_import_auto_csv(self):
if self.archive_sync.machine_status ==" logged-in":
# Define the directory to check
auto_import_dir = os.path.join('config', 'csv_import', 'auto_csv_import')
# Check if the directory exists and is not empty
if os.path.exists(auto_import_dir) and os.listdir(auto_import_dir):
# Perform backup
self.backup_current_recipes()
# Move and import each CSV file in the directory
for csv_file in os.listdir(auto_import_dir):
csv_path = os.path.join(auto_import_dir, csv_file)
if os.path.isfile(csv_path) and csv_file.endswith(".csv"):
self.import_recipes(csv_path=csv_path)
self.move_imported_csv(csv_path)