37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
import struct
|
|
from core.utils import console
|
|
from core.protocol import COMMANDS
|
|
|
|
class get_setting:
|
|
def __init__(self, bus):
|
|
self.bus = bus
|
|
|
|
def get(self, key: str):
|
|
key_bytes = key.encode('utf-8')
|
|
payload = struct.pack('B', len(key_bytes)) + key_bytes
|
|
|
|
self.bus.send_request(COMMANDS['get_setting'], payload)
|
|
|
|
# varlen_params=1 liest exakt 1 Byte Länge + entsprechend viele Datenbytes
|
|
data = self.bus.receive_response(length=0, varlen_params=1)
|
|
|
|
if not data or data.get('type') == 'error':
|
|
return None
|
|
|
|
raw = data['data']
|
|
val_len = raw[0]
|
|
val_buf = raw[1:1+val_len]
|
|
|
|
# Binärdaten zurück in Python-Typen parsen
|
|
if key == "audio/vol" and val_len == 1:
|
|
return struct.unpack('<B', val_buf)[0]
|
|
elif key == "play/norepeat" and val_len == 1:
|
|
return bool(struct.unpack('<B', val_buf)[0])
|
|
elif key == "settings/storage_interval" and val_len == 2:
|
|
return struct.unpack('<H', val_buf)[0]
|
|
else:
|
|
return None
|
|
|
|
def print(self, result, key: str):
|
|
if result is not None:
|
|
console.print(f"⚙️ [info]{key}[/info] = [info]{result}[/info]") |