sync
This commit is contained in:
@@ -8,13 +8,10 @@ def execute(conn, path: str) -> dict:
|
||||
raise BuzzerError("Keine Antwort auf 'check' empfangen.")
|
||||
|
||||
parts = lines[0].split()
|
||||
if len(parts) != 3 or parts[0] != "CRC32":
|
||||
if len(parts) != 1:
|
||||
raise BuzzerError(f"Unerwartetes Check-Format: {lines[0]}")
|
||||
|
||||
if parts[1] != path:
|
||||
raise BuzzerError(f"Unerwarteter Pfad in Check-Antwort: {parts[1]} (erwartet: {path})")
|
||||
|
||||
crc32 = int(parts[2], 16)
|
||||
crc32 = int(parts[0], 16)
|
||||
|
||||
return {
|
||||
"crc32": crc32
|
||||
|
||||
56
buzzer_tool/core/commands/get_tag.py
Normal file
56
buzzer_tool/core/commands/get_tag.py
Normal file
@@ -0,0 +1,56 @@
|
||||
# core/commands/get_tag.py
|
||||
from core.connection import BuzzerError
|
||||
from core.util import hex_to_bytearray
|
||||
|
||||
def execute(conn, path: str) -> dict:
|
||||
"""Holt Tags einer Datei und gibt sie als strukturiertes Dictionary zurück."""
|
||||
lines = conn.send_command("get_tag " + path)
|
||||
if not lines:
|
||||
raise BuzzerError("Keine Antwort auf 'get_tag' empfangen.")
|
||||
|
||||
parts = lines[0].split()
|
||||
if len(parts) != 1:
|
||||
raise BuzzerError(f"Unerwartetes get_tag-Format: {lines[0]}")
|
||||
|
||||
data = hex_to_bytearray(parts[0])
|
||||
if data is None:
|
||||
raise BuzzerError("Ungültiger Hex-String in get_tag-Antwort.")
|
||||
|
||||
pos = 0
|
||||
tags = {}
|
||||
while pos < len(data):
|
||||
tag_type = data[pos]
|
||||
pos += 1
|
||||
if pos >= len(data):
|
||||
raise BuzzerError(f"Unerwartetes Ende des Hex-Strings bei get_tag (Tag-Typ erwartet). Position: {pos}/{len(data)}")
|
||||
|
||||
match tag_type:
|
||||
case 0x01: # Kommentar
|
||||
length = data[pos]
|
||||
pos += 1
|
||||
if pos + length > len(data):
|
||||
raise BuzzerError(f"Unerwartetes Ende des Hex-Strings bei get_tag (Kommentar erwartet). Position: {pos}")
|
||||
comment = data[pos:pos+length].decode('utf-8')
|
||||
pos += length
|
||||
tags["comment"] = comment
|
||||
|
||||
case 0x02: # Author
|
||||
length = data[pos]
|
||||
pos += 1
|
||||
if pos + length > len(data):
|
||||
raise BuzzerError(f"Unerwartetes Ende des Hex-Strings bei get_tag (Author erwartet). Position: {pos}")
|
||||
author = data[pos:pos+length].decode('utf-8')
|
||||
pos += length
|
||||
tags["author"] = author
|
||||
|
||||
case 0x10: # CRC32
|
||||
if pos + 4 > len(data):
|
||||
raise BuzzerError(f"Unerwartetes Ende des Hex-Strings bei get_tag (CRC32 erwartet). Position: {pos}")
|
||||
crc32 = int.from_bytes(data[pos:pos+4], byteorder='big')
|
||||
pos += 4
|
||||
tags["crc32"] = hex(crc32)
|
||||
|
||||
case _: # Default / Unbekannter Tag
|
||||
tags[f"unknown_0x{tag_type:02x}"] = tag_value_raw.hex()
|
||||
|
||||
return tags
|
||||
25
buzzer_tool/core/util.py
Normal file
25
buzzer_tool/core/util.py
Normal file
@@ -0,0 +1,25 @@
|
||||
def hex_to_bytearray(hex_string):
|
||||
"""
|
||||
Wandelt einen Hex-String (z.B. "deadbeef") in ein bytearray um.
|
||||
Entfernt vorher Leerzeichen und prüft auf Gültigkeit.
|
||||
"""
|
||||
try:
|
||||
# Whitespace entfernen (falls vorhanden)
|
||||
clean_hex = hex_string.strip().replace(" ", "")
|
||||
|
||||
# Konvertierung
|
||||
return bytearray.fromhex(clean_hex)
|
||||
|
||||
except ValueError as e:
|
||||
print(f"Fehler bei der Konvertierung: {e}")
|
||||
return None
|
||||
|
||||
def string_to_hexstring(text):
|
||||
"""
|
||||
Wandelt einen String in einen UTF-8-kodierten Hex-String um.
|
||||
"""
|
||||
# 1. String zu UTF-8 Bytes
|
||||
utf8_bytes = text.encode('utf-8')
|
||||
|
||||
# 2. Bytes zu Hex-String
|
||||
return utf8_bytes.hex()
|
||||
Reference in New Issue
Block a user