diff --git a/daq/src/aaredaq/tellupdater.py b/daq/src/aaredaq/tellupdater.py index e213df60..58f9d211 100644 --- a/daq/src/aaredaq/tellupdater.py +++ b/daq/src/aaredaq/tellupdater.py @@ -21,6 +21,7 @@ aare_db = AareWrapper(bl=beamline) # Track current state current_pucks = [] +last_pucks_sent = [] def compare_and_report_change(old, new, key_func): old_ids = set(key_func(p) for p in old) @@ -33,11 +34,41 @@ def ws_update_samples_info(pucks): """Send sample info to TELL robot.""" tell_client.set_samples_info(pucks) +def convert_pucks(puck_list): + return [PuckWithTellPosition( + id=p.id, + puck_name=p.puck_name, + puck_type=getattr(p, 'puck_type', None), + puck_location_in_dewar=getattr(p, 'puck_location_in_dewar', None), + dewar_id=getattr(p, 'dewar_id', None), + dewar_name=getattr(p, 'dewar_name', None), + pgroup=getattr(p, 'pgroup', None), + samples=getattr(p, 'samples', None), + tell_position=getattr(p, 'tell_position', None), + ) for p in puck_list] + +def pucks_equal(pucks1, pucks2): + # Implement a simple equality check, e.g., comparing sorted ids, or full data + if len(pucks1) != len(pucks2): return False + return all(p1.id == p2.id for p1, p2 in zip(sorted(pucks1, key=lambda p: p.id), + sorted(pucks2, key=lambda p: p.id))) + def timer_update_pucks_beamline(): - """Poll tell for detected pucks, and update the DATABASE with their location.""" + global last_pucks_sent try: detected_pucks = tell_client.get_detected_pucks() aare_db.set_pucks_beamline(detected_pucks) + + # Convert to list[PuckWithTellPosition] + pwtp_list = convert_pucks(detected_pucks) + + # Only send if state changed + if not pucks_equal(pwtp_list, last_pucks_sent): + tell_client.set_samples_info(pwtp_list) + last_pucks_sent = pwtp_list + else: + print("[TIMER] No puck state change, not sending to TELL.") + except Exception as exc: print(f"[ERROR][Timer] {exc}")