def handle_truck_arrived(world_socket, truck_arrived):
    print("Entered handle_truck_arrived()")
    try:
        conn, cursor = connect_db()
        query = f"""
            UPDATE amazon_frontend_order
            SET truck_id={truck_arrived.truck_id}
            WHERE id={truck_arrived.package_id}
        """
        execute_and_commit(query, conn, cursor)

        query = f"""
            SELECT status 
            FROM amazon_frontend_order
            WHERE id={truck_arrived.package_id}
        """
        cursor.execute(query)
        row = cursor.fetchone()
        if row[0] == order_status.PACKED:
            start_loading(world_socket, truck_arrived.package_id,
                          truck_arrived.truck_id)

        cursor.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
def handle_packed(world_socket, packed):
    print("Entered handle_packed()")

    # Reply ACK to world
    reply_ack_to_world_with_lock(packed, world_socket)

    try:
        conn, cursor = connect_db()
        query = f"""
            UPDATE amazon_frontend_order
            SET status='{order_status.PACKED}', time_packed='{get_current_time()}'
            WHERE id={packed.shipid}
        """
        execute_and_commit(query, conn, cursor)

        query = f"""
            SELECT truck_id
            FROM amazon_frontend_order
            WHERE id={packed.shipid}
        """
        cursor.execute(query)
        truck_id = cursor.fetchone()[0]
        if truck_id is not None:
            start_loading(world_socket, packed.shipid, truck_id)

        cursor.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)

    print("Exited handle_packed()")
def handle_purchased(world_socket, ups_socket, purchased):
    print("Entered handle_purchased()")

    # Reply ACK to world
    reply_ack_to_world_with_lock(purchased, world_socket)

    try:
        conn, cursor = connect_db()
        query = ""
        for thing in purchased.things:
            query += f"""
                UPDATE amazon_frontend_warehousestock
                SET num_product=num_product+{thing.count}
                WHERE warehouse_id={purchased.whnum} AND product_id={thing.id};
            """
        execute_and_commit(query, conn, cursor)

        query = f"""
            SELECT id 
            FROM amazon_frontend_order
            WHERE status='{order_status.PURCHASING}'
        """
        cursor.execute(query)
        orders = cursor.fetchall()

        for order in orders:
            order_id = order[0]
            if check_warehouse_inventory(order_id, purchased.whnum) == True:
                start_packing(world_socket, order_id)
                start_getting_truck(ups_socket, order_id)

        cursor.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
def update_order_status(order_id, new_status):
    conn, cursor = connect_db()
    query = f"""
        UPDATE amazon_frontend_order
        SET status='{new_status}'
        WHERE id={order_id}
    """
    execute_and_commit(query, conn, cursor)
    cursor.close()
def handle_destination_changed(destination_changed):
    print("Entered handle_destination_changed()")
    try:
        conn, cursor = connect_db()
        query = f"""
            UPDATE amazon_frontend_order
            SET destination_x={destination_changed.new_destination_x}, destination_y={destination_changed.new_destination_y}
            WHERE id={destination_changed.package_id}
        """
        execute_and_commit(query, conn, cursor)

        cursor.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
def handle_delivered(delivered):
    print("Entered handle_delivered()")
    try:
        conn, cursor = connect_db()
        query = f"""
            UPDATE amazon_frontend_order
            SET status='{order_status.DELIVERED}', time_delivered='{get_current_time()}'
            WHERE id={delivered.package_id}
        """
        execute_and_commit(query, conn, cursor)

        cursor.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
def start_packing(world_socket, order_id):
    print("Entered start_packing()")
    command = world.ACommands()
    pack = command.topack.add()
    seqnum = generate_seqnum_and_add_to_waiting_ack_set()
    pack.seqnum = seqnum

    try:
        conn, cursor = connect_db()

        query = f"""    
            SELECT product_id, description, num_product
            FROM amazon_frontend_orderproducttuple, amazon_frontend_product
            WHERE amazon_frontend_orderproducttuple.product_id=amazon_frontend_product.id AND amazon_frontend_orderproducttuple.order_id={order_id}
        """
        cursor.execute(query)
        rows = cursor.fetchall()
        for row in rows:
            thing = pack.things.add()
            thing.id = row[0]
            thing.description = row[1]
            thing.count = row[2]

        query = f"""
            SELECT warehouse_id
            FROM amazon_frontend_order
            WHERE id={order_id}
        """
        cursor.execute(query)
        pack.whnum = cursor.fetchone()[0]
        pack.shipid = order_id

        # Update warehouse stock
        query = ""
        for thing in pack.things:
            query += f"""
                UPDATE amazon_frontend_warehousestock
                SET num_product=num_product-{thing.count}
                WHERE warehouse_id={pack.whnum} AND product_id={thing.id};
            """
        execute_and_commit(query, conn, cursor)
        cursor.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)

    update_order_status(order_id, order_status.PACKING)
    resend_to_world_until_ack_received(command, seqnum, world_socket)
