76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
# tool/core/cmd/get_file.py
|
|
import struct
|
|
import zlib
|
|
from pathlib import Path
|
|
from core.utils import console, console_err
|
|
from core.protocol import COMMANDS
|
|
|
|
class get_file:
|
|
def __init__(self, bus):
|
|
self.bus = bus
|
|
|
|
def get(self, source_path: str, dest_path: str):
|
|
try:
|
|
p = Path(dest_path)
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
with open(p, 'wb') as f:
|
|
pass
|
|
except Exception as e:
|
|
console_err.print(f"Fehler: Kann Zieldatei nicht anlegen: {e}")
|
|
return None
|
|
|
|
source_path_bytes = source_path.encode('utf-8')
|
|
payload = struct.pack('B', len(source_path_bytes)) + source_path_bytes
|
|
device_file_crc = None
|
|
try:
|
|
self.bus.send_request(COMMANDS['crc_32'], payload)
|
|
crc_resp = self.bus.receive_response(length=4)
|
|
if crc_resp and crc_resp.get('type') == 'response':
|
|
device_file_crc = struct.unpack('<I', crc_resp['data'])[0]
|
|
except Exception:
|
|
device_file_crc = None
|
|
|
|
self.bus.send_request(COMMANDS['get_file'], payload)
|
|
|
|
stream_res = self.bus.receive_stream()
|
|
|
|
if not stream_res or stream_res.get('type') == 'error':
|
|
return None
|
|
|
|
file_data = stream_res['data']
|
|
remote_crc = stream_res['crc32']
|
|
|
|
if local_crc == remote_crc:
|
|
with open(p, 'wb') as f:
|
|
f.write(file_data)
|
|
success = True
|
|
else:
|
|
with open(p, 'wb') as f:
|
|
f.write(file_data)
|
|
success = False
|
|
|
|
return {
|
|
'success': success,
|
|
'source_path': source_path,
|
|
'dest_path': dest_path,
|
|
'crc32_remote': remote_crc,
|
|
'crc32_local': local_crc,
|
|
'crc32_device_file': device_file_crc,
|
|
'size': len(file_data)
|
|
}
|
|
|
|
def print(self, result):
|
|
if not result:
|
|
return
|
|
|
|
if result['success']:
|
|
console.print(f"✓ Datei [info]{result['source_path']}[/info] erfolgreich heruntergeladen.")
|
|
console.print(f" • Größe: [info]{result['size'] / 1024:.2f} KB[/info]")
|
|
else:
|
|
console_err.print(f"❌ CRC-FEHLER: Datei [error]{result['source_path']}[/error] wurde nicht korrekt empfangen!")
|
|
|
|
console.print(f" • Remote CRC: [info]{result['crc32_remote']:08X}[/info]")
|
|
console.print(f" • Local CRC: [info]{result['crc32_local']:08X}[/info]")
|
|
if result.get('crc32_device_file') is not None:
|
|
console.print(f" • Device CRC: [info]{result['crc32_device_file']:08X}[/info]")
|
|
console.print(f" • Zielpfad: [info]{result['dest_path']}[/info]") |