Update WebSocket URL and improve data handling/debugging

Updated the WebSocket URL to point to the "tell_runner_router" path. Added detailed debug logging for incoming messages and refined data parsing to handle both "samples" dicts and raw lists. Removed commented-out code and ensured processed data is written to Redis with appropriate logging.
This commit is contained in:
GotthardG
2025-06-25 14:40:38 +02:00
parent 5b9169f2cf
commit d5887363ef
+13 -10
View File
@@ -10,7 +10,7 @@ from config import BeamlineConfig # Your BeamlineConfig implement
from aaredaqlib.beamline import MXBeamline # For beamline id/enum
SLOT_IDENTIFIER = "X06DA" # or whatever is relevant
WS_URL = f"wss://mx-db-01.psi.ch/dispatcher/protected_router/ws/samples-spreadsheet/{SLOT_IDENTIFIER}"
WS_URL = f"wss://mx-db-01.psi.ch/dispatcher/protected_router/tell_runner_router/ws/samples-spreadsheet/{SLOT_IDENTIFIER}"
WS_HEADERS = [f"X-Shared-Password: {os.getenv('AAREDB_SHARED_PASSWORD')}"]
beamline = MXBeamline.X06DA # Use your beamline enum/value
@@ -31,12 +31,19 @@ def set_spreadsheet_in_redis(spreadsheet):
def on_message(ws, message):
try:
data = json.loads(message)
pucks = [PuckWithTellPosition(**item) for item in data]
print("[WS][RAW] Message received:", message)
print("[WS][DEBUG] Incoming data:", data)
# Most messages will be dicts with a "samples" list
if isinstance(data, dict) and "samples" in data:
pucks_data = data["samples"]
else:
pucks_data = data # fallback: it might be a list directly
pucks = [PuckWithTellPosition(**item) for item in pucks_data]
sample_short_infos = []
for p in pucks:
for s in p.samples or []:
# Build DewarAddress if tell_position is provided and valid
dewar_address = None
if p.tell_position is not None and len(p.tell_position) == 2:
dewar_address = DewarAddress(
@@ -58,18 +65,14 @@ def on_message(ws, message):
aaredb_params=getattr(s, "data_collection_parameters", None)
)
)
# Wrap in SampleShortInfoList for serialization
ret = SampleShortInfoList(s=sample_short_infos)
config.spreadsheet = ret
print("DEBUG: Writing to redis key:", config._BeamlineConfig__bl + ":sample_spreadsheet")
print("[REDIS][INFO] Written spreadsheet to redis via .spreadsheet property")
# TESTING: Print the processed spreadsheet instead of storing to Redis
print("[WS][INFO] Processed spreadsheet for logging:")
print(ret.model_dump_json(indent=2))
## Store in Redis via your config property (will use model_dump_json for serialization)
#config.spreadsheet = ret
except Exception as exc:
print("[WS][ERROR] Failed to parse or convert message:", exc)
def on_error(ws, error):
print("[WS][ERROR]", error)