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]

[((138, 209), 'lora_multihop.serial_connection.execute_command', 'serial_connection.execute_command', (['configuration', '[variables.STATUS_OK]'], {}), '(configuration, [variables.STATUS_OK])\n', (171, 209), False, 'from lora_multihop import serial_connection, variables\n'), ((445, 491), 'logging.warning', 'logging.warning', (['"""could not set module config"""'], {}), "('could not set module config')\n", (460, 491), False, 'import logging\n'), ((766, 813), 'logging.warning', 'logging.warning', (['"""could not set module address"""'], {}), "('could not set module address')\n", (781, 813), False, 'import logging\n'), ((961, 1033), 'lora_multihop.serial_connection.response_q.get', 'serial_connection.response_q.get', (['variables.COMMAND_VERIFICATION_TIMEOUT'], {}), '(variables.COMMAND_VERIFICATION_TIMEOUT)\n', (993, 1033), False, 'from lora_multihop import serial_connection, variables\n'), ((1045, 1081), 'lora_multihop.serial_connection.bytes_to_str', 'serial_connection.bytes_to_str', (['addr'], {}), '(addr)\n', (1075, 1081), False, 'from lora_multihop import serial_connection, variables\n'), ((219, 288), 'lora_multihop.serial_connection.execute_command', 'serial_connection.execute_command', (['"""AT+SEND=1"""', '[variables.STATUS_OK]'], {}), "('AT+SEND=1', [variables.STATUS_OK])\n", (252, 288), False, 'from lora_multihop import serial_connection, variables\n'), ((297, 364), 'lora_multihop.serial_connection.execute_command', 'serial_connection.execute_command', (['"""a"""', "['AT,SENDING', 'AT,SENDED']"], {}), "('a', ['AT,SENDING', 'AT,SENDED'])\n", (330, 364), False, 'from lora_multihop import serial_connection, variables\n'), ((373, 420), 'logging.debug', 'logging.debug', (['"""module config successfully set"""'], {}), "('module config successfully set')\n", (386, 420), False, 'import logging\n'), ((609, 644), 'lora_multihop.serial_connection.str_to_bytes', 'serial_connection.str_to_bytes', (['cmd'], {}), '(cmd)\n', (639, 644), False, 'from lora_multihop import serial_connection, variables\n'), ((678, 741), 'logging.debug', 'logging.debug', (['f"""module address successfully set to: {address}"""'], {}), "(f'module address successfully set to: {address}')\n", (691, 741), False, 'import logging\n'), ((898, 948), 'lora_multihop.serial_connection.str_to_bytes', 'serial_connection.str_to_bytes', (['variables.GET_ADDR'], {}), '(variables.GET_ADDR)\n', (928, 948), False, 'from lora_multihop import serial_connection, variables\n')]