forked from danebjork/AutomatedMBAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
146 lines (125 loc) · 5.08 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
"""The websocket handler for connecting with the User Interface.
Runs until killed or crashes.
"""
#!/usr/bin/env python
# pip3 install WebSockets
import websockets
import os
import asyncio
import sys
from mbam import MbamUI
import json
import logging
# Setup logging
logging.basicConfig(format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt = '%m/%d/%Y %I:%M:%S %p')
logger = logging.getLogger("MBAM")
logger.setLevel(logging.INFO)
debug_handler = logging.FileHandler("debug.log", 'w')
debug_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s: %(message)s', datefmt = '%m/%d/%Y %I:%M:%S %p'))
logger.addHandler(debug_handler)
mbamui = MbamUI()
async def time(websocket, path):
path = os.path.realpath(__file__)
index = path.find(__file__)
path = path[:index]
while True:
# cmd = None
# t = await websocket.recv()
# cmd = json.loads(t)
try:
# Waiting for commands from UI
cmd = json.loads(await websocket.recv())
except:
# will break if page is refreshed
logger.info("Connection reset")
break
if cmd['type'] == "save":
logger.info("Saving model")
mbamui.save_model(cmd['md'], cmd["options"])
await websocket.send(mbamui.send_save_done())
if mbamui.scripts_ready():
await websocket.send(mbamui.send_scripts())
elif cmd['type'] == "load-model-id":
logger.info("Loading model %s" %cmd["id"])
send = mbamui.load_model_id(cmd['id'])
if 'model' in send:
await websocket.send(mbamui.send_model_load_done())
await websocket.send(mbamui.send_save_done())
if 'data' in send:
await websocket.send(mbamui.send_data_done())
if mbamui.scripts_ready():
await websocket.send(mbamui.send_scripts())
elif cmd['type'] == "hdf5":
logger.info("Getting data")
mbamui.read_model_data(await websocket.recv())
await websocket.send(mbamui.send_data_done())
logger.info("Data processed")
if mbamui.scripts_ready():
await websocket.send(mbamui.send_scripts())
elif cmd['type'] == "geo-options":
logger.info("Updating geodesic options")
send = mbamui.update_geo_options(cmd['options'])
await websocket.send(send)
elif cmd['type'] == "geo-update":
logger.info("Updating geodesic script")
mbamui.update_geo_script(cmd['script'])
elif cmd['type'] == "julia-update":
logger.info("Updating julia script")
mbamui.update_julia_script(cmd['script'])
elif cmd['type'] == "geo-start":
logger.info("Starting geodesic")
mbamui.start_geodesic()
elif cmd['type'] == "geo-done":
logger.info("Killing geodesic")
mbamui.kill_geodesic()
elif cmd['type'] == "collect":
logger.debug("Collecting geodesic")
await websocket.send(mbamui.query_geo())
elif cmd['type'] == "simplify":
logger.info("Simplifying expression")
send = mbamui.simplify_eq(cmd['eq'])
await websocket.send(send)
elif cmd['type'] == "epsilon":
logger.info("Evaluating limit epsilon -> zero")
send = mbamui.eval_epsilon(cmd['eq'])
await websocket.send(send)
elif cmd['type'] == "apply-theta":
logger.info("Applying substitutions")
if mbamui.load_ftheta(cmd['data']):
logger.info("Substitution success")
await websocket.send(mbamui.iterate())
else:
logger.warning("Substitution failed")
elif cmd['type'] == "ftheta":
logger.info("Loading ftheta")
mbamui.load_ftheta(cmd['data'])
elif cmd['type'] == "substitute":
logger.info("Substituting ftilde")
send = mbamui.sub_ftildes(cmd['eq'])
await websocket.send(send)
elif cmd['type'] == "manual-iter":
logger.info("Manual iteration")
send = mbamui.manual_iterate(cmd['model'])
await websocket.send(send)
if mbamui.scripts_ready():
await websocket.send(mbamui.send_scripts())
"""
elif cmd == "find-params":
data = await websocket.recv()
data = json.loads(data)
#print(data)
to_send = model.check_parameters(data)
#to_send["testing"] = model.get_latex_model()
await websocket.send(json.dumps(to_send))
"""
else:
logger.warning("Invalid command received", cmd)
pass
if __name__ == "__main__":
try:
start_server = websockets.serve(time, '127.0.0.1', 9000)
logger.info("Ready for Connection")
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
finally:
asyncio.get_event_loop().stop()