Files
a7l20-cup-grasp/runtime/hug/scripts/hand_identity.py
T

18 lines
975 B
Python

"""Validate G20 CAN serials by model and handedness, without a unit-specific pin."""
def validate_hand_identity(serial, side, expected_serial=None):
if side not in ('left', 'right'):
raise ValueError('Expected hand side left or right')
if not isinstance(serial, str) or not serial.isascii() or not serial or any(c.isspace() or ord(c)<33 for c in serial):
raise ValueError('Malformed hand serial')
parts = serial.split('-')
if len(parts) < 5 or any(not part for part in parts) or parts[0] != 'LHT20':
raise ValueError('Expected G20/LHT20 hand identity: '+serial)
sides = [part for part in parts[1:] if part in ('L', 'R')]
expected = 'L' if side == 'left' else 'R'
if sides != [expected]:
raise ValueError('Unexpected '+side+' hand identity: '+serial)
if expected_serial is not None and serial != expected_serial:
raise ValueError('Hand serial differs from explicitly pinned identity')
return serial