doorcontrol: Show hex value in hid.Credential exceptions
This commit is contained in:
parent
33b01af78a
commit
bde7828865
@ -12,13 +12,13 @@ class InvalidHexCode(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class Not26Bit(InvalidHexCode):
|
class Not26Bit(InvalidHexCode):
|
||||||
def __init__(self) -> None:
|
def __init__(self, bits: bitstring.Bits) -> None:
|
||||||
super().__init__("Card number > 26 bits")
|
super().__init__(f"Card number > 26 bits [{bits.hex}]")
|
||||||
|
|
||||||
|
|
||||||
class InvalidParity(InvalidHexCode):
|
class InvalidParity(InvalidHexCode):
|
||||||
def __init__(self, even_odd: Literal["even", "odd"]) -> None:
|
def __init__(self, bits: bitstring.Bits, even_odd: Literal["even", "odd"]) -> None:
|
||||||
super().__init__(f"Bad {even_odd} parity")
|
super().__init__(f"Bad {even_odd} parity [{bits.hex}]")
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
@ -41,11 +41,11 @@ class Credential:
|
|||||||
bits = bitstring.Bits(hex=hex_code)
|
bits = bitstring.Bits(hex=hex_code)
|
||||||
|
|
||||||
if bits[:6].any(1):
|
if bits[:6].any(1):
|
||||||
raise Not26Bit
|
raise Not26Bit(bits)
|
||||||
if bits[6] != bits[7:19].count(1) % 2:
|
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):
|
if bits[31] != (bits[19:31].count(0) % 2):
|
||||||
raise InvalidParity("odd")
|
raise InvalidParity(bits, "odd")
|
||||||
|
|
||||||
return cls(bits)
|
return cls(bits)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user