#%%

# 取得各網頁桶槽出料、入料優先順序
while True:
    # DB 取得桶槽狀態
    # 將 優先順序 轉換成 桶槽狀態
    # 桶槽狀態轉換
    # 桶槽狀態存回 DB
        # 若有改變狀態, 更新優先順序
    break

#%%
# 測試 : 乾燥出料 對應 乾燥桶槽桶槽, 以利傳值到網頁, 供使用者排序

import pymysql
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='Gold@53743001', database='CoffeeManage', charset='utf8')
c = db.cursor()

# # 1. 先找到乾燥出料儲豆槽編號, tank_type = 乾燥出料：DO1、DO2
c.execute("SELECT `tank_id` FROM `dry_tank_relation` WHERE `tank_type`='dry_out'")
Dry_Out_Items = c.fetchall()
for i in range(len(Dry_Out_Items)):
    print("===== ", Dry_Out_Items[i][0], " ==========")                  # DO1
    c.execute("SELECT `tank_id` FROM `dry_tank_relation` WHERE `tank_type` = 'dry_tank' AND `tank_export` = '" + Dry_Out_Items[i][0] + "'")
    result = c.fetchall()
    print(Dry_Out_Items[i][0], "_result:", result)
    exec(Dry_Out_Items[i][0] + "_DryOut_Order = []")
    # # 2. 找到出料為 DO1 且 tank_type = 乾燥桶槽 之桶槽號：D1、D2、D3；以此類推
    for x in range(len(result)):
        eval(Dry_Out_Items[i][0] + "_DryOut_Order.append(result[x][0])")
    # eval("print('" + Dry_Out_Items[i][0] + "_DryOut_Order: ', DI1_DryOut_Order)")
    print(Dry_Out_Items[i][0] + "_DryOut_Order:", eval(Dry_Out_Items[i][0] + "_DryOut_Order"))
    # # 3. 請使用者分別排序 DO1 對應桶槽、DO2 對應桶槽
    # print("-----------------------------------------------------------")
db.commit()
db.close()

#%%



#%%
































#%%
# # === 乾燥出豆 1/2 ======================================================
# # === 網頁取值 ===========================
DryOut_item = [
    'DO1',
    'DO2'
]
DryOut_item_status = []
DryOut_order = [
    ['D1', 'D2', 'D3'],
    ['D4', 'D5', 'D6'],
]  
DryOut_order_status = []

#%%
# # === 乾燥出豆 2/2 ======================================================
# # === DB 取得桶槽狀態 ===========================
D1 = 'ReadyForOutputtingBean'
D2 = 'Waiting'
D3 = 'Waiting'
D4 = 'Waiting'
D5 = 'Waiting'
D6 = 'Waiting'
D7 = 'ReadyForOutputtingBean'
D8 = 'OutputtingBean'
DO1 = 'Waiting'
DO2 = 'Waiting'
print("D1:", D1)
print("D2:", D2)
print("D3:", D3)
print("D4:", D4)
print("D5:", D5)
print("D6:", D6)
print("DO1:", DO1)
print("DO2:", DO2)

# # === 將桶槽號碼整理成狀態 ===========================
for i in range(len(DryOut_item)):
    DryOut_item_status.append(eval(DryOut_item[i]))
    # print(DryOut_item[i], eval(DryOut_item[i]))
    target_list = []
    for o in range(len(DryOut_order[i])):
        target_list.append(eval(DryOut_order[i][o]))
        # print(DryOut_order[i][o], eval(DryOut_order[i][o]))
    DryOut_order_status.append(target_list)
    # print("===================================")

# # === 桶槽狀態轉換 ===========================
for i in range(len(DryOut_item_status)):
    if DryOut_item_status[i] == "Waiting" \
    and DryOut_order_status[i].count('OutputtingBean') == 0 \
    and DryOut_order_status[i].count('ReadyForOutputtingBean') != 0:
        for o in range(len(DryOut_order_status[i])):
            # print("=== OutputtingBean ", DryOut_order[i][o], "===")
            if DryOut_order_status[i][o] == "ReadyForOutputtingBean":
                exec(DryOut_order[i][o] + " = 'OutputtingBean'")
                exec(DryOut_item[i] + " = 'InputtingBean'")
                finish_order = DryOut_order[i][o]
                # print(finish_order)
                DryOut_order[i].pop(o)
                DryOut_order[i].append(finish_order)
                break
