Esempio n. 1
0
def Assign_Component():
    global my_status
    global component_status
    global commands_queue
    global flag_assign_component_thread_created

    while flag_master == 1:
        for command in commands_queue:
            if command[2] == "":
                flag_free_component = 0  # 0 = All components are Busy, 1 = At least one component is Free

                while flag_free_component == 0:
                    if my_status == 0:
                        # The Master takes the order
                        flag_free_component = 1
                        my_status = 1
                        command[2] = my_address
                        receive_time = datetime.datetime.now().second
                        command[3] = receive_time
                        network.Broadcast_Message(
                            connection,
                            "[Lemur] " + "[Assignment] " + str(command[0]) +
                            " " + str(command[1]) + " " + my_address)
                        thread.start_new_thread(Execute_Command,
                                                (command[0], command[1], 0))
                    else:
                        # Try to find a free component
                        try:
                            free_component_index = component_status.index(0)
                        except:
                            pass
                        else:
                            # A free component was found and the order is assigned to it
                            flag_free_component = 1
                            component_status[free_component_index] = 1
                            command[2] = address[free_component_index]
                            receive_time = datetime.datetime.now().second
                            command[3] = receive_time
                            network.Broadcast_Message(
                                connection, "[Lemur] " + "[Assignment] " +
                                str(command[0]) + " " + str(command[1]) + " " +
                                address[free_component_index])
                        time.sleep(0.1)
            else:
                current_time = datetime.datetime.now().second
                difference_time = current_time - command[3]
                if difference_time < 0:
                    difference_time = difference_time + 60
                if difference_time >= elev_driver.ELEVATOR_STUCK_THRESHOLD:
                    # Elevator stuck
                    if command[2] != my_address:
                        network.Broadcast_Message(
                            connection, "[Lemur] " + "[Stuck] " +
                            str(command[0]) + " " + str(command[1]))
                        command[2] = ""
                        command[3] = -1
                time.sleep(0.1)

    flag_assign_component_thread_created = 0
Esempio n. 2
0
def Send_Status():
    while True:
        if my_status == 0:
            network.Broadcast_Message(connection,
                                      "[Lemur] " + "[Status] " + "Free")
        else:
            network.Broadcast_Message(connection,
                                      "[Lemur] " + "[Status] " + "Busy")

        time.sleep(1)
Esempio n. 3
0
def Connect_To_Components():
    global connection
    global address

    while True:
        for addr in ADDRESS_ELEVATOR:
            if (addr > my_address) and (addr not in address):
                try:
                    conn = network.Create_Socket(addr, PORT_NUMBER)
                except:
                    pass
                else:
                    connection.append(conn)
                    address.append(addr)
                    flag_component_alive.append(1)
                    component_status.append(0)

                    print "Connected to: ", addr

                    thread.start_new_thread(Receive_From_Component,
                                            (conn, addr))
                    thread.start_new_thread(Watchdog_Component_Alive,
                                            (conn, addr))

                    ## Synchronize queues (sending)
                    for command in commands_queue:
                        if command[2] == "":
                            network.Broadcast_Message(
                                connection,
                                "[Lemur] " + "[Queue] " + str(command[0]) +
                                " " + str(command[1]) + " " + command[2])

            time.sleep(0.5)
Esempio n. 4
0
def Listen_To_Components():
    global connection
    global address

    s = network.Bind_Socket(my_address, PORT_NUMBER)

    while True:
        s.listen(1)

        (conn, addr) = network.Accept_Connection(s)
        addr = addr[0]

        connection.append(conn)
        address.append(addr)
        flag_component_alive.append(1)
        component_status.append(0)

        print "Connected to: ", addr

        thread.start_new_thread(Receive_From_Component, (conn, addr))
        thread.start_new_thread(Watchdog_Component_Alive, (conn, addr))

        ## Synchronize queues (sending)
        for command in commands_queue:
            if command[2] == "":
                network.Broadcast_Message(
                    connection, "[Lemur] " + "[Queue] " + str(command[0]) +
                    " " + str(command[1]) + " " + command[2])
Esempio n. 5
0
def Execute_Command(button, floor, internal):
    # internal = 1 if it is an internal (cab) command, 0 if it is an external (hall) command

    global my_status
    global commands_queue
    global my_stuck_times

    my_status = 1
    network.Broadcast_Message(connection, "[Lemur] " + "[Status] " + "Busy")

    if internal == 0:
        if elev_driver.elev_driver_go_to_floor(floor) == -1:
            # It is stuck
            my_stuck_times = my_stuck_times + 1
            print my_address, "is STUCK for the ", my_stuck_times, " time"

            network.Broadcast_Message(
                connection,
                "[Lemur] " + "[Stuck] " + str(button) + " " + str(floor))

            for command in commands_queue:
                if command[2] == my_address and command[1] == floor:
                    command[2] = ""
                    command[3] = -1

            elev_driver.libelev.elev_set_motor_direction(
                elev_driver.ELEV_MOTOR_DIRECTION['STOP'])

            if my_stuck_times < STUCK_TIMES_THRESHOLD:
                time.sleep(3)
                elev_driver.elev_driver_init_after_stuck()

                my_status = 0
                network.Broadcast_Message(connection,
                                          "[Lemur] " + "[Status] " + "Free")
        else:
            # It is not stuck
            for command in commands_queue:
                if command[2] == my_address and command[
                        1] == elev_driver.libelev.elev_get_floor_sensor_signal(
                        ):
                    network.Broadcast_Message(
                        connection, "[Lemur] " + "[Accomplishment] " +
                        str(command[0]) + " " + str(command[1]))
                    commands_queue.pop(commands_queue.index(command))
                    break

            elev_driver.libelev.elev_set_button_lamp(command[0], floor, 0)

            my_status = 0
            network.Broadcast_Message(connection,
                                      "[Lemur] " + "[Status] " + "Free")
    else:
        elev_driver.elev_driver_go_to_floor(floor)

        elev_driver.libelev.elev_set_button_lamp(
            elev_driver.ELEV_LAMP['BUTTON_COMMAND'], floor, 0)

        my_status = 0
        network.Broadcast_Message(connection,
                                  "[Lemur] " + "[Status] " + "Free")
Esempio n. 6
0
    # Find the role in the network (the Master is the one with the smallest IP)
    if not address:
        flag_master = 1  # 1 if it is the Master, 0 if it is not
    else:
        if my_address < min(address):
            flag_master = 1
        else:
            flag_master = 0

    (button, floor) = elev_driver.elev_driver_poll_buttons()
    if button != -2 and floor != -2:
        if button == elev_driver.ELEV_BUTTON['BUTTON_COMMAND']:
            thread.start_new_thread(Execute_Command, (button, floor, 1))
        else:
            network.Broadcast_Message(
                connection,
                "[Lemur] " + "[Button] " + str(button) + " " + str(floor))
            if [
                    item for item in commands_queue
                    if item[0] == button and item[1] == floor
            ] == []:
                commands_queue.append([button, floor, "", -1])

    if flag_master == 1:
        # Master part
        if old_flag_master != flag_master:
            print "I am the Master!"

        if flag_assign_component_thread_created == 0:
            flag_assign_component_thread_created = 1
            thread.start_new_thread(Assign_Component, ())