"""Vehicle state contract shared by statistics and exports; mirrors BikeData.state.""" import math def battery(value): if value is None or isinstance(value, bool) or (isinstance(value, str) and not value.strip()): return None try: n = float(value) except (TypeError, ValueError): return None return n if math.isfinite(n) and 0 <= n <= 100 else None def classify(state, status, electric): try: state, status = int(state), int(status) except (TypeError, ValueError): return "unknown" electric = battery(electric) if state == 1 and status == 1: return "used" if state == 4: return "locked" if state == 2: return "reserved" if state == 10 or (electric is not None and electric < 20): return "low" if state == 1 and status == 0: return "available" return "unknown"