Files
barcode_reader_pc/server/config.py
T

44 lines
1.4 KiB
Python

import json
import os
import socket
from pathlib import Path
CONFIG_FILE = Path(__file__).parent / "config.json"
DEFAULT_CONFIG = {
"host": "0.0.0.0",
"port": 8765,
"udp_discovery_port": 8766,
"server_name": socket.gethostname(),
"pin": "", # Empty means no PIN required; or 4-digit code
"suffix": "enter", # "enter", "tab", "none" or custom string
"prefix": "",
"typing_delay": 0.005, # Delay between keystrokes in seconds
"mode": "type", # "type" or "paste"
"listening": True, # Whether the server actively processes and types barcodes
"minimize_to_tray": True, # Close/minimize button hides to system tray
"log_history": True,
}
def load_config() -> dict:
if CONFIG_FILE.exists():
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
config = {**DEFAULT_CONFIG, **data}
if not config.get("server_name"):
config["server_name"] = socket.gethostname()
return config
except Exception as e:
print(f"Avviso: Errore nella lettura di {CONFIG_FILE}: {e}. Uso configurazione predefinita.")
return DEFAULT_CONFIG.copy()
def save_config(config: dict) -> None:
try:
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(config, f, indent=4)
except Exception as e:
print(f"Errore nel salvataggio della configurazione: {e}")