-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
280 lines (227 loc) · 9.12 KB
/
Main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import asyncio
from threading import Thread
from queue import Queue
import sys
import time
import json
from OPC_UA.opc_client import *
from Receive_client_orders.Order import *
from Receive_client_orders.Order_receiver import *
from DB.db_handler import *
from Optimizer.transfgraph import Machine
#sys.path.insert(0, "Statistics") # Só assim é que me começou a funcionar, juro por deus que não percebo os retardanços do windows com as paths
from Statistics.GUI import GUI_V2
from Optimizer.baby_optimizer import HorOptimizer
from DB.db_handler import *
from lock import lock, optimization_lock
def parse_from_db_unload(data, db):
parsed_order = []
for order in data:
parsed_order.append(UnloadOrder(order_type = "Unload", order_number = order[0], piece_type = order[6],
destination = order[7], quantity = order[8], db = db, unloaded = order[9],
state = order[5], already_in_db= True))
return parsed_order
def parse_from_db_transformation(data, db):
parsed_order = []
for order in data:
parsed_order.append(TransformOrder(order_type = "Transform", order_number = order[0], before_type = order[6],
after_type = order[7], quantity = order[8], max_delay = order[4], processed = order[9],
state = order[5], db = db, already_in_db= True))
return parsed_order
def check_stock(optimizer, order):
if order.order_type == 'Transform':
#print(optimizer.stock[order.before_type])
return optimizer.stock[order.before_type] > 0
elif order.order_type == 'Unload':
return optimizer.stock[order.piece_type] > 0
else:
print(f'FATAL: Invalid order in check_stock()')
exit()
def test_thread(optimizer):
while True:
#print(f'{optimizer.state.num_pieces} {optimizer.dispatch_queue}')
time.sleep(1)
def compute_orders(optimizer, q_udp_in, pending_orders):
print("Ordens pendentes: ", len(pending_orders))
for o in pending_orders:
if o.order_type == 'Transform':
optimizer.order_handler(o)
optimizer.optimize_all_pieces()
elif o.order_type == 'Unload':
optimizer.order_handler(o)
while True:
# print('Update orders')
while not q_udp_in.empty():
order = q_udp_in.get()
for o in order:
if o.order_type == 'Transform':
optimizer.order_handler(o)
optimizer.optimize_all_pieces()
elif o.order_type == 'Unload':
optimizer.order_handler(o)
#optimizer.print_machine_schedule()
def order_scheduling(optimizer, q_udp_in, pending_orders, q_orders):
orders_received = []
orders_to_schedule = []
orders_wait_stock = []
orders_scheduled = []
if pending_orders:
for idx, order in enumerate(pending_orders):
orders_received.append(order)
pending_orders.clear()
while True:
if orders_wait_stock:
idx2remove = []
for idx, order in enumerate(orders_wait_stock):
if check_stock(optimizer, order):
print(f'Posso fazer a ordem {order.order_number}')
orders_received.append(order)
idx2remove.append(idx)
else:
continue
orders_wait_stock = [order for idx, order in enumerate(orders_wait_stock) if idx not in idx2remove]
# Orders received from UDP
while not q_udp_in.empty():
order = q_udp_in.get()
for o in order:
orders_received.append(o)
if optimizer.orders2do:
print(f'Orders 2 do: {len(optimizer.orders2do)}')
for order in optimizer.orders2do:
orders_received.append(order)
optimizer.orders2do.clear()
# Orders waiting for restock
# Check if warehouse has enough pieces for each order
if orders_received:
for idx, order in enumerate(orders_received):
if check_stock(optimizer, order):
print(f'TENHO STOCK para a ordem {order.order_number}')
orders_to_schedule.append(order)
#orders_received.pop(idx)
else:
print('OHHHHH SHIIITTT N TENHO STOCK')
orders_wait_stock.append(order)
#orders_received.pop(idx)
orders_received.clear()
# Schedule orders that can be complete
if orders_to_schedule:
for idx, order in enumerate(orders_to_schedule):
if isinstance(order, TransformOrder):
max_delay = order.max_delay
#order._db = None
sim_order = TransformOrder(order.order_type, order.order_number, order.before_type,
order.after_type, order.quantity, order.max_delay, state = "pending",
processed = order.processed, on_factory = order.on_factory, already_in_db=True)
_, cost = optimizer.simulate(sim_order)
priority = max_delay - cost
else:
priority = -9999999
orders_scheduled.append((priority, order))
orders_to_schedule.clear()
orders_scheduled.sort(key=lambda order: order[0])
print('## schedule ##')
for priority, order in orders_scheduled:
print(f'{order.order_number}: {priority}')
q_orders.put(order)
print('## end ##')
for _, order in orders_scheduled:
print(f'Optimizing order {order.order_number}')
optimizer.order_handler(order)
optimizer.optimize_single_order(order)
print(f'Optimization complete')
optimizer.active_orders.append(order)
#optimization_lock.release()
if optimizer.is_reversed:
#print('ma',optimizer.state.machines['Ma_3'].op_list)
#print('mc',optimizer.state.machines['Mc_3'].op_list)
if not optimizer.state.machines['Ma_3'].op_list \
and not optimizer.state.machines['Mc_3'].op_list:
optimizer.reset()
print('RESETTTTTTTTTTTTTT')
optimizer.direct()
print('SENTIDO DIRETO')
orders_scheduled.clear()
def update_dispatch(optimizer):
while True:
# print('Update dispatch')
#optimization_lock.acquire()
for machine in reversed(optimizer.state.machines.values()):
#print(f'{machine}: {machine.is_free}')
if machine.is_free and machine.op_list:
next_op = machine.op_list[0]
next_piece = next_op.piece_id
if next_op.step == 1:
if next_piece in optimizer.tracker.pieces_on_transit:
print('WARNING: Piece has already been dispatched')
#optimizer.dispatch_queue.append(optimizer.state.pieces[next_piece])
machine.make_unavailable()
elif next_piece in optimizer.dispatch_queue:
print('WARNING: Piece is already staged for dispatch')
machine.make_unavailable()
else:
optimizer.dispatch_queue.append(optimizer.state.pieces[next_piece])
machine.make_unavailable()
else:
pass
#machine.make_unavailable()
elif not(machine.is_free or machine.op_list):
pass
#machine.make_available()
#elif (not machine.is_free) and machine.op_list:
# next_op = machine.op_list[0]
# next_piece = next_op.piece_id
# if next_piece in optimizer.dispatch_queue:
# machine.make_available()
# #else:
# # machine.make_unavailable()
#optimization_lock.release()
time.sleep(0.1)
def run(optimizer, host, port):
loop = asyncio.new_event_loop()
loop.run_until_complete(opc_client_run(optimizer, loop, host, port))
loop.run_forever()
loop.close()
if __name__ == "__main__":
with open("config.json", "r") as config:
configuations = json.load(config)
db = DB_handler(configuations["DB"]["Host"], configuations["DB"]["Port"])
#Remover persistencia
#db.delete_all_content(['unload_orders', 'transform_orders', 'stock_orders', 'pieces'])
optimizer = HorOptimizer()
optimizer.state.pieces_optimized = db.select('pieces', content=['Max(piece_id)+1'])[0][0]
win = GUI_V2(db,optimizer)
Order.give_db(db)
# Para usar na persistencia, verifica se há ordens na base de dados que faltam processar
pending_orders = []
pending_orders.extend(parse_from_db_unload(db.select("unload_orders", where= { "curr_state" : "pending", "curr_state1" : "active"}, operand='OR'),db))
print('ordens pendentes unload:' + str(len(pending_orders)))
pending_orders.extend(parse_from_db_transformation(db.select("transform_orders", where= { "curr_state" : "pending", "curr_state1" : "active"}, operand='OR'),db)) #, "curr_state" : "suspended"
print('ordens pendentes transform + unload:' + str(len(pending_orders)))
q_udp = Queue() # Exchanges information from order receiver to the next stage of the program
t_order_rec = Thread(target = order_receive, args = (q_udp, False, configuations["UDP"]["Host"], configuations["UDP"]["Port"], ))
t_order_rec.name = "Thread_client_receive"
t_opc_run = Thread(target = run, args = (optimizer, configuations["PLC"]["Host"], configuations["PLC"]["Port"], ))
t_opc_run.name = "Thread_opc_run"
#t_compute_orders = Thread(target=compute_orders, args=(optimizer, q_udp, pending_orders))
#t_compute_orders.name = "Thread_compute_order"
q_orders = Queue()
t_order_scheduling = Thread(target=order_scheduling, args=(optimizer, q_udp, pending_orders, q_orders))
t_order_scheduling.name = "Thread_order_scheduling"
t_update_dispatch = Thread(target=update_dispatch, args=(optimizer,))
t_update_dispatch.name = "Thread_update_dispatch"
#t_optimize = Thread(target=optimize_stuff, args=(optimizer,q_orders))
#t_optimize.name = "Thread_optimize"
t_test = Thread(target=test_thread, args=(optimizer,))
t_test.name = "Test"
threads = [t_order_rec, t_opc_run, t_order_scheduling, t_update_dispatch] #, t_optimize]
t_opc_run.start()
t_order_rec.start()
#t_optimize.start()
#t_compute_orders.start()
t_update_dispatch.start()
t_order_scheduling.start()
# threads.append(t_test)
# t_test.start()
win.open_GUI()
## Joints all the threads
map(lambda x:x.join(),threads)