print("===== status update =====")
print("D1:", D1)
print("D2:", D2)
print("D3:", D3)
print("D4:", D4)
print("D5:", D5)
print("D6:", D6)
print("DO1:", DO1)
print("DO2:", DO2)
#%%



#%%
# # === 乾燥入豆 1/2 ======================================================
# # === 網頁取值 ===========================
DryIn_item = [
    'DI1',
    'DI2'
]
DryIn_item_status = []
DryIn_order = [
    ['D1', 'D2', 'D3'],
    ['D4', 'D5', 'D6'],
]  
DryIn_order_status = []

#%%
# # === 乾燥入豆 2/2 ======================================================
# # === DB 取得桶槽狀態 ===========================
DI1 = 'OutputtingBean'
DI2 = 'OutputtingBean'
D1 = 'Drying'
D2 = 'Waiting'
D3 = 'Waiting'
D4 = 'Drying'
D5 = 'Waiting'
D6 = 'Waiting'
print("DI1:", DI1)
print("DI2:", DI2)
print("D1:", D1)
print("D2:", D2)
print("D3:", D3)
print("D4:", D4)
print("D5:", D5)
print("D6:", D6)

# # === 將桶槽號碼整理成狀態 ===========================
for i in range(len(DryIn_item)):
    DryIn_item_status.append(eval(DryIn_item[i]))
    # print(DryIn_item[i], eval(DryIn_item[i]))
    target_list = []
    for o in range(len(DryIn_order[i])):
        target_list.append(eval(DryIn_order[i][o]))
        # print(DryIn_order[i][o], eval(DryIn_order[i][o]))
    DryIn_order_status.append(target_list)
    # print("===================================")

# # === 桶槽狀態轉換 ===========================
for i in range(len(DryIn_item)):
    if DryIn_item_status[i] == "OutputtingBean" \
    and DryIn_order_status[i].count('InputtingBean') == 0:
        if DryIn_order_status[i].count('InputtingBean_Pause') != 0:
            for o in range(len(DryIn_order_status[i])):
                # print("=== Waiting ", DryIn_order[i][o], " ===")
                if DryIn_order_status[i][o] == "InputtingBean_Pause":
                    # print(DryIn_order[i][o] + "= 'InputtingBean'")
                    exec(DryIn_order[i][o] + " = 'InputtingBean'")
                    break
        elif DryIn_order_status[i].count('Waiting') != 0:
            for o in range(len(DryIn_order_status[i])):
                # print("=== Waiting ", DryIn_order[i][o], " ===")
                if DryIn_order_status[i][o] == 'Waiting':
                    # print(DryIn_order[i][o] + "= 'InputtingBean'")
                    exec(DryIn_order[i][o] + " = 'InputtingBean'")
                    finish_order = DryIn_order[i][o]
                    print(finish_order)
                    DryIn_order[i].pop(o)
                    DryIn_order[i].append(finish_order)
                    print("NEW DryIn_order :", DryIn_order)
                    break
    elif DryIn_item_status[i] == "Waiting" or DryIn_item_status[i] == "InputtingBean":
        if DryIn_order_status.count('InputtingBean') != 0:
            for o in range(len(DryIn_order_status)):
                if DryIn_order_status[i][o] == 'InputtingBean':
                    exec(DryIn_order[i][o] + " = 'InputtingBean_Pause'")
                    break
print("===== status update =====")
print("DI1:", DI1)
print("DI2:", DI2)
print("D1:", D1)
print("D2:", D2)
print("D3:", D3)
print("D4:", D4)
print("D5:", D5)
print("D6:", D6)

# %%












#%%
# # === 發酵出豆 1/2 ======================================================
# # === 網頁取值 ===========================
FermentOut_item = [
    'FO1',
    'FO2'
]
FermentOut_item_status = []
FermentOut_order = [
    ['F1', 'F2', 'F3'],
    ['F4', 'F5', 'F6'],
]  
FermentOut_order_status = []


# # === 乾燥出豆 2/2 ======================================================
# # === DB 取得桶槽狀態 ===========================
F1 = 'Waiting'
F2 = 'ReadyForOutputtingBean'
F3 = 'Waiting'
F4 = 'Waiting'
F5 = 'Waiting'
F6 = 'Waiting'
F7 = 'ReadyForOutputtingBean'
F8 = 'OutputtingBean'
FO1 = 'Waiting'
FO2 = 'Waiting'
print("F1:", F1)
print("F2:", F2)
print("F3:", F3)
print("F4:", F4)
print("F5:", F5)
print("F6:", F6)
print("FO1:", FO1)
print("FO2:", FO2)

