-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOrchestrator.py
202 lines (164 loc) · 6.7 KB
/
Orchestrator.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
import sys, getopt
from statemachine import StateMachine, State, exceptions
from DeviceServer import DeviceServer
from GiftLightPad import GiftLightPad
from GiftSizeCalculator import GiftSizeCalculator
from LedController import LedController
from OrderHandler import OrderHandler
from PaperLengthController import PaperLengthController
from WebSocket import WebSocket
from AutoConnector import AutoConnector
led_unit_name = "LedUnit0"
class Orchestrator(StateMachine):
print("Started orchestrator")
fakeorder = False
testprojections = False
optlist, _ = getopt.getopt(sys.argv[1:], "", ["fakeorder=", "testprojections="])
print(optlist)
for o,a in optlist:
if o in ["--fakeorder"]:
fakeorder = True
elif o in ["--testprojections"]:
testprojections = True
else:
assert False, "unhandled option " + o
devices = {}
# states
idle = State('Idle', initial=True)
waitingForGift = State('WaitingForGift')
start = State('Start')
sizeCalculated = State('SizeCalculated')
paperPrepared = State('PaperPrepared')
paperCutOff = State('PaperCutOff')
knifeMovedBack = State('KnifeMovedBack')
giftPlaced = State('GiftProjected')
giftWrapped = State('GiftWrapped')
firstFold = State('FirstFold')
secondFold = State('SecondFold')
thirdFold = State('ThirdFold')
# actions
new_order = idle.to(start)
finished_size_calc = start.to(sizeCalculated) # when size was calculated continue with preparing paper
finished_paper_prep = sizeCalculated.to(paperPrepared) # paper is prepared. now project the paper on the table
paper_not_prepared = paperPrepared.to(sizeCalculated) # paper is not in range anymore
cut_paper_off = paperPrepared.to(paperCutOff) # paper is laid out (lightpad2 is semi-bright). now project the gift on top of the paper
moved_knife_back = paperCutOff.to(knifeMovedBack)
gift_placed = knifeMovedBack.to(giftPlaced)
gift_removed = giftPlaced.to(knifeMovedBack)
finish = giftPlaced.to(idle)
next_order = giftPlaced.to(waitingForGift)
tape_teared = giftPlaced.to(firstFold) | firstFold.to(secondFold) | secondFold.to(thirdFold)
finish_order = thirdFold.to(start)
test_projection = idle.to(knifeMovedBack)
def __init__(self):
super().__init__()
self.led = LedController(None, 43432)
self.gift_lightpad = GiftLightPad(self)
self.autoConnector = AutoConnector(self)
self.autoConnector.start()
self.webSocket = WebSocket(self)
self.webSocket.start()
self.paperLengthWatcher = PaperLengthController(self)
self.sizeCalculator = GiftSizeCalculator(self.finished_size_calc, self.gift_placed, self.led)
self.deviceServer = DeviceServer(self)
self.deviceServer.start()
self.orderHandler = OrderHandler(self.new_order)
if not self.fakeorder:
self.orderHandler.get_open_orders()
else:
current_order = {
"id" : 1,
"paper_id": 3,
"deco_ids": [5,6,7]
}
self.orderHandler.current_order = current_order
if self.testprojections:
self.sizeCalculator.generate_mock_values()
self.test_projection()
else:
self.new_order()
def on_enter_idle(self):
print('No orders')
def on_enter_waitingForGift(self):
self.webSocket.send_current_state()
def on_enter_start(self):
print('New Order')
self.sizeCalculator.active = True
self.webSocket.send_current_state()
def on_enter_sizeCalculated(self):
print('Size calculated - watching the paper now')
self.webSocket.send_current_state()
#self.motorDriver.set_paper_length(self.sizeCalculator.paper_width)
#self.motorDriver.start()
self.paperLengthWatcher.set_paper_dimensions(self.sizeCalculator.paper_height)
def on_enter_paperPrepared(self):
print('Finished paper prep!')
# project paper onto the table now
# send message to ws to render paper projection
self.webSocket.send_current_state()
def on_enter_paperCutOff(self):
print('Paper cut off')
self.webSocket.send_current_state()
def on_enter_knifeMovedBack(self):
print('Knife moved back')
self.webSocket.send_current_state()
self.led.set_rgb("0,0,0")
self.gift_lightpad.active = True
def on_enter_giftPlaced(self):
print('Project the arrows onto the paper')
# project arrows onto the paper now
# send message to ws to render arrow projection
self.webSocket.send_current_state()
def on_enter_giftWrapped(self):
print('gift was placed')
# project arrows onto the paper now
# send message to ws to render arrow projection
self.webSocket.send_current_state()
def on_enter_firstFold(self):
print("first fold done")
self.webSocket.send_current_state()
def on_enter_secondFold(self):
print("second fold done")
self.webSocket.send_current_state()
def on_enter_thirdFold(self):
print("third fold done")
self.webSocket.send_current_state()
def handle_lightpad_change(self, id, value):
try:
if id == 1:
self.gift_lightpad.set_value(value)
elif id == 2:
if value == 0:
self.cut_paper_off()
else:
self.moved_knife_back()
except exceptions.TransitionNotAllowed:
pass
#print("Transition not allowed")
#print(self.current_state)
def get_current_message(self):
state_id = self.current_state.identifier
if hasattr(self, 'orderHandler'):
#current_order = self.orderHandler.current_order_items
current_order = self.orderHandler.current_order
else:
current_order = {}
message = {
"state": state_id,
"gift_width": self.sizeCalculator.gift_width,
"gift_height": self.sizeCalculator.gift_height,
"gift_depth": self.sizeCalculator.gift_depth,
"paper_width": self.sizeCalculator.paper_width,
"paper_height": self.sizeCalculator.paper_height,
"current_order": current_order
}
return str(message).replace("'",'"')
def update_devices(self, devices):
self.devices = {**self.devices, **devices}
if led_unit_name in devices:
led_ip = devices[led_unit_name]
self.led.ip = led_ip
self.led.set_rgb("255,0,0")
print("Updated LED IP")
if __name__ == "__main__":
orchestrator = Orchestrator()