-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemulator.py
120 lines (101 loc) · 3.55 KB
/
emulator.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
import matlab.engine
import os
import json
from flask import Flask, request, jsonify, send_from_directory
# from flask_cors import CORS
app = Flask(__name__, static_url_path="/static")
eng = matlab.engine.start_matlab()
eng.cd(os.path.abspath("./matlab"), nargout=0)
port = 8088
# CORS(app)
@app.route('/')
def root():
return send_from_directory('html/', 'index.html')
@app.route('/_next/<path:subpath>')
def next(subpath):
return send_from_directory('html/_next/', subpath)
@app.route("/init", methods=["POST"])
def init():
data = request.json
try:
eng.emulator_init(
matlab.double(data["ue_size"]),
matlab.double(data["rangeOfPosition"]),
nargout=0,
)
return jsonify({"status": 1})
except matlab.engine.MatlabExecutionError as e:
print(e)
return jsonify({"status": 1, "message": e.args[0]})
@app.route("/ourAlgorithm", methods=["POST"])
def ourAlgorithm():
data = request.json
try:
data = eng.emulator_ourAlgorithm(
data["minDataTransferRateOfUEAcceptable"] * (10**6),
data["maxDataTransferRateOfUAVBS"] * (10**6),
nargout=1,
)
return jsonify({"status": 1, "data": json.loads(data)})
except matlab.engine.MatlabExecutionError as e:
print(e)
return jsonify({"status": 1, "message": e.args[0]})
@app.route("/spiralMBSPlacementAlgorithm", methods=["POST"])
def spiralMBSPlacementAlgorithm():
data = request.json
try:
data = eng.emulator_spiralMBSPlacementAlgorithm(
matlab.double(data["r_UAVBS"]),
data["minDataTransferRateOfUEAcceptable"] * (10**6),
data["maxDataTransferRateOfUAVBS"] * (10**6),
nargout=1,
)
return jsonify({"status": 1, "data": json.loads(data)})
except matlab.engine.MatlabExecutionError as e:
print(e)
return jsonify({"status": 1, "message": e.args[0]})
@app.route("/kmeans", methods=["POST"])
def kmeans():
data = request.json
try:
data = eng.emulator_kmeans(
matlab.double(data["index"]),
matlab.double(data["k"]),
data["minDataTransferRateOfUEAcceptable"] * (10**6),
data["maxDataTransferRateOfUAVBS"] * (10**6),
nargout=1,
)
return jsonify({"status": 1, "data": json.loads(data)})
except matlab.engine.MatlabExecutionError as e:
print(e)
return jsonify({"status": 1, "message": e.args[0]})
@app.route("/random", methods=["POST"])
def random():
data = request.json
try:
data = eng.emulator_random(
matlab.double(data["rangeOfPosition"]),
data["minDataTransferRateOfUEAcceptable"] * (10**6),
data["maxDataTransferRateOfUAVBS"] * (10**6),
nargout=1,
)
return jsonify({"status": 1, "data": json.loads(data)})
except matlab.engine.MatlabExecutionError as e:
print(e)
return jsonify({"status": 1, "message": e.args[0]})
@app.route("/voronoi", methods=["POST"])
def voronoi():
data = request.json
try:
data = eng.emulator_voronoi(
matlab.double(data["r_UAVBS"]),
data["minDataTransferRateOfUEAcceptable"] * (10**6),
data["maxDataTransferRateOfUAVBS"] * (10**6),
nargout=1,
)
return jsonify({"status": 1, "data": json.loads(data)})
except matlab.engine.MatlabExecutionError as e:
print(e)
return jsonify({"status": 1, "message": e.args[0]})
if __name__ == "__main__":
app.run(port=port, debug=True)