# # === 將桶槽號碼整理成狀態 ===========================
for i in range(len(FermentOut_item)):
    FermentOut_item_status.append(eval(FermentOut_item[i]))
    # print(FermentOut_item[i], eval(FermentOut_item[i]))
    target_list = []
    for o in range(len(FermentOut_order[i])):
        target_list.append(eval(FermentOut_order[i][o]))
        # print(FermentOut_order[i][o], eval(FermentOut_order[i][o]))
    FermentOut_order_status.append(target_list)
    # print("===================================")

# # === 桶槽狀態轉換 ===========================
for i in range(len(FermentOut_item_status)):
    if FermentOut_item_status[i] == "Waiting" \
    and FermentOut_order_status[i].count('OutputtingBean') == 0 \
    and FermentOut_order_status[i].count('ReadyForOutputtingBean') != 0:
        for o in range(len(FermentOut_order_status[i])):
            # print("=== OutputtingBean ", FermentOut_order[i][o], "===")
            if FermentOut_order_status[i][o] == "ReadyForOutputtingBean":
                exec(FermentOut_order[i][o] + " = 'OutputtingBean'")
                exec(FermentOut_item[i] + " = 'InputtingBean'")
                finish_order = FermentOut_order[i][o]
                # print(finish_order)
                FermentOut_order[i].pop(o)
                FermentOut_order[i].append(finish_order)
                print("NEW FermentOut_order", FermentOut_order)
                break
print("===== status update =====")
print("F1:", F1)
print("F2:", F2)
print("F3:", F3)
print("F4:", F4)
print("F5:", F5)
print("F6:", F6)
print("FO1:", FO1)
print("FO2:", FO2)

#%%



#%%
# # === 發酵入豆 1/2 ======================================================
# # === 網頁取值 ===========================
FermentIn_item = [
    'FI1',
    'FI2'
]
FermentIn_item_status = []
FermentIn_order = [
    ['F1', 'F2', 'F3'],
    ['F4', 'F5', 'F6'],
]  
FermentIn_order_status = []

#%%
# # === 發酵入豆 2/2 ======================================================
# # === DB 取得桶槽狀態 ===========================
FI1 = 'OutputtingBean'
FI2 = 'OutputtingBean'
F1 = 'Fermenting'
F2 = 'Waiting'
F3 = 'Waiting'
F4 = 'Fermenting'
F5 = 'Waiting'
F6 = 'Waiting'
print("FI1:", FI1)
print("FI2:", FI2)
print("F1:", F1)
print("F2:", F2)
print("F3:", F3)
print("F4:", F4)
print("F5:", F5)
print("F6:", F6)

# # === 將桶槽號碼整理成狀態 ===========================
for i in range(len(FermentIn_item)):
    FermentIn_item_status.append(eval(FermentIn_item[i]))
    # print(FermentIn_item[i], eval(FermentIn_item[i]))
    target_list = []
    for o in range(len(FermentIn_order[i])):
        target_list.append(eval(FermentIn_order[i][o]))
        # print(FermentIn_order[i][o], eval(FermentIn_order[i][o]))
    FermentIn_order_status.append(target_list)
    # print("===================================")

# # === 桶槽狀態轉換 ===========================
for i in range(len(FermentIn_item)):
    if FermentIn_item_status[i] == "OutputtingBean" \
    and FermentIn_order_status[i].count('InputtingBean') == 0:
        if FermentIn_order_status[i].count('InputtingBean_Pause') != 0:
            for o in range(len(FermentIn_order_status[i])):
                # print("=== Waiting ", FermentIn_order[i][o], " ===")
                if FermentIn_order_status[i][o] == "InputtingBean_Pause":
                    # print(FermentIn_order[i][o] + "= 'InputtingBean'")
                    exec(FermentIn_order[i][o] + " = 'InputtingBean'")
                    break
        elif FermentIn_order_status[i].count('Waiting') != 0:
            for o in range(len(FermentIn_order_status[i])):
                # print("=== Waiting ", FermentIn_order[i][o], " ===")
                if FermentIn_order_status[i][o] == 'Waiting':
                    # print(FermentIn_order[i][o] + "= 'InputtingBean'")
                    exec(FermentIn_order[i][o] + " = 'InputtingBean'")
                    finish_order = FermentIn_order[i][o]
                    print(finish_order)
                    FermentIn_order[i].pop(o)
                    FermentIn_order[i].append(finish_order)
                    print("NEW FermentIn_order :", FermentIn_order)
                    break
    elif FermentIn_item_status[i] == "Waiting" or FermentIn_item_status[i] == "InputtingBean":
        if FermentIn_order_status.count('InputtingBean') != 0:
            for o in range(len(FermentIn_order_status)):
                if FermentIn_order_status[i][o] == 'InputtingBean':
                    exec(FermentIn_order[i][o] + " = 'InputtingBean_Pause'")
                    break
