Skip to content

Commit

Permalink
simplify hex conversion code
Browse files Browse the repository at this point in the history
  • Loading branch information
pawl committed Dec 21, 2020
1 parent 444fe12 commit e5b3a0b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
8 changes: 2 additions & 6 deletions rfid.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def connect(ip, timeout=5, port=60000):

@staticmethod
def crc_16_ibm(data):
"""Returns hex string with CRC values added to positions 4 through 8.
"""Returns hex byte string with CRC values added to positions 4 through 8.
This CRC value is required by the controller or it will not process the
request.
Expand Down Expand Up @@ -156,7 +156,7 @@ def crc_16_ibm(data):
endian = struct.pack("<H", code)
data_list[4:8] = binascii.hexlify(endian).decode("utf8")

return "".join(data_list)
return bytearray.fromhex("".join(data_list))

def add_user(self, badge, doors):
if not isinstance(badge, int):
Expand Down Expand Up @@ -184,7 +184,6 @@ def add_user(self, badge, doors):
+ self.controller_serial
+ "00000200ffffffff"
)
add_packet1 = bytearray.fromhex(add_packet1)

self.s.send(self.start_transaction)
self.s.send(add_packet1)
Expand All @@ -207,7 +206,6 @@ def add_user(self, badge, doors):
+ doors_enabled
+ "00000000"
)
add_packet2 = bytearray.fromhex(add_packet2)

self.s.send(self.start_transaction)
self.s.send(add_packet2)
Expand All @@ -233,7 +231,6 @@ def remove_user(self, badge):
+ badge
+ "00000000204e460521149f3b0000000000000000"
)
remove_packet = bytearray.fromhex(remove_packet)

self.s.send(self.start_transaction)
self.s.send(remove_packet)
Expand All @@ -260,7 +257,6 @@ def open_door(self, door_number):
+ door_number
+ "000000"
)
open_door_packet = bytearray.fromhex(open_door_packet)

self.s.send(self.start_transaction)
self.s.send(open_door_packet)
Expand Down
8 changes: 5 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from mock import patch

from rfid import (RFIDClient, comma_format_to_ten_digit,
ten_digit_to_comma_format)
from rfid import RFIDClient, comma_format_to_ten_digit, ten_digit_to_comma_format

TEST_CONTROLLER_IP = "192.168.1.20"
TEST_CONTROLLER_SERIAL = 123106461
Expand Down Expand Up @@ -91,7 +90,10 @@ def test_crc_16_ibm(self):
+ "00000200ffffffff"
)
result = RFIDClient.crc_16_ibm(test_data)
expected_result = "201066f228000000000000009d74560700000200ffffffff"
expected_result = (
b" \x10f\xf2(\x00\x00\x00\x00\x00\x00\x00\x9dtV\x07\x00\x00\x02"
b"\x00\xff\xff\xff\xff"
)
self.assertEqual(result, expected_result)

@patch(
Expand Down

0 comments on commit e5b3a0b

Please sign in to comment.