-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
51 lines (41 loc) · 1.3 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
# Import tasks queue
from tasks import optimize
import pickle
from redis_client import redis_client
# Init flask server
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# Serve frontend
@app.route("/")
def home():
return app.send_static_file("index.html")
# API REST
@app.route("/data/current", methods=["GET"])
def waste_containers():
"""
This endpoint reads the serialized data from Redis, deserializes it using pickle,
and returns it as a JSON response.
"""
# Retrieve the pickled data from Redis
current_layout = redis_client.get("current_layout")
# If data is found, deserialize it using pickle
if current_layout:
data = pickle.loads(current_layout)
# Convert to json
json_data = [entity.to_json() for entity in data]
return json_data
else:
return jsonify({"error": "No data available"}), 404
@app.route("/start_task")
def start_task():
task = optimize.apply_async()
return jsonify({"task_id": task.id}), 202
# Route to get the task status
@app.route("/task_status", methods=["GET"])
def task_status():
task_id = request.args.get("task_id")
task = optimize.AsyncResult(task_id)
response = {"state": task.state, "result": task.result}
return jsonify(response)