print("===== status update =====")
print("FI1:", FI1)
print("FI2:", FI2)
print("F1:", F1)
print("F2:", F2)
print("F3:", F3)
print("F4:", F4)
print("F5:", F5)
print("F6:", F6)

#%%
# # === 發酵出料 to 乾燥入料 1/2 ======================================================
# # === 網頁取值 ===========================
FtoD_item = [
    'FO1',
    'FO2'
]
FtoD_item_status = []
FtoD_order = [
    ['DO1'],
    ['DO2']
]  
FtoD_order_status = []

#%%
# # === 發酵出料 to 乾燥入料 2/2 ======================================================
# # === DB 取得桶槽狀態 ===========================
FO1 = 'OutputtingBean'
FO2 = 'OutputtingBean'
DO1 = 'OutputtingBean'
DO2 = 'Waiting'
print("FO1:", FO1)
print("FO2:", FO2)
print("DO1:", DO1)
print("DO2:", DO2)

# # === 將桶槽號碼整理成狀態 ===========================
for i in range(len(FtoD_item)):
    # print(FtoD_item[i], eval(FtoD_item[i]))
    FtoD_item_status.append(eval(FtoD_item[i]))
    target_list = []
    for o in range(len(FtoD_order[i])):
        # print(FtoD_order[i][o], eval(FtoD_order[i][o]))
        target_list.append(eval(FtoD_order[i][o]))
    FtoD_order_status.append(target_list)
    # print("===================================")
# print("FtoD_item_status: ", FtoD_item_status)
# print("FtoD_order_status: ", FtoD_order_status)

# # === 桶槽狀態轉換 ===========================
for i in range(len(FtoD_item_status)):
    if FtoD_item_status[i] == "OutputtingBean" \
    and FtoD_order_status[i].count('InputtingBean') == 0 \
    and FtoD_order_status[i].count('Waiting') != 0 :
        for o in range(len(FtoD_order_status[i])):
            print("=== InputtingBean ", FtoD_order[i][o], "===")
            if FtoD_order_status[i][o] == "Waiting":
                exec(FtoD_order[i][o] + " = 'InputtingBean'")
                finish_order = FtoD_order[i][o]
                print(finish_order)
                FtoD_order[i].pop(o)
                FtoD_order[i].append(finish_order)
                print("NEW FtoD_order :", FtoD_order)
                break
print("===== status update =====")
print("FO1:", FO1)
print("FO2:", FO2)
print("DO1:", DO1)
print("DO2:", DO2)
#%%



#%%
# # === 脫皮出料 1/2 ======================================================
# # === 網頁取值 ===========================
PeelOut_item = [
    'P1',
    'P2'
]
PeelOut_item_status = []
PeelOut_order = [
    ['FI1'],
    ['FI2'],
]  
PeelOut_order_status = []
PeelFullHeight = 30.0

#%%
# # === 脫皮出料 2/2 ======================================================
# # === DB 取得桶槽狀態 ===========================
# !!!!! 請注意, 這邊的 item 還要增加 超音波數值 (儲豆槽內豆子高度)
P1 = 'Peeling'
P2 = 'Peeling'
P1_UltraSonic = 30.5
P2_UltraSonic = 40
FI1 = 'Outputting'
FI2 = 'Waiting'
print("P1: ", P1)
print("P2: ", P2)
print("P1_UltraSonic: ", P1_UltraSonic)
print("P2_UltraSonic: ", P2_UltraSonic)
print("FI1: ", FI1)
print("FI2: ", FI2)

# # === 將桶槽號碼整理成狀態 ===========================
for i in range(len(PeelOut_item)):
    PeelOut_item_status.append(eval(PeelOut_item[i]))
    # print(PeelOut_item[i], eval(PeelOut_item[i]))
    target_list = []
    for o in range(len(PeelOut_order[i])):
        target_list.append(eval(PeelOut_order[i][o]))
        # print(PeelOut_order[i][o], eval(PeelOut_order[i][o]))
    PeelOut_order_status.append(target_list)

