-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
99 lines (76 loc) · 2.81 KB
/
run.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
#!/usr/bin/python
import stepper
import linearalgebra
import exporter
import asyncio
import websockets
import json
import cam
import datetime
running = False
async def msg_receive(socket, _):
global running
print("client connected")
try:
await socket.send(json.dumps({"status": "Connection established"}))
while True:
msg = await socket.recv()
print("message received: {}".format(msg))
msg_parsed = json.loads(msg)
if "steps" in msg_parsed:
if not running:
stepper.set_steps_per_scan(msg_parsed["steps"])
if "running" in msg_parsed:
await socket.send(json.dumps({"status": "Connection established"}))
if "start" in msg_parsed:
if not running:
asyncio.ensure_future(scan(socket))
await socket.send(json.dumps({"status": "Scan started"}))
if "stop" in msg_parsed:
running = False
await socket.send(json.dumps({"status": "Scan stopped"}))
if "stepper" in msg_parsed:
for a in range(0, stepper.get_steps_per_scan()):
await stepper.scan_step()
except websockets.exceptions.ConnectionClosed:
print("client disconnected")
async def scan(socket):
global running
running = True
exporter.create()
steps = stepper.get_steps_per_scan();
for i in range(steps):
if not running:
break
print("step " + str(i))
progress_json = {'progress': ((i+1)/steps)*100}
await socket.send(json.dumps(progress_json))
points = await cam.get_points()
points_transformed = linearalgebra.transform(points, stepper.get_current_angle())
exporter.add_row(points_transformed)
#point_json = {'points': []}
#for p in range(len(points)):
# point_json['points'].append({'point': points[p].tolist()})
point_json = {'points': []}
for p in range(len(points_transformed)):
point_json['points'].append({'point': points_transformed[p].tolist()})
await socket.send(json.dumps(point_json))
await stepper.scan_step()
name = "scan_" + str(datetime.datetime.now()).replace(" ", "_")
url = {"url" : name}
await socket.send(json.dumps(url))
exporter.export(name)
export_finished = {"exportFinished" : "true"}
await socket.send(json.dumps(export_finished))
running = False
def main():
print("starting event loop")
linearalgebra.init(cam.width, cam.height)
loop = asyncio.get_event_loop()
open_socket = websockets.serve(msg_receive, '0.0.0.0', 8888)
loop.run_until_complete(open_socket)
loop.run_forever()
loop.close()
print("closed event loop")
if __name__ == "__main__":
main()