-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_distributor.py
70 lines (57 loc) · 2.31 KB
/
task_distributor.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
import csv
import zmq
import time
class WarehouseManager:
def __init__(self):
self.robots = []
def load_orders(self, csv_file):
orders = []
with open(csv_file, 'r') as file:
reader = csv.DictReader(file)
for row in reader:
orders.append(row)
return orders
def handle_task_request(self, robot_id):
if not self.orders:
return None
order = self.orders.pop(0)
client_id = order['Client']
shelves = [order[f'Shelf_{i}'] for i in range(1, 5)]
item_quantities = [int(order[f'Quantity_{i}']) for i in range(1, 5)]
return {
'client_id': client_id,
'shelves': shelves,
'item_quantities': item_quantities
}
def start(self):
self.orders = self.load_orders('data/sample_orders.csv')
# Initialize the CSV file for logging tasks
with open('data/robot_assignments.csv', 'w', newline='') as csvfile:
fieldnames = ['timestamp', 'robot_id', 'client_id', 'shelves', 'item_quantities']
self.writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
self.writer.writeheader()
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
print("Task distributor ready to serve tasks")
while True:
message = socket.recv_json()
robot_id = message['robot_id']
task = self.handle_task_request(robot_id)
if task:
# Log the task assignment to the CSV file
with open('data/robot_assignments.csv', 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=self.writer.fieldnames)
writer.writerow({
'timestamp': time.time(),
'robot_id': robot_id,
'client_id': task['client_id'],
'shelves': task['shelves'],
'item_quantities': task['item_quantities']
})
socket.send_json(task)
else:
socket.send_json({"error": "No tasks available"})
if __name__ == "__main__":
manager = WarehouseManager()
manager.start()