# # === 桶槽狀態轉換 ===========================
for i in range(len(PeelOut_item)):
    if eval(PeelOut_item[i] + "_UltraSonic") >= PeelFullHeight \
    and PeelOut_order_status[i].count('InputtingBean') == 0:
        if PeelOut_order_status[i].count('InputtingBean_Pause') != 0:
            for o in range(len(PeelOut_order_status[i])):
                # print("=== Waiting ", PeelOut_order[i][o], " ===")
                if PeelOut_order_status[i][o] == "InputtingBean_Pause":
                    # print(PeelOut_order[i][o] + "= 'InputtingBean'")
                    break
        elif PeelOut_order_status[i].count('Waiting') != 0:
            for o in range(len(PeelOut_order_status[i])):
                # print("=== Waiting ", PeelOut_order[i][o], " ===")
                if PeelOut_order_status[i][o] == 'Waiting':
                    # print(PeelOut_order[i][o] + "= 'InputtingBean'")
                    exec(PeelOut_order[i][o] + " = 'InputtingBean'")
                    finish_order = PeelOut_order[i][o]
                    print(finish_order)
                    PeelOut_order[i].pop(o)
                    PeelOut_order[i].append(finish_order)
                    print("NEW PeelOut_order :", PeelOut_order)
                    break
    else:
        if PeelOut_order_status.count('InputtingBean') != 0:
            for o in range(len(PeelOut_order_status)):
                if PeelOut_order_status[i][o] == 'InputtingBean':
                    exec(PeelOut_order[i][o] + " = 'InputtingBean_Pause'")
                    break
print("===== status update =====")
print("P1: ", P1)
print("P2: ", P2)
print("F1: ", FI1)
print("F2: ", FI2)

#%%



#%%
# # === 清洗出料 ======================================================
# # === 網頁取值 ===========================
# # !!!!! 請注意, 因為此處 C1、C2(多) S1(少), 多對少的狀況, 所以 S 為 item, C 為 order
CleanOut_item = [
    'S1',
    'S2'
]
CleanOut_item_status = []
CleanOut_order = [
    ['C1', 'C2'],
    ['C3', 'C4']
]  
CleanOut_order_status = []
ColorSelectFullHeight = 50.0

# # === DB 取得桶槽狀態 ===========================
# !!!!! 請注意, 這邊的 item 還要增加 超音波數值 (儲豆槽內豆子高度)
C1 = 'ReadyForOutputtingBean'
C2 = 'Waiting'
C3 = 'Waiting'
C4 = 'OutputtingBean'
S1 = 'Waiting'
S2 = 'ColorSelecting'
S1_UltraSonic = 30.5
S2_UltraSonic = 40
print("C1: ", C1)
print("C2: ", C2)
print("C3: ", C3)
print("C4: ", C4)
print("S1: ", S1)
print("S2: ", S2)

# # === 將桶槽號碼整理成狀態 ===========================
for i in range(len(CleanOut_item)):
    CleanOut_item_status.append(eval(CleanOut_item[i]))
    # print(CleanOut_item[i], eval(CleanOut_item[i]))
    target_list = []
    for o in range(len(CleanOut_order[i])):
        target_list.append(eval(CleanOut_order[i][o]))
        # print(CleanOut_order[i][o], eval(CleanOut_order[i][o]))
    CleanOut_order_status.append(target_list)

# # === 桶槽狀態轉換 ===========================
for i in range(len(CleanOut_item)):
    # print(eval(CleanOut_item[i] + "_UltraSonic"))
    if CleanOut_order_status[i].count('OutputtingBean') == 0 \
    and CleanOut_order_status[i].count('ReadyForOutputtingBean') != 0:
        for o in range(len(CleanOut_order_status[i])):
            # print("=== OutputtingBean ", CleanOut_order[i][o], " ===")
            if CleanOut_order_status[i][o] == 'ReadyForOutputtingBean':
                # print(CleanOut_order[i][o] + "= 'OutputtingBean'")
                exec(CleanOut_order[i][o] + " = 'OutputtingBean'")
                finish_order = CleanOut_order[i][o]
                print(finish_order)
                CleanOut_order[i].pop(o)
                CleanOut_order[i].append(finish_order)
                print("NEW CleanOut_order :", CleanOut_order)
                break
print("===== status update =====")
print("C1: ", C1)
print("C2: ", C2)
print("C3: ", C3)
print("C4: ", C4)
print("S1: ", S1)
print("S2: ", S2)


# %%
