-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
114 lines (97 loc) · 3.48 KB
/
server.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
from flask import Flask, escape, request
from flask_cors import CORS
import json
import zrna
import zrna.zr_pb2 as zr
from collections import namedtuple
ZrnaModule = namedtuple("ZrnaModule", ["id", "type", "parameters", "ports", "options"])
ZrnaLink = namedtuple("ZrnaLink", "id source sourcePort target targetPort")
ZrnaPort = namedtuple("ZrnaPort", "id name")
app = Flask(__name__)
CORS(app)
z = zrna.api.Client()
z.connect()
# A dictionary of instantiated ZRNA modules
live_modules = {}
def circuit(data):
"""
Build a ZRNA circuit from JSON data.
The JSON data is build by serializing 'react-diagram's model.
"""
links = data["model"]["layers"][0]['models']
nodes = data["model"]["layers"][1]['models']
zrna_modules = {}
for id, node in nodes.items():
print("Parameters:", node['parameters'])
ports = {}
for port in node['ports']:
ports[port['id']] = ZrnaPort(id=port['id'], name=port['name'])
zrna_modules[id] = ZrnaModule(type=node['zrnaType'], parameters=node['parameters'], id=id, ports=ports, options=node['zrnaOptions'])
zrna_links = {}
for id, link in links.items():
zrna_links[id] = ZrnaLink(id=id, source=link['source'], sourcePort=link['sourcePort'], target=link['target'], targetPort=link["targetPort"])
z.pause()
z.clear()
# Initialize the module and set the clock
for module in zrna_modules.values():
class_ = getattr(z, module.type)
live_modules[module.id] = class_()
live_modules[module.id].set_clock(z.CLOCK3)
z.add(live_modules[module.id])
for p, v in module.parameters.items():
setattr(live_modules[module.id], p, v)
for o in module.options.values():
setattr(live_modules[module.id], o['name'], zr.Option.Value.Value(o['value']))
# Connect input/outputs
for link in zrna_links.values():
source = live_modules[link.source]
target = live_modules[link.target]
sourceModule = zrna_modules[link.source]
targetModule = zrna_modules[link.target]
source_port_name = sourceModule.ports[link.sourcePort].name
target_port_name = targetModule.ports[link.targetPort].name
sourcePort = getattr(source, source_port_name)
targetPort = getattr(target, target_port_name)
sourcePort.connect(targetPort)
# Run the circuit
z.set_divisor(z.CLOCK_SYS1, 4)
# Print out modules and their connections
for m in live_modules.values():
print(m)
for o in m.inputs:
print(getattr(m, o).connected_to)
for o in m.outputs:
print(getattr(m, o).connected_to)
@app.route('/', methods=["POST"])
def func():
print("Circuit")
circuit(request.json)
return 'OK'
@app.route('/circuit/module/<module_id>/parameter/<parameter>', methods=["POST"])
def parameter(module_id, parameter):
print("parameter {0} {1}".format(module_id, parameter))
try:
m = live_modules[module_id]
setattr(m, parameter, request.json['value'])
print(parameter)
print(request.json)
return 'OK'
except KeyError:
return "MEH"
@app.route('/', methods=["DELETE"])
def clear_circuit():
print("Clear circuit")
z.pause()
z.clear()
live_modules = {}
return 'OK'
@app.route('/pause', methods=["POST"])
def pause_circuit():
print("Pause circuit")
z.pause()
return 'OK'
@app.route('/start', methods=["POST"])
def start_circuit():
print("Run circuit")
z.run()
return 'OK'