示例#1
0
async def run_command(msg):
    if cc.my_state_event[cc.State.TERMINATING].is_set():
        return False
    cc.change_my_state(state=cc.State.COORDINATING)
    formatted_command = format_command(cmd=msg.data)
    for module_name, command_key, command_dict in get_all_commands():
        if command_key in formatted_command:
            #if intersect_strings(command_key, formatted_command).get("char_count", 0) >= 2:
            # test = command_key.split(" ")
            # try:
            #     answer = set(test).issubset(set(formatted_command))
            # except:
            #     traceback.print_exc()
            # if answer:
            #actually run the command
            dprint(command_key)
            module_function = getattr(sys.modules[module_name],
                                      command_dict[Command.FUNCTION])
            result = await module_function(msg)
            if result != False:
                cc.change_my_state(state=cc.State.LISTENING)
                return result
    aprint("I don't know what you mean by \'", msg.data, "\'.")
    cc.change_my_state(state=cc.State.LISTENING)
    return False
示例#2
0
文件: info.py 项目: Hynchus/Hive
async def list_info(msg):
    cmd = command.format_command(cmd=msg.data)
    if "_cerebrate" in cmd:
        response = ""
        requested_attributes = []
        if "_everything_" in cmd:
            requested_attributes = cerebratesinfo.Record
        else:
            unsorted_requested_attributes = []
            for attribute in cerebratesinfo.Record:
                index = cmd.find(attribute.name.lower())
                if index >= 0:
                    unsorted_requested_attributes.append((index, attribute))

            if len(unsorted_requested_attributes) <= 0:
                requested_attributes = cerebratesinfo.Record
            else:

                def by_index(pair):
                    return pair[0]

                unsorted_requested_attributes.sort(key=by_index)
                requested_attributes = [
                    pair[1] for pair in unsorted_requested_attributes
                ]

        header = " | ".join(
            [attribute.name for attribute in requested_attributes])
        for record in cerebratesinfo.get_cerebrate_records():
            record_attributes = [
                str(record.get(attribute, "unknown"))
                for attribute in requested_attributes
            ]
            response = ''.join((response, "\t===] ",
                                " | ".join(record_attributes), " [===\n"))
        aprint("\nCerebrates Information\t( ", header, " )\n\n", response)
        return True
    if "_mic" in cmd:
        audio.list_microphones()
        aprint("")
        return True
    #print("I don't know what it is you want listed")
    return False
示例#3
0
文件: info.py 项目: Hynchus/Hive
async def change(msg):
    attribute_recognized = False
    while True:
        cmd = command.format_command(msg.data)
        if "_mic" in cmd:
            attribute_recognized = True
            audio.list_microphones()
            input = prompt(prompt_string="\nMicrophone index: ")
            index = int(re.search(r'\d+', input).group())
            if index:
                if audio.setup_microphone(mic_index=index):
                    print("Microphone ", index, " is active")
                else:
                    print("Could not set microphone ", index)
            else:
                print("index not recognized")
        record = cerebratesinfo.get_cerebrate_record(
            record_attribute=cerebratesinfo.Record.MAC,
            attribute_value=mysysteminfo.get_mac_address())
        if record is None:
            aprint("My record has been misplaced")
            return False
        if "_name_" in cmd:
            attribute_recognized = True
            current_name = record.get(cerebratesinfo.Record.NAME, "unknown")
            aprint("Current name: ", current_name)
            record[cerebratesinfo.Record.NAME] = await prompt(
                prompt_string="New name")
            cerebratesinfo.update_cerebrate_record(cerebrate_record=record)
            aprint(current_name, " => ",
                   record.get(cerebratesinfo.Record.NAME, "unknown"))
        if "_location_" in cmd:
            attribute_recognized = True
            current_location = record.get(cerebratesinfo.Record.LOCATION,
                                          "unknown")
            aprint("Current location: ", current_location)
            record[cerebratesinfo.Record.LOCATION] = await prompt(
                prompt_string="New location")
            cerebratesinfo.update_cerebrate_record(cerebrate_record=record)
            aprint(current_location, " => ",
                   record.get(cerebratesinfo.Record.LOCATION, "unknown"))
        '''
        if "_role_" in cmd:
            attribute_recognized = True
            current_role = record.get(cerebratesinfo.Record.ROLE, "unknown")
            aprint("Current Role: ", current_role)
            for role in cerebratesinfo.Role:
                aprint(role.value, ") ", role.name)
            number = int(await prompt("New role (number)"))
            for role in cerebratesinfo.Role:
                if role.value == number:
                    cerebratesinfo.update_cerebrate_attribute(mysysteminfo.get_mac_address(), cerebratesinfo.Record.ROLE, role)
                    aprint(current_role.name, " => ", role.name)
                    break
        '''
        response = None
        if not attribute_recognized:
            return False
            #response = ''.join(("_", await prompt(prompt_string="What do you want changed? "), "_"))
        else:
            response = ''.join(
                ("_", await prompt(prompt_string="Any other changes? "), "_"))
        if "_nothing_" in response or "_no_" in response:
            break
        msg.data = response
        aprint("")
    #broadcast the changes we made to other cerebrates
    cerebratesinfo.update_cerebrate_contact_time(
        mac=mysysteminfo.get_mac_address())
    msg = communication.Message(
        "update_records",
        data=[
            cerebratesinfo.get_cerebrate_record(
                record_attribute=cerebratesinfo.Record.MAC,
                attribute_value=mysysteminfo.get_mac_address())
        ])
    if cerebratesinfo.get_overmind_mac() == mysysteminfo.get_mac_address():
        communication.Secretary.broadcast_message(msg=msg)
    else:
        await communication.Secretary.communicate_message(
            cerebrate_mac=cerebratesinfo.get_overmind_mac(), msg=msg)
    return True
示例#4
0
def feedback(*args):
    output = ''.join(args)
    if not cc.feedback_on_commands():
        return False
    aprint(output)
    return True
示例#5
0
async def display_message(msg):
    aprint(msg.data)
    return cc.CLOSE_CONNECTION, cc.SUCCESS
示例#6
0
async def ping(msg):
    if msg.data != None:
        aprint(msg.data)
    else:
        aprint("PING from ", msg.sender_mac)
    return cc.CLOSE_CONNECTION, cc.SUCCESS