コード例 #1
0
 def prepare_product(self, product_id, description, count):
     # print(product_id, description, count)
     product = wapb.AProduct()
     product.id = product_id
     product.description = description
     product.count = count
     return product
コード例 #2
0
def make_pack_requests(world_comm_data):
    pending = db_interface.get_matching_state_orders('pending')
    for o in pending:
        ship_id = o[0]
        x, y = o[4], o[5]
        prodname = o[2]
        count = o[7]
        whid = db_interface.get_matching_wh(x, y)
        prodid = db_interface.get_matching_prodid(prodname)

        prod_desc = db_interface.get_prod_desc(prodid)
        prod_pb = wapb2.AProduct()
        prod_pb.id = prodid
        prod_pb.description = prod_desc
        prod_pb.count = count
        try:
            db_interface.increment_inventory(whid, prodid, -count)
        except db_interface.NoInventoryError:
            continue

        db_interface.adjust_order_status(ship_id, 'pending', 'to pack')
        db_interface.set_pack_loc(ship_id, whid)
        pack_pb = wapb2.APack()
        pack_pb.whnum = whid
        pack_pb.shipid = ship_id
        seqnum = get_seqnum()
        pack_pb.seqnum = seqnum
        world_comm_data.track_new_message(seqnum, 'APack', pack_pb)
コード例 #3
0
ファイル: server.py プロジェクト: yuangungun5/Mini-Amazon
def packProduct(worldSock, products, whnum, conn):
    print(f"Sending pack to world...")
    cursor = conn.cursor()
    pack = wa.APack()
    pack.whnum = whnum
    for item in products.things:
        product = wa.AProduct()
        product = pack.things.add()
        product.id = item.id
        product.description = item.description
        product.count = item.count

        #print(f"{item.description} stock -{item.count}")
        sql = '''UPDATE website_stock SET stock = stock - %s WHERE warehouse_id = %s AND product_id = %s;'''
        cursor.execute(sql, (item.count, whnum, item.id))
        conn.commit()
        #print("-stock update")
    pack.shipid = products.orderid
    pack.seqnum = getSeq_num()
    pack_command = wa.ACommands()
    pack_command.topack.append(pack)
    send_pack = sendMessage(worldSock, pack_command)
    print(pack_command)
    print(f"sent to the world")
    pack_response = waitACK(pack.seqnum, send_pack, worldSock)
    print(f"receive from the world")
    print(pack_response)
    packed = responseFromWorld(worldSock)
    print(packed)
    for p in packed.ready:
        replyACK(p.seqnum, worldSock)
コード例 #4
0
ファイル: tool.py プロジェクト: js896/ECE568
def pack_product(world_socket, bought):
    cmd = world_amazon_pb2.ACommands()
    pack = world_amazon_pb2.APack()
    shipid = FIFO.pop(0)
    seqnum = bought.seqnum + 1
    whnum = bought.whnum
    product = world_amazon_pb2.AProduct()
    for thing in bought.things:
        product.id = thing.id
        product.description = thing.description
        product.count = thing.count
    pack.whnum = whnum
    pack.things.append(product)
    pack.shipid = shipid
    pack.seqnum = seqnum
    cmd.disconnect = False
    cmd.topack.append(pack)
    send_to_world(world_socket, cmd)
    conn = connect_db()
    cursor = conn.cursor()
    for thing in bought.things:
        #cursor.execute("""SET IDENTITY_INSERT packed ON""")
        cursor.execute(
            """
                       Insert INTO packed (whid, item_id, seqnum, isRead)
                       VALUES (%s,%s,%s,%s)                 
                       """,
            [whnum, product.id, generate_seqnum_ups(), 0])
        conn.commit()
    cursor.close()
コード例 #5
0
ファイル: send_recv_world.py プロジェクト: js896/ECE568
def test():
    cmd = world_amazon_pb2.ACommands()
    cmd.disconnect = False
    cmd.acks.append(0)
    purchase_more = world_amazon_pb2.APurchaseMore()
    product = world_amazon_pb2.AProduct()
    product.id = 1
    product.description = "Ipad"
    product.count = 100
    purchase_more.whnum = 1
    purchase_more.things.append(product)
    purchase_more.seqnum = 2
    cmd.buy.append(purchase_more)
    return cmd
コード例 #6
0
def create_refill_request(world_comm_data, minimum=500):
    inv = db_interface.view_inventory()
    for wh_id in db_interface.get_whids():
        seqnum = get_seqnum()
        purchase_pb = wapb2.APurchaseMore()
        purchase_pb.whnum = wh_id
        purchase_pb.seqnum = seqnum
        for prodid in db_interface.get_prodids():
            if inv[wh_id][prodid] < minimum:
                print("refill needed")
                prod_pb = wapb2.AProduct()
                prod_pb.id, prod_pb.description, prod_pb.count = prodid, 'UNUSED FIELD', minimum
                purchase_pb.things.append(prod_pb)
                world_comm_data.track_new_message(seqnum, 'APurchaseMore',
                                                  purchase_pb)
コード例 #7
0
def handle_web_responses(world_socket, web_socket):
    conn = connect_db()
    # num_threads = 100
    # excutor = ThreadPoolExecutor(max_workers = num_threads)
    connection, addr = web_socket.accept()
    while (1):
        response = recv_from_web(web_amazon_pb2.Cmd, connection)
        request = world_amazon_pb2.ACommands()
        request.disconnect = False
        for buy in response.buy:
            conn = connect_db()
            cursor = conn.cursor()
            cursor.execute(
                """
                          INSERT INTO purchase (product_name,product_id,description,user_name,count, shipid, x, y)
                          VALUES(%s,%s,%s,%s,%s,%s,%s,%s);
                          """, [
                    buy.description, buy.productid, buy.description,
                    buy.username, buy.count, buy.packageid, buy.x, buy.y
                ])
            conn.commit()
            cursor.close()
            FIFO.append(buy.packageid)
            FI_FO.append(buy.packageid)
            purchase = world_amazon_pb2.APurchaseMore()
            purchase.seqnum = generate_seqnum_world()
            purchase.whnum = 1
            thing = world_amazon_pb2.AProduct()
            thing.id = buy.productid
            thing.description = buy.description
            thing.count = buy.count
            purchase.things.append(thing)
            request.buy.append(purchase)

        for query in response.queries:
            quer = world_amazon_pb2.AQuery()
            quer.packageid = query.packageid
            quer.seqnum = generate_seqnum_world()
            PURCHASED[quer.seqnum] = quer.packageid
            request.queries.append(quer)

        send_to_world(world_socket, request)
コード例 #8
0
ファイル: server.py プロジェクト: yuangungun5/Mini-Amazon
def callTruck(product_message, upsSock, conn, whnum):
    call = wa.UA_TruckCall()
    call.package_id = product_message.orderid
    for item in product_message.things:
        prod = wa.AProduct()
        prod.id = item.id
        prod.count = item.count
        prod.description = item.description
        call.products.append(prod)
    call.whnum = whnum
    call.owner_id = product_message.userid
    cursor = conn.cursor()
    sql = '''SELECT * FROM website_warehouse WHERE website_warehouse.id=%s;'''
    cursor.execute(sql, (whnum, ))
    records = cursor.fetchall()
    for record in records:
        call.dest_x = record[1]
        call.dest_y = record[2]
    call.seqnum = getSeq_num()
    message = wa.UA_Commands()
    message.acks.append(call.seqnum)
    message.truckCall.append(call)
    sendMessage(upsSock, message)
    print(message)