doorcontrol: Show hex value in hid.Credential exceptions

This commit is contained in:
Adam Goldsmith 2024-09-01 09:47:50 -04:00
parent 33b01af78a
commit bde7828865

View File

@ -12,13 +12,13 @@ class InvalidHexCode(Exception):
class Not26Bit(InvalidHexCode):
def __init__(self) -> None:
super().__init__("Card number > 26 bits")
def __init__(self, bits: bitstring.Bits) -> None:
super().__init__(f"Card number > 26 bits [{bits.hex}]")
class InvalidParity(InvalidHexCode):
def __init__(self, even_odd: Literal["even", "odd"]) -> None:
super().__init__(f"Bad {even_odd} parity")
def __init__(self, bits: bitstring.Bits, even_odd: Literal["even", "odd"]) -> None:
super().__init__(f"Bad {even_odd} parity [{bits.hex}]")
@dataclasses.dataclass(frozen=True)
@ -41,11 +41,11 @@ class Credential:
bits = bitstring.Bits(hex=hex_code)
if bits[:6].any(1):
raise Not26Bit
raise Not26Bit(bits)
if bits[6] != bits[7:19].count(1) % 2:
raise InvalidParity("even")
raise InvalidParity(bits, "even")
if bits[31] != (bits[19:31].count(0) % 2):
raise InvalidParity("odd")
raise InvalidParity(bits, "odd")
return cls(bits)