def handle_loaded(world_socket, ups_socket, loaded):
    print("Entered handle_loaded()")

    # Reply ACK to world
    reply_ack_to_world_with_lock(loaded, world_socket)

    try:
        conn, cursor = connect_db()
        query = f"""
            UPDATE amazon_frontend_order
            SET status='{order_status.LOADED}', time_loaded='{get_current_time()}'
            WHERE id={loaded.shipid}
        """
        execute_and_commit(query, conn, cursor)

        start_delivering(ups_socket, loaded.shipid)

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)

    print("Exited handle_loaded()")
def start_delivering(ups_socket, order_id):
    print("Entered start_delivering()")
    try:
        conn, cursor = connect_db()

        query = f"""
            UPDATE amazon_frontend_order
            SET status='{order_status.DELIVERING}'
            WHERE id={order_id}
        """
        execute_and_commit(query, conn, cursor)
        cursor.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)

    deliver = ups.request_init_delivery()
    deliver.package_id = order_id
    command = ups.AUCommands()
    command.init_delivery.CopyFrom(deliver)

    send_to_ups_with_lock(command, ups_socket)
    print("Exiting start_delivering")
Exemple #10
0
def handle_cancel(cancel):
    print("Entered handle_cancel()")
    try:
        conn, cursor = connect_db()

        query = f"""
            SELECT status 
            FROM amazon_frontend_order
            WHERE id={cancel.order_id}
        """
        cursor.execute(query)
        status = cursor.fetchone()[0]
        if status == order_status.PURCHASING:
            query = f"""
                UPDATE amazon_frontend_order
                SET status='{order_status.CANCELED}'
                WHERE id={cancel.order_id}
            """
            execute_and_commit(query, conn, cursor)

        cursor.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
Exemple #11
0
def handle_buy(world_socket, ups_socket, buy):
    print("Entered handle_buy()")
    Acommand = world.ACommands()
    purchase = Acommand.buy.add()
    seqnum = generate_seqnum_and_add_to_waiting_ack_set()
    purchase.seqnum = seqnum

    # Ucommand = ups.AUCommands()
    # get_truck = Ucommand.get_truck

    try:
        conn, cursor = connect_db()

        # Acommand
        # retrieve things in an order, be careful with the columns queried
        query = f"""    
            SELECT product_id, description, num_product
            FROM amazon_frontend_orderproducttuple, amazon_frontend_product
            WHERE amazon_frontend_orderproducttuple.product_id=amazon_frontend_product.id AND amazon_frontend_orderproducttuple.order_id={buy.order_id}
        """
        cursor.execute(query)
        rows = cursor.fetchall()
        for row in rows:
            thing = purchase.things.add()
            thing.id = row[0]
            thing.description = row[1]
            thing.count = row[2]

        query = f"""
            SELECT COUNT(*), MIN(id)
            FROM amazon_frontend_warehouse;
        """
        cursor.execute(query)
        num_warehouse, min_warehouse_id = cursor.fetchone()
        purchase.whnum = buy.order_id % int(num_warehouse) + int(
            min_warehouse_id)

        query = f"""
            UPDATE amazon_frontend_order
            SET warehouse_id={purchase.whnum}
            WHERE id={buy.order_id}
        """
        execute_and_commit(query, conn, cursor)

        # # Ucommand
        # query = f"""
        #     SELECT ups_account, warehouse_id, location_x, location_y, destination_x, destination_y
        #     FROM amazon_frontend_order, amazon_frontend_warehouse
        #     WHERE amazon_frontend_order.warehouse_id=amazon_frontend_warehouse.id AND amazon_frontend_order.id={buy.order_id}
        # """
        # cursor.execute(query)
        # row = cursor.fetchone()

        # get_truck.order_id = buy.order_id
        # get_truck.ups_account = row[0]
        # get_truck.warehouse_id = row[1]
        # get_truck.location_x = row[2]
        # get_truck.location_y = row[3]
        # get_truck.destination_x = row[4]
        # get_truck.destination_y = row[5]

        cursor.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)

    # send_to_ups_with_lock(Ucommand, ups_socket)
    resend_to_world_until_ack_received(Acommand, seqnum, world_socket)