import logging

from lora_multihop import serial_connection, variables


def config_module(configuration=variables.MODULE_CONFIG):
    if serial_connection.execute_command(configuration, [variables.STATUS_OK]):
        serial_connection.execute_command('AT+SEND=1', [variables.STATUS_OK])
        serial_connection.execute_command('a', ['AT,SENDING', 'AT,SENDED'])
        logging.debug('module config successfully set')
        return True
    logging.warning("could not set module config")
    return False


def set_address(address):
    cmd = f'AT+ADDR={address}'
    if serial_connection.execute_command(serial_connection.str_to_bytes(cmd), [variables.STATUS_OK]):
        logging.debug(f'module address successfully set to: {address}')
        return True
    logging.warning("could not set module address")
    return False


def get_current_address():
    serial_connection.execute_command(serial_connection.str_to_bytes(variables.GET_ADDR))
    addr = serial_connection.response_q.get(variables.COMMAND_VERIFICATION_TIMEOUT)
    addr = serial_connection.bytes_to_str(addr)
    addr_as_list = addr.split(variables.LORA_MODULE_DELIMITER)
    if addr_as_list[0].strip() != 'AT' or addr_as_list[2].strip() != 'OK':
        raise ValueError('could not get address of module')
    return addr_as_list[1]

[((7, 7, 7, 78), 'lora_multihop.serial_connection.execute_command', 'serial_connection.execute_command', ({(7, 41, 7, 54): 'configuration', (7, 56, 7, 77): '[variables.STATUS_OK]'}, {}), '(configuration, [variables.STATUS_OK])', False, 'from lora_multihop import serial_connection, variables\n'), ((12, 4, 12, 50), 'logging.warning', 'logging.warning', ({(12, 20, 12, 49): '"""could not set module config"""'}, {}), "('could not set module config')", False, 'import logging\n'), ((21, 4, 21, 51), 'logging.warning', 'logging.warning', ({(21, 20, 21, 50): '"""could not set module address"""'}, {}), "('could not set module address')", False, 'import logging\n'), ((27, 11, 27, 83), 'lora_multihop.serial_connection.response_q.get', 'serial_connection.response_q.get', ({(27, 44, 27, 82): 'variables.COMMAND_VERIFICATION_TIMEOUT'}, {}), '(variables.COMMAND_VERIFICATION_TIMEOUT)', False, 'from lora_multihop import serial_connection, variables\n'), ((28, 11, 28, 47), 'lora_multihop.serial_connection.bytes_to_str', 'serial_connection.bytes_to_str', ({(28, 42, 28, 46): 'addr'}, {}), '(addr)', False, 'from lora_multihop import serial_connection, variables\n'), ((8, 8, 8, 77), 'lora_multihop.serial_connection.execute_command', 'serial_connection.execute_command', ({(8, 42, 8, 53): '"""AT+SEND=1"""', (8, 55, 8, 76): '[variables.STATUS_OK]'}, {}), "('AT+SEND=1', [variables.STATUS_OK])", False, 'from lora_multihop import serial_connection, variables\n'), ((9, 8, 9, 75), 'lora_multihop.serial_connection.execute_command', 'serial_connection.execute_command', ({(9, 42, 9, 45): '"""a"""', (9, 47, 9, 74): "['AT,SENDING', 'AT,SENDED']"}, {}), "('a', ['AT,SENDING', 'AT,SENDED'])", False, 'from lora_multihop import serial_connection, variables\n'), ((10, 8, 10, 55), 'logging.debug', 'logging.debug', ({(10, 22, 10, 54): '"""module config successfully set"""'}, {}), "('module config successfully set')", False, 'import logging\n'), ((18, 41, 18, 76), 'lora_multihop.serial_connection.str_to_bytes', 'serial_connection.str_to_bytes', ({(18, 72, 18, 75): 'cmd'}, {}), '(cmd)', False, 'from lora_multihop import serial_connection, variables\n'), ((19, 8, 19, 71), 'logging.debug', 'logging.debug', ({(19, 22, 19, 70): 'f"""module address successfully set to: {address}"""'}, {}), "(f'module address successfully set to: {address}')", False, 'import logging\n'), ((26, 38, 26, 88), 'lora_multihop.serial_connection.str_to_bytes', 'serial_connection.str_to_bytes', ({(26, 69, 26, 87): 'variables.GET_ADDR'}, {}), '(variables.GET_ADDR)', False, 'from lora_multihop import serial_connection, variables\n')]