def run_keypad(): kc_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) kc_port = 10001 as_port = 10010 as_ip = '172.20.10.10' server_address = ('', kc_port) kc_socket.bind(server_address) while True: try: creds = None print('Creds set to {}'.format(creds)) # Wait for user to enter all credentials in keypad while creds == None or creds == (None, None, None): creds = read_keypad() print('Creds set to {}'.format(creds)) # Credentials were entered, save and print them user_code, hashed_passcode, safe_number = creds print('UC:{} --- HP:{} --- SN:{}'.format(user_code, hashed_passcode, safe_number)) send_user_data_udp(user_code, hashed_passcode, safe_number, as_ip, as_port, kc_socket) print('Data sent') ack, address = utils.receive_pkt(kc_socket, kc_port) decoded_ack, err = utils.decode_ack(ack) print('Received ACK: {} --- From {}'.format(decoded_ack, address)) set_LED(decoded_ack) except KeyboardInterrupt: print('\nQuitting application...') sys.exit(0)
def wait_dc_ack(socket, safe, thread_port): """ Wait Ack from Door Controller Parameters --------- socket: Socket socket to send possible error safe: int safe number thread_port: int Port of current thread Returns ------ Str Ack from Door Controller """ ack = None # Wait for ack from dc. Error occurs if timeout happens 3 times. for i in range(3): buf, address = udp_utils.receive_pkt(socket, thread_port) if buf != None: break if address is None: print("Timeout occured when waiting for DC") elif buf is None: # Error error_mes = udp_utils.create_error(address) udp_utils.send_pkt(socket, error_mes, address, dc_port) else: ack, error = udp_utils.decode_ack(buf) if ack is None: # Error error_mes = udp_utils.create_error(error) udp_utils.send_pkt(socket, error_mes, address, dc_port) return ack
def receive_udp_ack(s): '''Wait for an acknowledgment packet from the Access System.''' port = 8080 buf, address = utils.receive_pkt(s, port) ack_data = utils.decode_ack(buf) if ack_data is None: print('Error receiving ACK packet') return False return True
def test_decode_ack_5(): encoded = b'XYZ\0\0' expected_result = None, 'empty_packet' assert utils.decode_ack(encoded) == expected_result
def test_decode_ack_3(): encoded = None expected_result = None, 'empty_packet' assert utils.decode_ack(encoded) == expected_result
def test_decode_ack_2(): encoded = '0x02False\0'.encode() expected_result = False, '' assert utils.decode_ack(encoded) == expected_result
def test_decode_ack_1(): encoded = '0x02True\0'.encode() expected_result = True, '' assert utils.decode_ack(encoded) == expected_result